Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 108:3c404a296995
make Package module more standard;
return _ENV by default;
add "import" statement;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@109 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 23 May 2014 03:21:54 +0000 |
parents | b1e87f1bcc13 |
children | 219e05867366 |
comparison
equal
deleted
inserted
replaced
107:dbf459397217 | 108:3c404a296995 |
---|---|
8 import java.util.Map; | 8 import java.util.Map; |
9 import luan.Luan; | 9 import luan.Luan; |
10 import luan.LuanState; | 10 import luan.LuanState; |
11 import luan.LuanTable; | 11 import luan.LuanTable; |
12 import luan.LuanFunction; | 12 import luan.LuanFunction; |
13 import luan.LuanLoader; | |
14 import luan.LuanJavaFunction; | 13 import luan.LuanJavaFunction; |
15 import luan.LuanException; | 14 import luan.LuanException; |
16 import luan.LuanSource; | 15 import luan.LuanSource; |
17 import luan.LuanElement; | 16 import luan.LuanElement; |
18 import luan.interp.LuanCompiler; | 17 import luan.interp.LuanCompiler; |
20 | 19 |
21 public final class BasicLib { | 20 public final class BasicLib { |
22 | 21 |
23 public static final String NAME = "Basic"; | 22 public static final String NAME = "Basic"; |
24 | 23 |
25 public static final LuanLoader LOADER = new LuanLoader() { | 24 public static final LuanFunction LOADER = new LuanFunction() { |
26 @Override protected void load(LuanState luan) { | 25 @Override public Object[] call(LuanState luan,Object[] args) { |
27 LuanTable module = new LuanTable(); | 26 LuanTable module = new LuanTable(); |
28 LuanTable global = new LuanTable(); | 27 LuanTable global = luan.global(); |
29 module.put( LuanState._G, global ); | |
30 try { | 28 try { |
31 global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); | 29 global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); |
32 add( global, "assert_boolean", LuanState.class, Boolean.TYPE ); | 30 add( global, "assert_boolean", LuanState.class, Boolean.TYPE ); |
33 add( global, "assert_nil", LuanState.class, Object.class ); | 31 add( global, "assert_nil", LuanState.class, Object.class ); |
34 add( global, "assert_number", LuanState.class, Number.class ); | 32 add( global, "assert_number", LuanState.class, Number.class ); |
35 add( global, "assert_string", LuanState.class, String.class ); | 33 add( global, "assert_string", LuanState.class, String.class ); |
36 add( global, "assert_table", LuanState.class, LuanTable.class ); | 34 add( global, "assert_table", LuanState.class, LuanTable.class ); |
37 add( global, "do_file", LuanState.class, String.class, LuanTable.class ); | 35 add( global, "do_file", LuanState.class, String.class ); |
38 add( global, "error", LuanState.class, Object.class ); | 36 add( global, "error", LuanState.class, Object.class ); |
39 add( global, "get_metatable", LuanState.class, Object.class ); | 37 add( global, "get_metatable", LuanState.class, Object.class ); |
40 add( global, "ipairs", LuanState.class, LuanTable.class ); | 38 add( global, "ipairs", LuanState.class, LuanTable.class ); |
41 add( global, "load", LuanState.class, String.class, String.class, LuanTable.class ); | 39 add( global, "load", LuanState.class, String.class, String.class, Boolean.class ); |
42 add( global, "load_file", LuanState.class, String.class, LuanTable.class ); | 40 add( global, "load_file", LuanState.class, String.class ); |
43 add( global, "pairs", LuanState.class, LuanTable.class ); | 41 add( global, "pairs", LuanState.class, LuanTable.class ); |
44 add( global, "print", LuanState.class, new Object[0].getClass() ); | 42 add( global, "print", LuanState.class, new Object[0].getClass() ); |
45 add( global, "raw_equal", Object.class, Object.class ); | 43 add( global, "raw_equal", Object.class, Object.class ); |
46 add( global, "raw_get", LuanTable.class, Object.class ); | 44 add( global, "raw_get", LuanTable.class, Object.class ); |
47 add( global, "raw_len", LuanState.class, Object.class ); | 45 add( global, "raw_len", LuanState.class, Object.class ); |
53 add( global, "type", Object.class ); | 51 add( global, "type", Object.class ); |
54 global.put( "_VERSION", Luan.version ); | 52 global.put( "_VERSION", Luan.version ); |
55 } catch(NoSuchMethodException e) { | 53 } catch(NoSuchMethodException e) { |
56 throw new RuntimeException(e); | 54 throw new RuntimeException(e); |
57 } | 55 } |
58 luan.loaded().put(NAME,module); | 56 return new Object[]{module}; |
59 } | 57 } |
60 }; | 58 }; |
61 | 59 |
62 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 60 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
63 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 61 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
74 | 72 |
75 public static String type(Object obj) { | 73 public static String type(Object obj) { |
76 return Luan.type(obj); | 74 return Luan.type(obj); |
77 } | 75 } |
78 | 76 |
79 public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env) throws LuanException { | 77 public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean interactive) throws LuanException { |
80 return LuanCompiler.compile(luan,new LuanSource(sourceName,text),env); | 78 if( interactive!=null && interactive ) |
81 } | 79 return LuanCompiler.compileInteractive(luan,new LuanSource(sourceName,text)); |
82 | 80 else |
83 | 81 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text)); |
84 public static LuanFunction load_file(LuanState luan,String fileName,LuanTable env) throws LuanException { | 82 } |
83 | |
84 | |
85 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { | |
85 try { | 86 try { |
86 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); | 87 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); |
87 return load(luan,src,fileName,env); | 88 return load(luan,src,fileName,false); |
88 } catch(IOException e) { | 89 } catch(IOException e) { |
89 throw luan.JAVA.exception(e); | 90 throw luan.JAVA.exception(e); |
90 } | 91 } |
91 } | 92 } |
92 | 93 |
93 public static Object[] do_file(LuanState luan,String fileName,LuanTable env) throws LuanException { | 94 public static Object[] do_file(LuanState luan,String fileName) throws LuanException { |
94 LuanFunction fn = load_file(luan,fileName,env); | 95 LuanFunction fn = load_file(luan,fileName); |
95 return luan.JAVA.call(fn,null); | 96 return luan.JAVA.call(fn,null); |
96 } | 97 } |
97 | 98 |
98 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 99 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
99 return new LuanFunction() { | 100 return new LuanFunction() { |