comparison src/luan/modules/http/tools/Run.luan @ 1361:9eba6bf0163b

improve Run.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 22 Apr 2019 00:38:11 -0600
parents 007ceb8dcf89
children d9a5405a3102
comparison
equal deleted inserted replaced
1360:ee0f0e6c89a0 1361:9eba6bf0163b
10 10
11 11
12 local Run = {} 12 local Run = {}
13 13
14 local function lines(s) 14 local function lines(s)
15 local matcher = gmatch(s,"([^\n]*)\n|([^\n])+$") 15 local matcher = gmatch(s,"([^\n]*)\n|([^\n]+)$")
16 return function() 16 return function()
17 local m1, m2 = matcher() 17 local m1, m2 = matcher()
18 return m1 or m2 18 return m1 or m2
19 end 19 end
20 end 20 end
65 </form> 65 </form>
66 </body> 66 </body>
67 </html> 67 </html>
68 <% end 68 <% end
69 69
70 function Run.run(code,source_name)
71 return try {
72 function()
73 local run = load(code,source_name)
74 run()
75 return true
76 end
77 catch = function(e)
78 Http.response.reset()
79 Http.response.headers["content-type"] = "text/plain; charset=utf-8"
80 Io.stdout = Http.response.text_writer()
81 print(e)
82 print()
83 print()
84 print_with_line_numbers(code)
85 return false
86 end
87 }
88 end
89
70 function Run.respond() 90 function Run.respond()
71 local content_type = Http.request.parameters.content_type 91 local content_type = Http.request.parameters.content_type
72 if content_type ~= nil then 92 if content_type ~= nil then
73 Http.response.headers["content-type"] = content_type 93 Http.response.headers["content-type"] = content_type
74 end 94 end
76 local code = Http.request.parameters.code 96 local code = Http.request.parameters.code
77 if code == nil then 97 if code == nil then
78 form() 98 form()
79 return 99 return
80 end 100 end
81 try { 101 Run.run(code,"<web_run>")
82 function()
83 local run = load(code,"<web_run>")
84 run()
85 end
86 catch = function(e)
87 Http.response.reset()
88 Http.response.headers["content-type"] = "text/plain; charset=utf-8"
89 Io.stdout = Http.response.text_writer()
90 print(e)
91 print()
92 print()
93 print_with_line_numbers(code)
94 end
95 }
96 end 102 end
97 103
98 return Run 104 return Run