Mercurial Hosting > luan
changeset 1424:9ab267b9427c
better load_file()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 22 Nov 2019 22:58:39 -0700 |
parents | 2c06a7ff4173 |
children | b6030fa18839 |
files | src/luan/Luan.java src/luan/host/WebHandler.java src/luan/modules/BasicLuan.java src/luan/modules/Boot.luan src/luan/modules/Luan.luan src/luan/modules/http/Server.luan |
diffstat | 6 files changed, 51 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/Luan.java Tue Nov 19 17:42:11 2019 -0700 +++ b/src/luan/Luan.java Fri Nov 22 22:58:39 2019 -0700 @@ -1,6 +1,7 @@ package luan; -import java.lang.reflect.Array; +import java.lang.reflect.Array; +import java.io.IOException; import java.util.List; import java.util.ArrayList; import java.util.Map; @@ -11,9 +12,9 @@ import java.util.Set; import goodjava.logging.Logger; import goodjava.logging.LoggerFactory; -import luan.modules.BasicLuan; import luan.modules.JavaLuan; import luan.modules.PackageLuan; +import luan.modules.IoLuan; import luan.modules.logging.LuanLogger; import luan.impl.LuanCompiler; @@ -171,15 +172,22 @@ // static public static void main(String[] args) throws LuanException { - doFile( "classpath:luan/cmd_line.luan", args ); - } - - public static void doFile(String uri,String... args) throws LuanException { - Luan luan = new Luan(); - LuanFunction fn = (LuanFunction)BasicLuan.load_file(luan,uri); + Luan luan = new Luan(); + LuanFunction fn = loadClasspath(luan,"luan/cmd_line.luan"); fn.call((Object[])args); } - + + public static LuanFunction loadClasspath(Luan luan,String classpath) + throws LuanException + { + try { + String src = IoLuan.classpath(luan,classpath).read_text(); + return luan.load(src,"classpath:"+classpath); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + public static Object first(Object obj) { if( !(obj instanceof Object[]) ) return obj;
--- a/src/luan/host/WebHandler.java Tue Nov 19 17:42:11 2019 -0700 +++ b/src/luan/host/WebHandler.java Fri Nov 22 22:58:39 2019 -0700 @@ -13,7 +13,6 @@ import luan.LuanFunction; import luan.LuanClosure; import luan.LuanRuntimeException; -import luan.modules.BasicLuan; import luan.modules.http.LuanHandler; import luan.modules.logging.Log4j; @@ -62,7 +61,7 @@ private static void initLuan(Luan luan,String dir,String domain) { security(luan,dir); try { - LuanFunction fn = BasicLuan.load_file(luan,"classpath:luan/host/init.luan"); + LuanFunction fn = Luan.loadClasspath(luan,"luan/host/init.luan"); fn.call(dir,domain); } catch(LuanException e) { throw new LuanRuntimeException(e);
--- a/src/luan/modules/BasicLuan.java Tue Nov 19 17:42:11 2019 -0700 +++ b/src/luan/modules/BasicLuan.java Fri Nov 22 22:58:39 2019 -0700 @@ -30,7 +30,7 @@ Utils.checkNotNull(sourceName,1); return luan.load(text,sourceName,env); } - +/* public static LuanFunction load_file(Luan luan,String fileName) throws LuanException { if( fileName == null ) { fileName = "stdin:"; @@ -42,7 +42,7 @@ return null; return load(luan,src,fileName,null); } - +*/ public static LuanFunction pairs(final LuanTable t) throws LuanException { Utils.checkNotNull(t); return t.pairs();
--- 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 = {}
--- a/src/luan/modules/Luan.luan Tue Nov 19 17:42:11 2019 -0700 +++ b/src/luan/modules/Luan.luan Fri Nov 22 22:58:39 2019 -0700 @@ -10,7 +10,7 @@ Luan.hash_code = BasicLuan.hash_code Luan.ipairs = BasicLuan.ipairs Luan.load = BasicLuan.load -Luan.load_file = BasicLuan.load_file +Luan.load_file = Boot.load_file Luan.new_error = BasicLuan.new_error Luan.pairs = BasicLuan.pairs Luan.pcall = BasicLuan.pcall
--- a/src/luan/modules/http/Server.luan Tue Nov 19 17:42:11 2019 -0700 +++ b/src/luan/modules/http/Server.luan Fri Nov 22 22:58:39 2019 -0700 @@ -38,7 +38,9 @@ Http.dir = dir Http.is_serving = true function Io.schemes.site(path) - return uri( dir..path ) + local u = uri( dir..path ) + u.uri_string = "site:"..path + return u end end