1
1
import { satisfies } from "semver" ;
2
2
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" ;
4
4
5
5
type luaBindingFactoryFunc = ( luaGlue : LuaEmscriptenModule ) => Partial < Lua > ;
6
6
const luaBindings : Record < string , luaBindingFactoryFunc > = {
@@ -23,9 +23,17 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
23
23
} ,
24
24
"5.0.x" : function ( luaGlue : LuaEmscriptenModule ) {
25
25
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
+ } ,
26
30
lua_getfield : function ( L : LuaState , index : number , k : string ) {
27
31
( 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 ) ;
29
37
} ,
30
38
lua_setfield : function ( L : LuaState , index : number , k : string ) {
31
39
// The value to set is expected to be on the top of the stack
@@ -34,29 +42,31 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
34
42
( this as Lua ) . lua_pushstring ( L , k ) ;
35
43
36
44
// 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 ) ;
40
46
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 ) ;
45
51
46
52
return result ;
47
53
} ,
48
54
lua_tolstring : function ( _L : LuaState , _index : number , _size : number ) {
49
55
throw "lua_tolstring is currently not supported in 5.0" ;
50
56
} ,
51
- lua_tostring : luaGlue . cwrap ( "lua_tostring" , "number " , [ "number" , "number" ] )
57
+ lua_tostring : luaGlue . cwrap ( "lua_tostring" , "string " , [ "number" , "number" ] )
52
58
} ;
53
59
} ,
54
- "<= 5.1.0 " : function ( luaGlue : LuaEmscriptenModule ) {
60
+ "5.1.x " : function ( _luaGlue : LuaEmscriptenModule ) {
55
61
return {
56
62
// #define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, s)
57
63
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 {
60
70
// Need to overwrite because in lua 5.1 this is a function and not a #define (5.2 and higher)
61
71
lua_pcall : luaGlue . cwrap ( "lua_pcall" , "number" , [ "number" , "number" , "number" , "number" ] ) ,
62
72
// TODO there might be some way to mimic pcallk behaviour with 5.1 somehow
@@ -93,7 +103,7 @@ const luaBindings: Record<string, luaBindingFactoryFunc> = {
93
103
] )
94
104
} ;
95
105
} ,
96
- "<=5.2.0 " : function ( luaGlue : LuaEmscriptenModule ) {
106
+ "<=5.2.x " : function ( luaGlue : LuaEmscriptenModule ) {
97
107
return {
98
108
lua_copy : function ( _L : LuaState , _fromIndex : number , _toIndex : number ) {
99
109
throw "lua_copy not supported with Lua 5.2 and lower" ;
@@ -149,16 +159,18 @@ export function createLua(luaGlue: LuaEmscriptenModule, version: string): Lua {
149
159
150
160
type lauxBindingFactoryFunc = ( luaGlue : LuaEmscriptenModule , lua : Lua ) => Partial < LauxLib > ;
151
161
const lauxBindings : Record < string , lauxBindingFactoryFunc > = {
152
- "5.0.x" : function ( luaGlue : LuaEmscriptenModule , _lua : Lua ) {
162
+ "5.0.x" : function ( luaGlue : LuaEmscriptenModule , lua : Lua ) {
153
163
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
+ } ,
155
167
luaL_loadstring : function ( L : LuaState , s : string ) {
156
168
return ( this as LauxLib ) . luaL_loadbuffer ( L , s , s . length , s ) ;
157
169
} ,
158
170
luaL_newstate : luaGlue . cwrap ( "lua_open" , "number" , [ ] ) ,
159
171
}
160
172
} ,
161
- "<=5.1.0 " : function ( luaGlue : LuaEmscriptenModule , _lua : Lua ) {
173
+ "<=5.1.x " : function ( luaGlue : LuaEmscriptenModule , _lua : Lua ) {
162
174
return {
163
175
luaL_loadbuffer : luaGlue . cwrap ( "luaL_loadbuffer" , "number" , [ "number" , "string" , "number" , "string" ] ) ,
164
176
}
0 commit comments