Skip to content

Commit

Permalink
wip add helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jan 16, 2024
1 parent 1436122 commit 6cec009
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 25 deletions.
5 changes: 2 additions & 3 deletions examples/basic/assets/data/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ ShapeRenderer = java.import("com.badlogic.gdx.graphics.glutils.ShapeRenderer")

renderer = ShapeRenderer.new()

renderer.x = 10
renderer.begin()

print(renderer.x)
renderer:begin(50)
-- renderer:begin(50)

-- function render()
-- Gdx.gl.glClearColor(1.0, 1.0, 1.0, 1.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void buildScript(String scriptCode) {
ErrorStatus status = lua.buildScript(scriptCode);
long pointer = lua.getLuaState().lua_topointer(-1);
// String s = lua.dumpTable("ShapeRenderer");
String s = lua.dumpTable("Vector2");
// String s = lua.dumpTable("Vector2");
luaError = false;
if(!status.isValid()) {
String errorText = status.getError();
Expand Down
27 changes: 25 additions & 2 deletions lua/lua-core/src/main/java/lua/Lua.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lua;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.badlogic.gdx.utils.IntMap;
import lua.idl.helper.IDLString;

public class Lua {
Expand All @@ -10,9 +9,12 @@ public class Lua {

LuaImport luaImport;

public IntMap<Object> luaJavaInstances;

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

luaImport = new LuaImport();
luaImport.register(this);
Expand All @@ -31,6 +33,18 @@ public void registerGlobalFunction(String name, LuaFunction function) {
luaState.lua_setglobal(name);
}

/**
* Register function to table on top of the stack
*/
public boolean registerFunction(String name, LuaFunction function) {
if(luaState.lua_istable(-1) != 0) {
luaState.lua_pushcfunction(function);
luaState.lua_setfield(-2, name);
return true;
}
return false;
}

public String dumpTable() {
return dumpTable("", luaState);
}
Expand Down Expand Up @@ -74,6 +88,15 @@ public LuaImport getLuaImport() {
return luaImport;
}

void addObjectInstance(int key, Object object) {
luaJavaInstances.put(object.hashCode(), object);
}

boolean removeObjectInstance(int hash) {
Object remove = luaJavaInstances.remove(hash);
return remove != null;
}

/**
* Return the table log on top of the stack
*/
Expand Down
5 changes: 0 additions & 5 deletions lua/lua-core/src/main/java/lua/LuaImport.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package lua;

import java.util.HashMap;
import lua.DefaultImportFunction;
import lua.Lua;
import lua.LuaFunction;
import lua.LuaLibrary;
import lua.LuaState;
import lua.register.ImportListener;

public class LuaImport {
Expand Down
165 changes: 160 additions & 5 deletions lua/lua-core/src/main/java/lua/LuaLibrary.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package lua;

import lua.register.ImportListener;
import lua.register.LuaCreateClass;

public class LuaLibrary {

public static final String LUA_J_HASH = "java_hash";
private static final String TEMPLATE = "Template";

/**
* Get or create the table and push to stack.
* If parent exist, it will create or get as a child.
* If parent don't exist, it will create a global table.
*/
public static void getOrCreateTable(Lua lua, String table) {
private static void getOrCreateTable(Lua lua, String table) {
LuaState luaState = lua.luaState;
boolean hasParentTable = luaState.lua_istable(-1) != 0;
if(!hasParentTable) {
Expand Down Expand Up @@ -35,7 +41,7 @@ public static void getOrCreateTable(Lua lua, String table) {
}
}

public static void getOrCreateTables(Lua lua, String fullPath) {
private static void getOrCreateTables(Lua lua, String fullPath) {
LuaState luaState = lua.luaState;
String[] split = fullPath.split("\\.");
for(int i = 0; i < split.length; i++) {
Expand All @@ -54,10 +60,159 @@ public static void addLibrary(Lua lua, String fullPackage, LuaFunction function)

{
// Add function to table on top of the stack
luaState.lua_pushstring(lastItem);
luaState.lua_pushcfunction(function);
luaState.lua_settable(-3);
lua.registerFunction(lastItem, function);
}
luaState.lua_pop(-1); // remove table from stack
}

/**
* Will create tables and keep the last table into stack
*/
public static void getOrCreateClass(Lua lua, Class<?> clazz) {
String name = clazz.getName();
getOrCreateTables(lua, name);
}

/**
* Get and push meta table to top of stack. Return false if it doesn't exist.
*/
public static boolean getMetaTable(Lua lua, String metaTable) {
LuaState luaState = lua.luaState;
luaState.luaL_getmetatable(metaTable);
if(luaState.lua_isnil(-1) != 0) {
luaState.lua_pop(-1); // Pop nil
return false;
}
return true;
}

/**
* Get and push class template to top of stack. Return false if it doesn't exist.
*/
public static boolean getClassTemplate(Lua lua, String metaTable) {
if(getMetaTable(lua, metaTable)) {
LuaState luaState = lua.luaState;
luaState.lua_getfield(-1, TEMPLATE);
luaState.lua_remove(-2); // Remove metatable
return true;
}
return false;
}

public static void registerClass(Lua lua, Class<?> clazz, LuaCreateClass listener) {
String metaTable = clazz.getName();
LuaImport luaImport = lua.getLuaImport();

createMetaTableAndTemplate(lua, metaTable, listener);

luaImport.addImportListener(metaTable, new ImportListener() {
@Override
public int onImport(LuaState luaState) {
// Return a metatable
if(!getMetaTable(lua, metaTable)) {
luaState.luaL_error("Metatable don't exist");
}
return 1;
}
});
}

public static void registerClassFunction(Lua lua, Class<?> clazz, String functionName, LuaFunction function) {
LuaState luaState = lua.luaState;
String name = clazz.getName();
if(getClassTemplate(lua, name)) {
if(lua.registerFunction(functionName, function)) {
luaState.lua_pop(-1);
}
}
}

private static void createMetaTableAndTemplate(Lua lua, String metaTable, LuaCreateClass listener) {
LuaState luaState = lua.luaState;
luaState.luaL_newmetatable(metaTable);

// Put new function inside metatable
{
lua.registerFunction("new", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
System.out.println("NEW IS CALLED");
Object classObject = listener.onCreateClass(luaState);
if(classObject == null) {
luaState.luaL_error("Class object cannot be null");
return 0;
}

luaState.lua_newtable();

// Put hash key
{
int objectHash = classObject.hashCode();
lua.addObjectInstance(objectHash, classObject);
luaState.lua_pushinteger(objectHash);
luaState.lua_setfield(-2, LUA_J_HASH);
}

//Put metatable to the new created table
{
getClassTemplate(lua, metaTable);
luaState.lua_setmetatable(-2); // Set Template metatable to new table
}
return 1;
}
});
}

luaState.lua_newtable(); // create Template table

// mt.__index = mt
{
luaState.lua_pushvalue(-1); // copy Template reference
luaState.lua_setfield(-2, "__index");
}

// Set private metatable
{
luaState.lua_pushstring("Private");
luaState.lua_setfield(-2, "__metatable");
}

// Garbage collector
{
lua.registerFunction("__gc", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
luaState.lua_getfield(-1, LUA_J_HASH);
if(luaState.lua_isinteger(-1) != 0) {
int hash = luaState.lua_tointeger(-1);
lua.removeObjectInstance(hash);
}
return 0;
}
});
}

// Filter unknown properties
{
lua.registerFunction("__newindex", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
luaState.luaL_error("Class modification is not allowed");
// //Check if key is string
// if(luaState.lua_isstring(-2) != 0) {
// String key = luaState.lua_tostring(-2).c_str();
// if(key.equals("x")) {
// // allow only x
// luaState.lua_rawset(-3);
// }
// }
return 0;
}
});
}

luaState.lua_setfield(-2, TEMPLATE); // set Template to metatable

luaState.lua_pop(-1);
}
}
7 changes: 7 additions & 0 deletions lua/lua-core/src/main/java/lua/register/LuaCreateClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lua.register;

import lua.LuaState;

public interface LuaCreateClass {
Object onCreateClass(LuaState luaState);
}
2 changes: 1 addition & 1 deletion lua/lua-core/src/main/java/lua/register/gdx/LuaGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LuaGL {

public static void registerGL(Lua lua) {
LuaState luaState = lua.getLuaState();
LuaLibrary.getOrCreateTable(lua, "GL20");
LuaLibrary.getOrCreateClass(lua, GL20.class);

luaState.lua_pushstring("GL_COLOR_BUFFER_BIT");
luaState.lua_pushinteger(GL20.GL_COLOR_BUFFER_BIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import lua.Lua;
import lua.LuaFunction;
import lua.LuaImport;
import lua.LuaLibrary;
import lua.LuaState;
import lua.register.ImportListener;
import lua.register.LuaCreateClass;

public class LuaShapeRenderer {

Expand All @@ -23,18 +25,34 @@ public void register(Lua lua) {
LuaImport luaImport = lua.getLuaImport();
LuaState luaState = lua.getLuaState();

createTableTemplate(lua, luaState);

luaImport.addImportListener(METATABLE, new ImportListener() {
LuaLibrary.registerClass(lua, ShapeRenderer.class, new LuaCreateClass() {
@Override
public int onImport(LuaState luaState) {
// Return a table template to create our shape renderer

luaState.luaL_getmetatable(METATABLE);
public Object onCreateClass(LuaState luaState) {
return new ShapeRenderer();
}
});

return 1;
LuaLibrary.registerClassFunction(lua, ShapeRenderer.class, "begin", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
System.out.println("BEGIN CALLED");
return 0;
}
});


// createTableTemplate(lua, luaState);
//
// luaImport.addImportListener(METATABLE, new ImportListener() {
// @Override
// public int onImport(LuaState luaState) {
// // Return a table template to create our shape renderer
//
// luaState.luaL_getmetatable(METATABLE);
//
// return 1;
// }
// });
}

private void createTableTemplate(Lua lua, LuaState luaState) {
Expand Down

0 comments on commit 6cec009

Please sign in to comment.