Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing stable sort #809

Merged
merged 5 commits into from
Dec 10, 2020
Merged
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
40 changes: 40 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,45 @@ public void ArrayLengthFromInitialState()
var length = (int) array.Length;
Assert.Equal(0, length);
}

[Fact]
public void ArraySortIsStable()
{
const string code = @"
var items = [
{ name: 'Edward', value: 0 },
{ name: 'Sharpe', value: 0 },
{ name: 'And', value: 0 },
{ name: 'The', value: 1 },
{ name: 'Magnetic', value: 0 },
{ name: 'Zeros', value: 0 }
];

// sort by value
function compare(a, b) {
return a.value - b.value;
}

var a = items.sort();

assert(a[0].name == 'Edward');
assert(a[1].name == 'Sharpe');
assert(a[2].name == 'And');
assert(a[3].name == 'The');
assert(a[4].name == 'Magnetic');
assert(a[5].name == 'Zeros');

var a = items.sort(compare);

assert(a[0].name == 'Edward');
assert(a[1].name == 'Sharpe');
assert(a[2].name == 'And');
assert(a[3].name == 'Magnetic');
assert(a[4].name == 'Zeros');
assert(a[5].name == 'The');
";

_engine.Execute(code);
}
}
}
67 changes: 66 additions & 1 deletion Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using Jint.Native.Number;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;

namespace Jint.Native.Array
{
internal abstract class ArrayOperations
internal abstract class ArrayOperations : IEnumerable<JsValue>
{
protected internal const ulong MaxArrayLength = 4294967295;
protected internal const ulong MaxArrayLikeLength = NumberConstructor.MaxSafeInteger;
Expand Down Expand Up @@ -68,6 +70,69 @@ public virtual JsValue[] GetAll(Types elementTypes)

public abstract void DeletePropertyOrThrow(ulong index);

internal ArrayLikeIterator GetEnumerator() => new ArrayLikeIterator(this);

IEnumerator<JsValue> IEnumerable<JsValue>.GetEnumerator() => new ArrayLikeIterator(this);

IEnumerator IEnumerable.GetEnumerator() => new ArrayLikeIterator(this);

internal sealed class ArrayLikeIterator : IEnumerator<JsValue>
{
private readonly ArrayOperations _obj;
private ulong _current;
private bool _initialized;
private readonly uint _length;

public ArrayLikeIterator(ArrayOperations obj)
{
_obj = obj;
_length = obj.GetLength();

Reset();
}

public JsValue Current
{
get
{
if (!_initialized)
{
return null;
}
else
{
return _obj.TryGetValue(_current, out var temp) ? temp : null;
}
}
}

object IEnumerator.Current => Current;

public void Dispose()
{
}

public bool MoveNext()
{
if (!_initialized)
{
_initialized = true;
}
else
{
_current++;
}

return _current < _length;
}

public void Reset()
{
_initialized = false;
_current = 0;
}
}

private sealed class ObjectInstanceOperations : ArrayOperations<ObjectInstance>
{
public ObjectInstanceOperations(ObjectInstance target) : base(target)
Expand Down
171 changes: 101 additions & 70 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Jint.Collections;
using Jint.Native.Object;
using Jint.Native.Symbol;
Expand Down Expand Up @@ -801,91 +802,30 @@ private JsValue Sort(JsValue thisObj, JsValue[] arguments)
if (len <= 1)
{
return obj.Target;
}
}

int Comparer(JsValue x, JsValue y)
// don't eat inner exceptions
try
{
if (ReferenceEquals(x, null))
{
return 1;
}

if (ReferenceEquals(y, null))
{
return -1;
}
var array = obj.OrderBy(x => x, ArrayComparer.WithFunction(compareFn)).ToArray();

var xUndefined = x.IsUndefined();
var yUndefined = y.IsUndefined();
if (xUndefined && yUndefined)
for (uint i = 0; i < (uint) array.Length; ++i)
{
return 0;
}

if (xUndefined)
{
return 1;
}

if (yUndefined)
{
return -1;
}

if (compareFn != null)
{
var s = TypeConverter.ToNumber(compareFn.Call(Undefined, new[] {x, y}));
if (s < 0)
if (!ReferenceEquals(array[i], null))
{
return -1;
obj.Set(i, array[i], updateLength: false, throwOnError: false);
}

if (s > 0)
else
{
return 1;
obj.DeletePropertyOrThrow(i);
}

return 0;
}

var xString = TypeConverter.ToString(x);
var yString = TypeConverter.ToString(y);

var r = CompareOrdinal(xString, yString);
return r;
}

var array = new JsValue[len];
for (uint i = 0; i < (uint) array.Length; ++i)
{
var value = obj.TryGetValue(i, out var temp)
? temp
: null;
array[i] = value;
}

// don't eat inner exceptions
try
{
System.Array.Sort(array, Comparer);
}
catch (InvalidOperationException e)
{
throw e.InnerException;
}

for (uint i = 0; i < (uint) array.Length; ++i)
{
if (!ReferenceEquals(array[i], null))
{
obj.Set(i, array[i], updateLength: false, throwOnError: false);
}
else
{
obj.DeletePropertyOrThrow(i);
}
}

return obj.Target;
}

Expand Down Expand Up @@ -1290,5 +1230,96 @@ public JsValue Pop(JsValue thisObject, JsValue[] arguments)
o.SetLength(len);
return element;
}

private sealed class ArrayComparer : IComparer<JsValue>
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Default instance without any compare function.
/// </summary>
public static ArrayComparer Default = new ArrayComparer(null);
public static ArrayComparer WithFunction(ICallable compare)
{
if (compare == null)
{
return Default;
}

return new ArrayComparer(compare);
}

private readonly ICallable _compare;
private readonly JsValue[] _comparableArray = new JsValue[2];

private ArrayComparer(ICallable compare)
{
_compare = compare;
}

public int Compare(JsValue x, JsValue y)
{
var xIsNull = ReferenceEquals(x, null);
var yIsNull = ReferenceEquals(y, null);

if (xIsNull)
{
if (yIsNull)
{
return 0;
}

return 1;
}
else
{
if (yIsNull)
{
return -1;
}
}

var xUndefined = x.IsUndefined();
var yUndefined = y.IsUndefined();
if (xUndefined && yUndefined)
{
return 0;
}

if (xUndefined)
{
return 1;
}

if (yUndefined)
{
return -1;
}

if (_compare != null)
{
_comparableArray[0] = x;
_comparableArray[1] = y;

var s = TypeConverter.ToNumber(_compare.Call(Undefined, _comparableArray));
if (s < 0)
{
return -1;
}

if (s > 0)
{
return 1;
}

return 0;
}

var xString = TypeConverter.ToString(x);
var yString = TypeConverter.ToString(y);

var r = CompareOrdinal(xString, yString);
return r;
}
}
}

}