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