Mercurial Hosting > luan
comparison src/luan/LuaState.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 | 786699c78837 |
comparison
equal
deleted
inserted
replaced
39:e5bcb1eeafc1 | 40:e3624b7cd603 |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
3 | 5 |
4 public interface LuaState { | 6 |
5 public LuaTable global(); | 7 public abstract class LuaState { |
6 public String toString(Object obj) throws LuaException; | 8 private final LuaTable global = new LuaTable(); |
7 public LuaTable getMetatable(Object obj); | 9 private final List<MetatableGetter> mtGetters = new ArrayList<MetatableGetter>(); |
8 public void addMetatableGetter(MetatableGetter mg); | 10 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); |
11 | |
12 public final LuaTable global() { | |
13 return global; | |
14 } | |
15 | |
16 public final LuaTable getMetatable(Object obj) { | |
17 if( obj instanceof LuaTable ) { | |
18 LuaTable table = (LuaTable)obj; | |
19 return table.getMetatable(); | |
20 } | |
21 for( MetatableGetter mg : mtGetters ) { | |
22 LuaTable table = mg.getMetatable(obj); | |
23 if( table != null ) | |
24 return table; | |
25 } | |
26 return null; | |
27 } | |
28 | |
29 public final void addMetatableGetter(MetatableGetter mg) { | |
30 mtGetters.add(mg); | |
31 } | |
32 | |
33 public Object[] call(LuaFunction fn,LuaElement calledFrom,String fnName,Object... args) throws LuaException { | |
34 stackTrace.add( new StackTraceElement(calledFrom,fnName) ); | |
35 try { | |
36 return fn.call(this,args); | |
37 } finally { | |
38 stackTrace.remove(stackTrace.size()-1); | |
39 } | |
40 } | |
41 | |
42 public final String checkString(LuaElement el,Object obj) throws LuaException { | |
43 String s = Lua.asString(obj); | |
44 if( s == null ) | |
45 throw new LuaException( this, el, "attempt to use a " + Lua.type(obj) + " as a string" ); | |
46 return s; | |
47 } | |
48 | |
49 public final LuaNumber checkNumber(LuaElement el,Object obj) throws LuaException { | |
50 LuaNumber n = Lua.toNumber(obj); | |
51 if( n == null ) | |
52 throw new LuaException( this, el, "attempt to perform arithmetic on a " + Lua.type(obj) + " value" ); | |
53 return n; | |
54 } | |
55 | |
56 public final LuaFunction checkFunction(LuaElement el,Object obj) throws LuaException { | |
57 if( obj instanceof LuaFunction ) | |
58 return (LuaFunction)obj; | |
59 throw new LuaException( this, el, "attempt to call a " + Lua.type(obj) + " value" ); | |
60 } | |
61 | |
62 public final String toString(LuaElement el,Object obj) throws LuaException { | |
63 LuaFunction fn = getHandlerFunction(el,"__tostring",obj); | |
64 if( fn != null ) | |
65 return checkString( el, Lua.first( call(fn,el,"__tostring",obj) ) ); | |
66 if( obj == null ) | |
67 return "nil"; | |
68 return obj.toString(); | |
69 } | |
70 | |
71 public final LuaFunction getHandlerFunction(LuaElement el,String op,Object obj) throws LuaException { | |
72 Object f = getHandler(op,obj); | |
73 if( f == null ) | |
74 return null; | |
75 return checkFunction(el,f); | |
76 } | |
77 | |
78 public final Object getHandler(String op,Object obj) { | |
79 LuaTable t = getMetatable(obj); | |
80 return t==null ? null : t.get(op); | |
81 } | |
82 | |
9 } | 83 } |