Skip to content

Commit dc62a5e

Browse files
committed
Add LuaTable.SetNil()
1 parent 4508f2e commit dc62a5e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Laylua.Tests/Tests/Library/Entities/Table/LuaTableTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ public void GetValueOrDefault_MissingNullableInt_DefaultValue_ReturnsDefaultValu
144144
Assert.That(missingValue, Is.EqualTo(42));
145145
}
146146

147+
[Test]
148+
public void SetNil_ExistingValue_OverwritesItToNil()
149+
{
150+
// Arrange
151+
using var table = Lua.CreateTable();
152+
153+
// Act
154+
table.SetValue("Value", 1);
155+
table.SetNil("Value");
156+
var value = table.GetValueOrDefault<string, int?>("Value");
157+
158+
// Assert
159+
Assert.That(value, Is.Null);
160+
}
161+
147162
[Test]
148163
public void Count_ReturnsValidCount()
149164
{

src/Laylua/Library/Entities/Reference/Table/LuaTable.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,29 @@ public void SetValue<TKey, TValue>(TKey key, TValue? value)
393393
}
394394
}
395395

396+
/// <summary>
397+
/// Sets a nil value with the given key in the table.
398+
/// </summary>
399+
/// <param name="key"> The key of the value to set. </param>
400+
/// <typeparam name="TKey"> The type of the key. </typeparam>
401+
[MethodImpl(MethodImplOptions.NoInlining)]
402+
public void SetNil<TKey>(TKey key)
403+
where TKey : notnull
404+
{
405+
ThrowIfInvalid();
406+
407+
Lua.Stack.EnsureFreeCapacity(3);
408+
409+
var L = Lua.GetStatePointer();
410+
using (Lua.Stack.SnapshotCount())
411+
{
412+
PushValue(this);
413+
Lua.Stack.Push(key);
414+
Lua.Stack.PushNil();
415+
lua_settable(L, -3);
416+
}
417+
}
418+
396419
/// <summary>
397420
/// Clears this table by setting all values to nil.
398421
/// </summary>

0 commit comments

Comments
 (0)