changeset 1159:3ef883468fd0

remove Http.per_session_pages fix clone closure bug replace Thread.global with Thread.global_callable()
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 05 Feb 2018 12:37:59 -0700
parents 267fdf5e9fbd
children 4beabb087be6
files src/luan/impl/Closure.java src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java src/luan/modules/http/jetty/Http.luan src/luan/modules/http/jetty/HttpServicer.java src/luan/modules/http/tools/Shell_mod.luan
diffstat 6 files changed, 87 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/impl/Closure.java
--- a/src/luan/impl/Closure.java	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/impl/Closure.java	Mon Feb 05 12:37:59 2018 -0700
@@ -10,8 +10,8 @@
 
 
 public abstract class Closure extends LuanFunction implements LuanCloneable, Cloneable {
-	public Pointer[] upValues;
-	public LuanJavaOk javaOk;
+	protected Pointer[] upValues;
+	protected LuanJavaOk javaOk;
 	private LuanCloner cloner;
 
 	public Closure(int nUpValues,LuanJavaOk javaOk) throws LuanException {
@@ -20,6 +20,7 @@
 	}
 
 	@Override public Closure shallowClone() {
+		check();
 		try {
 			return (Closure)clone();
 		} catch(CloneNotSupportedException e) {
@@ -28,7 +29,6 @@
 	}
 
 	@Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) {
-		check();
 		Closure clone = (Closure)dc;
 		switch( cloner.type ) {
 		case COMPLETE:
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/modules/Thread.luan
--- a/src/luan/modules/Thread.luan	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/Thread.luan	Mon Feb 05 12:37:59 2018 -0700
@@ -11,7 +11,7 @@
 Thread.schedule = ThreadLuan.schedule
 Thread.sleep = ThreadLuan.sleep
 
-
+--[[
 Thread.global = {}  -- shared across threads
 local map = ThreadLuan.GlobalMap.new()
 local global_mt = {}
@@ -36,20 +36,21 @@
 function Thread.global_put(key,value)
 	return map.put(key,value)
 end
+]]
 
---[[
 function Thread.global_callable(name,timeout,fns)
 	local callable = ThreadLuan.globalCallable(name,timeout,fns)
 	local mt = {}
 	function mt.__index(_,key)
 		return function(...)
-			return callable.call(key,args)
+			return callable.call(key,...)
 		end
 	end
 	local tbl = {}
 	set_metatable(tbl,mt)
 	return tbl
 end
-]]
+
+Thread.remove_global_callable = ThreadLuan.removeGlobalCallable
 
 return Thread
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/modules/ThreadLuan.java
--- a/src/luan/modules/ThreadLuan.java	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/ThreadLuan.java	Mon Feb 05 12:37:59 2018 -0700
@@ -72,7 +72,7 @@
 		luan.onClose(c);
 	}
 
-
+/*
 	public static class GlobalMap {
 
 		private static class Value {
@@ -108,14 +108,13 @@
 			return val==null ? null : val.v;
 		}
 	}
-
+*/
 
 	public static void sleep(long millis) throws InterruptedException {
 		Thread.sleep(millis);
 	}
 
 
-/*
 	private static boolean isPrimitive(Object v) {
 		if( v instanceof Object[] ) {
 			Object[] a = (Object[])v;
@@ -176,5 +175,9 @@
 		callable.expires = System.currentTimeMillis() + timeout;
 		return callable;
 	}
-*/
+
+	public static synchronized void removeGlobalCallable(String name) {
+		callableMap.remove(name);
+	}
+
 }
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/modules/http/jetty/Http.luan
--- a/src/luan/modules/http/jetty/Http.luan	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/http/jetty/Http.luan	Mon Feb 05 12:37:59 2018 -0700
@@ -140,17 +140,6 @@
 -- response = new_response{}  -- filled in by HttpServicer
 
 
-Http.per_session_pages = {}
-
-function Http.per_session(page)
-	Http.per_session_pages[page] = true
-end
-
-function Http.clear_session()
-	Http.request.java.getSession().removeAttribute("luan")
-end
-
-
 function Http.uncache_site()
 	for k in pairs(Table.copy(Package.loaded)) do
 		if matches(k,"^site:") then
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/modules/http/jetty/HttpServicer.java
--- a/src/luan/modules/http/jetty/HttpServicer.java	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/http/jetty/HttpServicer.java	Mon Feb 05 12:37:59 2018 -0700
@@ -18,7 +18,6 @@
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 import javax.servlet.http.Part;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,28 +45,14 @@
 		synchronized(luan) {
 			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
 			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
-			LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages");
 			Object mod = PackageLuan.load(luan,modName);
 			if( mod.equals(Boolean.FALSE) )
 				return false;
 			if( !(mod instanceof LuanFunction) )
 				throw new LuanException( "module '"+modName+"' must return a function" );
-			if( Boolean.TRUE.equals(per_session_pages.rawGet(mod)) ) {
-				HttpSession session = request.getSession();
-				LuanState sessionLuan = (LuanState)session.getAttribute("luan");
-				if( sessionLuan!=null ) {
-					luan = sessionLuan;
-				} else {
-					LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
-					luan = (LuanState)cloner.clone(luan);
-					session.setAttribute("luan",luan);
-				}
-				fn = (LuanFunction)PackageLuan.require(luan,modName);
-			} else {
-				LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
-				luan = (LuanState)cloner.clone(luan);
-				fn = (LuanFunction)cloner.get(mod);
-			}
+			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+			luan = (LuanState)cloner.clone(luan);
+			fn = (LuanFunction)cloner.get(mod);
 		}
 
 		LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
diff -r 267fdf5e9fbd -r 3ef883468fd0 src/luan/modules/http/tools/Shell_mod.luan
--- a/src/luan/modules/http/tools/Shell_mod.luan	Mon Feb 05 10:04:07 2018 -0700
+++ b/src/luan/modules/http/tools/Shell_mod.luan	Mon Feb 05 12:37:59 2018 -0700
@@ -2,50 +2,90 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 local load = Luan.load or error()
+local to_string = Luan.to_string or error()
 local try = Luan.try or error()
+local String = require "luan:String.luan"
+local concat = String.concat or error()
+local Time = require "luan:Time.luan"
+local Thread = require "luan:Thread.luan"
 local Io = require "luan:Io.luan"
 local print = Io.print or error()
 local Http = require "luan:http/Http.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Shell_mod"
 
 
 local Shell_mod = {}
 
-local history = {}
-Shell_mod.env = {}
+local forever = Time.period{days=1000000}
+local count = 0
+local new_session = Thread.global_callable("shell.new_session",forever,{next=function()
+	count = count + 1
+	return to_string(count)
+end}).next
+
+local history = ""
+local env = {}
+
+local fns = {}
+
+function fns.history()
+	return history
+end
+
+function fns.run(cmd)
+	Io.stdout = {}
+	Io.stdout.write = function(...)
+		history = concat(history,...)
+	end
+	print( "% "..cmd )
+	try {
+		function()
+			local line
+			try {
+				function()
+					line = load("return "..cmd,"<web_shell>",env)
+				end
+				catch = function(e)
+					line = load(cmd,"<web_shell>",env)
+				end
+			}
+			print( line() )
+		end
+		catch = function(e)
+			Io.print_to(Io.stderr,e)
+			print(e)
+		end
+	}
+end
+
+local timeout = Time.period{hours=10}
+
+local function get_session(session_id)
+	return Thread.global_callable("shell.session"..session_id,timeout,fns)
+--	return fns
+end
+
+local function remove_session(session_id)
+	return Thread.remove_global_callable("shell.session"..session_id)
+end
 
 function Shell_mod.respond()
+	local session_id = Http.request.cookies.session
+	if session_id == nil then
+		session_id = new_session()
+		Http.response.set_cookie("session",session_id)
+	end
+	local session = get_session(session_id)
+
 	if Http.request.parameters.clear ~= nil then
-		Http.clear_session()
+		remove_session(session_id)
 		Http.response.send_redirect(Http.request.path)  -- reload page
 		return
 	else
 		local cmd = Http.request.parameters.cmd
 		if cmd ~= nil then
-			Io.stdout = {}
-			function Io.stdout.write(...)
-				for v in Luan.values(...) do
-					history[#history+1] = v
-				end
-			end
-			print( "% "..cmd )
-			try {
-				function()
-					local line
-					try {
-						function()
-							line = load("return "..cmd,"<web_shell>",Shell_mod.env)
-						end
-						catch = function(e)
-							line = load(cmd,"<web_shell>",Shell_mod.env)
-						end
-					}
-					print( line() )
-				end
-				catch = function(e)
-					Io.print_to(Io.stderr,e)
-					print(e)
-				end
-			}
+			session.run(cmd)
 		end
 	end
 
@@ -88,11 +128,7 @@
 	<body>
 		<h2>Luan Shell</h2>
 		<p>This is a command shell.  Enter commands below.</p>
-		<pre><%
-		for _,v in ipairs(history) do
-			Io.stdout.write(v)
-		end
-		%></pre>
+		<pre><%= session.history() %></pre>
 		<form name='form0' method='post'>
 			% <input type="text" name='cmd' size="80" autofocus>
 			<input type="submit" value="run">
@@ -103,6 +139,4 @@
 <%
 end
 
-Http.per_session(Shell_mod.respond)
-
 return Shell_mod