Skip to content

Commit

Permalink
add ArrayOperations.GetAll
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jul 22, 2019
1 parent c7c2770 commit 0f29b4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
12 changes: 6 additions & 6 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ArrayInstance : ObjectInstance
private const int MaxDenseArrayLength = 1024 * 10;

// we have dense and sparse, we usually can start with dense and fall back to sparse when necessary
private PropertyDescriptor[] _dense;
internal PropertyDescriptor[] _dense;
private Dictionary<uint, PropertyDescriptor> _sparse;

public ArrayInstance(Engine engine, uint capacity = 0) : base(engine, objectClass: "Array")
Expand Down Expand Up @@ -353,6 +353,11 @@ protected override bool TryGetProperty(in Key propertyName, out PropertyDescript

public override PropertyDescriptor GetOwnProperty(in Key propertyName)
{
if (propertyName == KnownKeys.Length)
{
return _length ?? PropertyDescriptor.Undefined;
}

if (IsArrayIndex(propertyName, out var index))
{
if (TryGetDescriptor(index, out var result))
Expand All @@ -363,11 +368,6 @@ public override PropertyDescriptor GetOwnProperty(in Key propertyName)
return PropertyDescriptor.Undefined;
}

if (propertyName == KnownKeys.Length)
{
return _length ?? PropertyDescriptor.Undefined;
}

return base.GetOwnProperty(propertyName);
}

Expand Down
37 changes: 37 additions & 0 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,18 @@ internal abstract class ArrayOperations

public abstract JsValue Get(ulong index);

public virtual JsValue[] GetAll()
{
var n = (int) GetLength();
var jsValues = new JsValue[n];
for (uint i = 0; i < (uint) jsValues.Length; i++)
{
jsValues[i] = Get(i);
}

return jsValues;
}

public abstract bool TryGetValue(ulong index, out JsValue value);

public abstract void Put(ulong index, JsValue value, bool throwOnError);
Expand Down Expand Up @@ -1432,6 +1444,31 @@ public override bool TryGetValue(ulong index, out JsValue value)

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

public override JsValue[] GetAll()
{
var n = _array.Length;

if (_array._dense == null || _array._dense.Length < n)
{
return base.GetAll();
}

// optimized
var jsValues = new JsValue[n];
for (uint i = 0; i < (uint) jsValues.Length; i++)
{
var prop = _array._dense[i] ?? PropertyDescriptor.Undefined;
if (prop == PropertyDescriptor.Undefined)
{
prop = _array.Prototype?.GetProperty(TypeConverter.ToString(i)) ?? PropertyDescriptor.Undefined;
}

jsValues[i] = _array.UnwrapJsValue(prop);
}

return jsValues;
}

public override void DeleteAt(ulong index) => _array.DeleteAt((uint) index);

public override void Put(ulong index, JsValue value, bool throwOnError) => _array.SetIndexValue((uint) index, value, throwOnError);
Expand Down
9 changes: 1 addition & 8 deletions Jint/Native/Function/FunctionPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,9 @@ private JsValue Apply(JsValue thisObject, JsValue[] arguments)

var argArrayObj = argArray as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(Engine);
var operations = ArrayPrototype.ArrayOperations.For(argArrayObj);

var n = (int) operations.GetLength();
var argList = _engine._jsValueArrayPool.RentArray(n);
for (uint i = 0; i < argList.Length; i++)
{
argList[i] = operations.Get(i);
}
var argList = operations.GetAll();

var result = func.Call(thisArg, argList);
_engine._jsValueArrayPool.ReturnArray(argList);

return result;
}
Expand Down

0 comments on commit 0f29b4e

Please sign in to comment.