Mercurial Hosting > luan
changeset 46:a443637829c1
remove LuaNumber
git-svn-id: https://luan-java.googlecode.com/svn/trunk@47 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 27 Dec 2012 01:48:36 +0000 |
parents | b1b14d09fc98 |
children | 659c7139e903 |
files | src/luan/Lua.java src/luan/LuaJavaFunction.java src/luan/LuaNumber.java src/luan/LuaState.java src/luan/LuaTable.java src/luan/interp/AddExpr.java src/luan/interp/DivExpr.java src/luan/interp/EqExpr.java src/luan/interp/LeExpr.java src/luan/interp/LenExpr.java src/luan/interp/LtExpr.java src/luan/interp/LuaParser.java src/luan/interp/ModExpr.java src/luan/interp/MulExpr.java src/luan/interp/NumericForStmt.java src/luan/interp/PowExpr.java src/luan/interp/SubExpr.java src/luan/interp/TableExpr.java src/luan/interp/UnmExpr.java src/luan/lib/BasicLib.java src/luan/lib/JavaLib.java src/luan/lib/StringLib.java src/luan/lib/TableLib.java src/luan/tools/CmdLine.java |
diffstat | 24 files changed, 126 insertions(+), 348 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/Lua.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/Lua.java Thu Dec 27 01:48:36 2012 +0000 @@ -11,7 +11,7 @@ return "string"; if( obj instanceof Boolean ) return "boolean"; - if( obj instanceof LuaNumber ) + if( obj instanceof Number ) return "number"; return "userdata"; } @@ -21,25 +21,27 @@ } public static String asString(Object obj) { - if( obj instanceof String || obj instanceof LuaNumber ) - return obj.toString(); + if( obj instanceof String ) + return (String)obj; + if( obj instanceof Number ) + return toString((Number)obj); return null; } - public static LuaNumber toNumber(Object obj) { + public static Number toNumber(Object obj) { return toNumber(obj,null); } - public static LuaNumber toNumber(Object obj,Integer base) { - if( obj instanceof LuaNumber ) - return (LuaNumber)obj; + public static Number toNumber(Object obj,Integer base) { + if( obj instanceof Number ) + return (Number)obj; if( obj instanceof String ) { String s = (String)obj; try { if( base==null ) - return LuaNumber.of( Double.parseDouble(s) ); + return Double.valueOf(s); else - return LuaNumber.of( Long.parseLong(s,base) ); + return Long.valueOf(s,base); } catch(NumberFormatException e) {} } return null; @@ -49,4 +51,30 @@ return a.length==0 ? null : a[0]; } + public static String toString(Number n) { + if( n instanceof Integer ) + return n.toString(); + String s = n.toString(); + int iE = s.indexOf('E'); + String ending = null; + if( iE != -1 ) { + ending = s.substring(iE); + s = s.substring(0,iE); + } + if( s.endsWith(".0") ) + s = s.substring(0,s.length()-2); + if( ending != null ) + s += ending; + return s; + } + + public static Integer asInteger(Object obj) { + if( obj instanceof Integer ) + return (Integer)obj; + if( !(obj instanceof Number) ) + return null; + Number n = (Number)obj; + int i = n.intValue(); + return i==n.doubleValue() ? Integer.valueOf(i) : null; + } }
--- a/src/luan/LuaJavaFunction.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/LuaJavaFunction.java Thu Dec 27 01:48:36 2012 +0000 @@ -125,56 +125,16 @@ private static final Object[] NULL_RTN = new Object[1]; - private static final RtnConverter RTN_NUMBER = new RtnConverter() { - public Object[] convert(Object obj) { - if( obj == null ) - return NULL_RTN; - Number n = (Number)obj; - LuaNumber ln = LuaNumber.of(n); - return new Object[]{ln}; - } - }; - - private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() { - public Object[] convert(Object obj) { - if( obj == null ) - return NULL_RTN; - Object[] rtn = new Object[Array.getLength(obj)]; - for( int i=0; i<rtn.length; i++ ) { - Number n = (Number)Array.get(obj,i); - if( n != null ) - rtn[i] = LuaNumber.of(n.doubleValue()); - } - return rtn; - } - }; - private static RtnConverter getRtnConverter(JavaMethod m) { Class<?> rtnType = m.getReturnType(); if( rtnType == Void.TYPE ) return RTN_EMPTY; - if( isNumber(rtnType) ) - return RTN_NUMBER; if( rtnType.isArray() ) { - rtnType = rtnType.getComponentType(); - if( isNumber(rtnType) ) - return RTN_NUMBER_ARRAY; return RTN_ARRAY; } return RTN_ONE; } - private static boolean isNumber(Class<?> rtnType) { - return Number.class.isAssignableFrom(rtnType) - || rtnType == Byte.TYPE - || rtnType == Short.TYPE - || rtnType == Integer.TYPE - || rtnType == Long.TYPE - || rtnType == Float.TYPE - || rtnType == Double.TYPE - ; - } - private interface ArgConverter { public Object convert(Object obj); @@ -186,110 +146,6 @@ } }; - private static final ArgConverter ARG_DOUBLE = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - return Double.valueOf(ln.n); - } - if( obj instanceof String ) { - String s = (String)obj; - try { - return Double.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - - private static final ArgConverter ARG_FLOAT = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - return Float.valueOf((float)ln.n); - } - if( obj instanceof String ) { - String s = (String)obj; - try { - return Float.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - - private static final ArgConverter ARG_LONG = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - long i = (long)ln.n; - if( i == ln.n ) - return Long.valueOf(i); - } - else if( obj instanceof String ) { - String s = (String)obj; - try { - return Long.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - - private static final ArgConverter ARG_INTEGER = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - int i = (int)ln.n; - if( i == ln.n ) - return Integer.valueOf(i); - } - else if( obj instanceof String ) { - String s = (String)obj; - try { - return Integer.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - - private static final ArgConverter ARG_SHORT = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - short i = (short)ln.n; - if( i == ln.n ) - return Short.valueOf(i); - } - else if( obj instanceof String ) { - String s = (String)obj; - try { - return Short.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - - private static final ArgConverter ARG_BYTE = new ArgConverter() { - public Object convert(Object obj) { - if( obj instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)obj; - byte i = (byte)ln.n; - if( i == ln.n ) - return Byte.valueOf(i); - } - else if( obj instanceof String ) { - String s = (String)obj; - try { - return Byte.valueOf(s); - } catch(NumberFormatException e) {} - } - return obj; - } - }; - private static boolean takesLuaState(JavaMethod m) { Class<?>[] paramTypes = m.getParameterTypes(); return paramTypes.length > 0 && paramTypes[0].equals(LuaState.class); @@ -314,18 +170,6 @@ } private static ArgConverter getArgConverter(Class<?> cls) { - if( cls == Double.TYPE || cls.equals(Double.class) ) - return ARG_DOUBLE; - if( cls == Float.TYPE || cls.equals(Float.class) ) - return ARG_FLOAT; - if( cls == Long.TYPE || cls.equals(Long.class) ) - return ARG_LONG; - if( cls == Integer.TYPE || cls.equals(Integer.class) ) - return ARG_INTEGER; - if( cls == Short.TYPE || cls.equals(Short.class) ) - return ARG_SHORT; - if( cls == Byte.TYPE || cls.equals(Byte.class) ) - return ARG_BYTE; return ARG_SAME; }
--- a/src/luan/LuaNumber.java Thu Dec 27 00:44:58 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -package luan; - - -public final class LuaNumber implements Comparable<LuaNumber> { - final double n; - - private LuaNumber(double n) { - this.n = n; - } - - public double value() { - return n; - } - - // convert to Lua format - @Override public String toString() { - String s = Double.toString(n); - int iE = s.indexOf('E'); - String ending = null; - if( iE != -1 ) { - ending = s.substring(iE); - s = s.substring(0,iE); - } - if( s.endsWith(".0") ) - s = s.substring(0,s.length()-2); - if( ending != null ) - s += ending; - return s; - } - - @Override public boolean equals(Object obj) { - if( !(obj instanceof LuaNumber) ) - return false; - LuaNumber ln = (LuaNumber)obj; - return n == ln.n; - } - - @Override public int hashCode() { - return Double.valueOf(n).hashCode(); - } - - @Override public int compareTo(LuaNumber ln) { - return Double.compare(n,ln.n); - } - - public static LuaNumber of(double n) { - return new LuaNumber(n); - } - - private static LuaNumber[] ints = new LuaNumber[100]; - static { - for( int i=0; i<ints.length; i++ ) { - ints[i] = new LuaNumber(i); - } - } - - public static LuaNumber of(int n) { - if( 0 <= n && n < ints.length ) - return ints[n]; - return new LuaNumber(n); - } - - public static LuaNumber of(Number n) { - return n instanceof Integer ? of(n.intValue()) : of(n.doubleValue()); - } - -}
--- a/src/luan/LuaState.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/LuaState.java Thu Dec 27 01:48:36 2012 +0000 @@ -46,8 +46,8 @@ return s; } - public final LuaNumber checkNumber(LuaElement el,Object obj) throws LuaException { - LuaNumber n = Lua.toNumber(obj); + public final Number checkNumber(LuaElement el,Object obj) throws LuaException { + Number n = Lua.toNumber(obj); if( n == null ) throw new LuaException( this, el, "attempt to perform arithmetic on a " + Lua.type(obj) + " value" ); return n; @@ -65,6 +65,8 @@ return checkString( el, Lua.first( call(fn,el,"__tostring",obj) ) ); if( obj == null ) return "nil"; + if( obj instanceof Number ) + return Lua.toString((Number)obj); if( obj instanceof LuaException ) { LuaException le = (LuaException)obj; return le.getMessage(); @@ -93,10 +95,10 @@ } public final boolean isLessThan(LuaElement el,Object o1,Object o2) throws LuaException { - if( o1 instanceof LuaNumber && o2 instanceof LuaNumber ) { - LuaNumber n1 = (LuaNumber)o1; - LuaNumber n2 = (LuaNumber)o2; - return n1.compareTo(n2) < 0; + if( o1 instanceof Number && o2 instanceof Number ) { + Number n1 = (Number)o1; + Number n2 = (Number)o2; + return n1.doubleValue() < n2.doubleValue(); } if( o1 instanceof String && o2 instanceof String ) { String s1 = (String)o1;
--- a/src/luan/LuaTable.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/LuaTable.java Thu Dec 27 01:48:36 2012 +0000 @@ -26,11 +26,10 @@ } public Object get(Object key) { - if( list != null && key instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)key; - int i = (int)ln.n; - if( i == ln.n ) { - i--; + if( list != null ) { + Integer iT = Lua.asInteger(key); + if( iT != null ) { + int i = iT - 1; if( i>=0 && i<list.size() ) return list.get(i); } @@ -41,43 +40,44 @@ } public Object put(Object key,Object val) { - if( key instanceof LuaNumber ) { - LuaNumber ln = (LuaNumber)key; - int i = (int)ln.n; - if( i == ln.n ) { - i--; - if( list == null && i == 0 ) - list = new ArrayList<Object>(); - if( list != null ) { - if( i == list.size() ) { - if( val != null ) { - list.add(val); - if( map != null ) { - while(true) { - Object v = map.remove(LuaNumber.of(list.size()+1)); - if( v == null ) - break; - list.add(v); - } + Integer iT = Lua.asInteger(key); + if( iT != null ) { + int i = iT - 1; + if( list == null && i == 0 ) + list = new ArrayList<Object>(); + if( list != null ) { + if( i == list.size() ) { + if( val != null ) { + list.add(val); + if( map != null ) { + while(true) { + Object v = map.remove(Double.valueOf(list.size()+1)); + if( v == null ) + break; + list.add(v); } } - return null; - } else if( i>=0 && i<list.size() ) { - Object old = list.get(i); - list.set(i,val); - if( val == null && i == list.size()-1 ) { - while( i>=0 && list.get(i)==null ) { - list.remove(i--); - } + } + return null; + } else if( i>=0 && i<list.size() ) { + Object old = list.get(i); + list.set(i,val); + if( val == null && i == list.size()-1 ) { + while( i>=0 && list.get(i)==null ) { + list.remove(i--); } - return old; } + return old; } } } if( map==null ) { map = new HashMap<Object,Object>(); } + if( key instanceof Number && !(key instanceof Double) ) { + Number n = (Number)key; + key = Double.valueOf(n.doubleValue()); + } if( val == null ) { return map.remove(key); } else { @@ -144,7 +144,7 @@ return iter.hasNext(); } public Map.Entry<Object,Object> next() { - LuaNumber key = LuaNumber.of(iter.nextIndex()+1); + Double key = Double.valueOf(iter.nextIndex()+1); return new MapEntry(key,iter.next()); } public void remove() {
--- a/src/luan/interp/AddExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/AddExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( n1.value() + n2.value() ); + return n1.doubleValue() + n2.doubleValue(); return arithmetic(lua,"__add",o1,o2); } }
--- a/src/luan/interp/DivExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/DivExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( n1.value() / n2.value() ); + return n1.doubleValue() / n2.doubleValue(); return arithmetic(lua,"__div",o1,o2); } }
--- a/src/luan/interp/EqExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/EqExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaTable; import luan.LuaException; @@ -19,6 +18,11 @@ Object o2 = op2.eval(lua); if( o1 == o2 || o1 != null && o1.equals(o2) ) return true; + if( o1 instanceof Number && o2 instanceof Number ) { + Number n1 = (Number)o1; + Number n2 = (Number)o2; + return n1.doubleValue() == n2.doubleValue(); + } if( !o1.getClass().equals(o2.getClass()) ) return false; LuaTable mt1 = lua.getMetatable(o1);
--- a/src/luan/interp/LeExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/LeExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaException; import luan.LuaSource; @@ -16,10 +15,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - if( o1 instanceof LuaNumber && o2 instanceof LuaNumber ) { - LuaNumber n1 = (LuaNumber)o1; - LuaNumber n2 = (LuaNumber)o2; - return n1.compareTo(n2) <= 0; + if( o1 instanceof Number && o2 instanceof Number ) { + Number n1 = (Number)o1; + Number n2 = (Number)o2; + return n1.doubleValue() <= n2.doubleValue(); } if( o1 instanceof String && o2 instanceof String ) { String s1 = (String)o1;
--- a/src/luan/interp/LenExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/LenExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaTable; import luan.LuaFunction; import luan.LuaException; @@ -18,7 +17,7 @@ Object o = op.eval(lua); if( o instanceof String ) { String s = (String)o; - return LuaNumber.of( s.length() ); + return s.length(); } LuaFunction fn = lua.getHandlerFunction(se,"__len",o); if( fn != null )
--- a/src/luan/interp/LtExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/LtExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaException; import luan.LuaSource;
--- a/src/luan/interp/LuaParser.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/LuaParser.java Thu Dec 27 01:48:36 2012 +0000 @@ -17,7 +17,6 @@ import org.parboiled.support.ValueStack; import org.parboiled.errors.ErrorUtils; import luan.Lua; -import luan.LuaNumber; import luan.LuaState; import luan.LuaSource; @@ -304,7 +303,7 @@ return Sequence( start.set(currentIndex()), Keyword("for"), Name(), '=', Spaces(), Expr(), Keyword("to"), Expr(), - push( new ConstExpr(LuaNumber.of(1)) ), // default step + push( new ConstExpr(1) ), // default step Optional( Keyword("step"), drop(), @@ -892,13 +891,6 @@ } Rule NumberLiteral() { - return Sequence( - Number(), - push(LuaNumber.of((Double)pop())) - ); - } - - Rule Number() { return FirstOf( Sequence( IgnoreCase("0x"), @@ -906,7 +898,7 @@ ), Sequence( DecNumber(), - push(Double.parseDouble(match())) + push(Double.valueOf(match())) ) ); }
--- a/src/luan/interp/ModExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/ModExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( n1.value() % n2.value() ); + return n1.doubleValue() % n2.doubleValue(); return arithmetic(lua,"__mod",o1,o2); } }
--- a/src/luan/interp/MulExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/MulExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( n1.value() * n2.value() ); + return n1.doubleValue() * n2.doubleValue(); return arithmetic(lua,"__mul",o1,o2); } }
--- a/src/luan/interp/NumericForStmt.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/NumericForStmt.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -23,12 +22,12 @@ } @Override public void eval(LuaStateImpl lua) throws LuaException { - double v = lua.checkNumber( se, fromExpr.eval(lua) ).value(); - double limit = lua.checkNumber( se, toExpr.eval(lua) ).value(); - double step = lua.checkNumber( se, stepExpr.eval(lua) ).value(); + double v = lua.checkNumber( se, fromExpr.eval(lua) ).doubleValue(); + double limit = lua.checkNumber( se, toExpr.eval(lua) ).doubleValue(); + double step = lua.checkNumber( se, stepExpr.eval(lua) ).doubleValue(); try { while( step > 0.0 && v <= limit || step < 0.0 && v >= limit ) { - lua.stackSet( iVar, LuaNumber.of(v) ); + lua.stackSet( iVar, v ); block.eval(lua); v += step; }
--- a/src/luan/interp/PowExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/PowExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( Math.pow( n1.value(), n2.value() ) ); + return Math.pow( n1.doubleValue(), n2.doubleValue() ); return arithmetic(lua,"__pow",o1,o2); } }
--- a/src/luan/interp/SubExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/SubExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaException; import luan.LuaSource; @@ -15,10 +14,10 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o1 = op1.eval(lua); Object o2 = op2.eval(lua); - LuaNumber n1 = Lua.toNumber(o1); - LuaNumber n2 = Lua.toNumber(o2); + Number n1 = Lua.toNumber(o1); + Number n2 = Lua.toNumber(o2); if( n1 != null && n2 != null ) - return LuaNumber.of( n1.value() - n2.value() ); + return n1.doubleValue() - n2.doubleValue(); return arithmetic(lua,"__sub",o1,o2); } }
--- a/src/luan/interp/TableExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/TableExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -2,7 +2,6 @@ import luan.LuaException; import luan.LuaTable; -import luan.LuaNumber; import luan.LuaSource; @@ -34,7 +33,7 @@ } Object[] a = expressions.eval(lua); for( int i=0; i<a.length; i++ ) { - table.put( LuaNumber.of(i+1), a[i] ); + table.put( i+1, a[i] ); } return table; }
--- a/src/luan/interp/UnmExpr.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/interp/UnmExpr.java Thu Dec 27 01:48:36 2012 +0000 @@ -1,7 +1,6 @@ package luan.interp; import luan.Lua; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaException; import luan.LuaSource; @@ -16,9 +15,9 @@ @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o = op.eval(lua); - LuaNumber n = Lua.toNumber(o); + Number n = Lua.toNumber(o); if( n != null ) - return LuaNumber.of( -n.value() ); + return -n.doubleValue(); LuaFunction fn = lua.getHandlerFunction(se,"__unm",o); if( fn != null ) { return Lua.first(lua.call(fn,se,"__unm",o));
--- a/src/luan/lib/BasicLib.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/lib/BasicLib.java Thu Dec 27 01:48:36 2012 +0000 @@ -11,7 +11,6 @@ import luan.Lua; import luan.LuaState; import luan.LuaTable; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaJavaFunction; import luan.LuaException; @@ -174,7 +173,7 @@ throw new LuaException( lua, LuaElement.JAVA, "bad argument #1 to 'raw_len' (table or string expected)" ); } - public static LuaNumber to_number(Object e,Integer base) { + public static Number to_number(Object e,Integer base) { return Lua.toNumber(e,base); }
--- a/src/luan/lib/JavaLib.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/lib/JavaLib.java Thu Dec 27 01:48:36 2012 +0000 @@ -11,7 +11,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Iterator; -import luan.LuaNumber; +import luan.Lua; import luan.LuaState; import luan.LuaTable; import luan.MetatableGetter; @@ -92,13 +92,9 @@ if( "length".equals(key) ) { return Array.getLength(obj); } - if( key instanceof LuaNumber ) { - LuaNumber n = (LuaNumber)key; - double d = n.value(); - int i = (int)d; - if( d==i ) { - return Array.get(obj,i); - } + Integer i = Lua.asInteger(key); + if( i != null ) { + return Array.get(obj,i); } throw new LuaException(lua,LuaElement.JAVA,"invalid index for java array: "+key); } @@ -122,12 +118,7 @@ Member member = members.get(0); if( member instanceof Field ) { Field field = (Field)member; - Object value = field.get(obj); - if( value instanceof Number ) { - Number n = (Number)value; - value = LuaNumber.of(n); - } - return value; + return field.get(obj); } else { Method method = (Method)member; return new LuaJavaFunction(method,obj);
--- a/src/luan/lib/StringLib.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/lib/StringLib.java Thu Dec 27 01:48:36 2012 +0000 @@ -7,7 +7,6 @@ import luan.LuaTable; import luan.LuaFunction; import luan.LuaJavaFunction; -import luan.LuaNumber; import luan.LuaElement; import luan.LuaException; @@ -157,7 +156,7 @@ i++; } m.appendTail(sb); - return new Object[]{ sb.toString(), LuaNumber.of(i) }; + return new Object[]{ sb.toString(), i }; } if( repl instanceof LuaTable ) { LuaTable t = (LuaTable)repl; @@ -175,7 +174,7 @@ i++; } m.appendTail(sb); - return new Object[]{ sb.toString(), LuaNumber.of(i) }; + return new Object[]{ sb.toString(), i }; } if( repl instanceof LuaFunction ) { LuaFunction fn = (LuaFunction)repl; @@ -202,7 +201,7 @@ i++; } m.appendTail(sb); - return new Object[]{ sb.toString(), LuaNumber.of(i) }; + return new Object[]{ sb.toString(), i }; } throw new LuaException( lua, LuaElement.JAVA, "bad argument #3 to 'gsub' (string/function/table expected)" ); }
--- a/src/luan/lib/TableLib.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/lib/TableLib.java Thu Dec 27 01:48:36 2012 +0000 @@ -6,7 +6,6 @@ import luan.Lua; import luan.LuaState; import luan.LuaTable; -import luan.LuaNumber; import luan.LuaFunction; import luan.LuaJavaFunction; import luan.LuaElement; @@ -41,7 +40,7 @@ int last = j==null ? list.length() : j; StringBuilder buf = new StringBuilder(); for( int k=first; k<=last; k++ ) { - Object val = list.get(LuaNumber.of(k)); + Object val = list.get(k); if( val==null ) break; if( sep!=null && k > first )
--- a/src/luan/tools/CmdLine.java Thu Dec 27 00:44:58 2012 +0000 +++ b/src/luan/tools/CmdLine.java Thu Dec 27 01:48:36 2012 +0000 @@ -10,7 +10,6 @@ import luan.LuaState; import luan.LuaFunction; import luan.LuaTable; -import luan.LuaNumber; import luan.LuaException; import luan.interp.LuaCompiler; @@ -72,7 +71,7 @@ System.arraycopy(args,1,varArgs,0,varArgs.length); LuaTable argsTable = new LuaTable(); for( int j=0; j<args.length; j++ ) { - argsTable.put( LuaNumber.of(j), args[j] ); + argsTable.put( j, args[j] ); } lua.global().put("arg",argsTable); try {