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