Mercurial Hosting > luan
comparison core/src/luan/modules/JavaLuan.java @ 195:24ede40ee0aa
make MetatableGetter DeepCloneable, scoped, and secure
git-svn-id: https://luan-java.googlecode.com/svn/trunk@196 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 03 Jul 2014 08:19:48 +0000 |
parents | 3dcb0f9bee82 |
children | be0275bda373 |
comparison
equal
deleted
inserted
replaced
194:08df375e2e5f | 195:24ede40ee0aa |
---|---|
22 import luan.MetatableGetter; | 22 import luan.MetatableGetter; |
23 import luan.LuanException; | 23 import luan.LuanException; |
24 import luan.LuanFunction; | 24 import luan.LuanFunction; |
25 import luan.LuanJavaFunction; | 25 import luan.LuanJavaFunction; |
26 import luan.LuanElement; | 26 import luan.LuanElement; |
27 import luan.DeepCloner; | |
27 | 28 |
28 | 29 |
29 public final class JavaLuan { | 30 public final class JavaLuan { |
30 | 31 |
31 public static final LuanFunction LOADER = new LuanFunction() { | 32 public static final LuanFunction LOADER = new LuanFunction() { |
32 @Override public Object call(LuanState luan,Object[] args) { | 33 @Override public Object call(LuanState luan,Object[] args) { |
33 luan.addMetatableGetter(mg); | |
34 LuanTable module = new LuanTable(); | 34 LuanTable module = new LuanTable(); |
35 module.metatableGetter = mg; | |
35 try { | 36 try { |
36 module.put( "class", new LuanJavaFunction(JavaLuan.class.getMethod("getClass",LuanState.class,String.class),null) ); | 37 module.put( "class", new LuanJavaFunction(JavaLuan.class.getMethod("getClass",LuanState.class,String.class),null) ); |
37 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class ); | 38 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class ); |
38 } catch(NoSuchMethodException e) { | 39 } catch(NoSuchMethodException e) { |
39 throw new RuntimeException(e); | 40 throw new RuntimeException(e); |
71 add( mt, "__index", LuanState.class, Object.class, Object.class ); | 72 add( mt, "__index", LuanState.class, Object.class, Object.class ); |
72 add( mt, "__newindex", LuanState.class, Object.class, Object.class, Object.class ); | 73 add( mt, "__newindex", LuanState.class, Object.class, Object.class, Object.class ); |
73 } | 74 } |
74 | 75 |
75 private static final MetatableGetter mg = new MetatableGetter() { | 76 private static final MetatableGetter mg = new MetatableGetter() { |
77 | |
76 public LuanTable getMetatable(Object obj) { | 78 public LuanTable getMetatable(Object obj) { |
77 if( obj==null ) | 79 if( obj==null ) |
78 return null; | 80 return null; |
79 return mt; | 81 return mt; |
80 } | 82 } |
83 | |
84 @Override public MetatableGetter shallowClone() { | |
85 return this; | |
86 } | |
87 @Override public void deepenClone(MetatableGetter clone,DeepCloner cloner) {} | |
81 }; | 88 }; |
82 | 89 |
83 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException { | 90 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException { |
91 LuanTable mt = luan.getMetatable(obj,mg); | |
92 if( mt != null ) { | |
93 Object h = mt.get("__index"); | |
94 if( h instanceof LuanFunction ) { | |
95 LuanFunction fn = (LuanFunction)h; | |
96 Object rtn = Luan.first(luan.call(fn,new Object[]{obj,key})); | |
97 if( rtn != null ) | |
98 return rtn; | |
99 } | |
100 } | |
84 if( obj instanceof Static ) { | 101 if( obj instanceof Static ) { |
85 if( key instanceof String ) { | 102 if( key instanceof String ) { |
86 String name = (String)key; | 103 String name = (String)key; |
87 Static st = (Static)obj; | 104 Static st = (Static)obj; |
88 Class cls = st.cls; | 105 Class cls = st.cls; |