Skip to content

Commit

Permalink
Fix more issues related to built-in objects
Browse files Browse the repository at this point in the history
* fix more Object problems
* fix Array related problems
* fix proxy issues
* fix property descriptor handling
* remove now invalid tests
  • Loading branch information
lahma committed Jan 16, 2022
1 parent 2771118 commit ab3d63d
Show file tree
Hide file tree
Showing 47 changed files with 756 additions and 597 deletions.
4 changes: 2 additions & 2 deletions Jint.Tests.Ecma/TestCases/alltests.json
Original file line number Diff line number Diff line change
Expand Up @@ -41310,8 +41310,8 @@
source: "ch15/15.4/15.4.4/15.4.4.15/15.4.4.15-3-7.js"
},
{
skip: false,
reason: "",
skip: true,
reason: "not tested anymore, really slow",
source: "ch15/15.4/15.4.4/15.4.4.15/15.4.4.15-3-8.js"
},
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions Jint.Tests/Runtime/NullPropagation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Esprima;
using Esprima;
using Jint.Native;
using Jint.Runtime;
using Jint.Runtime.Interop;
Expand Down Expand Up @@ -30,7 +29,7 @@ public bool TryGetCallable(Engine engine, object callee, out JsValue value)
var name = reference.GetReferencedName().AsString();
if (name == "filter")
{
value = new ClrFunctionInstance(engine, "map", (thisObj, values) => engine.Realm.Intrinsics.Array.ConstructFast(0));
value = new ClrFunctionInstance(engine, "map", (thisObj, values) => engine.Realm.Intrinsics.Array.ArrayCreate(0));
return true;
}
}
Expand Down
63 changes: 29 additions & 34 deletions Jint/Native/Array/ArrayConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Jint.Collections;
using Jint.Native.Function;
using Jint.Native.Iterator;
Expand Down Expand Up @@ -62,17 +61,17 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)

if (source is JsString jsString)
{
var a = _realm.Intrinsics.Array.ConstructFast((uint) jsString.Length);
var a = _realm.Intrinsics.Array.ArrayCreate((uint) jsString.Length);
for (int i = 0; i < jsString._value.Length; i++)
{
a.SetIndexValue((uint) i, JsString.Create(jsString._value[i]), updateLength: false);
}
return a;
}

if (thisObj.IsNull() || !(source is ObjectInstance objectInstance))
if (thisObj.IsNull() || source is not ObjectInstance objectInstance)
{
return _realm.Intrinsics.Array.ConstructFast(0);
return _realm.Intrinsics.Array.ArrayCreate(0);
}

if (objectInstance is IObjectWrapper wrapper && wrapper.Target is IEnumerable enumerable)
Expand All @@ -92,7 +91,7 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
}
else
{
instance = _realm.Intrinsics.Array.ConstructFast(0);
instance = _realm.Intrinsics.Array.ArrayCreate(0);
}

if (objectInstance.TryGetIterator(_realm, out var iterator))
Expand Down Expand Up @@ -124,7 +123,7 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
}
else
{
a = _realm.Intrinsics.Array.ConstructFast(length);
a = _realm.Intrinsics.Array.ArrayCreate(length);
}

var args = !ReferenceEquals(callable, null)
Expand All @@ -151,7 +150,7 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
jsValue = value;
}

target.Set(i, jsValue, updateLength: false, throwOnError: false);
target.CreateDataPropertyOrThrow(i, jsValue);
n++;
}

Expand All @@ -164,7 +163,7 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
return a;
}

internal sealed class ArrayProtocol : IteratorProtocol
private sealed class ArrayProtocol : IteratorProtocol
{
private readonly JsValue _thisArg;
private readonly ArrayOperations _instance;
Expand Down Expand Up @@ -198,7 +197,7 @@ protected override void ProcessItem(JsValue[] args, JsValue currentValue)
jsValue = currentValue;
}

_instance.Set((uint) _index, jsValue, updateLength: false, throwOnError: true);
_instance.CreateDataPropertyOrThrow((ulong) _index, jsValue);
}

protected override void IterationEnd()
Expand Down Expand Up @@ -288,12 +287,12 @@ internal ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
static intrinsics => intrinsics.Array.PrototypeObject);

// check if we can figure out good size
var capacity = arguments.Length > 0 ? (uint) arguments.Length : 0;
var capacity = arguments.Length > 0 ? (ulong) arguments.Length : 0;
if (arguments.Length == 1 && arguments[0].IsNumber())
{
var number = ((JsNumber) arguments[0])._value;
ValidateLength(number);
capacity = (uint) number;
capacity = (ulong) number;
}
return Construct(arguments, capacity, proto);
}
Expand All @@ -313,7 +312,7 @@ public ArrayInstance Construct(JsValue[] arguments, uint capacity)
return Construct(arguments, capacity, PrototypeObject);
}

private ArrayInstance Construct(JsValue[] arguments, uint capacity, ObjectInstance prototypeObject)
private ArrayInstance Construct(JsValue[] arguments, ulong capacity, ObjectInstance prototypeObject)
{
var instance = ArrayCreate(capacity, prototypeObject);

Expand Down Expand Up @@ -350,10 +349,15 @@ private ArrayInstance Construct(JsValue[] arguments, uint capacity, ObjectInstan
/// <summary>
/// https://tc39.es/ecma262/#sec-arraycreate
/// </summary>
internal ArrayInstance ArrayCreate(uint length, ObjectInstance proto = null)
internal ArrayInstance ArrayCreate(ulong length, ObjectInstance proto = null)
{
if (length > ArrayOperations.MaxArrayLength)
{
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length " + length);
}

proto ??= PrototypeObject;
var instance = new ArrayInstance(Engine, length)
var instance = new ArrayInstance(Engine, (uint) length)
{
_prototype = proto,
_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable)
Expand All @@ -378,7 +382,7 @@ private ArrayInstance ConstructArrayFromIEnumerable(IEnumerable enumerable)

public ArrayInstance ConstructFast(JsValue[] contents)
{
var instance = ConstructFast((ulong) contents.Length);
var instance = ArrayCreate((ulong) contents.Length);
for (var i = 0; i < contents.Length; i++)
{
instance.SetIndexValue((uint) i, contents[i], updateLength: false);
Expand All @@ -388,32 +392,23 @@ public ArrayInstance ConstructFast(JsValue[] contents)

internal ArrayInstance ConstructFast(List<JsValue> contents)
{
var instance = ConstructFast((ulong) contents.Count);
var instance = ArrayCreate((ulong) contents.Count);
for (var i = 0; i < contents.Count; i++)
{
instance.SetIndexValue((uint) i, contents[i], updateLength: false);
}
return instance;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ArrayInstance ConstructFast(ulong length)
{
ValidateLength(length);
var instance = new ArrayInstance(Engine, (uint) length)
{
_prototype = PrototypeObject,
_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable)
};
return instance;
}

/// <summary>
/// https://tc39.es/ecma262/#sec-arrayspeciescreate
/// </summary>
public ObjectInstance ArraySpeciesCreate(ObjectInstance originalArray, ulong length)
{
var isArray = originalArray.IsArray();
if (!isArray)
{
return ConstructFast(length);
return ArrayCreate(length);
}

var c = originalArray.Get(CommonProperties.Constructor);
Expand All @@ -431,9 +426,9 @@ public ObjectInstance ArraySpeciesCreate(ObjectInstance originalArray, ulong len
}
}

if (c is ObjectInstance oi)
if (c.IsObject())
{
c = oi.Get(GlobalSymbolRegistry.Species);
c = c.Get(GlobalSymbolRegistry.Species);
if (c.IsNull())
{
c = Undefined;
Expand All @@ -442,7 +437,7 @@ public ObjectInstance ArraySpeciesCreate(ObjectInstance originalArray, ulong len

if (c.IsUndefined())
{
return ConstructFast(length);
return ArrayCreate(length);
}

if (!c.IsConstructor)
Expand All @@ -455,7 +450,7 @@ public ObjectInstance ArraySpeciesCreate(ObjectInstance originalArray, ulong len

internal JsValue CreateArrayFromList(List<JsValue> values)
{
var jsArray = ConstructFast((uint) values.Count);
var jsArray = ArrayCreate((uint) values.Count);
var index = 0;
for (; index < values.Count; index++)
{
Expand All @@ -469,7 +464,7 @@ internal JsValue CreateArrayFromList(List<JsValue> values)

private void ValidateLength(double length)
{
if (length < 0 || length > ArrayOperations.MaxArrayLength || ((long) length) != length)
if (length < 0 || length > ArrayOperations.MaxArrayLikeLength || ((long) length) != length)
{
ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");
}
Expand Down

0 comments on commit ab3d63d

Please sign in to comment.