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