comparison src/luan/interp/LuaStateImpl.java @ 40:e3624b7cd603

implement stack trace git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 21 Dec 2012 10:45:54 +0000
parents 8a57ebfdfd78
children 57054fa43189
comparison
equal deleted inserted replaced
39:e5bcb1eeafc1 40:e3624b7cd603
6 import luan.LuaState; 6 import luan.LuaState;
7 import luan.LuaTable; 7 import luan.LuaTable;
8 import luan.LuaFunction; 8 import luan.LuaFunction;
9 import luan.MetatableGetter; 9 import luan.MetatableGetter;
10 import luan.LuaException; 10 import luan.LuaException;
11 import luan.LuaElement;
11 12
12 13
13 final class LuaStateImpl implements LuaState { 14 final class LuaStateImpl extends LuaState {
14 private final LuaTable global = new LuaTable();
15 private final List<MetatableGetter> mtGetters = new ArrayList<MetatableGetter>();
16 15
17 @Override public LuaTable global() { 16 LuaFunction getBinHandler(LuaElement el,String op,Object o1,Object o2) throws LuaException {
18 return global; 17 LuaFunction f1 = getHandlerFunction(el,op,o1);
18 if( f1 != null )
19 return f1;
20 return getHandlerFunction(el,op,o2);
19 } 21 }
20 22
21 @Override public String toString(Object obj) throws LuaException { 23 final Object arithmetic(LuaElement el,String op,Object o1,Object o2) throws LuaException {
22 LuaFunction fn = getHandlerFunction("__tostring",obj); 24 LuaFunction fn = getBinHandler(el,op,o1,o2);
23 if( fn != null ) 25 if( fn != null )
24 return Lua.checkString( Utils.first( fn.call(this,obj) ) ); 26 return Lua.first(call(fn,el,op,o1,o2));
25 if( obj == null )
26 return "nil";
27 return obj.toString();
28 }
29
30 @Override public LuaTable getMetatable(Object obj) {
31 if( obj instanceof LuaTable ) {
32 LuaTable table = (LuaTable)obj;
33 return table.getMetatable();
34 }
35 for( MetatableGetter mg : mtGetters ) {
36 LuaTable table = mg.getMetatable(obj);
37 if( table != null )
38 return table;
39 }
40 return null;
41 }
42
43 public void addMetatableGetter(MetatableGetter mg) {
44 mtGetters.add(mg);
45 }
46
47 Object getHandler(String op,Object obj) throws LuaException {
48 LuaTable t = getMetatable(obj);
49 return t==null ? null : t.get(op);
50 }
51
52 LuaFunction getHandlerFunction(String op,Object obj) throws LuaException {
53 Object f = getHandler(op,obj);
54 if( f == null )
55 return null;
56 return Lua.checkFunction(f);
57 }
58
59 LuaFunction getBinHandler(String op,Object o1,Object o2) throws LuaException {
60 LuaFunction f1 = getHandlerFunction(op,o1);
61 if( f1 != null )
62 return f1;
63 return getHandlerFunction(op,o2);
64 }
65
66 final Object arithmetic(String op,Object o1,Object o2) throws LuaException {
67 LuaFunction fn = getBinHandler(op,o1,o2);
68 if( fn != null )
69 return Utils.first(fn.call(this,o1,o2));
70 String type = Lua.toNumber(o1)==null ? Lua.type(o1) : Lua.type(o2); 27 String type = Lua.toNumber(o1)==null ? Lua.type(o1) : Lua.type(o2);
71 throw new LuaException("attempt to perform arithmetic on a "+type+" value"); 28 throw new LuaException(this,el,"attempt to perform arithmetic on a "+type+" value");
72 } 29 }
73 30
74 31
75 private static class Frame { 32 private static class Frame {
76 final Frame previousFrame; 33 final Frame previousFrame;