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