Mercurial Hosting > luan
comparison core/src/luan/modules/JavaLuan.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 |
---|---|
21 import luan.LuanTable; | 21 import luan.LuanTable; |
22 import luan.LuanException; | 22 import luan.LuanException; |
23 import luan.LuanFunction; | 23 import luan.LuanFunction; |
24 import luan.LuanJavaFunction; | 24 import luan.LuanJavaFunction; |
25 import luan.LuanElement; | 25 import luan.LuanElement; |
26 import luan.LuanBit; | |
26 | 27 |
27 | 28 |
28 public final class JavaLuan { | 29 public final class JavaLuan { |
29 | 30 |
30 public static void java(LuanState luan) throws LuanException { | 31 public static void java(LuanState luan) throws LuanException { |
42 } | 43 } |
43 | 44 |
44 private static void checkJava(LuanState luan) throws LuanException { | 45 private static void checkJava(LuanState luan) throws LuanException { |
45 if( !luan.hasJava() ) | 46 if( !luan.hasJava() ) |
46 throw luan.exception("Java isn't allowed"); | 47 throw luan.exception("Java isn't allowed"); |
48 } | |
49 | |
50 static final Object FAIL = new Object(); | |
51 | |
52 public static Object __index(LuanBit bit,Object obj,Object key) throws LuanException { | |
53 LuanState luan = bit.luan; | |
54 Object rtn = __index(luan,obj,key); | |
55 if( rtn != FAIL ) | |
56 return rtn; | |
57 if( bit.el != null ) | |
58 throw bit.exception( "invalid index ["+luan.toString(key)+"] for ("+bit.el.text()+") java: "+obj.getClass() ); | |
59 else | |
60 throw bit.exception( "invalid index ["+luan.toString(key)+"] for java: "+obj.getClass() ); | |
47 } | 61 } |
48 | 62 |
49 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException { | 63 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException { |
50 checkJava(luan); | 64 checkJava(luan); |
51 if( obj instanceof Static ) { | 65 if( obj instanceof Static ) { |
79 if( !members.isEmpty() ) { | 93 if( !members.isEmpty() ) { |
80 return member(null,members); | 94 return member(null,members); |
81 } | 95 } |
82 } | 96 } |
83 } | 97 } |
84 throw luan.exception("invalid member '"+key+"' for: "+obj); | 98 } else { |
85 } | 99 Class cls = obj.getClass(); |
86 Class cls = obj.getClass(); | 100 if( cls.isArray() ) { |
87 if( cls.isArray() ) { | 101 if( "length".equals(key) ) { |
88 if( "length".equals(key) ) { | 102 return Array.getLength(obj); |
89 return Array.getLength(obj); | 103 } |
90 } | 104 Integer i = Luan.asInteger(key); |
91 Integer i = Luan.asInteger(key); | 105 if( i != null ) { |
92 if( i != null ) { | 106 return Array.get(obj,i); |
93 return Array.get(obj,i); | 107 } |
94 } | 108 throw luan.exception("invalid member '"+key+"' for java array: "+obj); |
95 throw luan.exception("invalid member '"+key+"' for java array: "+obj); | 109 } |
96 } | 110 if( key instanceof String ) { |
97 if( key instanceof String ) { | 111 String name = (String)key; |
98 String name = (String)key; | 112 if( "instanceof".equals(name) ) { |
99 if( "instanceof".equals(name) ) { | 113 return new LuanJavaFunction(instanceOf,new InstanceOf(obj)); |
100 return new LuanJavaFunction(instanceOf,new InstanceOf(obj)); | 114 } else { |
101 } else { | 115 List<Member> members = getMembers(cls,name); |
102 List<Member> members = getMembers(cls,name); | 116 if( !members.isEmpty() ) { |
103 if( !members.isEmpty() ) { | 117 return member(obj,members); |
104 return member(obj,members); | 118 } |
105 } | 119 } |
106 } | 120 } |
107 } | 121 } |
108 // throw luan.exception("invalid member '"+key+"' for java object: "+obj); | |
109 //System.out.println("invalid member '"+key+"' for java object: "+obj); | 122 //System.out.println("invalid member '"+key+"' for java object: "+obj); |
110 return null; | 123 return FAIL; |
111 } | 124 } |
112 | 125 |
113 private static Object member(Object obj,List<Member> members) throws LuanException { | 126 private static Object member(Object obj,List<Member> members) throws LuanException { |
114 try { | 127 try { |
115 if( members.size()==1 ) { | 128 if( members.size()==1 ) { |