Mercurial Hosting > luan
comparison core/src/luan/modules/StringLuan.java @ 197:4c96cb73dd93
fix metatable cloning
git-svn-id: https://luan-java.googlecode.com/svn/trunk@198 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 03 Jul 2014 18:36:46 +0000 |
parents | be0275bda373 |
children | 75750ceb45ee |
comparison
equal
deleted
inserted
replaced
196:be0275bda373 | 197:4c96cb73dd93 |
---|---|
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 module.put( MetatableGetter.KEY, new MyMetatableGetter(module) ); | 21 MyMetatableGetter mmg = new MyMetatableGetter(); |
22 mmg.init(module); | |
23 module.put( MetatableGetter.KEY, mmg ); | |
22 try { | 24 try { |
23 add( module, "to_binary", String.class ); | 25 add( module, "to_binary", String.class ); |
24 add( module, "to_integers", String.class ); | 26 add( module, "to_integers", String.class ); |
25 add( module, "from_integers", new int[0].getClass() ); | 27 add( module, "from_integers", new int[0].getClass() ); |
26 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); | 28 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); |
49 private LuanTable module; | 51 private LuanTable module; |
50 private LuanTable metatable; | 52 private LuanTable metatable; |
51 | 53 |
52 private MyMetatableGetter() {} | 54 private MyMetatableGetter() {} |
53 | 55 |
54 MyMetatableGetter(LuanTable module) { | 56 private void init(LuanTable module) { |
55 this.module = module; | 57 this.module = module; |
56 this.metatable = table(); | 58 this.metatable = new LuanTable(); |
59 try { | |
60 metatable.put( "__index", new LuanJavaFunction( | |
61 MyMetatableGetter.class.getMethod( "__index", LuanState.class, String.class, Object.class ), this | |
62 ) ); | |
63 } catch(NoSuchMethodException e) { | |
64 throw new RuntimeException(e); | |
65 } | |
66 } | |
67 | |
68 @Override public MetatableGetter shallowClone() { | |
69 return new MyMetatableGetter(); | |
70 } | |
71 | |
72 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) { | |
73 MyMetatableGetter clone = (MyMetatableGetter)c; | |
74 clone.module = cloner.deepClone(module); | |
75 clone.metatable = cloner.deepClone(metatable); | |
57 } | 76 } |
58 | 77 |
59 @Override public LuanTable getMetatable(Object obj) { | 78 @Override public LuanTable getMetatable(Object obj) { |
60 return obj instanceof String ? metatable : null; | 79 return obj instanceof String ? metatable : null; |
61 } | 80 } |
79 Object h = mt.get("__index"); | 98 Object h = mt.get("__index"); |
80 if( !(h instanceof LuanFunction) ) | 99 if( !(h instanceof LuanFunction) ) |
81 return null; | 100 return null; |
82 LuanFunction fn = (LuanFunction)h; | 101 LuanFunction fn = (LuanFunction)h; |
83 return luan.call(fn,new Object[]{s,key}); | 102 return luan.call(fn,new Object[]{s,key}); |
84 } | |
85 | |
86 LuanTable table() { | |
87 LuanTable tbl = new LuanTable(); | |
88 try { | |
89 tbl.put( "__index", new LuanJavaFunction( | |
90 MyMetatableGetter.class.getMethod( "__index", LuanState.class, String.class, Object.class ), this | |
91 ) ); | |
92 } catch(NoSuchMethodException e) { | |
93 throw new RuntimeException(e); | |
94 } | |
95 return tbl; | |
96 } | |
97 | |
98 @Override public MetatableGetter shallowClone() { | |
99 return new MyMetatableGetter(); | |
100 } | |
101 | |
102 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) { | |
103 MyMetatableGetter clone = (MyMetatableGetter)c; | |
104 clone.module = cloner.deepClone(module); | |
105 clone.metatable = clone.table(); | |
106 } | 103 } |
107 } | 104 } |
108 | 105 |
109 static int start(String s,int i) { | 106 static int start(String s,int i) { |
110 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; | 107 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; |