comparison core/src/luan/modules/BinaryLuan.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 23a93c118042
children 2da0bcb979b5
comparison
equal deleted inserted replaced
508:9218f9cf45d3 509:e3b0846dc2ef
4 import luan.LuanState; 4 import luan.LuanState;
5 import luan.LuanTable; 5 import luan.LuanTable;
6 import luan.LuanFunction; 6 import luan.LuanFunction;
7 import luan.LuanException; 7 import luan.LuanException;
8 import luan.LuanMethod; 8 import luan.LuanMethod;
9 import luan.LuanBit;
9 10
10 11
11 public final class BinaryLuan { 12 public final class BinaryLuan {
12 13
13 public static Object __index(LuanState luan,final byte[] binary,Object key) throws LuanException { 14 public static Object __index(LuanBit bit,final byte[] binary,Object key) throws LuanException {
15 LuanState luan = bit.luan;
14 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:Binary"); 16 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:Binary");
15 Object obj = mod.get(luan,key); 17 Object obj = mod.get(luan,key);
16 if( obj instanceof LuanFunction ) { 18 if( obj instanceof LuanFunction ) {
17 final LuanFunction fn = (LuanFunction)obj; 19 final LuanFunction fn = (LuanFunction)obj;
18 return new LuanFunction() { 20 return new LuanFunction() {
22 System.arraycopy(args,0,a,1,args.length); 24 System.arraycopy(args,0,a,1,args.length);
23 return fn.call(luan,a); 25 return fn.call(luan,a);
24 } 26 }
25 }; 27 };
26 } 28 }
27 return null; 29 if( bit.el != null )
30 throw bit.exception( "invalid index ["+luan.toString(key)+"] for binary ("+bit.el.text()+")" ) ;
31 else
32 throw bit.exception( "invalid index ["+luan.toString(key)+"] for binary") ;
28 } 33 }
29 34
30 static int start(byte[] binary,int i) { 35 static int start(byte[] binary,int i) {
31 int len = binary.length; 36 int len = binary.length;
32 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0); 37 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);