Mercurial Hosting > luan
comparison core/src/luan/LuanState.java @ 404:d55e873e1f0d
metatables now only apply to tables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 29 Apr 2015 07:04:40 -0600 |
parents | 8f1be9704726 |
children | 1b38de2b1845 |
comparison
equal
deleted
inserted
replaced
403:637f7ad85654 | 404:d55e873e1f0d |
---|---|
19 public abstract class LuanState implements DeepCloneable<LuanState> { | 19 public abstract class LuanState implements DeepCloneable<LuanState> { |
20 | 20 |
21 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); | 21 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); |
22 | 22 |
23 private LuanTableImpl registry; | 23 private LuanTableImpl registry; |
24 private LuanTableImpl metatable; // generic metatable | |
25 | 24 |
26 protected LuanState() { | 25 protected LuanState() { |
27 registry = new LuanTableImpl(); | 26 registry = new LuanTableImpl(); |
28 metatable = newMetatable(); | |
29 } | 27 } |
30 | 28 |
31 protected LuanState(LuanState luan) {} | 29 protected LuanState(LuanState luan) {} |
32 | 30 |
33 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { | 31 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { |
34 clone.registry = cloner.deepClone(registry); | 32 clone.registry = cloner.deepClone(registry); |
35 clone.metatable = cloner.deepClone(metatable); | |
36 } | 33 } |
37 | 34 |
38 public abstract LuanTable currentEnvironment(); | 35 public abstract LuanTable currentEnvironment(); |
39 public abstract LuanSource currentSource(); | 36 public abstract LuanSource currentSource(); |
40 | 37 |
57 public final Object eval(String cmd,LuanTable env) throws LuanException { | 54 public final Object eval(String cmd,LuanTable env) throws LuanException { |
58 LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); | 55 LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); |
59 return call(fn); | 56 return call(fn); |
60 } | 57 } |
61 | 58 |
62 public final LuanTable getMetatable(Object obj) { | |
63 if( obj==null ) | |
64 return null; | |
65 if( obj instanceof LuanTable ) { | |
66 LuanTable table = (LuanTable)obj; | |
67 return table.getMetatable(); | |
68 } | |
69 return metatable; | |
70 } | |
71 | |
72 public final LuanBit bit(LuanElement el) { | 59 public final LuanBit bit(LuanElement el) { |
73 return new LuanBit(this,el); | 60 return new LuanBit(this,el); |
74 } | 61 } |
75 | 62 |
76 public final Object getHandler(String op,Object obj) { | 63 public final static Object getHandler(String op,LuanTable table) { |
77 LuanTable t = getMetatable(obj); | |
78 return t==null ? null : t.get(op); | |
79 } | |
80 | |
81 public final Object getHandler(String op,LuanTable table) { | |
82 LuanTable t = table.getMetatable(); | 64 LuanTable t = table.getMetatable(); |
83 return t==null ? null : t.get(op); | 65 return t==null ? null : t.get(op); |
84 } | |
85 | |
86 private static LuanTableImpl newMetatable() { | |
87 LuanTableImpl metatable = new LuanTableImpl(); | |
88 try { | |
89 metatable.put( "__index", new LuanJavaFunction( | |
90 LuanState.class.getMethod("__index",LuanState.class,Object.class,Object.class), null | |
91 ) ); | |
92 metatable.put( "__newindex", new LuanJavaFunction( | |
93 LuanState.class.getMethod("__newindex",LuanState.class,Object.class,Object.class,Object.class), null | |
94 ) ); | |
95 } catch(NoSuchMethodException e) { | |
96 throw new RuntimeException(e); | |
97 } | |
98 return metatable; | |
99 } | |
100 | |
101 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException { | |
102 if( obj instanceof String ) { | |
103 Object rtn = StringLuan.__index(luan,(String)obj,key); | |
104 if( rtn != null ) | |
105 return rtn; | |
106 } | |
107 if( obj instanceof byte[] ) { | |
108 Object rtn = BinaryLuan.__index(luan,(byte[])obj,key); | |
109 if( rtn != null ) | |
110 return rtn; | |
111 } | |
112 return JavaLuan.__index(luan,obj,key); | |
113 } | |
114 | |
115 public static void __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException { | |
116 JavaLuan.__newindex(luan,obj,key,value); | |
117 } | 66 } |
118 | 67 |
119 // convenience methods | 68 // convenience methods |
120 | 69 |
121 private final LuanBit JAVA = bit(LuanElement.JAVA); | 70 private final LuanBit JAVA = bit(LuanElement.JAVA); |