Skip to content

Commit

Permalink
getAsStruct: Filter members to fields only (#33)
Browse files Browse the repository at this point in the history
* getAsStruct: Filter members to fields only

* Add a unittest for #32
  • Loading branch information
ArthaTi authored Jun 23, 2023
1 parent c66d14f commit e9d23d6
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions source/lumars/state.d
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct LuaState
{
return this._G;
}

@nogc
lua_CFunction atPanic(lua_CFunction func) nothrow
{
Expand Down Expand Up @@ -167,7 +167,7 @@ struct LuaState
{
return lua_lessthan(this.handle, index1, index2) != 0;
}

@nogc
bool rawEqual(int index1, int index2) nothrow
{
Expand Down Expand Up @@ -376,7 +376,7 @@ struct LuaState
static LuaValue.Kind luaValueKindFor(T)() pure nothrow
{
import std.typecons : Nullable;
import std.traits : isNumeric, isDynamicArray, isAssociativeArray,
import std.traits : isNumeric, isDynamicArray, isAssociativeArray,
isPointer, KeyType, ValueType,
isInstanceOf, TemplateArgsOf;

Expand Down Expand Up @@ -644,7 +644,7 @@ struct LuaState
{
import std.conv : to;
import std.format : format;
import std.traits : isNumeric, isDynamicArray, isAssociativeArray, isPointer, KeyType, ValueType, TemplateOf, TemplateArgsOf;
import std.traits : isNumeric, isDynamicArray, isAssociativeArray, isPointer, KeyType, ValueType, TemplateOf, TemplateArgsOf, FieldNameTuple;
import std.typecons : isTuple;

static if(is(T == string))
Expand Down Expand Up @@ -825,7 +825,7 @@ struct LuaState
}
else static if(is(T == struct))
{
return getAsStruct!(T, __traits(allMembers, T))(index);
return getAsStruct!(T, FieldNameTuple!T)(index);
}
else static assert(false, "Don't know how to convert any LUA values into type: "~T.stringof);
}
Expand Down Expand Up @@ -853,7 +853,42 @@ struct LuaState

this.pop(1);
}
return ret;
return ret;
}

unittest
{
static struct MyStruct
{
int number;
string text;

int getNumber() const
{
return number;
}

string getText() const
{
return text;
}
}

auto lua = LuaState(null);
auto before = MyStruct(1, "foo");

lua.push(before);

auto table = lua.get!LuaTable(-1);
assert(table.get!int("number") == 1);
assert(table.get!(const(char)[])("text") == "foo");
assert(table.get!string("text") == "foo");

auto after = lua.get!MyStruct(-1);

assert(before == after);

lua.pop(1);
}

@nogc
Expand Down Expand Up @@ -890,7 +925,7 @@ struct LuaState
case LUA_TFUNCTION: return LuaValue.Kind.func;
case LUA_TLIGHTUSERDATA: return LuaValue.Kind.userData;

default:
default:
return LuaValue.Kind.nil;
}
}
Expand Down Expand Up @@ -957,9 +992,9 @@ private void loadLuaIfNeeded()
{
const ret = loadLua();
if(ret != luaSupport) {
if(ret == LuaSupport.noLibrary)
if(ret == LuaSupport.noLibrary)
throw new LuaException("Lua library not found.");
else if(ret == LuaSupport.badLibrary)
else if(ret == LuaSupport.badLibrary)
throw new LuaException("Lua library is corrupt or for a different platform.");
else
throw new LuaException("Lua library is the wrong version, or some unknown error occured.");
Expand Down Expand Up @@ -1082,7 +1117,7 @@ unittest
{
string a;
}

static struct A
{
string a;
Expand Down

0 comments on commit e9d23d6

Please sign in to comment.