Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1286:9eb3a9724853
uri bug fixes
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 26 Dec 2018 16:58:53 -0700 |
parents | 503bde9a7c80 |
children | ca742d51b31f |
rev | line source |
---|---|
1280 | 1 java() |
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 |
1280 | 8 local StringLuan = require "java:luan.modules.StringLuan" |
9 local match = StringLuan.match -- String.match | |
10 local IoLuan = require "java:luan.modules.IoLuan" | |
11 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
12 | |
13 | |
14 local Boot = {} | |
15 | |
16 | |
17 local function error(message) | |
18 new_error(message).throw() | |
19 end | |
20 Boot.error = error | |
21 | |
22 | |
23 local function new_LuanIn(io) | |
24 local this = {} | |
25 this.java = io | |
26 this.to_string = io.to_string | |
27 this.to_uri_string = io.to_uri_string | |
28 this.read_text = io.read_text | |
29 this.read_binary = io.read_binary | |
30 this.read_lines = io.read_lines | |
31 this.read_blocks = io.read_blocks | |
32 this.exists = io.exists | |
33 this.checksum = io.checksum | |
34 this.charset = io.charset | |
35 this.set_charset = io.set_charset | |
36 return this | |
37 end | |
38 Boot.new_LuanIn = new_LuanIn | |
39 | |
40 local function new_writer(writer) | |
41 local this = {} | |
42 this.java = writer | |
43 this.write = writer.write | |
44 this.close = writer.close | |
45 return this | |
46 end | |
47 | |
48 function Boot.text_writer(out) | |
49 return new_writer( IoLuan.luanWriter(out) ) | |
50 end | |
51 | |
52 Boot.binary_writer = new_writer | |
53 | |
54 local function new_LuanIO(io) | |
55 local this = new_LuanIn(io) | |
56 this.write = io.write | |
57 this.write_text = io.write_text | |
58 | |
59 function this.text_writer() | |
60 return new_writer( io.text_writer() ) | |
61 end | |
62 | |
63 function this.binary_writer() | |
64 return new_writer( io.binary_writer() ) | |
65 end | |
66 | |
67 return this | |
68 end | |
69 | |
70 local schemes = {} | |
71 | |
72 function schemes.null(path) | |
73 return new_LuanIO( IoLuan.nullIO ) | |
74 end | |
75 | |
76 function schemes.string(path) | |
77 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
78 end | |
79 | |
80 function schemes.classpath(path) | |
81 local cp = IoLuan.classpath(path) | |
82 return cp and new_LuanIn(cp) | |
83 end | |
84 | |
85 function schemes.luan(path) | |
86 return schemes.classpath("luan/modules/"..path) | |
87 end | |
88 | |
89 function schemes.stdin(path) | |
90 local Io = require "luan:Io.luan" | |
91 return Io.stdin | |
92 end | |
93 | |
94 local function url(path,options) | |
95 return new_LuanIn( LuanUrl.new(URL.new(path),options) ) | |
96 end | |
97 | |
98 function schemes.http(path,options) | |
99 return url( "http:"..path, options ) | |
100 end | |
101 | |
102 function schemes.https(path,options) | |
103 return url( "https:"..path, options ) | |
104 end | |
105 | |
106 local function new_BaseOs(io) | |
107 local this = new_LuanIO(io) | |
108 this.wait_for = io.wait_for | |
109 return this | |
110 end | |
111 | |
112 function schemes.os(path,options) | |
113 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
114 end | |
115 | |
116 function schemes.bash(path,options) | |
117 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
118 end | |
119 | |
120 local function new_LuanFile(io) | |
121 local this = new_LuanIO(io) | |
122 this.name = io.file.getName | |
123 this.is_directory = io.file.isDirectory | |
124 this.is_file = io.file.isFile | |
125 this.delete = io.delete | |
126 this.delete_on_exit = io.file.deleteOnExit | |
127 this.mkdir = io.mkdir | |
128 this.last_modified = io.file.lastModified | |
129 this.set_last_modified = io.set_last_modified | |
130 this.length = io.file.length | |
131 this.rename_to = io.rename_to | |
132 | |
133 function this.child(name) | |
134 return new_LuanFile( io.child(name) ) | |
135 end | |
136 | |
137 function this.children() | |
138 local raw = io.children() | |
139 if raw == nil then | |
140 return nil | |
141 end | |
142 local rtn = {} | |
143 for _, child in ipairs(raw) do | |
144 rtn[#rtn+1] = new_LuanFile(child) | |
145 end | |
146 return rtn | |
147 end | |
148 | |
149 function this.parent() | |
150 return new_LuanFile( io.parent() ) | |
151 end | |
152 | |
153 function this.canonical() | |
154 return new_LuanFile( io.canonical() ) | |
155 end | |
156 | |
157 function this.create_temp_file(prefix,suffix) | |
158 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
159 end | |
160 | |
161 return this | |
162 end | |
163 | |
164 function schemes.file(path) | |
165 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
166 end | |
167 | |
168 Boot.schemes = schemes | |
169 | |
170 | |
171 local function uri(name,options) | |
1286 | 172 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) |
1280 | 173 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 174 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 175 return opener(location,options) |
176 end | |
177 Boot.uri = uri | |
178 | |
179 | |
180 function Boot.read(uri_str) -- for PackageLuan.java | |
181 local u = uri(uri_str) | |
182 if u==nil or not u.exists() then | |
183 return nil | |
184 end | |
185 return u.read_text() | |
186 end | |
187 | |
188 | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
189 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
190 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
191 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
192 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
193 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
194 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
195 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
|
196 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
197 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
198 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
199 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
200 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
201 this.get_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
202 this.get_java_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
203 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
204 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
205 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
206 |
1280 | 207 return Boot |