From 4d0e977d6e7093765b7687bc8b93a45699c93cef Mon Sep 17 00:00:00 2001 From: Domain Date: Sat, 23 Dec 2023 23:45:37 +0800 Subject: [PATCH 1/2] push nil into lua when possible --- source/lumars/state.d | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/lumars/state.d b/source/lumars/state.d index 02b9a36..77d930a 100644 --- a/source/lumars/state.d +++ b/source/lumars/state.d @@ -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 = null)) + { + if (value is null) + { + lua_pushnil(this.handle); + return; + } + } static if(is(T == typeof(null)) || is(T == LuaNil)) lua_pushnil(this.handle); @@ -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 { From 406f01b06ff0ea2fb4a31491d36a630bd877d8b0 Mon Sep 17 00:00:00 2001 From: Domain Date: Mon, 25 Dec 2023 15:37:06 +0800 Subject: [PATCH 2/2] Use is operator instead of assignment. --- source/lumars/state.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/lumars/state.d b/source/lumars/state.d index 77d930a..bd96519 100644 --- a/source/lumars/state.d +++ b/source/lumars/state.d @@ -689,7 +689,7 @@ struct LuaState import std.traits : isNumeric, isDynamicArray, isAssociativeArray, isDelegate, isPointer, isFunction, PointerTarget, KeyType, ValueType, FieldNameTuple, TemplateOf, TemplateArgsOf; - static if (__traits(compiles, value = null)) + static if (__traits(compiles, value is null)) { if (value is null) {