Mercurial Hosting > luan
comparison src/luan/impl/LuanImpl.java @ 1166:7ef40e1923b7
add back Thread.global
allow metatables to have metatables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 08 Feb 2018 02:22:51 -0700 |
parents | ba4daf107e07 |
children | 9fa8b8389578 |
comparison
equal
deleted
inserted
replaced
1165:668f29bc52ea | 1166:7ef40e1923b7 |
---|---|
32 | 32 |
33 public static Object unm(LuanState luan,Object o) throws LuanException { | 33 public static Object unm(LuanState luan,Object o) throws LuanException { |
34 if( o instanceof Number ) | 34 if( o instanceof Number ) |
35 return -((Number)o).doubleValue(); | 35 return -((Number)o).doubleValue(); |
36 if( o instanceof LuanTable ) { | 36 if( o instanceof LuanTable ) { |
37 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); | 37 LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o); |
38 if( fn != null ) { | 38 if( fn != null ) { |
39 return Luan.first(fn.call(luan,new Object[]{o})); | 39 return Luan.first(fn.call(luan,new Object[]{o})); |
40 } | 40 } |
41 } | 41 } |
42 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); | 42 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
43 } | 43 } |
44 | 44 |
45 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { | 45 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { |
46 LuanFunction fn = Luan.getBinHandler(op,o1,o2); | 46 LuanFunction fn = luan.getBinHandler(op,o1,o2); |
47 if( fn != null ) | 47 if( fn != null ) |
48 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | 48 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
49 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); | 49 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
50 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); | 50 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
51 } | 51 } |
88 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); | 88 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
89 return arithmetic(luan,"__sub",o1,o2); | 89 return arithmetic(luan,"__sub",o1,o2); |
90 } | 90 } |
91 | 91 |
92 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { | 92 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { |
93 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); | 93 LuanFunction fn = luan.getBinHandler("__concat",o1,o2); |
94 if( fn != null ) | 94 if( fn != null ) |
95 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | 95 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
96 String s1 = luan.toString(o1); | 96 String s1 = luan.toString(o1); |
97 String s2 = luan.toString(o2); | 97 String s2 = luan.toString(o2); |
98 return s1 + s2; | 98 return s1 + s2; |
135 if( o1 instanceof String && o2 instanceof String ) { | 135 if( o1 instanceof String && o2 instanceof String ) { |
136 String s1 = (String)o1; | 136 String s1 = (String)o1; |
137 String s2 = (String)o2; | 137 String s2 = (String)o2; |
138 return s1.compareTo(s2) <= 0; | 138 return s1.compareTo(s2) <= 0; |
139 } | 139 } |
140 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); | 140 LuanFunction fn = luan.getBinHandler("__le",o1,o2); |
141 if( fn != null ) | 141 if( fn != null ) |
142 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | 142 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
143 fn = Luan.getBinHandler("__lt",o1,o2); | 143 fn = luan.getBinHandler("__lt",o1,o2); |
144 if( fn != null ) | 144 if( fn != null ) |
145 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); | 145 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); |
146 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | 146 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
147 } | 147 } |
148 | 148 |
149 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { | 149 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { |
150 return Luan.isLessThan(luan,o1,o2); | 150 return luan.isLessThan(o1,o2); |
151 } | 151 } |
152 | 152 |
153 public static boolean cnd(Object o) throws LuanException { | 153 public static boolean cnd(Object o) throws LuanException { |
154 return !(o == null || Boolean.FALSE.equals(o)); | 154 return !(o == null || Boolean.FALSE.equals(o)); |
155 } | 155 } |