comparison src/luan/lib/StringLib.java @ 119:f1bf2890d80f

support String methods git-svn-id: https://luan-java.googlecode.com/svn/trunk@120 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 01 Jun 2014 09:38:40 +0000
parents 8c706d6eb5dc
children f537ff5e511d
comparison
equal deleted inserted replaced
118:735708619119 119:f1bf2890d80f
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;
12 13
13 14
14 public final class StringLib { 15 public final class StringLib {
15 16
16 public static final String NAME = "String"; 17 public static final String NAME = "String";
17 18
18 public static final LuanFunction LOADER = new LuanFunction() { 19 public static final LuanFunction LOADER = new LuanFunction() {
19 @Override public Object call(LuanState luan,Object[] args) { 20 @Override public Object call(LuanState luan,Object[] args) {
21 luan.addMetatableGetter(mg);
20 LuanTable module = new LuanTable(); 22 LuanTable module = new LuanTable();
21 try { 23 try {
22 add( module, "to_binary", String.class ); 24 add( module, "to_binary", String.class );
23 add( module, "to_integers", String.class ); 25 add( module, "to_integers", String.class );
24 add( module, "from_integers", new int[0].getClass() ); 26 add( module, "from_integers", new int[0].getClass() );
42 44
43 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 45 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
44 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) ); 46 t.put( method, new LuanJavaFunction(StringLib.class.getMethod(method,parameterTypes),null) );
45 } 47 }
46 48
49 private static final LuanTable mt = new LuanTable();
50 static {
51 try {
52 add( mt, "__index", LuanState.class, String.class, Object.class );
53 } catch(NoSuchMethodException e) {
54 throw new RuntimeException(e);
55 }
56 }
57
58 private static final MetatableGetter mg = new MetatableGetter() {
59 public LuanTable getMetatable(Object obj) {
60 return obj instanceof String ? mt : null;
61 }
62 };
63
64 public static Object __index(LuanState luan,final String s,Object key) throws LuanException {
65 LuanTable mod = (LuanTable)luan.global().get("String");
66 if( mod!=null ) {
67 Object obj = mod.get(key);
68 if( obj instanceof LuanFunction ) {
69 final LuanFunction fn = (LuanFunction)obj;
70 return new LuanFunction() {
71 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
72 Object[] a = new Object[args.length+1];
73 a[0] = s;
74 System.arraycopy(args,0,a,1,args.length);
75 return fn.call(luan,a);
76 }
77 };
78 }
79 }
80 if( luan.global().get("Java") != null )
81 return JavaLib.__index(luan,s,key);
82 return null;
83 }
84
47 static int start(String s,int i) { 85 static int start(String s,int i) {
48 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i; 86 return i==0 ? 0 : i > 0 ? i - 1 : s.length() + i;
49 } 87 }
50 88
51 static int start(String s,Integer i,int dflt) { 89 static int start(String s,Integer i,int dflt) {