Mercurial Hosting > luan
changeset 74:f003338d503b
improve package lib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@75 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 13 Feb 2013 06:27:56 +0000 |
parents | f86e4f77ef32 |
children | aa7538ae5fb6 |
files | src/luan/LuanState.java src/luan/lib/HtmlLib.java src/luan/lib/JavaLib.java src/luan/lib/MathLib.java src/luan/lib/PackageLib.java src/luan/lib/StringLib.java src/luan/lib/TableLib.java |
diffstat | 7 files changed, 28 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
diff -r f86e4f77ef32 -r f003338d503b src/luan/LuanState.java --- a/src/luan/LuanState.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/LuanState.java Wed Feb 13 06:27:56 2013 +0000 @@ -28,16 +28,13 @@ final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); - public Object load(LuanFunction loader,String modName) throws LuanException { - return load(loader,modName,null); - } - - public Object load(LuanFunction loader,String modName,Object extra) throws LuanException { - Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName,extra)); + public void load(LuanFunction loader,String modName) throws LuanException { + Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName)); if( mod == null ) mod = true; loaded.put(modName,mod); - return mod; + if( mod instanceof LuanTable ) + global.put(modName,mod); } public static LuanState newStandard() {
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/HtmlLib.java --- a/src/luan/lib/HtmlLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/HtmlLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -14,13 +14,12 @@ public Object[] call(LuanState luan,Object[] args) { LuanTable module = new LuanTable(); LuanTable global = luan.global; - global.put(NAME,module); try { add( module, "encode", String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } };
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/JavaLib.java --- a/src/luan/lib/JavaLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/JavaLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -34,7 +34,6 @@ luan.addMetatableGetter(mg); LuanTable module = new LuanTable(); LuanTable global = luan.global; - global.put(NAME,module); try { global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) ); module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) ); @@ -42,7 +41,7 @@ } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } };
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/MathLib.java --- a/src/luan/lib/MathLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/MathLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -14,13 +14,12 @@ public Object[] call(LuanState luan,Object[] args) { LuanTable module = new LuanTable(); LuanTable global = luan.global; - global.put(NAME,module); try { add( module, "floor", Double.TYPE ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } };
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/PackageLib.java --- a/src/luan/lib/PackageLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/PackageLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -21,12 +21,12 @@ LuanTable global = luan.global; LuanTable module = new LuanTable(); List<Object> searchers = new ArrayList<Object>(); - global.put(NAME,module); module.put("loaded",luan.loaded); module.put("preload",luan.preload); module.put("path","?.lua"); try { add( global, "require", LuanState.class, String.class ); + add( module, "module", LuanState.class, String.class ); add( module, "search_path", String.class, String.class ); searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("preloadSearcher",LuanState.class,String.class),null) ); searchers.add( new LuanJavaFunction(PackageLib.class.getMethod("fileSearcher",LuanState.class,String.class),null) ); @@ -34,7 +34,7 @@ throw new RuntimeException(e); } module.put("searchers",new LuanTable(searchers)); - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } }; @@ -42,7 +42,13 @@ t.put( method, new LuanJavaFunction(PackageLib.class.getMethod(method,parameterTypes),null) ); } - public static Object require(LuanState luan,String modName) throws LuanException { + public static void require(LuanState luan,String modName) throws LuanException { + Object mod = module(luan,modName); + if( mod instanceof LuanTable ) + luan.global.put(modName,mod); + } + + public static Object module(LuanState luan,String modName) throws LuanException { LuanTable module = (LuanTable)luan.global.get(NAME); Object mod = luan.loaded.get(modName); if( mod == null ) { @@ -53,7 +59,10 @@ if( a.length >= 1 && a[0] instanceof LuanFunction ) { LuanFunction loader = (LuanFunction)a[0]; Object extra = a.length >= 2 ? a[1] : null; - mod = luan.load(loader,modName,extra); + mod = Luan.first(luan.call(loader,LuanElement.JAVA,"loader",modName,extra)); + if( mod == null ) + mod = true; + luan.loaded.put(modName,mod); } } if( mod == null ) @@ -62,9 +71,9 @@ return mod; } - public static String search_path(String NAME,String path) { + public static String search_path(String name,String path) { for( String s : path.split(";") ) { - String file = s.replaceAll("\\?",NAME); + String file = s.replaceAll("\\?",name); if( new File(file).exists() ) return file; } @@ -73,7 +82,10 @@ private static final LuanFunction fileLoader = new LuanFunction() { public Object[] call(LuanState luan,Object[] args) throws LuanException { - return BasicLib.do_file(luan,(String)args[1]); + String modName = (String)args[0]; + String fileName = (String)args[1]; + LuanFunction fn = BasicLib.load_file(luan,fileName); + return luan.call(fn,LuanElement.JAVA,modName,args); } };
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/StringLib.java --- a/src/luan/lib/StringLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/StringLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -19,7 +19,6 @@ public Object[] call(LuanState luan,Object[] args) throws LuanException { LuanTable module = new LuanTable(); LuanTable global = luan.global; - global.put(NAME,module); try { module.put( "byte", new LuanJavaFunction(StringLib.class.getMethod("byte_",String.class,Integer.class,Integer.class),null) ); module.put( "char", new LuanJavaFunction(StringLib.class.getMethod("char_",new byte[0].getClass()),null) ); @@ -36,7 +35,7 @@ } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } };
diff -r f86e4f77ef32 -r f003338d503b src/luan/lib/TableLib.java --- a/src/luan/lib/TableLib.java Tue Feb 12 09:46:45 2013 +0000 +++ b/src/luan/lib/TableLib.java Wed Feb 13 06:27:56 2013 +0000 @@ -20,7 +20,6 @@ public Object[] call(LuanState luan,Object[] args) throws LuanException { LuanTable module = new LuanTable(); LuanTable global = luan.global; - global.put(NAME,module); try { add( module, "concat", LuanState.class, LuanTable.class, String.class, Integer.class, Integer.class ); add( module, "insert", LuanState.class, LuanTable.class, Integer.TYPE, Object.class ); @@ -32,7 +31,7 @@ } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - return LuanFunction.EMPTY_RTN; + return new Object[]{module}; } };