Skip to content

Commit

Permalink
Add LuaFunction interface
Browse files Browse the repository at this point in the history
- Also fix AndroidScriptTest
  • Loading branch information
gudzpoz committed May 23, 2024
1 parent 082a7d4 commit eb67bd1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package party.iroiro.luajava;

import android.util.Log;
import org.junit.Test;
import party.iroiro.luajava.lua51.Lua51;
import party.iroiro.luajava.lua52.Lua52;
Expand Down Expand Up @@ -40,15 +39,15 @@ public void lua54Test() {
@Test
public void luaJitTest() {
try (LuaJit L = new LuaJit()) {
new LuaScriptSuite<>(L, s -> Log.i("test", s)).test();
new LuaScriptSuite<>(L).test();
}
}

@Test
public void luaJTest() {
org.junit.Assume.assumeTrue(android.os.Build.VERSION.SDK_INT >= 30);
try (AbstractLua L = AndroidLuaTest.getLuaJ()) {
new LuaScriptSuite<>(L, s -> Log.i("test", s)).test();
new LuaScriptSuite<>(L).test();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,14 @@ private void testProxy() {
private void testOthers() {
L.openLibrary("math");
L.run("assert(1.0 == math.abs(-1.0))");

L.register("testOthersFunction", l -> {
L.push(l -> {
l.push("Hello");
return 1;
});
L.run("assert('Hello' == testOthersFunction())");
L.setGlobal("testOthersFunction1");
L.register("testOthersFunction2", (l, args) -> new LuaValue[]{ l.from("Hello") });
L.run("assert('Hello' == testOthersFunction1())");
L.run("assert('Hello' == testOthersFunction2())");

L.newRegisteredMetatable("myusertype");
L.push(l -> {
Expand Down
40 changes: 39 additions & 1 deletion luajava/src/main/java/party/iroiro/luajava/AbstractLua.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public void push(@Nullable Object object, Conversion degree) {
} else if (object instanceof LuaValue) {
LuaValue value = (LuaValue) object;
value.push(this);
} else if (object instanceof LuaFunction) {
LuaFunction function = (LuaFunction) object;
this.push(function);
} else if (degree == Conversion.NONE) {
pushJavaObjectOrArray(object);
} else {
Expand Down Expand Up @@ -244,6 +247,18 @@ public void push(@NotNull JFunction function) {
C.luaJ_pushfunction(L, function);
}

@Override
public void push(@NotNull LuaValue value) {
checkStack(1);
value.push(this);
}

@Override
public void push(@NotNull LuaFunction function) {
checkStack(1);
push(new LuaFunctionWrapper(function));
}

@Override
public void pushJavaObject(@NotNull Object object) throws IllegalArgumentException {
if (object.getClass().isArray()) {
Expand Down Expand Up @@ -825,7 +840,7 @@ public Object createProxy(Class<?>[] interfaces, Conversion degree)
}

@Override
public void register(String name, JFunction function) {
public void register(String name, LuaFunction function) {
push(function);
setGlobal(name);
}
Expand Down Expand Up @@ -1144,4 +1159,27 @@ private void recycleReferences() {
protected boolean shouldSynchronize() {
return true;
}

private static class LuaFunctionWrapper implements JFunction {
private final @NotNull LuaFunction function;

public LuaFunctionWrapper(@NotNull LuaFunction function) {
this.function = function;
}

@Override
public int __call(Lua L) {
LuaValue[] args = new LuaValue[L.getTop()];
for (int i = 0; i < args.length; i++) {
args[args.length - i - 1] = L.get();
}
LuaValue[] results = function.call(L, args);
if (results != null) {
for (LuaValue result : results) {
L.push(result);
}
}
return results == null ? 0 : results.length;
}
}
}
16 changes: 16 additions & 0 deletions luajava/src/main/java/party/iroiro/luajava/Lua.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import party.iroiro.luajava.value.LuaFunction;
import party.iroiro.luajava.value.LuaThread;
import party.iroiro.luajava.value.LuaValue;

Expand Down Expand Up @@ -149,6 +150,21 @@ public interface Lua extends AutoCloseable, LuaThread {
*/
void pushJavaClass(@NotNull Class<?> clazz);

/**
* Push a {@link LuaValue} onto the stack, equivalent to {@link LuaValue#push(Lua)}
*
* @param value the value
*/
void push(@NotNull LuaValue value);

/**
* Push the function onto the stack, converted to a callable element
*
* @param value the function
* @see #push(JFunction)
*/
void push(@NotNull LuaFunction value);

/**
* Push the element onto the stack, converted as is to Java objects
*
Expand Down
22 changes: 22 additions & 0 deletions luajava/src/main/java/party/iroiro/luajava/value/LuaFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package party.iroiro.luajava.value;

import party.iroiro.luajava.Lua;

/**
* Interface for functions implemented in Java.
*/
public interface LuaFunction {
/**
* Implements the function body
*
* <p>
* Unlike {@link party.iroiro.luajava.JFunction#__call(Lua)}, before actually calling this function,
* the library converts all the arguments to {@link LuaValue LuaValues} and pops them off the stack.
* </p>
*
* @param L the Lua state
* @param args the arguments
* @return the return values (nullable)
*/
LuaValue[] call(Lua L, LuaValue[] args);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package party.iroiro.luajava.value;

import org.jetbrains.annotations.Nullable;
import party.iroiro.luajava.JFunction;
import party.iroiro.luajava.LuaException;

public interface LuaThread {
Expand All @@ -27,7 +26,7 @@ public interface LuaThread {
* @param name the global name
* @param function the function
*/
void register(String name, JFunction function);
void register(String name, LuaFunction function);

/**
* Executes Lua code
Expand Down

0 comments on commit eb67bd1

Please sign in to comment.