Skip to content

Commit

Permalink
push nil into lua when possible (#41)
Browse files Browse the repository at this point in the history
* push nil into lua when possible

* Use is operator instead of assignment.
  • Loading branch information
Domain authored Dec 27, 2023
1 parent de05a9b commit 9b8f9a1
Showing 1 changed file with 42 additions and 0 deletions.
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

0 comments on commit 9b8f9a1

Please sign in to comment.