Skip to content

Commit afbf61e

Browse files
committed
fix: semver checks, globals index addresses
1 parent d5731b3 commit afbf61e

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/binding-factory.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { satisfies } from "semver";
22
import { LuaEmscriptenModule } from "./glue/glue";
3-
import { LauxLib, Lua, LuaLib, LuaState, LUA_GLOBALSINDEX, LUA_MULTRET } from "./lua";
3+
import { LauxLib, Lua, LuaLib, LuaState, LUA_GLOBALSINDEX_50, LUA_GLOBALSINDEX_51, LUA_MULTRET } from "./lua";
44

55
type luaBindingFactoryFunc = (luaGlue: LuaEmscriptenModule) => Partial<Lua>;
66
const luaBindings: Record<string, luaBindingFactoryFunc> = {
@@ -23,6 +23,10 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
2323
},
2424
"5.0.x": function(luaGlue: LuaEmscriptenModule){
2525
return {
26+
// #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, s)
27+
lua_getglobal: function (L: LuaState, name: string) {
28+
return (this as Lua).lua_getfield(L, LUA_GLOBALSINDEX_50, name);
29+
},
2630
lua_getfield: function(L: LuaState, index: number, k: string) {
2731
(this as Lua).lua_pushstring(L, k);
2832
return (this as Lua).lua_gettable(L, index);
@@ -51,12 +55,16 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
5155
lua_tostring: luaGlue.cwrap("lua_tostring", "number", ["number", "number"])
5256
};
5357
},
54-
"<=5.1.0": function(luaGlue: LuaEmscriptenModule){
58+
"5.1.x": function(_: LuaEmscriptenModule){
5559
return {
5660
// #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, s)
5761
lua_getglobal: function (L: LuaState, name: string) {
58-
return (this as Lua).lua_getfield(L, LUA_GLOBALSINDEX, name);
59-
},
62+
return (this as Lua).lua_getfield(L, LUA_GLOBALSINDEX_51, name);
63+
}
64+
};
65+
},
66+
"<=5.1.x": function(luaGlue: LuaEmscriptenModule){
67+
return {
6068
// Need to overwrite because in lua 5.1 this is a function and not a #define (5.2 and higher)
6169
lua_pcall: luaGlue.cwrap("lua_pcall", "number", ["number", "number", "number", "number"]),
6270
// TODO there might be some way to mimic pcallk behaviour with 5.1 somehow
@@ -93,7 +101,7 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
93101
])
94102
};
95103
},
96-
"<=5.2.0": function(luaGlue: LuaEmscriptenModule){
104+
"<=5.2.x": function(luaGlue: LuaEmscriptenModule){
97105
return {
98106
lua_copy: function (_L: LuaState, _fromIndex: number, _toIndex: number) {
99107
throw "lua_copy not supported with Lua 5.2 and lower";
@@ -158,7 +166,7 @@ const lauxBindings: Record<string, lauxBindingFactoryFunc> = {
158166
luaL_newstate: luaGlue.cwrap("lua_open", "number", []),
159167
}
160168
},
161-
"<=5.1.0": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
169+
"<=5.1.x": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
162170
return {
163171
luaL_loadbuffer: luaGlue.cwrap("luaL_loadbuffer", "number", ["number", "string", "number", "string"]),
164172
}

src/lua.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export type LuaState = number & LuaStateUnique;
77
export const LUA_MULTRET = -1;
88

99
// 5.0 & 5.1
10-
export const LUA_GLOBALSINDEX = -1002;
10+
export const LUA_GLOBALSINDEX_50 = -10001;
11+
export const LUA_GLOBALSINDEX_51 = -10002;
1112

1213
// 5.2^
1314
export const LUA_RIDX_GLOBALS = 2;

0 commit comments

Comments
 (0)