Mercurial Hosting > luan
changeset 119:f1bf2890d80f
support String methods
git-svn-id: https://luan-java.googlecode.com/svn/trunk@120 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 01 Jun 2014 09:38:40 +0000 |
parents | 735708619119 |
children | 8d7730a5e3b4 |
files | src/luan/LuanState.java src/luan/init.luan src/luan/lib/JavaLib.java src/luan/lib/StringLib.java src/luan/lib/init.luan |
diffstat | 5 files changed, 79 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
diff -r 735708619119 -r f1bf2890d80f src/luan/LuanState.java --- a/src/luan/LuanState.java Sun Jun 01 07:07:31 2014 +0000 +++ b/src/luan/LuanState.java Sun Jun 01 09:38:40 2014 +0000 @@ -108,14 +108,14 @@ LuanState luan = LuanCompiler.newLuanState(); luan.load(BasicLib.NAME,BasicLib.LOADER); luan.load(PackageLib.NAME,PackageLib.LOADER); - luan.load(JavaLib.NAME,JavaLib.LOADER); luan.load(MathLib.NAME,MathLib.LOADER); luan.load(StringLib.NAME,StringLib.LOADER); luan.load(TableLib.NAME,TableLib.LOADER); luan.load(HtmlLib.NAME,HtmlLib.LOADER); luan.load(BinaryLib.NAME,BinaryLib.LOADER); luan.load(IoLib.NAME,IoLib.LOADER); - BasicLib.do_java_resource(luan,"luan/init.luan"); + luan.load(JavaLib.NAME,JavaLib.LOADER); + BasicLib.do_java_resource(luan,"luan/lib/init.luan"); return luan; } catch(LuanException e) { throw new RuntimeException(e);
diff -r 735708619119 -r f1bf2890d80f src/luan/init.luan --- a/src/luan/init.luan Sun Jun 01 07:07:31 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ ---Io.stdout.write "this is init.luan\n" - -local function print(...) - local list = {} - for v in Basic.values(...) do - list[#list+1] = to_string(v) - list[#list+1] = '\t' - end - if #list == 0 then - Io.stdout.write( '\n' ) - else - list[#list] = '\n' - Io.stdout.write( Table.unpack(list) ) - end -end - -_G.print = print - - -local Debug = {} -Package.loaded.Debug = Debug -_G.Debug = Debug - -function Debug.debug(prompt) - prompt = prompt or "luan_debug> " - local function console() - return Io.read_console_line(prompt) - end - for line in console do - local fn = load(line,"stdin",true) - local result = Table.pack( fn() ) - if result.n > 0 then - print( Table.unpack(result,1,result.n) ) - end - end -end
diff -r 735708619119 -r f1bf2890d80f src/luan/lib/JavaLib.java --- a/src/luan/lib/JavaLib.java Sun Jun 01 07:07:31 2014 +0000 +++ b/src/luan/lib/JavaLib.java Sun Jun 01 09:38:40 2014 +0000 @@ -136,7 +136,8 @@ } } } - throw luan.JAVA.exception("invalid member '"+key+"' for java object: "+obj); +// throw luan.JAVA.exception("invalid member '"+key+"' for java object: "+obj); + return null; } private static Object member(Object obj,List<Member> members) throws LuanException {
diff -r 735708619119 -r f1bf2890d80f src/luan/lib/StringLib.java --- a/src/luan/lib/StringLib.java Sun Jun 01 07:07:31 2014 +0000 +++ b/src/luan/lib/StringLib.java Sun Jun 01 09:38:40 2014 +0000 @@ -9,6 +9,7 @@ import luan.LuanJavaFunction; import luan.LuanElement; import luan.LuanException; +import luan.MetatableGetter; public final class StringLib { @@ -17,6 +18,7 @@ public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { + luan.addMetatableGetter(mg); LuanTable module = new LuanTable(); try { add( module, "to_binary", String.class ); @@ -44,6 +46,42 @@ t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); } + private static final LuanTable mt = new LuanTable(); + static { + try { + add( mt, "__index", LuanState.class, String.class, Object.class ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + private static final MetatableGetter mg = new MetatableGetter() { + public LuanTable getMetatable(Object obj) { + return obj instanceof String ? mt : null; + } + }; + + public static Object __index(LuanState luan,final String s,Object key) throws LuanException { + LuanTable mod = (LuanTable)luan.global().get("String"); + if( mod!=null ) { + Object obj = mod.get(key); + if( obj instanceof LuanFunction ) { + final LuanFunction fn = (LuanFunction)obj; + return new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) throws LuanException { + Object[] a = new Object[args.length+1]; + a[0] = s; + System.arraycopy(args,0,a,1,args.length); + return fn.call(luan,a); + } + }; + } + } + if( luan.global().get("Java") != null ) + return JavaLib.__index(luan,s,key); + return null; + } + static int start(String s,int i) { return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; }
diff -r 735708619119 -r f1bf2890d80f src/luan/lib/init.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/init.luan Sun Jun 01 09:38:40 2014 +0000 @@ -0,0 +1,37 @@ +--Io.stdout.write "this is init.luan\n" + +local function print(...) + local list = {} + for v in Basic.values(...) do + list[#list+1] = to_string(v) + list[#list+1] = '\t' + end + if #list == 0 then + Io.stdout.write( '\n' ) + else + list[#list] = '\n' + Io.stdout.write( Table.unpack(list) ) + end +end + +_G.print = print + + +local Debug = {} +Package.loaded.Debug = Debug +_G.Debug = Debug + +function Debug.debug(prompt) + prompt = prompt or "luan_debug> " + local function console() + return Io.read_console_line(prompt) + end + for line in console do + local fn = load(line,"stdin",true) + local result = Table.pack( fn() ) + if result.n > 0 then + print( Table.unpack(result,1,result.n) ) + end + end +end +