Mercurial Hosting > luan
comparison src/luan/interp/LuaStateImpl.java @ 37:8a57ebfdfd78
add JavaLib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@38 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 20 Dec 2012 02:36:07 +0000 |
parents | 2a35154aec14 |
children | e3624b7cd603 |
comparison
equal
deleted
inserted
replaced
36:2a35154aec14 | 37:8a57ebfdfd78 |
---|---|
1 package luan.interp; | 1 package luan.interp; |
2 | 2 |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
3 import luan.Lua; | 5 import luan.Lua; |
4 import luan.LuaState; | 6 import luan.LuaState; |
5 import luan.LuaTable; | 7 import luan.LuaTable; |
6 import luan.LuaFunction; | 8 import luan.LuaFunction; |
9 import luan.MetatableGetter; | |
7 import luan.LuaException; | 10 import luan.LuaException; |
8 | 11 |
9 | 12 |
10 final class LuaStateImpl implements LuaState { | 13 final class LuaStateImpl implements LuaState { |
11 private final LuaTable global = new LuaTable(); | 14 private final LuaTable global = new LuaTable(); |
15 private final List<MetatableGetter> mtGetters = new ArrayList<MetatableGetter>(); | |
12 | 16 |
13 @Override public LuaTable global() { | 17 @Override public LuaTable global() { |
14 return global; | 18 return global; |
15 } | 19 } |
16 | 20 |
17 @Override public String toString(Object obj) throws LuaException { | 21 @Override public String toString(Object obj) throws LuaException { |
18 LuaFunction fn = Utils.getHandlerFunction("__tostring",obj); | 22 LuaFunction fn = getHandlerFunction("__tostring",obj); |
19 if( fn != null ) | 23 if( fn != null ) |
20 return Lua.checkString( Utils.first( fn.call(this,obj) ) ); | 24 return Lua.checkString( Utils.first( fn.call(this,obj) ) ); |
21 if( obj == null ) | 25 if( obj == null ) |
22 return "nil"; | 26 return "nil"; |
23 return obj.toString(); | 27 return obj.toString(); |
24 } | 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); | |
71 throw new LuaException("attempt to perform arithmetic on a "+type+" value"); | |
72 } | |
73 | |
25 | 74 |
26 private static class Frame { | 75 private static class Frame { |
27 final Frame previousFrame; | 76 final Frame previousFrame; |
28 final LuaClosure closure; | 77 final LuaClosure closure; |
29 final Object[] stack; | 78 final Object[] stack; |