comparison core/src/luan/impl/LenExpr.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/impl/LenExpr.java@4eaee12f6c65
children d55e873e1f0d
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
1 package luan.impl;
2
3 import luan.Luan;
4 import luan.LuanTable;
5 import luan.LuanFunction;
6 import luan.LuanException;
7 import luan.LuanSource;
8 import luan.LuanBit;
9
10
11 final class LenExpr extends UnaryOpExpr {
12
13 LenExpr(LuanSource.Element se,Expr op) {
14 super(se,op);
15 }
16
17 @Override public Object eval(LuanStateImpl luan) throws LuanException {
18 Object o = op.eval(luan);
19 if( o instanceof String ) {
20 String s = (String)o;
21 return s.length();
22 }
23 if( o instanceof byte[] ) {
24 byte[] a = (byte[])o;
25 return a.length;
26 }
27 LuanBit bit = luan.bit(se);
28 LuanFunction fn = bit.getHandlerFunction("__len",o);
29 if( fn != null )
30 return Luan.first(bit.call(fn,"__len",new Object[]{o}));
31 if( o instanceof LuanTable ) {
32 LuanTable t = (LuanTable)o;
33 return t.length();
34 }
35 throw bit.exception( "attempt to get length of a " + Luan.type(o) + " value" );
36 }
37 }