comparison core/src/luan/modules/StringLuan.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
16 public final class StringLuan { 16 public final class StringLuan {
17 17
18 public static final LuanFunction LOADER = new LuanFunction() { 18 public static final LuanFunction LOADER = new LuanFunction() {
19 @Override public Object call(LuanState luan,Object[] args) { 19 @Override public Object call(LuanState luan,Object[] args) {
20 LuanTable module = new LuanTable(); 20 LuanTable module = new LuanTable();
21 MyMetatableGetter mmg = new MyMetatableGetter(); 21 module.put( MetatableGetter.KEY, new MyMetatableGetter(module) );
22 mmg.init(module);
23 module.put( MetatableGetter.KEY, mmg );
24 try { 22 try {
25 add( module, "to_binary", String.class ); 23 add( module, "to_binary", String.class );
26 add( module, "to_integers", String.class ); 24 add( module, "to_integers", String.class );
27 add( module, "from_integers", new int[0].getClass() ); 25 add( module, "from_integers", new int[0].getClass() );
28 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); 26 add( module, "find", String.class, String.class, Integer.class, Boolean.class );
49 47
50 public static class MyMetatableGetter implements MetatableGetter { 48 public static class MyMetatableGetter implements MetatableGetter {
51 private LuanTable module; 49 private LuanTable module;
52 private LuanTable metatable; 50 private LuanTable metatable;
53 51
54 private MyMetatableGetter() {} 52 private MyMetatableGetter(LuanTable module) {
55
56 private void init(LuanTable module) {
57 this.module = module; 53 this.module = module;
58 this.metatable = new LuanTable(); 54 this.metatable = new LuanTable();
59 try { 55 try {
60 metatable.put( "__index", new LuanJavaFunction( 56 metatable.put( "__index", new LuanJavaFunction(
61 MyMetatableGetter.class.getMethod( "__index", LuanState.class, String.class, Object.class ), this 57 MyMetatableGetter.class.getMethod( "__index", LuanState.class, String.class, Object.class ), this
63 } catch(NoSuchMethodException e) { 59 } catch(NoSuchMethodException e) {
64 throw new RuntimeException(e); 60 throw new RuntimeException(e);
65 } 61 }
66 } 62 }
67 63
64 private MyMetatableGetter(MyMetatableGetter mmg) {}
65
68 @Override public MetatableGetter shallowClone() { 66 @Override public MetatableGetter shallowClone() {
69 return new MyMetatableGetter(); 67 return new MyMetatableGetter(this);
70 } 68 }
71 69
72 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) { 70 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) {
73 MyMetatableGetter clone = (MyMetatableGetter)c; 71 MyMetatableGetter clone = (MyMetatableGetter)c;
74 clone.module = cloner.deepClone(module); 72 clone.module = cloner.deepClone(module);