Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 86:6db8f286fa6c
_ENV is per module, not global
git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 08:03:51 +0000 |
parents | 4bf3d0c0b6b9 |
children | 6ca02b188dba |
comparison
equal
deleted
inserted
replaced
85:b2551f00bc51 | 86:6db8f286fa6c |
---|---|
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; | |
13 import luan.LuanJavaFunction; | 14 import luan.LuanJavaFunction; |
14 import luan.LuanException; | 15 import luan.LuanException; |
15 import luan.LuanSource; | 16 import luan.LuanSource; |
16 import luan.LuanElement; | 17 import luan.LuanElement; |
17 import luan.interp.LuanCompiler; | 18 import luan.interp.LuanCompiler; |
19 | 20 |
20 public final class BasicLib { | 21 public final class BasicLib { |
21 | 22 |
22 public static final String NAME = "basic"; | 23 public static final String NAME = "basic"; |
23 | 24 |
24 public static final LuanFunction LOADER = new LuanFunction() { | 25 public static final LuanLoader LOADER = new LuanLoader() { |
25 public Object[] call(LuanState luan,Object[] args) throws LuanException { | 26 @Override protected void load(LuanState luan) { |
26 LuanTable global = luan.global(); | 27 LuanTable module = new LuanTable(); |
27 global.put( "_G", global ); | 28 LuanTable global = new LuanTable(); |
29 module.put( LuanState._G, global ); | |
28 try { | 30 try { |
29 global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); | 31 global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); |
30 add( global, "assert_boolean", LuanState.class, Boolean.TYPE ); | 32 add( global, "assert_boolean", LuanState.class, Boolean.TYPE ); |
31 add( global, "assert_nil", LuanState.class, Object.class ); | 33 add( global, "assert_nil", LuanState.class, Object.class ); |
32 add( global, "assert_number", LuanState.class, Number.class ); | 34 add( global, "assert_number", LuanState.class, Number.class ); |
33 add( global, "assert_string", LuanState.class, String.class ); | 35 add( global, "assert_string", LuanState.class, String.class ); |
34 add( global, "assert_table", LuanState.class, LuanTable.class ); | 36 add( global, "assert_table", LuanState.class, LuanTable.class ); |
35 add( global, "do_file", LuanState.class, String.class ); | 37 add( global, "do_file", LuanState.class, String.class, LuanTable.class ); |
36 add( global, "error", LuanState.class, Object.class ); | 38 add( global, "error", LuanState.class, Object.class ); |
37 add( global, "get_metatable", LuanState.class, Object.class ); | 39 add( global, "get_metatable", LuanState.class, Object.class ); |
38 add( global, "ipairs", LuanTable.class ); | 40 add( global, "ipairs", LuanState.class, LuanTable.class ); |
39 add( global, "load", LuanState.class, String.class, String.class ); | 41 add( global, "load", LuanState.class, String.class, String.class, LuanTable.class ); |
40 add( global, "load_file", LuanState.class, String.class ); | 42 add( global, "load_file", LuanState.class, String.class, LuanTable.class ); |
41 add( global, "pairs", LuanTable.class ); | 43 add( global, "pairs", LuanState.class, LuanTable.class ); |
42 add( global, "print", LuanState.class, new Object[0].getClass() ); | 44 add( global, "print", LuanState.class, new Object[0].getClass() ); |
43 add( global, "raw_equal", Object.class, Object.class ); | 45 add( global, "raw_equal", Object.class, Object.class ); |
44 add( global, "raw_get", LuanTable.class, Object.class ); | 46 add( global, "raw_get", LuanTable.class, Object.class ); |
45 add( global, "raw_len", LuanState.class, Object.class ); | 47 add( global, "raw_len", LuanState.class, Object.class ); |
46 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); | 48 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); |
47 add( global, "set_metatable", LuanTable.class, LuanTable.class ); | 49 add( global, "set_metatable", LuanTable.class, LuanTable.class ); |
48 add( global, "to_number", Object.class, Integer.class ); | 50 add( global, "to_number", Object.class, Integer.class ); |
49 add( global, "to_string", LuanState.class, Object.class ); | 51 add( global, "to_string", LuanState.class, Object.class ); |
50 add( global, "type", Object.class ); | 52 add( global, "type", Object.class ); |
51 global.put( "_VERSION", Luan.version ); | 53 global.put( "_VERSION", Luan.version ); |
52 | |
53 add( global, "make_standard", LuanState.class ); | |
54 } catch(NoSuchMethodException e) { | 54 } catch(NoSuchMethodException e) { |
55 throw new RuntimeException(e); | 55 throw new RuntimeException(e); |
56 } | 56 } |
57 return LuanFunction.EMPTY_RTN; | 57 luan.loaded().put(NAME,module); |
58 } | 58 } |
59 }; | 59 }; |
60 | 60 |
61 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 61 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
62 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 62 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
63 } | |
64 | |
65 public static void make_standard(LuanState luan) { | |
66 LuanTable global = luan.global(); | |
67 global.put( "dofile", global.get("do_file") ); | |
68 global.put( "getmetatable", global.get("get_metatable") ); | |
69 global.put( "loadfile", global.get("load_file") ); | |
70 global.put( "rawequal", global.get("raw_equal") ); | |
71 global.put( "rawget", global.get("raw_get") ); | |
72 global.put( "rawlen", global.get("raw_len") ); | |
73 global.put( "rawset", global.get("raw_set") ); | |
74 global.put( "setmetatable", global.get("set_metatable") ); | |
75 global.put( "tonumber", global.get("to_number") ); | |
76 global.put( "tostring", global.get("to_string") ); | |
77 } | 63 } |
78 | 64 |
79 public static void print(LuanState luan,Object... args) throws LuanException { | 65 public static void print(LuanState luan,Object... args) throws LuanException { |
80 for( int i=0; i<args.length; i++ ) { | 66 for( int i=0; i<args.length; i++ ) { |
81 if( i > 0 ) | 67 if( i > 0 ) |
87 | 73 |
88 public static String type(Object obj) { | 74 public static String type(Object obj) { |
89 return Luan.type(obj); | 75 return Luan.type(obj); |
90 } | 76 } |
91 | 77 |
92 public static LuanFunction load(LuanState luan,String text,String sourceName) throws LuanException { | 78 public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env) throws LuanException { |
93 return LuanCompiler.compile(luan,new LuanSource(sourceName,text)); | 79 return LuanCompiler.compile(luan,new LuanSource(sourceName,text),env); |
94 } | 80 } |
95 | 81 |
96 | 82 |
97 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { | 83 public static LuanFunction load_file(LuanState luan,String fileName,LuanTable env) throws LuanException { |
98 try { | 84 try { |
99 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); | 85 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); |
100 return load(luan,src,fileName); | 86 return load(luan,src,fileName,env); |
101 } catch(IOException e) { | 87 } catch(IOException e) { |
102 throw new LuanException(luan,LuanElement.JAVA,e); | 88 throw new LuanException(luan,LuanElement.JAVA,e); |
103 } | 89 } |
104 } | 90 } |
105 | 91 |
106 public static Object[] do_file(LuanState luan,String fileName) throws LuanException { | 92 public static Object[] do_file(LuanState luan,String fileName,LuanTable env) throws LuanException { |
107 LuanFunction fn = load_file(luan,fileName); | 93 LuanFunction fn = load_file(luan,fileName,env); |
108 return luan.call(fn,LuanElement.JAVA,null); | 94 return luan.call(fn,LuanElement.JAVA,null); |
109 } | 95 } |
110 | 96 |
111 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 97 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
112 return new LuanFunction() { | 98 return new LuanFunction() { |
113 public Object[] call(LuanState luan,Object[] args) { | 99 public Object[] call(LuanState luan,Object[] args) { |
114 if( !iter.hasNext() ) | 100 if( !iter.hasNext() ) |
115 return LuanFunction.EMPTY_RTN; | 101 return LuanFunction.EMPTY; |
116 Map.Entry<Object,Object> entry = iter.next(); | 102 Map.Entry<Object,Object> entry = iter.next(); |
117 return new Object[]{entry.getKey(),entry.getValue()}; | 103 return new Object[]{entry.getKey(),entry.getValue()}; |
118 } | 104 } |
119 }; | 105 }; |
120 } | 106 } |
121 | 107 |
122 public static LuanFunction pairs(LuanTable t) { | 108 public static LuanFunction pairs(LuanState luan,LuanTable t) throws LuanException { |
109 Utils.checkNotNull(luan,t,"table"); | |
123 return pairs( t.iterator() ); | 110 return pairs( t.iterator() ); |
124 } | 111 } |
125 | 112 |
126 public static LuanFunction ipairs(LuanTable t) { | 113 public static LuanFunction ipairs(LuanState luan,LuanTable t) throws LuanException { |
114 Utils.checkNotNull(luan,t,"table"); | |
127 return pairs( t.listIterator() ); | 115 return pairs( t.listIterator() ); |
128 } | 116 } |
129 | 117 |
130 public static LuanTable get_metatable(LuanState luan,Object obj) { | 118 public static LuanTable get_metatable(LuanState luan,Object obj) { |
131 return luan.getMetatable(obj); | 119 return luan.getMetatable(obj); |
179 if( msg == null ) | 167 if( msg == null ) |
180 msg = "assertion failed!"; | 168 msg = "assertion failed!"; |
181 throw new LuanException( luan, LuanElement.JAVA, msg ); | 169 throw new LuanException( luan, LuanElement.JAVA, msg ); |
182 } | 170 } |
183 | 171 |
184 private static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException { | |
185 if( v == null ) | |
186 throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 ("+expected+" expected, got nil)"); | |
187 } | |
188 | |
189 public static String assert_string(LuanState luan,String v) throws LuanException { | 172 public static String assert_string(LuanState luan,String v) throws LuanException { |
190 checkNotNull(luan,v,"string"); | 173 Utils.checkNotNull(luan,v,"string"); |
191 return v; | 174 return v; |
192 } | 175 } |
193 | 176 |
194 public static Number assert_number(LuanState luan,Number v) throws LuanException { | 177 public static Number assert_number(LuanState luan,Number v) throws LuanException { |
195 checkNotNull(luan,v,"number"); | 178 Utils.checkNotNull(luan,v,"number"); |
196 return v; | 179 return v; |
197 } | 180 } |
198 | 181 |
199 public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException { | 182 public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException { |
200 checkNotNull(luan,v,"table"); | 183 Utils.checkNotNull(luan,v,"table"); |
201 return v; | 184 return v; |
202 } | 185 } |
203 | 186 |
204 public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException { | 187 public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException { |
205 return v; | 188 return v; |