comparison 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
comparison
equal deleted inserted replaced
1577:60e5c324adf9 1578:c922446f53aa
5 local range = Luan.range or error() 5 local range = Luan.range or error()
6 local Table = require "luan:Table.luan" 6 local Table = require "luan:Table.luan"
7 local concat = Table.concat or error() 7 local concat = Table.concat or error()
8 local pack = Table.pack or error() 8 local pack = Table.pack or error()
9 local unpack = Table.unpack or error() 9 local unpack = Table.unpack or error()
10 local copy = Table.copy or error()
10 local Time = require "luan:Time.luan" 11 local Time = require "luan:Time.luan"
11 local Thread = require "luan:Thread.luan" 12 local Thread = require "luan:Thread.luan"
12 local Html = require "luan:Html.luan" 13 local Html = require "luan:Html.luan"
13 local html_encode = Html.encode or error() 14 local html_encode = Html.encode or error()
14 local javascript_encode = Html.javascript_encode or error() 15 local javascript_encode = Html.javascript_encode or error()
18 local logger = Logging.logger "Shell" 19 local logger = Logging.logger "Shell"
19 20
20 21
21 local Shell = {} 22 local Shell = {}
22 23
23 local count = 0 24 local function counter()
24 local new_session = Thread.global_callable("shell.new_session",{next=function() 25 local fns = {}
25 count = count + 1 26 local count = 0
26 return to_string(count) 27 function fns.next()
27 end}).next 28 count = count + 1
29 return to_string(count)
30 end
31 return fns
32 end
33 local new_session = Thread.global_callable("shell.new_session",counter).next
28 34
29 local env = {} 35 Shell.env = {}
30 Shell.env = env
31 36
32 local fns = {} 37 local function init_env()
33 38 local env = copy(Shell.env)
34 function fns.run(cmd) 39 local fns = {}
35 try 40 function fns.run(cmd)
36 local line
37 try 41 try
38 line = load("return "..cmd,"<web_shell>",env) 42 local line
43 try
44 line = load("return "..cmd,"<web_shell>",env)
45 catch e
46 line = load(cmd,"<web_shell>",env)
47 end
48 return line()
39 catch e 49 catch e
40 line = load(cmd,"<web_shell>",env) 50 --Io.print_to(Io.stderr,e)
51 return to_string(e)
41 end 52 end
42 return line()
43 catch e
44 -- Io.print_to(Io.stderr,e)
45 return to_string(e)
46 end 53 end
54 return fns
47 end 55 end
48 56
49 local timeout = Time.period{hours=10} 57 local timeout = Time.period{hours=10}
50 58
51 local function get_session(session_id) 59 local function get_session(session_id)
52 return Thread.global_callable("shell.session"..session_id,fns,timeout) 60 return Thread.global_callable("shell.session"..session_id,init_env,timeout)
53 end 61 end
54 62
55 function Shell.respond() 63 function Shell.respond()
56 local cmd = Http.request.parameters.cmd 64 local cmd = Http.request.parameters.cmd
57 if cmd ~= nil then 65 if cmd ~= nil then