From 78f4a99830cd68c2508b228b2040c919d04c81a7 Mon Sep 17 00:00:00 2001 From: pilaoda <793493083@qq.com> Date: Tue, 26 Aug 2025 00:07:20 +0800 Subject: [PATCH 1/2] add lua_call, lua_callk api --- scripts/build.sh | 4 ++++ src/binding-factory.ts | 8 ++++++++ src/lua.ts | 2 ++ 3 files changed, 14 insertions(+) diff --git a/scripts/build.sh b/scripts/build.sh index 1a81c68..39868a6 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -30,6 +30,7 @@ if [[ "$1" == "lua-5.0.3" ]]; then -s WASM_ASYNC_COMPILATION=0 \ -s EXPORTED_FUNCTIONS="[ '_luaL_loadbuffer', \ + '_lua_call', \ '_lua_close', \ '_lua_gettable', \ '_lua_gettop', \ @@ -69,6 +70,7 @@ elif [[ "$1" == "lua-5.1.5" ]]; then '_luaL_openlibs', \ '_luaL_loadbuffer', \ '_luaL_loadstring', \ + '_lua_call', \ '_lua_close', \ '_lua_getfield', \ '_lua_gettable', \ @@ -108,6 +110,7 @@ elif [[ "$1" == "lua-5.2.4" ]]; then '_luaL_newstate', \ '_luaL_openlibs', \ '_luaL_loadstring', \ + '_lua_callk', \ '_lua_close', \ '_lua_getfield', \ '_lua_getglobal', \ @@ -148,6 +151,7 @@ else '_luaL_newstate', \ '_luaL_openlibs', \ '_luaL_loadstring', \ + '_lua_callk', \ '_lua_close', \ '_lua_copy', \ '_lua_getfield', \ diff --git a/src/binding-factory.ts b/src/binding-factory.ts index ca5e49b..a014d19 100644 --- a/src/binding-factory.ts +++ b/src/binding-factory.ts @@ -68,6 +68,10 @@ const luaBindings: Record = { }, "<=5.1.x": function(luaGlue: LuaEmscriptenModule){ return { + lua_call: luaGlue.cwrap("lua_call", "number", ["number", "number", "number"]), + lua_callk: function (_L: LuaState, _nargs: number, _nresults: number, _ctx: number, _k: number) { + throw "callk not supported with Lua 5.1 and lower"; + }, // Need to overwrite because in lua 5.1 this is a function and not a #define (5.2 and higher) lua_pcall: luaGlue.cwrap("lua_pcall", "number", ["number", "number", "number", "number"]), // TODO there might be some way to mimic pcallk behaviour with 5.1 somehow @@ -78,6 +82,10 @@ const luaBindings: Record = { }, ">=5.1.0": function(luaGlue: LuaEmscriptenModule){ return { + lua_call: function (L: LuaState, nargs: number, nresults: number) { + return (this as Lua).lua_callk(L, nargs, nresults, 0, 0); + }, + lua_callk: luaGlue.cwrap("lua_callk", "number", ["number", "number", "number", "number", "number"]), lua_getfield: luaGlue.cwrap("lua_getfield", "number", ["number", "number", "string"]), lua_setfield: luaGlue.cwrap("lua_setfield", null, ["number", "number", "string"]), // TODO 3rd param is a output param (currently ignored) diff --git a/src/lua.ts b/src/lua.ts index c27b7d7..deb1f8f 100644 --- a/src/lua.ts +++ b/src/lua.ts @@ -22,6 +22,8 @@ export const LUA_ERRMEM = 4; export const LUA_ERRERR = 5; export interface Lua { + lua_call(L: LuaState, nargs: number, nresults: number): number; + lua_callk(L: LuaState, nargs: number, nresults: number, ctx: number, k: number): number; lua_close(L: LuaState): void; lua_copy(L: LuaState, fromIndex: number, toIndex: number): void; // TODO returns int in some lua versions void in others From 3fa39f7d464717677b201761e0a494e3192c72ff Mon Sep 17 00:00:00 2001 From: pilaoda <793493083@qq.com> Date: Tue, 26 Aug 2025 00:15:43 +0800 Subject: [PATCH 2/2] fix duplicate lua_call binding for 5.1 --- src/binding-factory.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/binding-factory.ts b/src/binding-factory.ts index a014d19..59d22cb 100644 --- a/src/binding-factory.ts +++ b/src/binding-factory.ts @@ -82,10 +82,6 @@ const luaBindings: Record = { }, ">=5.1.0": function(luaGlue: LuaEmscriptenModule){ return { - lua_call: function (L: LuaState, nargs: number, nresults: number) { - return (this as Lua).lua_callk(L, nargs, nresults, 0, 0); - }, - lua_callk: luaGlue.cwrap("lua_callk", "number", ["number", "number", "number", "number", "number"]), lua_getfield: luaGlue.cwrap("lua_getfield", "number", ["number", "number", "string"]), lua_setfield: luaGlue.cwrap("lua_setfield", null, ["number", "number", "string"]), // TODO 3rd param is a output param (currently ignored) @@ -98,6 +94,10 @@ const luaBindings: Record = { }, ">=5.2.0": function(luaGlue: LuaEmscriptenModule){ return { + lua_call: function (L: LuaState, nargs: number, nresults: number) { + return (this as Lua).lua_callk(L, nargs, nresults, 0, 0); + }, + lua_callk: luaGlue.cwrap("lua_callk", "number", ["number", "number", "number", "number", "number"]), lua_getglobal: luaGlue.cwrap("lua_getglobal", "number", ["number", "string"]), lua_pcall: function (L: LuaState, nargs: number, nresults: number, msgh: number) { return (this as Lua).lua_pcallk(L, nargs, nresults, msgh, 0, 0);