comparison src/luan/LuanState.java @ 88:6ca02b188dba

add LuanBit to clean up code; add repr(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@89 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 23:50:32 +0000
parents 6db8f286fa6c
children 1f8b6edc2b08
comparison
equal deleted inserted replaced
87:eaf37cfa30c2 88:6ca02b188dba
16 import luan.lib.HtmlLib; 16 import luan.lib.HtmlLib;
17 17
18 18
19 public abstract class LuanState implements DeepCloneable<LuanState> { 19 public abstract class LuanState implements DeepCloneable<LuanState> {
20 public static final String _G = "_G"; 20 public static final String _G = "_G";
21
22 public final LuanBit JAVA = bit(LuanElement.JAVA);
23 public final LuanBit COMPILER = bit(LuanElement.COMPILER);
21 24
22 private LuanTable loaded; 25 private LuanTable loaded;
23 private LuanTable preload; 26 private LuanTable preload;
24 private final List<String> defaultMods; 27 private final List<String> defaultMods;
25 28
122 } 125 }
123 } 126 }
124 127
125 public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException { 128 public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException {
126 LuanFunction fn = BasicLib.load(this,cmd,sourceName,env); 129 LuanFunction fn = BasicLib.load(this,cmd,sourceName,env);
127 return call(fn,null,null); 130 return JAVA.call(fn,null);
128 } 131 }
129 132
130 133
131 public final LuanTable getMetatable(Object obj) { 134 public final LuanTable getMetatable(Object obj) {
132 if( obj instanceof LuanTable ) { 135 if( obj instanceof LuanTable ) {
143 146
144 public final void addMetatableGetter(MetatableGetter mg) { 147 public final void addMetatableGetter(MetatableGetter mg) {
145 mtGetters.add(mg); 148 mtGetters.add(mg);
146 } 149 }
147 150
148 public final Object[] call(LuanFunction fn,LuanElement calledFrom,String fnName,Object... args) throws LuanException { 151 public final LuanBit bit(LuanElement el) {
149 stackTrace.add( new StackTraceElement(calledFrom,fnName) ); 152 return new LuanBit(this,el);
150 try {
151 return fn.call(this,args);
152 } finally {
153 stackTrace.remove(stackTrace.size()-1);
154 }
155 }
156
157 public final String checkString(LuanElement el,Object obj) throws LuanException {
158 String s = Luan.asString(obj);
159 if( s == null )
160 throw new LuanException( this, el, "attempt to use a " + Luan.type(obj) + " as a string" );
161 return s;
162 }
163
164 public final Number checkNumber(LuanElement el,Object obj) throws LuanException {
165 Number n = Luan.toNumber(obj);
166 if( n == null )
167 throw new LuanException( this, el, "attempt to perform arithmetic on a " + Luan.type(obj) + " value" );
168 return n;
169 }
170
171 public final LuanFunction checkFunction(LuanElement el,Object obj) throws LuanException {
172 if( obj instanceof LuanFunction )
173 return (LuanFunction)obj;
174 throw new LuanException( this, el, "attempt to call a " + Luan.type(obj) + " value" );
175 }
176
177 public final String toString(LuanElement el,Object obj) throws LuanException {
178 LuanFunction fn = getHandlerFunction(el,"__tostring",obj);
179 if( fn != null )
180 return checkString( el, Luan.first( call(fn,el,"__tostring",obj) ) );
181 return Luan.toString(obj);
182 }
183
184 public final LuanFunction getHandlerFunction(LuanElement el,String op,Object obj) throws LuanException {
185 Object f = getHandler(op,obj);
186 if( f == null )
187 return null;
188 return checkFunction(el,f);
189 } 153 }
190 154
191 public final Object getHandler(String op,Object obj) { 155 public final Object getHandler(String op,Object obj) {
192 LuanTable t = getMetatable(obj); 156 LuanTable t = getMetatable(obj);
193 return t==null ? null : t.get(op); 157 return t==null ? null : t.get(op);
194 } 158 }
195 159
196
197 public final LuanFunction getBinHandler(LuanElement el,String op,Object o1,Object o2) throws LuanException {
198 LuanFunction f1 = getHandlerFunction(el,op,o1);
199 if( f1 != null )
200 return f1;
201 return getHandlerFunction(el,op,o2);
202 }
203
204 public final boolean isLessThan(LuanElement el,Object o1,Object o2) throws LuanException {
205 if( o1 instanceof Number && o2 instanceof Number ) {
206 Number n1 = (Number)o1;
207 Number n2 = (Number)o2;
208 return n1.doubleValue() < n2.doubleValue();
209 }
210 if( o1 instanceof String && o2 instanceof String ) {
211 String s1 = (String)o1;
212 String s2 = (String)o2;
213 return s1.compareTo(s2) < 0;
214 }
215 LuanFunction fn = getBinHandler(el,"__lt",o1,o2);
216 if( fn != null )
217 return Luan.toBoolean( Luan.first(call(fn,el,"__lt",o1,o2)) );
218 throw new LuanException( this, el, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
219 }
220 } 160 }