Skip to content

Commit b677f8d

Browse files
committed
Move LuaTable.Length to SequenceCollection and adjust method parameters
1 parent de46f53 commit b677f8d

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

src/Laylua.Tests/Tests/Library/Entities/Table/LuaTable.Sequence.Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void Insert_EmptyTable_InsertsAtTheEnd()
9595
var table = Lua.CreateTable();
9696

9797
// Act
98-
table.Sequence.Insert(42, 1);
98+
table.Sequence.Insert(1, 42);
9999

100100
// Assert
101101
Assert.That(table.Sequence.ToArray<int>(), Is.EqualTo(new[] { 42 }));
@@ -111,7 +111,7 @@ public void Insert_ThreeElementSequence_InsertsAtTheEnd()
111111
table.SetValue(3, 3);
112112

113113
// Act
114-
table.Sequence.Insert(42, 4);
114+
table.Sequence.Insert(4, 42);
115115

116116
// Assert
117117
Assert.That(table.Sequence.ToArray<int>(), Is.EqualTo(new[] { 1, 2, 3, 42 }));
@@ -127,7 +127,7 @@ public void Insert_IndexOneOfThreeElementSequence_InsertsAtIndexOne()
127127
table.SetValue(3, 3);
128128

129129
// Act
130-
table.Sequence.Insert(42, 1);
130+
table.Sequence.Insert(1, 42);
131131

132132
// Assert
133133
Assert.That(table.Sequence.ToArray<int>(), Is.EqualTo(new[] { 42, 1, 2, 3 }));
@@ -143,7 +143,7 @@ public void Insert_IndexTwoOfThreeElementSequence_InsertsAtIndexTwo()
143143
table.SetValue(3, 3);
144144

145145
// Act
146-
table.Sequence.Insert(42, 2);
146+
table.Sequence.Insert(2, 42);
147147

148148
// Assert
149149
Assert.That(table.Sequence.ToArray<int>(), Is.EqualTo(new[] { 1, 42, 2, 3 }));

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5+
using System.Runtime.CompilerServices;
56
using Laylua.Moon;
67
using Qommon;
78

@@ -19,9 +20,34 @@ public unsafe partial class LuaTable
1920
public readonly struct SequenceCollection
2021
{
2122
/// <summary>
22-
/// Gets the length of the sequence part.
23+
/// Gets the length of the sequence.
24+
/// This returns the same value as the length operator (<c>#table</c>).
2325
/// </summary>
24-
public lua_Integer Length => _table.Length;
26+
/// <remarks>
27+
/// <b>To get the number of key/value pairs in the table,
28+
/// use <see cref="Count"/> instead, as it works for all keys.</b>
29+
/// <para/>
30+
/// <inheritdoc cref="Add{T}"/>
31+
/// <br/>
32+
/// See <a href="https://www.lua.org/manual/5.4/manual.html#3.4.7">Lua manual</a>.
33+
/// </remarks>
34+
public lua_Integer Length
35+
{
36+
[MethodImpl(MethodImplOptions.NoInlining)]
37+
get
38+
{
39+
var lua = _table.Lua;
40+
var L = lua.State.L;
41+
42+
lua.Stack.EnsureFreeCapacity(1);
43+
44+
using (lua.Stack.SnapshotCount())
45+
{
46+
lua.Stack.Push(_table);
47+
return luaL_len(L, -1);
48+
}
49+
}
50+
}
2551

2652
private readonly LuaTable _table;
2753

@@ -34,7 +60,9 @@ internal SequenceCollection(LuaTable table)
3460
/// Adds the value at the end of the sequence.
3561
/// </summary>
3662
/// <remarks>
37-
/// <inheritdoc cref="Add{T}"/>
63+
/// If the table is not a sequence,
64+
/// i.e. if the keys of the table are not consecutive integers,
65+
/// this might not return the expected result.
3866
/// </remarks>
3967
/// <param name="value"> The value to add. </param>
4068
public void Add<T>(T value)
@@ -60,9 +88,9 @@ public void Add<T>(T value)
6088
/// <remarks>
6189
/// <inheritdoc cref="Add{T}"/>
6290
/// </remarks>
63-
/// <param name="value"> The value to insert. </param>
6491
/// <param name="index"> The one-based index in the sequence. </param>
65-
public void Insert<T>(T value, lua_Integer index)
92+
/// <param name="value"> The value to insert. </param>
93+
public void Insert<T>(lua_Integer index, T value)
6694
{
6795
var length = Length;
6896
if (index < 1 || index > length + 1)
@@ -99,7 +127,7 @@ public void Insert<T>(T value, lua_Integer index)
99127
/// <inheritdoc cref="Add{T}"/>
100128
/// </remarks>
101129
/// <param name="index"> The one-based index in the sequence. </param>
102-
public void RemoveAt(int index)
130+
public void RemoveAt(lua_Integer index)
103131
{
104132
var length = Length;
105133
if (index < 1 || index > length)

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,6 @@ public bool IsEmpty
5656
}
5757
}
5858

59-
/// <summary>
60-
/// Gets the length of this table.
61-
/// This returns the same value as the length operator (<c>#table</c>).
62-
/// </summary>
63-
/// <remarks>
64-
/// <b>If you want to get the amount of key/value pairs in the table,
65-
/// use <see cref="Count"/> instead, as it works for all keys.</b>
66-
/// <para/>
67-
/// If the keys of the table are not consecutive integers
68-
/// or if there are holes in the sequence,
69-
/// this may not return the expected result.
70-
/// <br/>
71-
/// See <a href="https://www.lua.org/manual/5.4/manual.html#3.4.7">Lua manual</a>.
72-
/// </remarks>
73-
public lua_Integer Length
74-
{
75-
[MethodImpl(MethodImplOptions.NoInlining)]
76-
get
77-
{
78-
ThrowIfInvalid();
79-
80-
Lua.Stack.EnsureFreeCapacity(2);
81-
82-
var L = Lua.State.L;
83-
using (Lua.Stack.SnapshotCount())
84-
{
85-
Lua.Stack.Push(this);
86-
return luaL_len(L, -1);
87-
}
88-
}
89-
}
90-
9159
/// <summary>
9260
/// Gets or sets a value with the given key in the table.
9361
/// </summary>

0 commit comments

Comments
 (0)