-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move to itsallcode org * Fix sonar warnings * Add dependency submission workflow --------- Co-authored-by: kaklakariada <[email protected]> Co-authored-by: kaklakariada <[email protected]>
- Loading branch information
1 parent
818cbde
commit 5419af8
Showing
12 changed files
with
347 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Dependency Submission | ||
|
||
on: | ||
# Only runs on main branch | ||
push: | ||
branches: [ main ] | ||
|
||
permissions: | ||
contents: write # Required for dependency submission | ||
|
||
jobs: | ||
dependency-submission: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v4 | ||
- name: Setup Java | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: 23 | ||
- name: Generate and submit dependency graph | ||
uses: gradle/actions/dependency-submission@v4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,85 @@ | ||
package org.itsallcode.luava; | ||
|
||
import java.lang.foreign.Arena; | ||
import static java.util.Collections.emptyList; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.UnaryOperator; | ||
import java.util.logging.Logger; | ||
|
||
import org.itsallcode.luava.ffi.Lua; | ||
import org.itsallcode.luava.ffi.lua_CFunction; | ||
|
||
public class LuaFunction { | ||
private static final Logger LOG = Logger.getLogger(LuaFunction.class.getName()); | ||
private final LowLevelLua lua; | ||
private final String name; | ||
private lua_CFunction.Function messageHandler; | ||
private List<Object> argumentValues = emptyList(); | ||
private List<Class<?>> resultTypes = emptyList(); | ||
|
||
private final MemorySegment state; | ||
private final LuaStack stack; | ||
private final Arena arena; | ||
private final int idx; | ||
LuaFunction(final LowLevelLua lowLevelLua, final String name) { | ||
this.lua = lowLevelLua; | ||
this.name = name; | ||
} | ||
|
||
LuaFunction(final MemorySegment state, final LuaStack stack, final Arena arena, final int idx) { | ||
this.state = state; | ||
this.stack = stack; | ||
this.arena = arena; | ||
this.idx = idx; | ||
@SuppressWarnings("java:S923") // Using varargs by intention | ||
public LuaFunction argumentValues(final Object... argumentValues) { | ||
this.argumentValues = Arrays.asList(argumentValues); | ||
return this; | ||
} | ||
|
||
public void addArgInteger(final int value) { | ||
stack.pushInteger(value); | ||
@SuppressWarnings("java:S923") // Using varargs by intention | ||
public LuaFunction resultTypes(final Class<?>... resultTypes) { | ||
this.resultTypes = Arrays.asList(resultTypes); | ||
return this; | ||
} | ||
|
||
public void call(final int nargs, final int nresults) { | ||
call(nargs, nresults, 0, 0, Lua.NULL()); | ||
public LuaFunction messageUpdateHandler(final UnaryOperator<String> messageHandler) { | ||
return messageHandler((final MemorySegment l) -> { | ||
LOG.fine("Message handler: popping error message..."); | ||
final String msg = lua.stack().popString(); | ||
final String updatedMsg = messageHandler.apply(msg); | ||
LOG.fine(() -> "Pushing updated error message '" + updatedMsg + "'"); | ||
lua.stack().pushString(updatedMsg); | ||
return Lua.LUA_OK(); | ||
}); | ||
} | ||
|
||
private void call(final int nargs, final int nresults, final int errfunc, final long ctx, final MemorySegment k) { | ||
final int status = Lua.lua_pcallk(state, nargs, nresults, errfunc, ctx, k); | ||
if (status != Lua.LUA_OK()) { | ||
final String errorMessage = stack.toString(-1); | ||
stack.pop(1); | ||
throw new FunctionCallException("lua_pcallk", status, errorMessage); | ||
public LuaFunction messageHandler(final lua_CFunction.Function messageHandler) { | ||
this.messageHandler = messageHandler; | ||
return this; | ||
} | ||
|
||
public List<Object> call() { | ||
final int messageHandlerIdx = getMessageHandlerIdx(); | ||
lua.getGlobal(name); | ||
pushArguments(); | ||
try { | ||
lua.pcall(this.argumentValues.size(), this.resultTypes.size(), messageHandlerIdx); | ||
return getResultValues(); | ||
} finally { | ||
if (this.messageHandler != null) { | ||
LOG.fine("Popping message handler"); | ||
lua.stack().pop(); | ||
} | ||
} | ||
} | ||
|
||
public long getIntegerResult() { | ||
final long value = stack.toInteger(-1); | ||
stack.pop(1); | ||
return value; | ||
private void pushArguments() { | ||
this.argumentValues.forEach(v -> lua.stack().pushObject(v)); | ||
} | ||
|
||
private List<Object> getResultValues() { | ||
return this.resultTypes.stream().map(t -> lua.stack().popObject(t)).toList(); | ||
} | ||
|
||
private int getMessageHandlerIdx() { | ||
if (messageHandler == null) { | ||
return 0; | ||
} | ||
lua.stack().pushCFunction(messageHandler); | ||
return lua.stack().getTop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.