From 366ca671b2481695bd1d56b80be0cf124b149c04 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 9 Aug 2023 19:50:53 +0300 Subject: [PATCH] Lua 5.2 support (WIP) (#34) --- dub.sdl | 7 +++++++ source/lumars/state.d | 32 +++++++++++++++++++++++++------- source/lumars/table.d | 6 +++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/dub.sdl b/dub.sdl index 28b3b70..5bac5f9 100644 --- a/dub.sdl +++ b/dub.sdl @@ -5,6 +5,7 @@ copyright "Copyright © 2023, Bradley Chatha" license "MIT" dependency "bindbc-lua" version="~>0.5.0" dependency "taggedalgebraic" version="~>0.11.22" + configuration "lua51" { targetType "library" dflags "-L$PACKAGE_DIR/deps/linux64/lua51.a" platform="linux-x86_64" @@ -14,6 +15,7 @@ configuration "lua51" { versions "BindLua_Static" "LUA_51" subConfiguration "bindbc-lua" "static" } + configuration "lua51-dynamic" { targetType "library" copyFiles "$PACKAGE_DIR/deps/win64/lua51.dll" platform="windows-x86_64" @@ -21,3 +23,8 @@ configuration "lua51-dynamic" { copyFiles "$PACKAGE_DIR/deps/macx64/liblua.5.1.dylib" platform="osx-x86_64" versions "LUA_51" } + +configuration "lua52-dynamic" { + targetType "library" + versions "LUA_52" +} diff --git a/source/lumars/state.d b/source/lumars/state.d index 613b6e7..7dfc9b0 100644 --- a/source/lumars/state.d +++ b/source/lumars/state.d @@ -4,6 +4,10 @@ import bindbc.lua, taggedalgebraic, lumars; import taggedalgebraic : visit; import std.typecons : Nullable; +version(LUA_52) { + enum LUA_GLOBALSINDEX = -10_002; +} + /// Used to represent LUA's `nil`. struct LuaNil {} @@ -282,7 +286,13 @@ struct LuaState static foreach(i; 0..Args.length/2) reg[i] = luaL_Reg(Args[i*2].ptr, &luaCWrapperSmart!(Args[i*2+1])); - luaL_register(this.handle, libname.toStringz, reg.ptr); + version(LUA_52) { + lua_newtable(this.handle); + luaL_setfuncs(this.handle, reg.ptr, 0); + lua_setglobal(this.handle, libname.toStringz); + } else { + luaL_register(this.handle, libname.toStringz, reg.ptr); + } } @nogc @@ -435,9 +445,13 @@ struct LuaState } table.push(); - const fenvResult = lua_setfenv(this.handle, -2); - if(fenvResult == 0) - throw new LuaException("Failed to set function environment"); + version(LUA_52) { + lua_setupvalue(this.handle, -2, 1); + } else { + const fenvResult = lua_setfenv(this.handle, -2); + if(fenvResult == 0) + throw new LuaException("Failed to set function environment"); + } const callStatus = lua_pcall(this.handle, 0, 0, 0); if(callStatus != LuaStatus.ok) @@ -470,9 +484,13 @@ struct LuaState } table.push(); - const fenvResult = lua_setfenv(this.handle, -2); - if(fenvResult == 0) - throw new LuaException("Failed to set function environment"); + version(LUA_52) { + lua_setupvalue(this.handle, -2, 1); + } else { + const fenvResult = lua_setfenv(this.handle, -2); + if(fenvResult == 0) + throw new LuaException("Failed to set function environment"); + } const callStatus = lua_pcall(this.handle, 0, 0, 0); if(callStatus != LuaStatus.ok) diff --git a/source/lumars/table.d b/source/lumars/table.d index 1dbe734..71b7c38 100644 --- a/source/lumars/table.d +++ b/source/lumars/table.d @@ -264,7 +264,11 @@ struct LuaTablePseudo void pushElement(IndexT)(IndexT index) if(isNumeric!IndexT || is(IndexT == string)) { - this.lua.push(index); + version(LUA_52) { + if (index == LUA_GLOBALSINDEX) { + lua_pushglobaltable(this.lua.handle); + } else this.lua.push(index); + } else this.lua.push(index); lua_gettable(this.lua.handle, this._index); }