Skip to content

Commit 588d875

Browse files
authored
Merge pull request #3 from YoRyan/lua5.0-fixes
Fixes for Lua 5.0 and 5.1
2 parents d5731b3 + 0292879 commit 588d875

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/binding-factory.ts

Lines changed: 29 additions & 17 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,9 +23,17 @@ 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);
28-
return (this as Lua).lua_gettable(L, index);
32+
33+
// Relative offsets must move if the stack pointer moves
34+
const isRelativeOffset = index < 0 && index !== LUA_GLOBALSINDEX_50;
35+
36+
return (this as Lua).lua_gettable(L, isRelativeOffset ? index - 1 : index);
2937
},
3038
lua_setfield: function(L: LuaState, index: number, k: string) {
3139
// The value to set is expected to be on the top of the stack
@@ -34,29 +42,31 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
3442
(this as Lua).lua_pushstring(L, k);
3543

3644
// Swap key and value because settable expects stack in that order
37-
38-
// Copy value to top of stack
39-
(this as Lua).lua_pushvalue(L, -2);
45+
(this as Lua).lua_insert(L, -2);
4046

41-
// Remove original value from stack
42-
(this as Lua).lua_remove(L, -3);
43-
44-
const result = (this as Lua).lua_settable(L, index);
47+
// Relative offsets must move if the stack pointer moves
48+
const isRelativeOffset = index < 0 && index !== LUA_GLOBALSINDEX_50;
49+
50+
const result = (this as Lua).lua_settable(L, isRelativeOffset ? index - 1 : index);
4551

4652
return result;
4753
},
4854
lua_tolstring: function(_L: LuaState, _index: number, _size: number) {
4955
throw "lua_tolstring is currently not supported in 5.0";
5056
},
51-
lua_tostring: luaGlue.cwrap("lua_tostring", "number", ["number", "number"])
57+
lua_tostring: luaGlue.cwrap("lua_tostring", "string", ["number", "number"])
5258
};
5359
},
54-
"<=5.1.0": function(luaGlue: LuaEmscriptenModule){
60+
"5.1.x": function(_luaGlue: LuaEmscriptenModule){
5561
return {
5662
// #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, s)
5763
lua_getglobal: function (L: LuaState, name: string) {
58-
return (this as Lua).lua_getfield(L, LUA_GLOBALSINDEX, name);
59-
},
64+
return (this as Lua).lua_getfield(L, LUA_GLOBALSINDEX_51, name);
65+
}
66+
};
67+
},
68+
"<=5.1.x": function(luaGlue: LuaEmscriptenModule){
69+
return {
6070
// Need to overwrite because in lua 5.1 this is a function and not a #define (5.2 and higher)
6171
lua_pcall: luaGlue.cwrap("lua_pcall", "number", ["number", "number", "number", "number"]),
6272
// TODO there might be some way to mimic pcallk behaviour with 5.1 somehow
@@ -93,7 +103,7 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
93103
])
94104
};
95105
},
96-
"<=5.2.0": function(luaGlue: LuaEmscriptenModule){
106+
"<=5.2.x": function(luaGlue: LuaEmscriptenModule){
97107
return {
98108
lua_copy: function (_L: LuaState, _fromIndex: number, _toIndex: number) {
99109
throw "lua_copy not supported with Lua 5.2 and lower";
@@ -149,16 +159,18 @@ export function createLua(luaGlue: LuaEmscriptenModule, version: string): Lua {
149159

150160
type lauxBindingFactoryFunc = (luaGlue: LuaEmscriptenModule, lua: Lua) => Partial<LauxLib>;
151161
const lauxBindings: Record<string, lauxBindingFactoryFunc> = {
152-
"5.0.x": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
162+
"5.0.x": function(luaGlue: LuaEmscriptenModule, lua: Lua) {
153163
return {
154-
luaL_dostring: luaGlue.cwrap("luaL_dostring", "number", ["number", "string"]),
164+
luaL_dostring: function(L: LuaState, s: string) {
165+
return (this as LauxLib).luaL_loadstring(L, s) || lua.lua_pcall(L, 0, LUA_MULTRET, 0);
166+
},
155167
luaL_loadstring: function(L: LuaState, s: string) {
156168
return (this as LauxLib).luaL_loadbuffer(L, s, s.length, s);
157169
},
158170
luaL_newstate: luaGlue.cwrap("lua_open", "number", []),
159171
}
160172
},
161-
"<=5.1.0": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
173+
"<=5.1.x": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
162174
return {
163175
luaL_loadbuffer: luaGlue.cwrap("luaL_loadbuffer", "number", ["number", "string", "number", "string"]),
164176
}

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)