Skip to content

Commit

Permalink
auto-convert Java-Array to Lua-Table;fix the incorrection of java Num…
Browse files Browse the repository at this point in the history
…ber to lua issue
  • Loading branch information
hyee committed Jan 24, 2017
1 parent 647ec39 commit d39797d
Show file tree
Hide file tree
Showing 28 changed files with 105 additions and 205 deletions.
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.naef.jnlua.console.LuaConsole

1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: Converter.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
74 changes: 55 additions & 19 deletions src/main/java/com/naef/jnlua/DefaultConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* $Id: DefaultConverter.java 161 2012-10-06 13:53:02Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

import com.naef.jnlua.util.AbstractTableList;
import com.naef.jnlua.util.AbstractTableMap;
import sun.misc.FloatingDecimal;

import java.lang.reflect.Array;
import java.math.BigDecimal;
Expand All @@ -24,12 +24,10 @@ public class DefaultConverter implements Converter {
* Raw byte array.
*/
private static final boolean RAW_BYTE_ARRAY = Boolean.parseBoolean(System.getProperty(DefaultConverter.class.getPackage().getName() + ".rawByteArray"));

/**
* Static instance.
*/
private static final DefaultConverter INSTANCE = new DefaultConverter();

/**
* Boolean distance map.
*/
Expand Down Expand Up @@ -221,26 +219,44 @@ public void convert(LuaState luaState, Boolean booleanValue) {
};
JAVA_OBJECT_CONVERTERS.put(Boolean.class, booleanConverter);
JAVA_OBJECT_CONVERTERS.put(Boolean.TYPE, booleanConverter);
JavaObjectConverter<Number> numberConverter = new JavaObjectConverter<Number>() {
JavaObjectConverter<Number> doubleConverter = new JavaObjectConverter<Number>() {
@Override
public void convert(LuaState luaState, Number number) {
luaState.pushNumber(number.doubleValue());
Double d=number.doubleValue();
switch (number.getClass().getSimpleName()) {
case "Double":
luaState.pushNumber((Double)number);
break;
case "Float":
luaState.pushNumber(new FloatingDecimal((Float)number).doubleValue());
break;
case "BigInteger":
if(number.toString().equals(new BigInteger(d.toString()))) luaState.pushNumber(d);
else luaState.pushString(number.toString());
break;
case "BigDecimal":
if(number.toString().equals(new BigDecimal(d).toString())) luaState.pushNumber(d);
else luaState.pushString(number.toString());
break;
default:
luaState.pushNumber(d);;
}
}
};
JAVA_OBJECT_CONVERTERS.put(Byte.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Byte.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Short.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Short.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Integer.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Integer.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Long.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Long.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Float.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Float.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Double.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Double.TYPE, numberConverter);
JAVA_OBJECT_CONVERTERS.put(BigInteger.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(BigDecimal.class, numberConverter);
JAVA_OBJECT_CONVERTERS.put(Byte.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Byte.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Short.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Short.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Integer.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Integer.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Long.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Long.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Double.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Double.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Float.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(Float.TYPE, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(BigInteger.class, doubleConverter);
JAVA_OBJECT_CONVERTERS.put(BigDecimal.class, doubleConverter);
JavaObjectConverter<Character> characterConverter = new JavaObjectConverter<Character>() {
@Override
public void convert(LuaState luaState, Character character) {
Expand All @@ -256,6 +272,19 @@ public void convert(LuaState luaState, String string) {
}
};
JAVA_OBJECT_CONVERTERS.put(String.class, stringConverter);
final JavaObjectConverter<Object[]> arrayConverter = new JavaObjectConverter<Object[]>() {
@Override
public void convert(LuaState luaState, Object[] obj) {
luaState.newTable(obj.length, 0);
for (int i = 0; i < obj.length; i++) {
luaState.getConverter().convertJavaObject(luaState, obj[i]);
luaState.rawSet(-2, i + 1);
}
}
};

JAVA_OBJECT_CONVERTERS.put(Object[].class, arrayConverter);

if (!RAW_BYTE_ARRAY) {
JavaObjectConverter<byte[]> byteArrayConverter = new JavaObjectConverter<byte[]>() {
@Override
Expand Down Expand Up @@ -523,6 +552,13 @@ public void convertJavaObject(LuaState luaState, Object object) {
javaObjectConverter.convert(luaState, object);
return;
}

if (object instanceof Object[]) {
JavaObjectConverter<Object[]> converter = (JavaObjectConverter<Object[]>) JAVA_OBJECT_CONVERTERS.get(Object[].class);
converter.convert(luaState, (Object[]) object);
return;
}

if (object instanceof JavaFunction) {
luaState.pushJavaFunction((JavaFunction) object);
return;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/naef/jnlua/DefaultJavaReflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: DefaultJavaReflector.java 174 2013-07-28 20:46:22Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

import java.beans.BeanInfo;
Expand All @@ -23,7 +22,6 @@ public class DefaultJavaReflector implements JavaReflector {
private static final DefaultJavaReflector INSTANCE = new DefaultJavaReflector();
private static final Object JAVA_FUNCTION_TYPE = new Object();
private static final Object[] EMPTY_ARGUMENTS = new Object[0];

// -- State
private Map<Class<?>, Map<String, Accessor>> accessors = new HashMap<Class<?>, Map<String, Accessor>>();
private ReadWriteLock accessorLock = new ReentrantReadWriteLock();
Expand Down Expand Up @@ -572,7 +570,6 @@ public String toString() {
private static class InvocableProxy implements Invocable {
// -- Static
private static final Class<?>[] PARAMETER_TYPES = new Class<?>[]{LuaValueProxy.class};

// -- State
private Class<?> interfaze;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/JavaFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: JavaFunction.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/naef/jnlua/JavaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: JavaModule.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

import com.naef.jnlua.JavaReflector.Metamethod;
Expand Down Expand Up @@ -538,7 +537,6 @@ private static class LuaMap implements JavaReflector, TypedJavaObject {
// -- Static
private static final JavaFunction INDEX = new Index();
private static final JavaFunction NEW_INDEX = new NewIndex();

// -- State
private Map<Object, Object> map;

Expand Down Expand Up @@ -639,7 +637,6 @@ private static class LuaList implements JavaReflector, TypedJavaObject {
private static final JavaFunction INDEX = new Index();
private static final JavaFunction NEW_INDEX = new NewIndex();
private static final JavaFunction LENGTH = new Length();

// -- State
private List<Object> list;

Expand Down
73 changes: 18 additions & 55 deletions src/main/java/com/naef/jnlua/JavaReflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: JavaReflector.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down Expand Up @@ -38,94 +37,58 @@ public enum Metamethod {
/**
* <code>__index</code> metamethod.
*/
INDEX,

/**
INDEX, /**
* <code>__newindex</code> metamethod.
*/
NEWINDEX,

/**
NEWINDEX, /**
* <code>__len</code> metamethod.
*/
LEN,

/**
LEN, /**
* <code>__eq</code> metamethod.
*/
EQ,

/**
EQ, /**
* <code>__lt</code> metamethod.
*/
LT,

/**
LT, /**
* <code>__le</code> metamethod.
*/
LE,

/**
LE, /**
* <code>__unm</code> metamethod.
*/
UNM,

/**
UNM, /**
* <code>__add</code> metamethod.
*/
ADD,

/**
ADD, /**
* <code>__sub</code> metamethod.
*/
SUB,

/**
SUB, /**
* <code>__mul</code> metamethod.
*/
MUL,

/**
MUL, /**
* <code>__div</code> metamethod.
*/
DIV,

/**
DIV, /**
* <code>__mod</code> metamethod.
*/
MOD,

/**
MOD, /**
* <code>__pow</code> metamethod.
*/
POW,

/**
POW, /**
* <code>__concat</code> metamethod.
*/
CONCAT,

/**
CONCAT, /**
* <code>__call</code> metamethod.
*/
CALL,

/**
CALL, /**
* <code>__tostring</code> metamethod.
*/
TOSTRING,

/**
TOSTRING, /**
* <code>__javafields</code> metamethod.
*/
JAVAFIELDS,

/**
JAVAFIELDS, /**
* <code>__javamethods</code> metamethod.
*/
JAVAMETHODS,

/**
JAVAMETHODS, /**
* <code>__javaproperties</code> metamethod.
*/
JAVAPROPERTIES;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/LuaError.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaError.java 154 2012-02-01 20:40:01Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

import java.io.PrintWriter;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/LuaException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaException.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/LuaGcMetamethodException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id$
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaMemoryAllocationException.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaMessageHandlerException.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/naef/jnlua/LuaRuntimeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaRuntimeException.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

import java.io.PrintStream;
Expand All @@ -21,7 +20,6 @@ public class LuaRuntimeException extends LuaException {
// -- Static
private static final long serialVersionUID = 1L;
private static final LuaStackTraceElement[] EMPTY_LUA_STACK_TRACE = new LuaStackTraceElement[0];

// -- State
private LuaStackTraceElement[] luaStackTrace;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/naef/jnlua/LuaStackTraceElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* $Id: LuaStackTraceElement.java 121 2012-01-22 01:40:14Z [email protected] $
* See LICENSE.txt for license terms.
*/

package com.naef.jnlua;

/**
Expand Down
Loading

0 comments on commit d39797d

Please sign in to comment.