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

Implement change Array by copy #1366

Merged
merged 6 commits into from
Nov 20, 2022
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
1 change: 0 additions & 1 deletion Jint.Tests.Test262/Test262Harness.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"ExcludedFeatures": [
"async-iteration",
"Atomics",
"change-array-by-copy",
"class-fields-private",
"class-fields-public",
"class-methods-private",
Expand Down
36 changes: 18 additions & 18 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private protected ArrayInstance(Engine engine, InternalTypes type) : base(engine
_dense = System.Array.Empty<object?>();
}

private protected ArrayInstance(Engine engine, uint capacity = 0, uint length = 0) : base(engine)
private protected ArrayInstance(Engine engine, uint capacity = 0, uint length = 0) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
{
_prototype = engine.Realm.Intrinsics.Array.PrototypeObject;

Expand All @@ -48,30 +48,30 @@ private protected ArrayInstance(Engine engine, uint capacity = 0, uint length =
_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
}

private protected ArrayInstance(Engine engine, JsValue[] items) : base(engine)
private protected ArrayInstance(Engine engine, JsValue[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
{
_prototype = engine.Realm.Intrinsics.Array.PrototypeObject;
_isObjectArray = false;
Initialize(engine, items);
}

int length;
if (items == null || items.Length == 0)
{
_dense = System.Array.Empty<object>();
length = 0;
}
else
{
_dense = items;
length = items.Length;
}
private protected ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
{
Initialize(engine, items);
}

_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
private protected ArrayInstance(Engine engine, object[] items) : base(engine, type: InternalTypes.Object | InternalTypes.Array)
{
Initialize(engine, items);
}

private protected ArrayInstance(Engine engine, PropertyDescriptor[] items) : base(engine)
private void Initialize<T>(Engine engine, T[] items) where T : class
{
if (items.Length > engine.Options.Constraints.MaxArraySize)
{
ThrowMaximumArraySizeReachedException(engine, (uint) items.Length);
}

_prototype = engine.Realm.Intrinsics.Array.PrototypeObject;
_isObjectArray = false;
_isObjectArray = typeof(T) == typeof(object);

int length;
if (items == null || items.Length == 0)
Expand Down
30 changes: 14 additions & 16 deletions Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ public static ArrayOperations For(Realm realm, JsValue thisObj)

public abstract JsValue Get(ulong index);

public virtual JsValue[] GetAll(Types elementTypes)
public virtual JsValue[] GetAll(
Types elementTypes = Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object,
bool skipHoles = false)
{
uint writeIndex = 0;
var n = (int) GetLength();
var jsValues = new JsValue[n];
for (uint i = 0; i < (uint) jsValues.Length; i++)
{
var jsValue = Get(i);
var jsValue = skipHoles && !HasProperty(i) ? JsValue.Undefined : Get(i);
if ((jsValue.Type & elementTypes) == 0)
{
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
}

jsValues[i] = jsValue;
jsValues[writeIndex++] = jsValue;
}

return jsValues;
Expand All @@ -75,7 +78,7 @@ public virtual JsValue[] GetAll(Types elementTypes)

public abstract void DeletePropertyOrThrow(ulong index);

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

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

Expand All @@ -100,14 +103,9 @@ public JsValue Current
{
get
{
if (!_initialized)
{
return JsValue.Undefined;
}
else
{
return _obj.TryGetValue(_current, out var temp) ? temp : JsValue.Undefined;
}
return !_initialized
? JsValue.Undefined
: _obj.TryGetValue(_current, out var temp) ? temp : JsValue.Undefined;
}
}

Expand Down Expand Up @@ -232,7 +230,7 @@ public override bool TryGetValue(ulong index, out JsValue value)

public override JsValue Get(ulong index) => _target.Get((uint) index);

public override JsValue[] GetAll(Types elementTypes)
public override JsValue[] GetAll(Types elementTypes, bool skipHoles = false)
{
var n = _target.GetLength();

Expand All @@ -242,6 +240,7 @@ public override JsValue[] GetAll(Types elementTypes)
}

// optimized
uint writeIndex = 0;
var jsValues = new JsValue[n];
for (uint i = 0; i < (uint) jsValues.Length; i++)
{
Expand All @@ -255,13 +254,12 @@ public override JsValue[] GetAll(Types elementTypes)
prop = _target.UnwrapJsValue((PropertyDescriptor) prop);
}

var jsValue = (JsValue) prop;
if ((jsValue.Type & elementTypes) == 0)
if (prop is JsValue jsValue && (jsValue.Type & elementTypes) == 0)
{
ExceptionHelper.ThrowTypeErrorNoEngine("invalid type");
}

jsValues[i] = jsValue;
jsValues[writeIndex++] = (JsValue?) prop ?? JsValue.Undefined;
}

return jsValues;
Expand Down
Loading