Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jan 17, 2024
1 parent aa478c6 commit 41bb6cd
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 332 deletions.
6 changes: 5 additions & 1 deletion examples/basic/assets/data/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ ShapeRenderer = java.import("com.badlogic.gdx.graphics.glutils.ShapeRenderer")

renderer = ShapeRenderer.new()

renderer.begin()
renderer:begin()

print(ShapeRenderer)
print(renderer)


-- renderer:begin(50)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import imgui.extension.textedit.LanguageDefinition;
import imgui.extension.textedit.TextEditor;
import imgui.idl.helper.IDLString;
import lua.ErrorStatus;
import lua.LuaErrorStatus;
import lua.Lua;
import lua.LuaState;
import lua.example.basic.imgui.ImGuiRenderer;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void update() {
luaError = true;
}
else {
ErrorStatus status = lua.callFunction(0, 0, 0);
LuaErrorStatus status = lua.callFunction(0, 0, 0);
if(!status.isValid()) {
String errorText = status.getError();
System.err.println(errorText);
Expand All @@ -97,10 +97,11 @@ void renderEditText() {
}

public void buildScript(String scriptCode) {
ErrorStatus status = lua.buildScript(scriptCode);
LuaErrorStatus status = lua.buildScript(scriptCode);
long pointer = lua.getLuaState().lua_topointer(-1);
// String s = lua.dumpTable("ShapeRenderer");
// String s = lua.dumpTable("Vector2");
String ShapeRenderer = lua.dumpTable("ShapeRenderer");
String renderer = lua.dumpTable("renderer");
luaError = false;
if(!status.isValid()) {
String errorText = status.getError();
Expand Down
4 changes: 1 addition & 3 deletions lua/lua-build/src/main/cpp/cpp-source/custom/LuaCustom.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ class LuaState {
tableStr.append(space + " " + keyStr + " = " + valueStr + "\n");

if (lua_istable(L, -1)) {
indent++;

int count = 0;
for (int i = 0; i < vector.size(); i++) {
std::string value = vector[i];
Expand All @@ -327,7 +325,7 @@ class LuaState {
std::string stack = dumpStack();

if (count < 2) {
tableStr += dumpTable(vector, indent);
tableStr += dumpTable(vector, indent + 1);
}
}
lua_pop(L, 1);
Expand Down
49 changes: 38 additions & 11 deletions lua/lua-core/src/main/java/lua/Lua.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package lua;

import com.badlogic.gdx.utils.IntMap;
import java.util.HashMap;
import lua.idl.helper.IDLString;
import lua.register.DefaultImportFunction;
import lua.register.ImportListener;

public class Lua {

LuaState luaState;

LuaImport luaImport;
private final HashMap<String, ImportListener> importMap;

public IntMap<Object> luaJavaInstances;

public Lua() {
importMap = new HashMap<>();
luaState = new LuaState();
luaState.createContext();
luaJavaInstances = new IntMap<>();

luaImport = new LuaImport();
luaImport.register(this);
register(this);
}

public void dispose() {
Expand Down Expand Up @@ -58,8 +61,8 @@ public String dumpStack() {
return dumpStack(luaState);
}

public ErrorStatus buildScript(String script) {
ErrorStatus status = ErrorStatus.get();
public LuaErrorStatus buildScript(String script) {
LuaErrorStatus status = LuaErrorStatus.get();
luaState.luaL_loadstring(script);
status.code = luaState.lua_pcall(0, 0, 0);
status.error = "";
Expand All @@ -73,8 +76,8 @@ public ErrorStatus buildScript(String script) {
/**
* Call function
*/
public ErrorStatus callFunction(int nargs, int nresults, int msgh) {
ErrorStatus status = ErrorStatus.get();
public LuaErrorStatus callFunction(int nargs, int nresults, int msgh) {
LuaErrorStatus status = LuaErrorStatus.get();
status.code = luaState.lua_pcall(nargs, nresults, msgh);
status.error = "";
if(!status.isValid()) {
Expand All @@ -84,10 +87,6 @@ public ErrorStatus callFunction(int nargs, int nresults, int msgh) {
return status;
}

public LuaImport getLuaImport() {
return luaImport;
}

void addObjectInstance(int key, Object object) {
luaJavaInstances.put(object.hashCode(), object);
}
Expand All @@ -97,6 +96,34 @@ boolean removeObjectInstance(int hash) {
return remove != null;
}

public void addImportListener(String key, ImportListener importListener) {
importMap.put(key, importListener);
}

public void removeImportListener(String key) {
importMap.remove(key);
}

void register(Lua lua) {
LuaFunction function = new DefaultImportFunction() {
@Override
public int onImport(LuaState luaState) {
String importParam = luaState.lua_tostring(-1).c_str();
ImportListener importListener = importMap.get(importParam);
if(importListener != null) {
luaState.lua_pop(-1);
return importListener.onImport(luaState);
}
else {
luaState.luaL_error("Import param is not registered: " + importParam);
}
return 0;
}
};
LuaLibrary.addLibrary(lua, "java.import", function);
}


/**
* Return the table log on top of the stack
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lua;

public class ErrorStatus {
private ErrorStatus() {}
public class LuaErrorStatus {
private LuaErrorStatus() {}

static ErrorStatus errorStatus = new ErrorStatus();
public static ErrorStatus get() { errorStatus.reset(); return errorStatus; }
static LuaErrorStatus errorStatus = new LuaErrorStatus();
public static LuaErrorStatus get() { errorStatus.reset(); return errorStatus; }

int code;
String error;
Expand All @@ -25,4 +25,4 @@ void reset() {
code = 0;
error = "";
}
}
}
40 changes: 0 additions & 40 deletions lua/lua-core/src/main/java/lua/LuaImport.java

This file was deleted.

Loading

0 comments on commit 41bb6cd

Please sign in to comment.