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