Skip to content

Commit

Permalink
Lua 5.2 support (WIP) (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl authored Aug 9, 2023
1 parent e9d23d6 commit 366ca67
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,10 +15,16 @@ 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"
copyFiles "$PACKAGE_DIR/deps/macamd/liblua.5.1.dylib" platform="osx-aarch64"
copyFiles "$PACKAGE_DIR/deps/macx64/liblua.5.1.dylib" platform="osx-x86_64"
versions "LUA_51"
}

configuration "lua52-dynamic" {
targetType "library"
versions "LUA_52"
}
32 changes: 25 additions & 7 deletions source/lumars/state.d
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion source/lumars/table.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 366ca67

Please sign in to comment.