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