comparison core/src/luan/modules/BasicLuan.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/modules/BasicLuan.java@ebe9db183eb7
children 75750ceb45ee
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
1 package luan.modules;
2
3 import java.io.File;
4 import java.io.InputStreamReader;
5 import java.io.IOException;
6 import java.lang.reflect.Method;
7 import java.util.Iterator;
8 import java.util.Map;
9 import java.util.List;
10 import java.util.ArrayList;
11 import luan.Luan;
12 import luan.LuanState;
13 import luan.LuanTable;
14 import luan.LuanFunction;
15 import luan.LuanJavaFunction;
16 import luan.LuanException;
17 import luan.LuanSource;
18 import luan.LuanElement;
19 import luan.impl.LuanCompiler;
20
21
22 public final class BasicLuan {
23
24 public static final LuanFunction LOADER = new LuanFunction() {
25 @Override public Object call(LuanState luan,Object[] args) {
26 LuanTable module = new LuanTable();
27 try {
28 module.put( "assert", new LuanJavaFunction(BasicLuan.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) );
29 add( module, "assert_boolean", LuanState.class, Boolean.TYPE );
30 add( module, "assert_nil", LuanState.class, Object.class );
31 add( module, "assert_number", LuanState.class, Number.class );
32 add( module, "assert_string", LuanState.class, String.class );
33 add( module, "assert_table", LuanState.class, LuanTable.class );
34 add( module, "do_file", LuanState.class, String.class );
35 add( module, "error", LuanState.class, Object.class );
36 add( module, "get_metatable", LuanState.class, Object.class );
37 add( module, "ipairs", LuanState.class, LuanTable.class );
38 add( module, "load", LuanState.class, String.class, String.class, LuanTable.class, Boolean.class );
39 add( module, "load_file", LuanState.class, String.class );
40 add( module, "pairs", LuanState.class, LuanTable.class );
41 add( module, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class );
42 add( module, "raw_equal", Object.class, Object.class );
43 add( module, "raw_get", LuanTable.class, Object.class );
44 add( module, "raw_len", LuanState.class, Object.class );
45 add( module, "raw_set", LuanTable.class, Object.class, Object.class );
46 add( module, "repr", LuanState.class, Object.class );
47 add( module, "set_metatable", LuanTable.class, LuanTable.class );
48 add( module, "to_number", Object.class, Integer.class );
49 add( module, "to_string", LuanState.class, Object.class );
50 add( module, "type", Object.class );
51 add( module, "values", new Object[0].getClass() );
52 } catch(NoSuchMethodException e) {
53 throw new RuntimeException(e);
54 }
55 return module;
56 }
57 };
58
59 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
60 t.put( method, new LuanJavaFunction(BasicLuan.class.getMethod(method,parameterTypes),null) );
61 }
62
63 public static String type(Object obj) {
64 return Luan.type(obj);
65 }
66
67 public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env,Boolean allowExpr)
68 throws LuanException
69 {
70 if( allowExpr==null )
71 allowExpr = false;
72 return LuanCompiler.compile(luan,new LuanSource(sourceName,text),env,allowExpr);
73 }
74
75 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
76 try {
77 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : IoLuan.luanIo(luan,fileName).read_text();
78 return load(luan,src,fileName,null,false);
79 } catch(IOException e) {
80 throw luan.exception(e);
81 }
82 }
83
84 public static Object do_file(LuanState luan,String fileName) throws LuanException {
85 LuanFunction fn = load_file(luan,fileName);
86 return luan.call(fn);
87 }
88
89 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) {
90 return new LuanFunction() {
91 @Override public Object[] call(LuanState luan,Object[] args) {
92 if( !iter.hasNext() )
93 return LuanFunction.NOTHING;
94 Map.Entry<Object,Object> entry = iter.next();
95 return new Object[]{entry.getKey(),entry.getValue()};
96 }
97 };
98 }
99
100 public static LuanFunction pairs(LuanState luan,LuanTable t) throws LuanException {
101 Utils.checkNotNull(luan,t,"table");
102 return pairs( t.iterator() );
103 }
104
105 public static LuanFunction ipairs(LuanState luan,LuanTable t) throws LuanException {
106 Utils.checkNotNull(luan,t,"table");
107 return pairs( t.listIterator() );
108 }
109
110 public static LuanTable get_metatable(LuanState luan,Object obj) {
111 return luan.getMetatable(obj);
112 }
113
114 public static LuanTable set_metatable(LuanTable table,LuanTable metatable) {
115 table.setMetatable(metatable);
116 return table;
117 }
118
119 public static boolean raw_equal(Object v1,Object v2) {
120 return v1 == v2 || v1 != null && v1.equals(v2);
121 }
122
123 public static Object raw_get(LuanTable table,Object index) {
124 return table.get(index);
125 }
126
127 public static LuanTable raw_set(LuanTable table,Object index,Object value) {
128 table.put(index,value);
129 return table;
130 }
131
132 public static int raw_len(LuanState luan,Object v) throws LuanException {
133 if( v instanceof String ) {
134 String s = (String)v;
135 return s.length();
136 }
137 if( v instanceof LuanTable ) {
138 LuanTable t = (LuanTable)v;
139 return t.length();
140 }
141 throw luan.exception( "bad argument #1 to 'raw_len' (table or string expected)" );
142 }
143
144 public static Number to_number(Object e,Integer base) {
145 return Luan.toNumber(e,base);
146 }
147
148 public static String to_string(LuanState luan,Object v) throws LuanException {
149 return luan.toString(v);
150 }
151
152 public static void error(LuanState luan,Object msg) throws LuanException {
153 throw luan.exception(msg);
154 }
155
156 public static Object assert_(LuanState luan,Object v,String msg) throws LuanException {
157 if( Luan.toBoolean(v) )
158 return v;
159 if( msg == null )
160 msg = "assertion failed!";
161 throw luan.exception( msg );
162 }
163
164 public static String assert_string(LuanState luan,String v) throws LuanException {
165 Utils.checkNotNull(luan,v,"string");
166 return v;
167 }
168
169 public static Number assert_number(LuanState luan,Number v) throws LuanException {
170 Utils.checkNotNull(luan,v,"number");
171 return v;
172 }
173
174 public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException {
175 Utils.checkNotNull(luan,v,"table");
176 return v;
177 }
178
179 public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException {
180 return v;
181 }
182
183 public static Object assert_nil(LuanState luan,Object v) throws LuanException {
184 if( v != null )
185 throw luan.exception("bad argument #1 (nil expected, got "+Luan.type(v)+")");
186 return v;
187 }
188
189 public static String repr(LuanState luan,Object v) throws LuanException {
190 return luan.repr(v);
191 }
192
193 public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
194 final double step = stepV==null ? 1.0 : stepV;
195 if( step == 0.0 )
196 throw luan.exception("bad argument #3 (step may not be zero)");
197 return new LuanFunction() {
198 double v = from;
199
200 @Override public Object call(LuanState luan,Object[] args) {
201 if( step > 0.0 && v > to || step < 0.0 && v < to )
202 return LuanFunction.NOTHING;
203 double rtn = v;
204 v += step;
205 return rtn;
206 }
207 };
208 }
209
210 public static LuanFunction values(final Object... args) throws LuanException {
211 return new LuanFunction() {
212 int i = 0;
213
214 @Override public Object call(LuanState luan,Object[] unused) {
215 if( ++i > args.length )
216 return LuanFunction.NOTHING;
217 return new Object[]{i,args[i-1]};
218 }
219 };
220 }
221
222 }