Mercurial Hosting > luan
annotate src/luan/impl/LuanImpl.java @ 1330:f41919741100
fix security
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 11 Feb 2019 01:38:55 -0700 |
parents | 9fa8b8389578 |
children | 25746915a241 |
rev | line source |
---|---|
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 package luan.impl; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
3 import java.util.Arrays; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.util.List; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.util.ArrayList; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import luan.Luan; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
7 import luan.LuanState; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import luan.LuanTable; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
9 import luan.LuanFunction; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 import luan.LuanException; |
660 | 11 import luan.modules.JavaLuan; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 |
654 | 14 public final class LuanImpl { |
15 private LuanImpl() {} // never | |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 |
1267 | 17 public static int len(Object o) throws LuanException { |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 if( o instanceof String ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 String s = (String)o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 return s.length(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 if( o instanceof byte[] ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 byte[] a = (byte[])o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 return a.length; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 if( o instanceof LuanTable ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 LuanTable t = (LuanTable)o; |
1267 | 28 return t.length(); |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 } |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
32 |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
33 public static Object unm(LuanState luan,Object o) throws LuanException { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
34 if( o instanceof Number ) |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
35 return -((Number)o).doubleValue(); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
36 if( o instanceof LuanTable ) { |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
37 LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o); |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
38 if( fn != null ) { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
39 return Luan.first(fn.call(luan,new Object[]{o})); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
40 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
41 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
42 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
43 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
44 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
45 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
46 LuanFunction fn = luan.getBinHandler(op,o1,o2); |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
47 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
48 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
49 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
50 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
51 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
52 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
53 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
54 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
55 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
56 return arithmetic(luan,"__pow",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
57 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
58 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
59 public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
60 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
61 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
62 return arithmetic(luan,"__mul",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
63 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
64 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
65 public static Object div(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
66 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
67 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
68 return arithmetic(luan,"__div",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
69 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
70 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
71 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
72 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
73 double d1 = ((Number)o1).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
74 double d2 = ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
75 return d1 - Math.floor(d1/d2)*d2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
76 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
77 return arithmetic(luan,"__mod",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
78 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
79 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
80 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
81 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
82 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
83 return arithmetic(luan,"__add",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
84 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
85 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
86 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
87 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
88 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
89 return arithmetic(luan,"__sub",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
90 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
91 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
92 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
93 LuanFunction fn = luan.getBinHandler("__concat",o1,o2); |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
94 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
95 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
96 String s1 = luan.toString(o1); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
97 String s2 = luan.toString(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
98 return s1 + s2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
99 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
100 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
101 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
102 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
103 return true; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
104 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
105 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
106 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
107 return n1.doubleValue() == n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
108 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
109 if( o1 instanceof byte[] && o2 instanceof byte[] ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
110 byte[] b1 = (byte[])o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
111 byte[] b2 = (byte[])o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
112 return Arrays.equals(b1,b2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
113 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
114 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
115 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
116 LuanTable t1 = (LuanTable)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
117 LuanTable t2 = (LuanTable)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
118 LuanTable mt1 = t1.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
119 LuanTable mt2 = t2.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
120 if( mt1==null || mt2==null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
121 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
122 Object f = mt1.rawGet("__eq"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
123 if( f == null || !f.equals(mt2.rawGet("__eq")) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
124 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
125 LuanFunction fn = Luan.checkFunction(f); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
126 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
127 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
128 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
129 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
130 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
131 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
132 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
133 return n1.doubleValue() <= n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
134 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
135 if( o1 instanceof String && o2 instanceof String ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
136 String s1 = (String)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
137 String s2 = (String)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
138 return s1.compareTo(s2) <= 0; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
139 } |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
140 LuanFunction fn = luan.getBinHandler("__le",o1,o2); |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
141 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
142 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
143 fn = luan.getBinHandler("__lt",o1,o2); |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
144 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
145 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
146 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
147 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
148 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
149 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1133
diff
changeset
|
150 return luan.isLessThan(o1,o2); |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
151 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
152 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
153 public static boolean cnd(Object o) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
154 return !(o == null || Boolean.FALSE.equals(o)); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
155 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
156 |
651
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
157 public static void nop(Object o) {} |
140cc5191b7a
start compiling statements
Franklin Schmidt <fschmidt@gmail.com>
parents:
650
diff
changeset
|
158 |
660 | 159 public static void put(LuanState luan,Object t,Object key,Object value) throws LuanException { |
160 if( t instanceof LuanTable ) { | |
161 LuanTable tbl = (LuanTable)t; | |
1267 | 162 tbl.put(key,value); |
660 | 163 return; |
164 } | |
1330 | 165 if( t != null && luan.peek().javaOk ) |
660 | 166 JavaLuan.__new_index(luan,t,key,value); |
167 else | |
168 throw new LuanException( "attempt to index a " + Luan.type(t) + " value" ); | |
169 } | |
170 | |
662 | 171 public static Object pick(Object o,int i) { |
172 if( i < 1 ) | |
173 throw new RuntimeException(); | |
174 if( !(o instanceof Object[]) ) | |
175 return null; | |
176 Object[] a = (Object[])o; | |
1089
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
177 return pick(a,i); |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
178 } |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
179 |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
180 public static Object pick(Object[] a,int i) { |
662 | 181 return i<a.length ? a[i] : null; |
182 } | |
183 | |
1089
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
184 public static void noMore(final Object[] a,final int n) throws LuanException { |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
185 if( a.length > n ) { |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
186 for( int i=n; i<a.length; i++ ) { |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
187 if( a[i] != null ) |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
188 throw new LuanException("too many arguments"); |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
189 } |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
190 } |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
191 } |
894786a03d22
check for too many arguments
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
192 |
674 | 193 public static Object[] varArgs(Object[] a,int i) { |
670
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
194 if( i >= a.length ) |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
195 return LuanFunction.NOTHING; |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
196 Object[] rtn = new Object[a.length - i]; |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
197 System.arraycopy(a,i,rtn,0,rtn.length); |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
198 return rtn; |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
199 } |
58ebfec6178b
all luan now compiles
Franklin Schmidt <fschmidt@gmail.com>
parents:
664
diff
changeset
|
200 |
654 | 201 public static Object[] concatArgs(Object o1,Object o2) { |
202 if( o1 instanceof Object[] ) { | |
203 Object[] a1 = (Object[])o1; | |
204 if( o2 instanceof Object[] ) { | |
205 Object[] a2 = (Object[])o2; | |
206 Object[] rtn = new Object[a1.length+a2.length]; | |
207 System.arraycopy(a1,0,rtn,0,a1.length); | |
208 System.arraycopy(a2,0,rtn,a1.length,a2.length); | |
209 return rtn; | |
210 } else { | |
211 Object[] rtn = new Object[a1.length+1]; | |
212 System.arraycopy(a1,0,rtn,0,a1.length); | |
213 rtn[a1.length] = o2; | |
214 return rtn; | |
215 } | |
216 } else { | |
217 if( o2 instanceof Object[] ) { | |
218 Object[] a2 = (Object[])o2; | |
219 Object[] rtn = new Object[1+a2.length]; | |
220 rtn[0] = o1; | |
221 System.arraycopy(a2,0,rtn,1,a2.length); | |
222 return rtn; | |
223 } else { | |
224 Object[] rtn = new Object[2]; | |
225 rtn[0] = o1; | |
226 rtn[2] = o2; | |
227 return rtn; | |
228 } | |
229 } | |
230 } | |
231 | |
1330 | 232 public static LuanTable table(LuanState luan,Object[] a) throws LuanException { |
1267 | 233 LuanTable table = new LuanTable(luan); |
655 | 234 int i = 0; |
674 | 235 for( Object fld : a ) { |
655 | 236 if( fld instanceof TableField ) { |
237 TableField tblFld = (TableField)fld; | |
238 Object key = tblFld.key; | |
239 Object value = tblFld.value; | |
240 if( key != null && value != null ) | |
241 table.rawPut(key,value); | |
242 } else { | |
243 i++; | |
244 if( fld != null ) | |
245 table.rawPut(i,fld); | |
246 } | |
247 } | |
248 return table; | |
249 } | |
250 | |
674 | 251 public static Object first(Object[] a) { |
252 return a.length==0 ? null : a[0]; | |
253 } | |
254 | |
684 | 255 public static String strconcat(String... a) { |
256 StringBuilder sb = new StringBuilder(); | |
257 for( String s : a ) { | |
258 sb.append(s); | |
259 } | |
260 return sb.toString(); | |
261 } | |
262 | |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 } |