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