Mercurial Hosting > luan
view core/src/luan/impl/IndexExpr.java @ 421:b31d614343e8
add Io.LuanString.text_writer();
improve java related errors;
fix to_string;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2015 15:13:14 -0600 |
parents | 8fbb961aabd5 |
children | 23a93c118042 |
line wrap: on
line source
package luan.impl; import luan.Luan; import luan.LuanException; import luan.LuanTable; import luan.LuanFunction; import luan.LuanSource; import luan.LuanMeta; import luan.modules.StringLuan; import luan.modules.BinaryLuan; import luan.modules.JavaLuan; final class IndexExpr extends BinaryOpExpr { IndexExpr(LuanSource.Element se,Expr op1,Expr op2) { super(se,op1,op2); } @Override public Object eval(LuanStateImpl luan) throws LuanException { return index(luan,op1.eval(luan),op2.eval(luan)); } private Object index(LuanStateImpl luan,Object obj,Object key) throws LuanException { if( obj instanceof LuanTable ) { LuanTable tbl = (LuanTable)obj; Object value = tbl.get(key); if( value != null ) return value; Object h = tbl.getHandler("__index"); if( h==null ) return null; if( h instanceof LuanFunction ) { LuanFunction fn = (LuanFunction)h; return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{tbl,key})); } if( h instanceof LuanMeta ) { LuanMeta meta = (LuanMeta)h; return meta.__index(luan,tbl,key); } return index(luan,h,key); } if( obj instanceof String ) return StringLuan.__index(luan,(String)obj,key); if( obj instanceof byte[] ) return BinaryLuan.__index(luan,(byte[])obj,key); if( obj != null && luan.currentEnvironment().hasJava() ) return JavaLuan.__index(luan,obj,key); else throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(obj) + " value)" ); } }