Skip to content

Commit

Permalink
style: test conversions and prune code
Browse files Browse the repository at this point in the history
  • Loading branch information
gudzpoz committed Oct 7, 2024
1 parent a82b493 commit 46df1a0
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void fullCoverageJunk() throws NoSuchMethodException {
Lua54Consts lua54Consts = new Lua54Consts() {};
LuaJitConsts luaJitConsts = new LuaJitConsts() {};
LuaJConsts luaJConsts = new LuaJConsts() {};
assertNull(JuaAPI.CONSTRUCTOR_WRAPPER.getName(FullCoverageJunkTest.class.getConstructor()));

Lua51 L = new Lua51();
ImmutableLuaValue.NIL(L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public static class OtherTypes {
public Override annotation;
public Runnable intf;
public Buffer buffer;

public Object any;
}

public static abstract class AbstractClass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.math.BigInteger;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -395,10 +396,13 @@ private void testRequire() {
L.run("assert(1024 == require('party.iroiro.luajava.JavaLibTest.open').getNumber())");
}
try (T L = constructor.get()) {
L.setExternalLoader(new ClassPathLoader());
assertThrowsLua(
LuaError.RUNTIME,
() -> L.require("luajava.wrongLuaFile")
);
L.require("suite.luajava-compat");
L.require("suite.empty");
}
}

Expand Down Expand Up @@ -875,14 +879,22 @@ private void testLuaToJavaConversions() {
for (int i = 0; i < classes.size(); i += 3) {
for (int j = 0; j < 3; j++) {
Object o = L.toObject(-1, classes.get(i + j));
assertInstanceOf(classes.get(i + 2), Objects.requireNonNull(o));
assertNotNull(o);
assertInstanceOf(classes.get(i + 2), o);
assertInstanceOf(Number.class, o);
assertEquals(127.0, ((Number) Objects.requireNonNull(o)).doubleValue(), 0.000001);
}
}
assertNull(L.toObject(-1, BigDecimal.class));
L.pop(1);

L.push("100");
assertEquals(STRING, L.type(-1));
Object buffer = L.toObject(-1, ByteBuffer.class);
assertNotNull(buffer);
assertEquals("100", StandardCharsets.UTF_8.decode(assertInstanceOf(ByteBuffer.class, buffer)).toString());
L.pop(1);

testToMap(luaNative);

testToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public void test() {
assertEquals(0, L.getTop());
luaStateTest();
assertEquals(0, L.getTop());
bufferTest();
assertEquals(0, L.getTop());
}

private void bufferTest() {
ByteBuffer buffer = L.from(100).toBuffer();
assertNotNull(buffer);
assertEquals(0, buffer.position());
assertEquals(3, buffer.remaining());
assertEquals("100", StandardCharsets.UTF_8.decode(buffer).toString());
}

@SuppressWarnings("SuspiciousMethodCalls")
Expand Down Expand Up @@ -126,6 +136,8 @@ private void stringTest() {

L.openLibrary("string");
assertEquals('t', L.require("string").get("byte").call("test", 1)[0].toInteger());

assertEquals("sss", L.from("sss").toString());
}

private void luaStateTest() {
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions example/suite/src/main/resources/suite/otherConvTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ assert(others.buffer:limit() == 3)
assert(others.buffer:get(0) == s:byte(1))
assert(others.buffer:get(1) == s:byte(2))
assert(others.buffer:get(2) == s:byte(3))

assert(others.any == nil)
others.any = true
assert(others.any == true)
others.any = 1
assert(others.any == 1)
others.any = '2'
assert(others.any == '2')
others.any = {1, 2, 3}
others.any = function() end
8 changes: 8 additions & 0 deletions luaj/src/main/java/party/iroiro/luajava/luaj/LuaJNatives.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ public long lua_topointer(long ptr, int index) {
public String lua_tostring(long ptr, int index) {
LuaJState L = instances.get((int) ptr);
LuaValue value = L.toLuaValue(index);
if (value.isnumber()) {
value = value.tostring();
L.replace(index, value);
}
return value.tojstring();
}

Expand Down Expand Up @@ -997,6 +1001,10 @@ public Object luaJ_tobuffer(long ptr, int index) {
if (!value.isstring()) {
return null;
}
if (!(value instanceof LuaString)) {
value = value.tostring();
L.replace(index, value);
}
byte[] bytes = ((LuaString) value).m_bytes;
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
buffer.put(bytes);
Expand Down
35 changes: 4 additions & 31 deletions luajava/src/main/java/party/iroiro/luajava/AbstractLua.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,38 +362,11 @@ public boolean toBoolean(int index) {

@Override
public @Nullable Object toObject(int index, Class<?> type) {
Object converted = toObject(index);
if (converted == null) {
try {
return JuaAPI.convertFromLua(this, type, index);
} catch (IllegalArgumentException ignored) {
return null;
} else if (type.isAssignableFrom(converted.getClass())) {
return converted;
} else if (Number.class.isAssignableFrom(converted.getClass())) {
Number number = ((Number) converted);
if (type == byte.class || type == Byte.class) {
return number.byteValue();
}
if (type == short.class || type == Short.class) {
return number.shortValue();
}
if (type == int.class || type == Integer.class) {
return number.intValue();
}
if (type == long.class || type == Long.class) {
return number.longValue();
}
if (type == float.class || type == Float.class) {
return number.floatValue();
}
if (type == double.class || type == Double.class) {
return number.doubleValue();
}
} else {
try {
return JuaAPI.convertFromLua(this, type, index);
} catch (IllegalArgumentException ignored) {
}
}
return null;
}

@Override
Expand Down Expand Up @@ -1108,7 +1081,7 @@ public LuaValue require(String module) throws LuaException {
}
}
LuaValue[] results = req.call(module);
return results.length > 0 ? results[0] : fromNull();
return results[0];
}

@Override
Expand Down
36 changes: 17 additions & 19 deletions luajava/src/main/java/party/iroiro/luajava/JuaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* Most reflection features on the lua side rely on this class.
* </p>
*/
@SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"})
public abstract class JuaAPI {
/**
* Allocates a direct buffer whose memory is managed by Java
Expand Down Expand Up @@ -928,7 +929,9 @@ private static Constructor<?> matchMethod(Class<?> clazz, String notSignature) {
}
Class<?>[] classes = getClasses(notSignature);
try {
return clazz.getConstructor(classes);
Constructor<?> constructor = clazz.getConstructor(classes);
CONSTRUCTOR_CACHE.put(clazz, notSignature, constructor);
return constructor;
} catch (NoSuchMethodException e) {
return null;
}
Expand All @@ -944,13 +947,16 @@ private static Constructor<?> matchMethod(Class<?> clazz, String notSignature) {
*/
@Nullable
private static Method matchMethod(Class<?> clazz, String name, String notSignature) {
Method cached = METHOD_CACHE.get(clazz, name + ",," + notSignature);
String key = name + ",," + notSignature;
Method cached = METHOD_CACHE.get(clazz, key);
if (cached != null) {
return cached;
}
Class<?>[] classes = getClasses(notSignature);
try {
return clazz.getMethod(name, classes);
Method method = clazz.getMethod(name, classes);
METHOD_CACHE.put(clazz, key, method);
return method;
} catch (NoSuchMethodException e) {
return null;
}
Expand All @@ -976,7 +982,7 @@ public static Object convertFromLua(Lua L, Class<?> clazz, int index)
return null;
}
} else if (type == Lua.LuaType.BOOLEAN) {
if (clazz == boolean.class || clazz == Boolean.class) {
if (clazz == boolean.class || clazz.isAssignableFrom(Boolean.class)) {
return L.toBoolean(index);
}
} else if (type == Lua.LuaType.STRING) {
Expand All @@ -1003,8 +1009,13 @@ public static Object convertFromLua(Lua L, Class<?> clazz, int index)
}
} else if (type == Lua.LuaType.USERDATA) {
Object object = L.toJavaObject(index);
if (object != null && clazz.isAssignableFrom(object.getClass())) {
return object;
if (object != null) {
if (clazz.isAssignableFrom(object.getClass())) {
return object;
}
if (Number.class.isAssignableFrom(object.getClass())) {
return convertNumber((Number) object, clazz);
}
}
} else if (type == Lua.LuaType.TABLE) {
if (clazz.isAssignableFrom(List.class)) {
Expand Down Expand Up @@ -1101,19 +1112,11 @@ public static Class<?>[] getClasses(String notSignature) {
* since {@code Executable} is not introduced until Java 8.
*/
interface ExecutableWrapper<T> {
@Nullable
String getName(T executable);

Class<?>[] getParameterTypes(T executable);
}

final static ExecutableWrapper<Constructor<?>> CONSTRUCTOR_WRAPPER =
new ExecutableWrapper<Constructor<?>>() {
@Override
public @Nullable String getName(Constructor<?> executable) {
return null;
}

@Override
public Class<?>[] getParameterTypes(Constructor<?> executable) {
return executable.getParameterTypes();
Expand All @@ -1122,11 +1125,6 @@ public Class<?>[] getParameterTypes(Constructor<?> executable) {

final static ExecutableWrapper<Method> METHOD_WRAPPER =
new ExecutableWrapper<Method>() {
@Override
public String getName(Method executable) {
return executable.getName();
}

@Override
public Class<?>[] getParameterTypes(Method executable) {
return executable.getParameterTypes();
Expand Down

0 comments on commit 46df1a0

Please sign in to comment.