Skip to content

Commit 4a466c6

Browse files
committed
Fixed 5.1.5 glue not loading because of missing pcallk
1 parent a22e8ba commit 4a466c6

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

.github/workflows/release.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: Release
32

43
on:
@@ -23,4 +22,4 @@ jobs:
2322
- run: npm run complete-build
2423
- run: npm publish
2524
env:
26-
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
25+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

README.md

+27-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
WASM bindings and binaries for Lua 5.1 to 5.4.
44

55
Make sure to run `./scripts/setup.ts` (requires emscripten sdk) first before using `npm run build`.
6+
67
### Important: This currently only includes the bindings used to test TypescriptToLua
78

89
In the future Bindings for the complete API may be added.
@@ -14,38 +15,36 @@ import { LUA_OK } from "lua-wasm-bindings/dist/lua";
1415

1516
import { lauxlib, lua, lualib } from "lua-wasm-bindings/dist/lua.54";
1617

17-
1818
const luaCode = `return "Hello"`;
1919
consol.log(executeLua(luaCode));
2020

21-
function executeLua (luaCode: string): string | Error | never {
22-
const L = lauxlib.luaL_newstate();
23-
lualib.luaL_openlibs(L);
24-
25-
// Optional Load modules
26-
// lua.lua_getglobal(L, "package");
27-
// lua.lua_getfield(L, -1, "preload");
28-
// lauxlib.luaL_loadstring(L, jsonLib); // Load extenal package from string
29-
// lua.lua_setfield(L, -2, "json");
30-
31-
const status = lauxlib.luaL_dostring(L, luaCode);
32-
33-
if (status === LUA_OK) {
34-
if (lua.lua_isstring(L, -1)) {
35-
const result = lua.lua_tostring(L, -1);
36-
lua.lua_close(L);
37-
return result === null ? undefined : result;
38-
} else {
39-
const returnType = lua.lua_typename(L, lua.lua_type(L, -1));
40-
lua.lua_close(L);
41-
throw new Error(`Unsupported Lua return type: ${returnType}`);
42-
}
21+
function executeLua(luaCode: string): string | Error | never {
22+
const L = lauxlib.luaL_newstate();
23+
lualib.luaL_openlibs(L);
24+
25+
// Optional Load modules
26+
// lua.lua_getglobal(L, "package");
27+
// lua.lua_getfield(L, -1, "preload");
28+
// lauxlib.luaL_loadstring(L, jsonLib); // Load extenal package from string
29+
// lua.lua_setfield(L, -2, "json");
30+
31+
const status = lauxlib.luaL_dostring(L, luaCode);
32+
33+
if (status === LUA_OK) {
34+
if (lua.lua_isstring(L, -1)) {
35+
const result = lua.lua_tostring(L, -1);
36+
lua.lua_close(L);
37+
return result === null ? undefined : result;
4338
} else {
44-
const luaStackString = lua.lua_tostring(L, -1);
45-
const message = luaStackString.replace(/^\[string "(--)?\.\.\."\]:\d+: /, "");
46-
lua.lua_close(L);
47-
return new Error(message);
39+
const returnType = lua.lua_typename(L, lua.lua_type(L, -1));
40+
lua.lua_close(L);
41+
throw new Error(`Unsupported Lua return type: ${returnType}`);
4842
}
43+
} else {
44+
const luaStackString = lua.lua_tostring(L, -1);
45+
const message = luaStackString.replace(/^\[string "(--)?\.\.\."\]:\d+: /, "");
46+
lua.lua_close(L);
47+
return new Error(message);
48+
}
4949
}
5050
```
51-

src/binding-factory.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import { LuaEmscriptenModule } from "./glue/glue";
22
import { LauxLib, Lua, LuaLib, LuaState, LUA_MULTRET } from "./lua";
33

4+
function safeCwrap(
5+
glue: LuaEmscriptenModule,
6+
ident: string,
7+
returnType: Emscripten.JSType | null,
8+
argTypes: Emscripten.JSType[],
9+
opts?: Emscripten.CCallOpts
10+
): (...args: any[]) => any {
11+
try {
12+
return glue.cwrap(ident, returnType, argTypes, opts);
13+
} catch {
14+
return _args => {
15+
throw `${ident} not supported in this Lua version!`;
16+
};
17+
}
18+
}
19+
420
/** @internal */
521
export function createLua(glue: LuaEmscriptenModule, overrides: Partial<Lua>): Lua {
622
const defaultLua: Lua = {
@@ -12,7 +28,14 @@ export function createLua(glue: LuaEmscriptenModule, overrides: Partial<Lua>): L
1228
lua_pcall: function (L: LuaState, nargs: number, nresults: number, msgh: number) {
1329
return this.lua_pcallk(L, nargs, nresults, msgh, 0, 0);
1430
},
15-
lua_pcallk: glue.cwrap("lua_pcallk", "number", ["number", "number", "number", "number", "number", "number"]),
31+
lua_pcallk: safeCwrap(glue, "lua_pcallk", "number", [
32+
"number",
33+
"number",
34+
"number",
35+
"number",
36+
"number",
37+
"number",
38+
]),
1639
lua_setfield: glue.cwrap("lua_setfield", null, ["number", "number", "string"]),
1740
lua_tolstring: glue.cwrap("lua_tolstring", "string", ["number", "number", "number"]),
1841
// In C this is just a #define so we have to recreate it ourself

src/glue/glue-lua-5.1.5.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import type { EmscriptenModuleFactorySync, LuaEmscriptenModule } from "./glue";
22

33
declare const glue: EmscriptenModuleFactorySync<LuaEmscriptenModule>;
44

5-
export default glue;
5+
export default glue;

src/glue/glue-lua-5.2.4.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import type { EmscriptenModuleFactorySync, LuaEmscriptenModule } from "./glue";
22

33
declare const glue: EmscriptenModuleFactorySync<LuaEmscriptenModule>;
44

5-
export default glue;
5+
export default glue;

src/glue/glue-lua-5.3.6.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import type { EmscriptenModuleFactorySync, LuaEmscriptenModule } from "./glue";
22

33
declare const glue: EmscriptenModuleFactorySync<LuaEmscriptenModule>;
44

5-
export default glue;
5+
export default glue;

src/glue/glue-lua-5.4.2.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import type { EmscriptenModuleFactorySync, LuaEmscriptenModule } from "./glue";
22

33
declare const glue: EmscriptenModuleFactorySync<LuaEmscriptenModule>;
44

5-
export default glue;
5+
export default glue;

0 commit comments

Comments
 (0)