comparison core/src/luan/modules/StringLuan.java @ 426:23a93c118042

fix LuanTable.get() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 18:44:20 -0600
parents e9d4d5854e54
children 93e6e67768d7
comparison
equal deleted inserted replaced
425:0a2fb80907f9 426:23a93c118042
14 14
15 public final class StringLuan { 15 public final class StringLuan {
16 16
17 public static Object __index(LuanState luan,final String s,Object key) throws LuanException { 17 public static Object __index(LuanState luan,final String s,Object key) throws LuanException {
18 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String"); 18 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String");
19 Object obj = mod.get(key); 19 Object obj = mod.get(luan,key);
20 if( obj instanceof LuanFunction ) { 20 if( obj instanceof LuanFunction ) {
21 final LuanFunction fn = (LuanFunction)obj; 21 final LuanFunction fn = (LuanFunction)obj;
22 return new LuanFunction() { 22 return new LuanFunction() {
23 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 23 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
24 Object[] a = new Object[args.length+1]; 24 Object[] a = new Object[args.length+1];
178 LuanTable t = (LuanTable)repl; 178 LuanTable t = (LuanTable)repl;
179 int i = 0; 179 int i = 0;
180 StringBuffer sb = new StringBuffer(); 180 StringBuffer sb = new StringBuffer();
181 while( i<max && m.find() ) { 181 while( i<max && m.find() ) {
182 String match = m.groupCount()==0 ? m.group() : m.group(1); 182 String match = m.groupCount()==0 ? m.group() : m.group(1);
183 Object val = t.get(match); 183 Object val = t.get(luan,match);
184 if( Luan.toBoolean(val) ) { 184 if( Luan.toBoolean(val) ) {
185 String replacement = Luan.asString(val); 185 String replacement = Luan.asString(val);
186 if( replacement==null ) 186 if( replacement==null )
187 throw luan.exception( "invalid replacement value (a "+Luan.type(val)+")" ); 187 throw luan.exception( "invalid replacement value (a "+Luan.type(val)+")" );
188 m.appendReplacement(sb,replacement); 188 m.appendReplacement(sb,replacement);