comparison core/src/luan/modules/StringLuan.java @ 509:e3b0846dc2ef

throw exception for invalid indexes of string, binary, or java
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 May 2015 02:02:49 -0600
parents 92c3d22745b8
children 2da0bcb979b5
comparison
equal deleted inserted replaced
508:9218f9cf45d3 509:e3b0846dc2ef
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.LuanMethod; 12 import luan.LuanMethod;
13 import luan.LuanBit;
13 14
14 15
15 public final class StringLuan { 16 public final class StringLuan {
16 17
17 public static Object __index(LuanState luan,final String s,Object key) throws LuanException { 18 public static Object __index(LuanBit bit,final String s,Object key) throws LuanException {
19 LuanState luan = bit.luan;
18 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String"); 20 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String");
19 Object obj = mod.get(luan,key); 21 Object obj = mod.get(luan,key);
20 if( obj instanceof LuanFunction ) { 22 if( obj instanceof LuanFunction ) {
21 final LuanFunction fn = (LuanFunction)obj; 23 final LuanFunction fn = (LuanFunction)obj;
22 return new LuanFunction() { 24 return new LuanFunction() {
26 System.arraycopy(args,0,a,1,args.length); 28 System.arraycopy(args,0,a,1,args.length);
27 return fn.call(luan,a); 29 return fn.call(luan,a);
28 } 30 }
29 }; 31 };
30 } 32 }
31 if( luan.hasJava() ) 33 if( luan.hasJava() ) {
32 return JavaLuan.__index(luan,s,key); 34 Object rtn = JavaLuan.__index(luan,s,key);
33 return null; 35 if( rtn != JavaLuan.FAIL )
36 return rtn;
37 }
38 if( bit.el != null )
39 throw bit.exception( "invalid index ["+luan.toString(key)+"] for string ("+bit.el.text()+")" ) ;
40 else
41 throw bit.exception( "invalid index ["+luan.toString(key)+"] for string") ;
34 } 42 }
35 43
36 static int start(String s,int i) { 44 static int start(String s,int i) {
37 int len = s.length(); 45 int len = s.length();
38 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0); 46 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);