Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1520:d9a5405a3102
try statement
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 21 Jun 2020 18:14:13 -0600 |
parents | f04bfbb08721 |
children | b89212fd04b5 |
rev | line source |
---|---|
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 require "java" |
1280 | 2 local System = require "java:java.lang.System" |
3 local URL = require "java:java.net.URL" | |
4 local BasicLuan = require "java:luan.modules.BasicLuan" | |
5 local new_error = BasicLuan.new_error | |
6 local ipairs = BasicLuan.ipairs | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
7 local set_metatable = BasicLuan.set_metatable |
1424 | 8 local load = BasicLuan.load |
9 local type = BasicLuan.type | |
1280 | 10 local StringLuan = require "java:luan.modules.StringLuan" |
11 local match = StringLuan.match -- String.match | |
1424 | 12 local matches = StringLuan.matches -- String.matches |
1280 | 13 local IoLuan = require "java:luan.modules.IoLuan" |
14 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
1330 | 15 local LuanJava = require "java:luan.Luan" |
16 local LuanTable = require "java:luan.LuanTable" | |
1280 | 17 |
18 | |
19 local Boot = {} | |
20 | |
21 | |
22 local function error(message) | |
23 new_error(message).throw() | |
24 end | |
25 Boot.error = error | |
26 | |
1330 | 27 local function no_security(fn) |
28 LuanJava.checkCallerSecurity("no_security") | |
29 return function(...) | |
30 local security = LuanJava.setSecurity(nil) | |
1520 | 31 try |
32 return fn(...) | |
33 finally | |
34 security and LuanJava.setSecurity(security) | |
35 end | |
1330 | 36 end |
37 end | |
38 Boot.no_security = no_security | |
39 | |
1280 | 40 |
41 local function new_LuanIn(io) | |
42 local this = {} | |
43 this.java = io | |
44 this.to_string = io.to_string | |
45 this.read_text = io.read_text | |
46 this.read_binary = io.read_binary | |
47 this.read_lines = io.read_lines | |
48 this.read_blocks = io.read_blocks | |
49 this.exists = io.exists | |
50 this.checksum = io.checksum | |
51 this.charset = io.charset | |
52 this.set_charset = io.set_charset | |
1426 | 53 this.to_uri_string = io.to_uri_string |
1280 | 54 return this |
55 end | |
56 Boot.new_LuanIn = new_LuanIn | |
57 | |
58 local function new_writer(writer) | |
59 local this = {} | |
60 this.java = writer | |
61 this.write = writer.write | |
62 this.close = writer.close | |
63 return this | |
64 end | |
65 | |
66 function Boot.text_writer(out) | |
67 return new_writer( IoLuan.luanWriter(out) ) | |
68 end | |
69 | |
70 Boot.binary_writer = new_writer | |
71 | |
72 local function new_LuanIO(io) | |
73 local this = new_LuanIn(io) | |
74 this.write = io.write | |
75 this.write_text = io.write_text | |
1470 | 76 this.write_binary = io.write_binary |
1280 | 77 |
78 function this.text_writer() | |
79 return new_writer( io.text_writer() ) | |
80 end | |
81 | |
82 function this.binary_writer() | |
83 return new_writer( io.binary_writer() ) | |
84 end | |
85 | |
86 return this | |
87 end | |
88 | |
89 local schemes = {} | |
1330 | 90 LuanTable.setSecurity(schemes,"schemes") |
1280 | 91 |
92 function schemes.null(path) | |
93 return new_LuanIO( IoLuan.nullIO ) | |
94 end | |
95 | |
96 function schemes.string(path) | |
97 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
98 end | |
99 | |
100 function schemes.classpath(path) | |
101 local cp = IoLuan.classpath(path) | |
102 return cp and new_LuanIn(cp) | |
103 end | |
104 | |
105 function schemes.luan(path) | |
106 return schemes.classpath("luan/modules/"..path) | |
107 end | |
108 | |
109 function schemes.stdin(path) | |
110 local Io = require "luan:Io.luan" | |
111 return Io.stdin | |
112 end | |
113 | |
1364 | 114 |
115 local function new_LuanUrl(io) | |
116 local this = new_LuanIn(io) | |
117 | |
118 local function check() | |
119 io.httpCon or error "must open first" | |
120 end | |
121 | |
122 function this.get_header(name) | |
123 check() | |
124 return io.httpCon.getHeaderField(name) | |
125 end | |
126 | |
127 function this.get_response_code() | |
128 check() | |
129 return io.httpCon.getResponseCode() | |
130 end | |
131 | |
132 function this.get_response_message() | |
133 check() | |
134 return io.httpCon.getResponseMessage() | |
135 end | |
136 | |
137 return this | |
138 end | |
139 | |
1280 | 140 local function url(path,options) |
1364 | 141 return new_LuanUrl( LuanUrl.new(URL.new(path),options) ) |
1280 | 142 end |
143 | |
144 function schemes.http(path,options) | |
145 return url( "http:"..path, options ) | |
146 end | |
147 | |
148 function schemes.https(path,options) | |
149 return url( "https:"..path, options ) | |
150 end | |
151 | |
152 local function new_BaseOs(io) | |
153 local this = new_LuanIO(io) | |
154 this.wait_for = io.wait_for | |
155 return this | |
156 end | |
157 | |
158 function schemes.os(path,options) | |
159 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
160 end | |
161 | |
162 function schemes.bash(path,options) | |
163 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
164 end | |
165 | |
166 local function new_LuanFile(io) | |
167 local this = new_LuanIO(io) | |
168 this.name = io.file.getName | |
169 this.is_directory = io.file.isDirectory | |
170 this.is_file = io.file.isFile | |
171 this.delete = io.delete | |
172 this.delete_on_exit = io.file.deleteOnExit | |
173 this.mkdir = io.mkdir | |
174 this.last_modified = io.file.lastModified | |
175 this.set_last_modified = io.set_last_modified | |
176 this.length = io.file.length | |
177 this.rename_to = io.rename_to | |
1497 | 178 this.link_from = io.link_from |
179 this.symlink_from = io.symlink_from | |
1311
ca742d51b31f
don't delete into symbolic links
Franklin Schmidt <fschmidt@gmail.com>
parents:
1286
diff
changeset
|
180 this.is_symbolic_link = io.is_symbolic_link |
1280 | 181 |
182 function this.child(name) | |
183 return new_LuanFile( io.child(name) ) | |
184 end | |
185 | |
186 function this.children() | |
187 local raw = io.children() | |
188 if raw == nil then | |
189 return nil | |
190 end | |
191 local rtn = {} | |
192 for _, child in ipairs(raw) do | |
193 rtn[#rtn+1] = new_LuanFile(child) | |
194 end | |
195 return rtn | |
196 end | |
197 | |
198 function this.parent() | |
199 return new_LuanFile( io.parent() ) | |
200 end | |
201 | |
202 function this.canonical() | |
203 return new_LuanFile( io.canonical() ) | |
204 end | |
205 | |
206 function this.create_temp_file(prefix,suffix) | |
207 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
208 end | |
209 | |
210 return this | |
211 end | |
212 | |
213 function schemes.file(path) | |
214 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
215 end | |
216 | |
217 Boot.schemes = schemes | |
218 | |
219 | |
220 local function uri(name,options) | |
1286 | 221 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) |
1280 | 222 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 223 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 224 return opener(location,options) |
225 end | |
226 Boot.uri = uri | |
227 | |
228 | |
229 function Boot.read(uri_str) -- for PackageLuan.java | |
230 local u = uri(uri_str) | |
231 if u==nil or not u.exists() then | |
232 return nil | |
233 end | |
234 return u.read_text() | |
235 end | |
236 | |
1424 | 237 function Boot.load_file(file) |
238 if type(file) == "string" then | |
239 if not matches(file,":") then | |
240 file = "file:"..file | |
241 end | |
242 local u = uri(file) | |
243 if u==nil or not u.exists() then | |
244 return nil | |
245 end | |
246 local src = u.read_text() | |
1434
56fb5cd8228d
cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents:
1426
diff
changeset
|
247 return load(src,file,true) |
1424 | 248 elseif type(file) == "table" and file.read_text ~= nil then |
249 local src = file.read_text() | |
1426 | 250 local src_file = file.uri_string or file.to_uri_string() |
1434
56fb5cd8228d
cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents:
1426
diff
changeset
|
251 return load(src,src_file,true) |
1424 | 252 else |
253 error("bad argument, expected string or uri table but got "..type(file)) | |
254 end | |
255 end | |
256 | |
1280 | 257 |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
258 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
259 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
260 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
261 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
262 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
263 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
264 function Boot.new_error_table(ex) -- for LuanException.java |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
265 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
266 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
267 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
268 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
269 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
270 this.get_stack_trace_string = ex.getLuanStackTraceString |
1347
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
271 this.get_java_stack_trace_string = ex.getJavaStackTraceString |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
272 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
273 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
274 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
275 |
1280 | 276 return Boot |