Mercurial Hosting > luan
comparison core/src/luan/modules/StringLuan.java @ 208:5ba136769034
remove MetatableGetter and use a generic metatable instead
git-svn-id: https://luan-java.googlecode.com/svn/trunk@209 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 08 Jul 2014 07:04:47 +0000 |
parents | 75750ceb45ee |
children | ec016471c6eb |
comparison
equal
deleted
inserted
replaced
207:5aafb5b9f70f | 208:5ba136769034 |
---|---|
7 import luan.LuanTable; | 7 import luan.LuanTable; |
8 import luan.LuanFunction; | 8 import luan.LuanFunction; |
9 import luan.LuanJavaFunction; | 9 import luan.LuanJavaFunction; |
10 import luan.LuanElement; | 10 import luan.LuanElement; |
11 import luan.LuanException; | 11 import luan.LuanException; |
12 import luan.MetatableGetter; | |
13 import luan.DeepCloner; | |
14 | 12 |
15 | 13 |
16 public final class StringLuan { | 14 public final class StringLuan { |
17 | 15 |
18 public static final LuanFunction LOADER = new LuanFunction() { | 16 public static final LuanFunction LOADER = new LuanFunction() { |
19 @Override public Object call(LuanState luan,Object[] args) { | 17 @Override public Object call(LuanState luan,Object[] args) { |
20 LuanTable module = new LuanTable(); | 18 LuanTable module = new LuanTable(); |
21 module.put( MetatableGetter.KEY, new MyMetatableGetter(module) ); | |
22 try { | 19 try { |
23 add( module, "to_binary", String.class ); | 20 add( module, "to_binary", String.class ); |
24 add( module, "to_integers", String.class ); | 21 add( module, "to_integers", String.class ); |
25 add( module, "from_integers", new int[0].getClass() ); | 22 add( module, "from_integers", new int[0].getClass() ); |
26 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); | 23 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); |
43 | 40 |
44 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 41 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
45 t.put( method, new LuanJavaFunction(StringLuan.class.getMethod(method,parameterTypes),null) ); | 42 t.put( method, new LuanJavaFunction(StringLuan.class.getMethod(method,parameterTypes),null) ); |
46 } | 43 } |
47 | 44 |
48 public static class MyMetatableGetter implements MetatableGetter { | 45 public static Object __index(LuanState luan,final String s,Object key) throws LuanException { |
49 private LuanTable module; | 46 LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("String"); |
50 private LuanTable metatable; | 47 if( mod!=null ) { |
51 | 48 Object obj = mod.get(key); |
52 private MyMetatableGetter(LuanTable module) { | |
53 this.module = module; | |
54 this.metatable = new LuanTable(); | |
55 try { | |
56 metatable.put( "__index", new LuanJavaFunction( | |
57 MyMetatableGetter.class.getMethod( "__index", LuanState.class, String.class, Object.class ), this | |
58 ) ); | |
59 } catch(NoSuchMethodException e) { | |
60 throw new RuntimeException(e); | |
61 } | |
62 } | |
63 | |
64 private MyMetatableGetter(MyMetatableGetter mmg) {} | |
65 | |
66 @Override public MetatableGetter shallowClone() { | |
67 return new MyMetatableGetter(this); | |
68 } | |
69 | |
70 @Override public void deepenClone(MetatableGetter c,DeepCloner cloner) { | |
71 MyMetatableGetter clone = (MyMetatableGetter)c; | |
72 clone.module = cloner.deepClone(module); | |
73 clone.metatable = cloner.deepClone(metatable); | |
74 } | |
75 | |
76 @Override public LuanTable getMetatable(Object obj) { | |
77 return obj instanceof String ? metatable : null; | |
78 } | |
79 | |
80 public Object __index(LuanState luan,final String s,Object key) throws LuanException { | |
81 Object obj = module.get(key); | |
82 if( obj instanceof LuanFunction ) { | 49 if( obj instanceof LuanFunction ) { |
83 final LuanFunction fn = (LuanFunction)obj; | 50 final LuanFunction fn = (LuanFunction)obj; |
84 return new LuanFunction() { | 51 return new LuanFunction() { |
85 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 52 @Override public Object call(LuanState luan,Object[] args) throws LuanException { |
86 Object[] a = new Object[args.length+1]; | 53 Object[] a = new Object[args.length+1]; |
88 System.arraycopy(args,0,a,1,args.length); | 55 System.arraycopy(args,0,a,1,args.length); |
89 return fn.call(luan,a); | 56 return fn.call(luan,a); |
90 } | 57 } |
91 }; | 58 }; |
92 } | 59 } |
93 LuanTable mt = luan.getMetatable(s,this); | 60 } |
94 if( mt == null ) | 61 return null; |
95 return null; | |
96 Object h = mt.get("__index"); | |
97 if( !(h instanceof LuanFunction) ) | |
98 return null; | |
99 LuanFunction fn = (LuanFunction)h; | |
100 return luan.call(fn,new Object[]{s,key}); | |
101 } | |
102 } | 62 } |
103 | 63 |
104 static int start(String s,int i) { | 64 static int start(String s,int i) { |
105 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; | 65 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; |
106 } | 66 } |