Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'party.iroiro.luajava'
version(System.getenv('IS_RELEASE') == 'true' ? '4.0.2' : '4.0.4-SNAPSHOT')
version(System.getenv('IS_RELEASE') == 'true' ? '4.0.2' : '4.0.5-SNAPSHOT')



Expand Down
10 changes: 10 additions & 0 deletions luajava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ plugins {

repositories {
mavenCentral()
maven {
url = uri("https://maven.pkg.github.com/lualink/fast-reflection")
name = "FastReflection"
credentials {
username = findProperty("gpr.actor") ?: System.getenv("GITHUB_ACTOR")
password = findProperty("gpr.token") ?: System.getenv("GITHUB_TOKEN")
}
}

}

group = rootProject.group
Expand All @@ -14,6 +23,7 @@ version = rootProject.version
dependencies {
implementation 'org.jetbrains:annotations:24.1.0'
implementation 'com.badlogicgames.gdx:gdx-jnigen-loader:2.5.2'
implementation 'me.sunlan:fast-reflection:1.0.3'
}

tasks.withType(JavaCompile) {
Expand Down
70 changes: 45 additions & 25 deletions luajava/src/main/java/party/iroiro/luajava/JuaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import party.iroiro.luajava.value.LuaValue;

import java.lang.reflect.*;
import me.sunlan.fastreflection.FastMethod;
import me.sunlan.fastreflection.FastField;
import me.sunlan.fastreflection.FastConstructor;
import me.sunlan.fastreflection.FastClass;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -155,18 +159,24 @@ public static int loadLib(int id, String className, String methodName) {
AbstractLua L = Jua.get(id);
try {
Class<?> clazz = ClassUtils.forName(className);
Method method = clazz.getDeclaredMethod(methodName, Lua.class);
if (method.getReturnType() == int.class) {
//noinspection Convert2Lambda
FastClass<?> fastClass = FastClass.create(clazz);
FastMethod method;
try {
method = fastClass.getMethod(methodName, Lua.class);
} catch (NoSuchMethodException e) {
L.pushNil();
L.push("\n no method '" + methodName + "': no such method");
return 2;
}
// FastMethod.getReturnType() returns FastClass<?>, so get the raw class
if (method.getReturnType().getRawClass() == int.class) {
L.push(new JFunction() {
@Override
public int __call(Lua l) {
try {
return (Integer) method.invoke(null, l);
} catch (IllegalAccessException e) {
} catch (Throwable e) {
return l.error(e);
} catch (InvocationTargetException e) {
return l.error(e.getCause());
}
}
});
Expand All @@ -176,7 +186,7 @@ public int __call(Lua l) {
L.push("\n no method '" + methodName + "': not returning int values");
return 2;
}
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
} catch (ClassNotFoundException ignored) {
L.pushNil();
L.push("\n no method '" + methodName + "': no such method");
return 2;
Expand Down Expand Up @@ -817,10 +827,10 @@ private static Object[] transformVarArgs(Executable executable, Object[] objects

private final static class OptionalField {
@Nullable
public final Field field;
public final FastField fastField;

private OptionalField(@Nullable Field field) {
this.field = field;
private OptionalField(@Nullable FastField fastField) {
this.fastField = fastField;
}
}

Expand All @@ -845,20 +855,24 @@ private OptionalField(@Nullable Field field) {
public static int fieldIndex(Lua L, Class<?> clazz, @Nullable Object object, String name) {
try {
OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name);
Field field;
FastField fastField;
if (optionalField == null) {
field = clazz.getField(name);
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field));
FastClass<?> fastClass = FastClass.create(clazz);
fastField = fastClass.getField(name);
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField));
} else {
field = optionalField.field;
if (field == null) {
fastField = optionalField.fastField;
if (fastField == null) {
return 2;
}
}
Object obj = field.get(object);
if (fastField == null) {
return 2;
}
Object obj = fastField.get(object);
L.push(obj, Lua.Conversion.SEMI);
return 1;
} catch (NoSuchFieldException | IllegalAccessException | NullPointerException ignored) {
} catch (Throwable ignored) {
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null));
return 2;
}
Expand All @@ -877,21 +891,27 @@ private static int fieldNewIndex(int index, Class<?> clazz, Object object, Strin
Lua L = Jua.get(index);
try {
OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name);
Field field;
FastField fastField;
if (optionalField == null) {
field = clazz.getField(name);
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field));
FastClass<?> fastClass = FastClass.create(clazz);
fastField = fastClass.getField(name);
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField));
} else {
field = optionalField.field;
if (field == null) {
fastField = optionalField.fastField;
if (fastField == null) {
return L.error(new NoSuchFieldException(name));
}
}
Class<?> type = field.getType();
if (fastField == null) {
return L.error(new NoSuchFieldException(name));
}
// FastField does not have getType(), but has getDeclaringClass() and getName().
// To get the type, use reflection on the underlying class:
Class<?> type = fastField.getDeclaringClass().getRawClass().getDeclaredField(fastField.getName()).getType();
Object o = convertFromLua(L, type, 3);
field.set(object, o);
fastField.set(object, o);
return 0;
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
} catch (Throwable e) {
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null));
return L.error(e);
}
Expand Down
Loading