Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push nil into lua when possible #41

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions source/lumars/state.d
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,15 @@ struct LuaState
import std.conv : to;
import std.traits : isNumeric, isDynamicArray, isAssociativeArray, isDelegate, isPointer, isFunction,
PointerTarget, KeyType, ValueType, FieldNameTuple, TemplateOf, TemplateArgsOf;

static if (__traits(compiles, value is null))
{
if (value is null)
{
lua_pushnil(this.handle);
return;
}
}

static if(is(T == typeof(null)) || is(T == LuaNil))
lua_pushnil(this.handle);
Expand Down Expand Up @@ -1040,6 +1049,39 @@ struct LuaState
lua.pop(1);
}

unittest
{
string str = null;
auto lua = LuaState(null);

lua.globalTable["a"] = str;
lua.doString("assert(a == nil)");

lua.globalTable["b"] = "";
lua.doString("assert(b == '')");

class C {}
C c = null;
lua.globalTable["c"] = c;
lua.doString("assert(c == nil)");

string[int] d;
lua.globalTable["d"] = d;
lua.doString("assert(d == nil)");

int[] e;
lua.globalTable["e"] = e;
lua.doString("assert(e == nil)");

int function() f;
lua.globalTable["f"] = f;
lua.doString("assert(f == nil)");

int delegate() g;
lua.globalTable["g"] = g;
lua.doString("assert(g == nil)");
}

@nogc
bool next(int index) nothrow
{
Expand Down
Loading