Mercurial Hosting > luan
comparison core/src/luan/LuanTable.java @ 502:d3183a330ff5
improve the __index metamethod to work with any type;
simplify luan_proxy to eliminate base;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 May 2015 17:57:20 -0600 |
parents | ee55be414a34 |
children | 92c3d22745b8 |
comparison
equal
deleted
inserted
replaced
501:f26485a3692c | 502:d3183a330ff5 |
---|---|
11 import java.util.Comparator; | 11 import java.util.Comparator; |
12 import java.util.Set; | 12 import java.util.Set; |
13 import java.util.HashSet; | 13 import java.util.HashSet; |
14 import java.util.IdentityHashMap; | 14 import java.util.IdentityHashMap; |
15 import java.util.regex.Pattern; | 15 import java.util.regex.Pattern; |
16 import luan.modules.StringLuan; | |
17 import luan.modules.BinaryLuan; | |
18 import luan.modules.JavaLuan; | |
16 | 19 |
17 | 20 |
18 public final class LuanTable implements DeepCloneable { | 21 public final class LuanTable implements DeepCloneable { |
19 private Map map = null; | 22 private Map map = null; |
20 private List list = null; | 23 private List list = null; |
118 } | 121 } |
119 if( h instanceof LuanMeta ) { | 122 if( h instanceof LuanMeta ) { |
120 LuanMeta meta = (LuanMeta)h; | 123 LuanMeta meta = (LuanMeta)h; |
121 return meta.__index(luan,this,key); | 124 return meta.__index(luan,this,key); |
122 } | 125 } |
123 if( h instanceof LuanTable ) { | 126 return index(luan.JAVA,h,key); |
124 LuanTable tbl = (LuanTable)h; | 127 } |
128 | |
129 public static Object index(LuanBit bit,Object obj,Object key) throws LuanException { | |
130 LuanState luan = bit.luan; | |
131 if( obj instanceof LuanTable ) { | |
132 LuanTable tbl = (LuanTable)obj; | |
125 return tbl.get(luan,key); | 133 return tbl.get(luan,key); |
126 } | 134 } |
127 throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __index"); | 135 if( obj instanceof String ) |
136 return StringLuan.__index(luan,(String)obj,key); | |
137 if( obj instanceof byte[] ) | |
138 return BinaryLuan.__index(luan,(byte[])obj,key); | |
139 if( obj != null && luan.currentEnvironment().hasJava() ) | |
140 return JavaLuan.__index(luan,obj,key); | |
141 else if( bit.el==null ) | |
142 throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" ); | |
143 else | |
144 throw bit.exception( "attempt to index '"+bit.el.text()+"' (a " + Luan.type(obj) + " value)" ); | |
128 } | 145 } |
129 | 146 |
130 public Object rawGet(Object key) { | 147 public Object rawGet(Object key) { |
131 if( list != null ) { | 148 if( list != null ) { |
132 Integer iT = Luan.asInteger(key); | 149 Integer iT = Luan.asInteger(key); |