comparison core/src/luan/modules/JavaLuan.java @ 202:75750ceb45ee

add LuanState.registry git-svn-id: https://luan-java.googlecode.com/svn/trunk@203 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 04 Jul 2014 17:18:39 +0000
parents 4c96cb73dd93
children 5ba136769034
comparison
equal deleted inserted replaced
201:27abb3746917 202:75750ceb45ee
30 public final class JavaLuan { 30 public final class JavaLuan {
31 31
32 public static final LuanFunction LOADER = new LuanFunction() { 32 public static final LuanFunction LOADER = new LuanFunction() {
33 @Override public Object call(LuanState luan,Object[] args) { 33 @Override public Object call(LuanState luan,Object[] args) {
34 LuanTable module = new LuanTable(); 34 LuanTable module = new LuanTable();
35 MyMetatableGetter mmg = new MyMetatableGetter(); 35 module.put( MetatableGetter.KEY, new MyMetatableGetter() );
36 mmg.init();
37 module.put( MetatableGetter.KEY, mmg );
38 try { 36 try {
39 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) );
40 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class ); 38 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class );
41 } catch(NoSuchMethodException e) { 39 } catch(NoSuchMethodException e) {
42 throw new RuntimeException(e); 40 throw new RuntimeException(e);
43 } 41 }
44 luan.searchers().add(javaSearcher);
45 return module; 42 return module;
46 } 43 }
47 }; 44 };
45
46 private static boolean isLoaded(LuanState luan) {
47 return PackageLuan.loaded(luan).get("Java") != null;
48 }
48 49
49 public static final LuanFunction javaSearcher = new LuanFunction() { 50 public static final LuanFunction javaSearcher = new LuanFunction() {
50 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 51 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
52 if( !isLoaded(luan) )
53 return LuanFunction.NOTHING;
51 String modName = (String)args[0]; 54 String modName = (String)args[0];
52 final Static s = JavaLuan.getClass(luan,modName); 55 final Static s = JavaLuan.getClass(luan,modName);
53 if( s==null ) 56 if( s==null )
54 return null; 57 return null;
55 LuanFunction loader = new LuanFunction() { 58 LuanFunction loader = new LuanFunction() {
70 } 73 }
71 74
72 public static class MyMetatableGetter implements MetatableGetter { 75 public static class MyMetatableGetter implements MetatableGetter {
73 private LuanTable metatable; 76 private LuanTable metatable;
74 77
75 private MyMetatableGetter() {} 78 private MyMetatableGetter() {
76
77 private void init() {
78 this.metatable = new LuanTable(); 79 this.metatable = new LuanTable();
79 try { 80 try {
80 metatable.put( "__index", new LuanJavaFunction( 81 metatable.put( "__index", new LuanJavaFunction(
81 MyMetatableGetter.class.getMethod( "__index", LuanState.class, Object.class, Object.class ), this 82 MyMetatableGetter.class.getMethod( "__index", LuanState.class, Object.class, Object.class ), this
82 ) ); 83 ) );
84 throw new RuntimeException(e); 85 throw new RuntimeException(e);
85 } 86 }
86 add( metatable, "__newindex", LuanState.class, Object.class, Object.class, Object.class ); 87 add( metatable, "__newindex", LuanState.class, Object.class, Object.class, Object.class );
87 } 88 }
88 89
90 private MyMetatableGetter(MyMetatableGetter mmg) {}
91
89 @Override public MetatableGetter shallowClone() { 92 @Override public MetatableGetter shallowClone() {
90 return new MyMetatableGetter(); 93 return new MyMetatableGetter(this);
91 } 94 }
92 95
93 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) { 96 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) {
94 MyMetatableGetter clone = (MyMetatableGetter)c; 97 MyMetatableGetter clone = (MyMetatableGetter)c;
95 clone.metatable = cloner.deepClone(metatable); 98 clone.metatable = cloner.deepClone(metatable);