Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 5 additions & 12 deletions src/MoonSharp.Interpreter/CoreLib/TableIteratorsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static DynValue ipairs(ScriptExecutionContext executionContext, CallbackA

DynValue meta = executionContext.GetMetamethodTailCall(table, "__ipairs", args.GetArray());

return meta.IsNotNil() ? meta : DynValue.NewTuple(DynValue.NewCallback(__next_i), table, DynValue.NewNumber(0));
return meta.IsNotNil() ? meta : DynValue.NewTuple(DynValue.NewCallback(__next_i), table, DynValue.NewNumber(executionContext.OwnerScript.Options.IndexTablesFrom == 0 ? -1 : 0));
}

// pairs (t)
Expand Down Expand Up @@ -64,8 +64,8 @@ public static DynValue next(ScriptExecutionContext executionContext, CallbackArg

if (pair.HasValue)
return DynValue.NewTuple(pair.Value.Key, pair.Value.Value);
else
throw new ScriptRuntimeException("invalid key to 'next'");

throw new ScriptRuntimeException("invalid key to 'next'");
}

// __next_i (table [, index])
Expand All @@ -76,17 +76,10 @@ public static DynValue __next_i(ScriptExecutionContext executionContext, Callbac
DynValue table = args.AsType(0, "!!next_i!!", DataType.Table);
DynValue index = args.AsType(1, "!!next_i!!", DataType.Number);

int idx = ((int)index.Number) + 1;
int idx = (int) index.Number + 1;
DynValue val = table.Table.Get(idx);

if (val.Type != DataType.Nil)
{
return DynValue.NewTuple(DynValue.NewNumber(idx), val);
}
else
{
return DynValue.Nil;
}
return val.Type != DataType.Nil ? DynValue.NewTuple(DynValue.NewNumber(idx), val) : DynValue.Nil;
}
}
}
89 changes: 37 additions & 52 deletions src/MoonSharp.Interpreter/CoreLib/TableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static DynValue pack(ScriptExecutionContext executionContext, CallbackArg
DynValue v = DynValue.NewTable(t);

for (int i = 0; i < args.Count; i++)
t.Set(i + 1, args[i]);
t.Set(i + executionContext.OwnerScript.Options.IndexTablesFrom, args[i]);

t.Set("n", DynValue.NewNumber(args.Count));

Expand All @@ -61,7 +61,7 @@ public static DynValue sort(ScriptExecutionContext executionContext, CallbackArg

List<DynValue> values = new List<DynValue>();

for (int i = 1; i <= end; i++)
for (int i = executionContext.OwnerScript.Options.IndexTablesFrom; i < end + executionContext.OwnerScript.Options.IndexTablesFrom; i++)
values.Add(vlist.Table.Get(i));

try
Expand All @@ -76,39 +76,34 @@ public static DynValue sort(ScriptExecutionContext executionContext, CallbackArg

for (int i = 0; i < values.Count; i++)
{
vlist.Table.Set(i + 1, values[i]);
vlist.Table.Set(i + executionContext.OwnerScript.Options.IndexTablesFrom, values[i]);
}

return vlist;
}

private static int SortComparer(ScriptExecutionContext executionContext, DynValue a, DynValue b, DynValue lt)
{
if (lt.IsNil())
{
lt = executionContext.GetBinaryMetamethod(a, b, "__lt");

if (lt.IsNil())
{
if (a.Type == DataType.Number && b.Type == DataType.Number)
return a.Number.CompareTo(b.Number);
if (a.Type == DataType.String && b.Type == DataType.String)
return a.String.CompareTo(b.String);
if (!lt.IsNil())
return LuaComparerToClrComparer(
executionContext.GetScript().Call(lt, a, b),
executionContext.GetScript().Call(lt, b, a));

lt = executionContext.GetBinaryMetamethod(a, b, "__lt");

throw ScriptRuntimeException.CompareInvalidType(a, b);
}
else
{
return LuaComparerToClrComparer(
executionContext.GetScript().Call(lt, a, b),
executionContext.GetScript().Call(lt, b, a));
}
}
else
{
if (!lt.IsNil())
return LuaComparerToClrComparer(
executionContext.GetScript().Call(lt, a, b),
executionContext.GetScript().Call(lt, b, a));

switch (a.Type)
{
case DataType.Number when b.Type == DataType.Number:
return a.Number.CompareTo(b.Number);
case DataType.String when b.Type == DataType.String:
return a.String.CompareTo(b.String);
default:
throw ScriptRuntimeException.CompareInvalidType(a, b);
}
}

Expand Down Expand Up @@ -144,20 +139,20 @@ public static DynValue insert(ScriptExecutionContext executionContext, CallbackA
if (vvalue.IsNil())
{
vvalue = vpos;
vpos = DynValue.NewNumber(len + 1);
vpos = DynValue.NewNumber(len + executionContext.OwnerScript.Options.IndexTablesFrom);
}

if (vpos.Type != DataType.Number)
throw ScriptRuntimeException.BadArgument(1, "table.insert", DataType.Number, vpos.Type, false);

int pos = (int)vpos.Number;
int pos = vpos.Int;

if (pos > len + 1 || pos < 1)
if (pos > len + 1 || pos < executionContext.OwnerScript.Options.IndexTablesFrom)
throw new ScriptRuntimeException("bad argument #2 to 'insert' (position out of bounds)");

for (int i = len; i >= pos; i--)
{
list.Set(i + 1, list.Get(i));
list.Set(i + executionContext.OwnerScript.Options.IndexTablesFrom, list.Get(i));
}

list.Set(pos, vvalue);
Expand All @@ -181,7 +176,7 @@ public static DynValue remove(ScriptExecutionContext executionContext, CallbackA

int pos = vpos.IsNil() ? len : (int)vpos.Number;

if (pos >= len + 1 || (pos < 1 && len > 0))
if (pos >= len + 1 || (pos < executionContext.OwnerScript.Options.IndexTablesFrom && len > 0))
throw new ScriptRuntimeException("bad argument #1 to 'remove' (position out of bounds)");

for (int i = pos; i <= len; i++)
Expand Down Expand Up @@ -210,24 +205,15 @@ public static DynValue concat(ScriptExecutionContext executionContext, CallbackA

Table list = vlist.Table;
string sep = vsep.IsNil() ? "" : vsep.String;
int start = vstart.IsNilOrNan() ? 1 : (int)vstart.Number;
int end;

if (vend.IsNilOrNan())
{
end = GetTableLength(executionContext, vlist);
}
else
{
end = (int)vend.Number;
}
int start = vstart.IsNilOrNan() ? executionContext.OwnerScript.Options.IndexTablesFrom : vstart.Int;
int end = vend.IsNilOrNan() ? GetTableLength(executionContext, vlist) : vend.Int;

if (end < start)
return DynValue.NewString(string.Empty);

StringBuilder sb = new StringBuilder();

for (int i = start; i <= end; i++)
for (int i = start; i < end + executionContext.OwnerScript.Options.IndexTablesFrom; i++)
{
DynValue v = list.Get(i);

Expand All @@ -250,21 +236,20 @@ private static int GetTableLength(ScriptExecutionContext executionContext, DynVa
{
DynValue __len = executionContext.GetMetamethod(vlist, "__len");

if (__len.IsNotNil())
if (!__len.IsNotNil())
{
DynValue lenv = executionContext.GetScript().Call(__len, vlist);
return vlist.Table.Length;
}

DynValue lenv = executionContext.GetScript().Call(__len, vlist);

double? len = lenv.CastToNumber();
double? len = lenv.CastToNumber();

if (len == null)
throw new ScriptRuntimeException("object length is not a number");
if (len == null)
throw new ScriptRuntimeException("object length is not a number");

return (int)len;

return (int)len;
}
else
{
return (int)vlist.Table.Length;
}
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/MoonSharp.Interpreter/DataStructs/Extension_Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ internal static class Extension_Methods
/// <returns></returns>
public static TValue GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
{
TValue v;

if (dictionary.TryGetValue(key, out v))
return v;

return default(TValue);
return dictionary.TryGetValue(key, out TValue v) ? v : default;
}


/// <summary>
/// Gets a value from the dictionary or creates it
/// </summary>
Expand All @@ -38,13 +32,13 @@ public static TValue GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> di
/// <returns></returns>
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> creator)
{
TValue v;

if (!dictionary.TryGetValue(key, out v))
if (dictionary.TryGetValue(key, out TValue v))
{
v = creator();
dictionary.Add(key, v);
return v;
}

v = creator();
dictionary.Add(key, v);

return v;
}
Expand Down
39 changes: 9 additions & 30 deletions src/MoonSharp.Interpreter/DataStructs/FastStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ internal class FastStack<T> : IList<T>
int m_HeadIdx = 0;
private int maxCapacity;

public int Count => m_HeadIdx;
public int MaxCapacity => maxCapacity;

public ref T this[int index] => ref m_Storage[index];

public FastStack(int initialCapacity, int maxCapacity)
{
m_Storage = new T[initialCapacity];
this.maxCapacity = maxCapacity;
}

public ref T this[int index] => ref m_Storage[index];



void Grow(int newSize)
{
if (newSize < m_Storage.Length) return;
Expand Down Expand Up @@ -64,7 +62,7 @@ private void Zero(int from, int to)

private void Zero(int index)
{
m_Storage[index] = default(T);
m_Storage[index] = default;
}

public ref T Peek(int idxofs = 0)
Expand All @@ -88,7 +86,7 @@ public void RemoveLast( int cnt = 1)
if (cnt == 1)
{
--m_HeadIdx;
m_Storage[m_HeadIdx] = default(T);
m_Storage[m_HeadIdx] = default;
}
else
{
Expand Down Expand Up @@ -117,13 +115,6 @@ public void ClearSection(int index, int length)
Array.Clear(m_Storage, index, length);
}


public int Count
{
get { return m_HeadIdx; }
}


#region IList<T> Impl.

int IList<T>.IndexOf(T item)
Expand All @@ -143,14 +134,8 @@ void IList<T>.RemoveAt(int index)

T IList<T>.this[int index]
{
get
{
return this[index];
}
set
{
this[index] = value;
}
get => this[index];
set => this[index] = value;
}

void ICollection<T>.Add(T item)
Expand All @@ -173,15 +158,9 @@ void ICollection<T>.CopyTo(T[] array, int arrayIndex)
throw new NotImplementedException();
}

int ICollection<T>.Count
{
get { return this.Count; }
}
int ICollection<T>.Count => this.Count;

bool ICollection<T>.IsReadOnly
{
get { return false; }
}
bool ICollection<T>.IsReadOnly => false;

bool ICollection<T>.Remove(T item)
{
Expand Down
Loading