comparison src/luan/modules/Boot.luan @ 1424:9ab267b9427c

better load_file()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 Nov 2019 22:58:39 -0700
parents 836e00bf7ce2
children 94a6a209d4e2
comparison
equal deleted inserted replaced
1423:2c06a7ff4173 1424:9ab267b9427c
4 local BasicLuan = require "java:luan.modules.BasicLuan" 4 local BasicLuan = require "java:luan.modules.BasicLuan"
5 local new_error = BasicLuan.new_error 5 local new_error = BasicLuan.new_error
6 local ipairs = BasicLuan.ipairs 6 local ipairs = BasicLuan.ipairs
7 local set_metatable = BasicLuan.set_metatable 7 local set_metatable = BasicLuan.set_metatable
8 local try = BasicLuan.try_ 8 local try = BasicLuan.try_
9 local load = BasicLuan.load
10 local type = BasicLuan.type
9 local StringLuan = require "java:luan.modules.StringLuan" 11 local StringLuan = require "java:luan.modules.StringLuan"
10 local match = StringLuan.match -- String.match 12 local match = StringLuan.match -- String.match
13 local matches = StringLuan.matches -- String.matches
11 local IoLuan = require "java:luan.modules.IoLuan" 14 local IoLuan = require "java:luan.modules.IoLuan"
12 local LuanUrl = require "java:luan.modules.url.LuanUrl" 15 local LuanUrl = require "java:luan.modules.url.LuanUrl"
13 local LuanJava = require "java:luan.Luan" 16 local LuanJava = require "java:luan.Luan"
14 local LuanTable = require "java:luan.LuanTable" 17 local LuanTable = require "java:luan.LuanTable"
15 18
39 42
40 local function new_LuanIn(io) 43 local function new_LuanIn(io)
41 local this = {} 44 local this = {}
42 this.java = io 45 this.java = io
43 this.to_string = io.to_string 46 this.to_string = io.to_string
44 this.to_uri_string = io.to_uri_string
45 this.read_text = io.read_text 47 this.read_text = io.read_text
46 this.read_binary = io.read_binary 48 this.read_binary = io.read_binary
47 this.read_lines = io.read_lines 49 this.read_lines = io.read_lines
48 this.read_blocks = io.read_blocks 50 this.read_blocks = io.read_blocks
49 this.exists = io.exists 51 this.exists = io.exists
50 this.checksum = io.checksum 52 this.checksum = io.checksum
51 this.charset = io.charset 53 this.charset = io.charset
52 this.set_charset = io.set_charset 54 this.set_charset = io.set_charset
55
56 function this.to_uri_string()
57 return this.uri_string or io.to_uri_string()
58 end
59
53 return this 60 return this
54 end 61 end
55 Boot.new_LuanIn = new_LuanIn 62 Boot.new_LuanIn = new_LuanIn
56 63
57 local function new_writer(writer) 64 local function new_writer(writer)
230 return nil 237 return nil
231 end 238 end
232 return u.read_text() 239 return u.read_text()
233 end 240 end
234 241
242 function Boot.load_file(file)
243 if type(file) == "string" then
244 if not matches(file,":") then
245 file = "file:"..file
246 end
247 local u = uri(file)
248 if u==nil or not u.exists() then
249 return nil
250 end
251 local src = u.read_text()
252 return load(src,file)
253 elseif type(file) == "table" and file.read_text ~= nil then
254 local src = file.read_text()
255 return load(src,file.to_uri_string())
256 else
257 error("bad argument, expected string or uri table but got "..type(file))
258 end
259 end
260
235 261
236 local error_mt = {} 262 local error_mt = {}
237 263
238 function error_mt.__to_string(t) 264 function error_mt.__to_string(t)
239 return t.java.getLuanStackTraceString() 265 return t.java.getLuanStackTraceString()