diff src/luan/modules/http/tools/Shell.luan @ 1578:c922446f53aa

immutable threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 08 Feb 2021 14:16:19 -0700
parents d3e61cd2aca0
children
line wrap: on
line diff
--- a/src/luan/modules/http/tools/Shell.luan	Sun Jan 31 16:04:39 2021 -0700
+++ b/src/luan/modules/http/tools/Shell.luan	Mon Feb 08 14:16:19 2021 -0700
@@ -7,6 +7,7 @@
 local concat = Table.concat or error()
 local pack = Table.pack or error()
 local unpack = Table.unpack or error()
+local copy = Table.copy or error()
 local Time = require "luan:Time.luan"
 local Thread = require "luan:Thread.luan"
 local Html = require "luan:Html.luan"
@@ -20,36 +21,43 @@
 
 local Shell = {}
 
-local count = 0
-local new_session = Thread.global_callable("shell.new_session",{next=function()
-	count = count + 1
-	return to_string(count)
-end}).next
+local function counter()
+	local fns = {}
+	local count = 0
+	function fns.next()
+		count = count + 1
+		return to_string(count)
+	end
+	return fns
+end
+local new_session = Thread.global_callable("shell.new_session",counter).next
 
-local env = {}
-Shell.env = env
-
-local fns = {}
+Shell.env = {}
 
-function fns.run(cmd)
-	try
-		local line
+local function init_env()
+	local env = copy(Shell.env)
+	local fns = {}
+	function fns.run(cmd)
 		try
-			line = load("return "..cmd,"<web_shell>",env)
+			local line
+			try
+				line = load("return "..cmd,"<web_shell>",env)
+			catch e
+				line = load(cmd,"<web_shell>",env)
+			end
+			return line()
 		catch e
-			line = load(cmd,"<web_shell>",env)
+			--Io.print_to(Io.stderr,e)
+			return to_string(e)
 		end
-		return line()
-	catch e
---		Io.print_to(Io.stderr,e)
-		return to_string(e)
 	end
+	return fns
 end
 
 local timeout = Time.period{hours=10}
 
 local function get_session(session_id)
-	return Thread.global_callable("shell.session"..session_id,fns,timeout)
+	return Thread.global_callable("shell.session"..session_id,init_env,timeout)
 end
 
 function Shell.respond()