diff 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
line wrap: on
line diff
--- a/src/luan/modules/Boot.luan	Tue Nov 19 17:42:11 2019 -0700
+++ b/src/luan/modules/Boot.luan	Fri Nov 22 22:58:39 2019 -0700
@@ -6,8 +6,11 @@
 local ipairs = BasicLuan.ipairs
 local set_metatable = BasicLuan.set_metatable
 local try = BasicLuan.try_
+local load = BasicLuan.load
+local type = BasicLuan.type
 local StringLuan = require "java:luan.modules.StringLuan"
 local match = StringLuan.match  -- String.match
+local matches = StringLuan.matches  -- String.matches
 local IoLuan = require "java:luan.modules.IoLuan"
 local LuanUrl = require "java:luan.modules.url.LuanUrl"
 local LuanJava = require "java:luan.Luan"
@@ -41,7 +44,6 @@
 	local this = {}
 	this.java = io
 	this.to_string = io.to_string
-	this.to_uri_string = io.to_uri_string
 	this.read_text = io.read_text
 	this.read_binary = io.read_binary
 	this.read_lines = io.read_lines
@@ -50,6 +52,11 @@
 	this.checksum = io.checksum
 	this.charset = io.charset
 	this.set_charset = io.set_charset
+
+	function this.to_uri_string()
+		return this.uri_string or io.to_uri_string()
+	end
+
 	return this
 end
 Boot.new_LuanIn = new_LuanIn
@@ -232,6 +239,25 @@
 	return u.read_text()
 end
 
+function Boot.load_file(file)
+	if type(file) == "string" then
+		if not matches(file,":") then
+			file = "file:"..file
+		end
+		local u = uri(file)
+		if u==nil or not u.exists() then
+			return nil
+		end
+		local src = u.read_text()
+		return load(src,file)
+	elseif type(file) == "table" and file.read_text ~= nil then
+		local src = file.read_text()
+		return load(src,file.to_uri_string())
+	else
+		error("bad argument, expected string or uri table but got "..type(file))
+	end
+end
+
 
 local error_mt = {}