Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Aug 29, 2023
1 parent 7cbb3ac commit 943e59e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
46 changes: 25 additions & 21 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,15 @@ private protected ArrayInstance(Engine engine, JsValue[] items) : base(engine, t
{
InitializePrototypeAndValidateCapacity(engine, capacity: 0);

int length;
if (items == null || items.Length == 0)
{
_dense = System.Array.Empty<JsValue>();
length = 0;
}
else
{
_dense = items;
length = items.Length;
}

_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
_dense = items;
_length = new PropertyDescriptor(items.Length, PropertyFlag.OnlyWritable);
}

private void InitializePrototypeAndValidateCapacity(Engine engine, uint capacity)
{
_prototype = engine.Realm.Intrinsics.Array.PrototypeObject;

if (capacity > engine.Options.Constraints.MaxArraySize)
if (capacity > 0 && capacity > engine.Options.Constraints.MaxArraySize)
{
ThrowMaximumArraySizeReachedException(engine, capacity);
}
Expand Down Expand Up @@ -434,7 +423,7 @@ public sealed override PropertyDescriptor GetOwnProperty(JsValue property)

if (IsArrayIndex(property, out var index))
{
if (TryGetDescriptor(index, out var result))
if (TryGetDescriptor(index, createIfMissing: true, out var result))
{
return result;
}
Expand Down Expand Up @@ -477,14 +466,25 @@ public sealed override JsValue Get(JsValue property, JsValue receiver)
public sealed override bool Set(JsValue property, JsValue value, JsValue receiver)
{
var isSafeSelfTarget = IsSafeSelfTarget(receiver);
var isArrayIndex = IsArrayIndex(property, out var index);
if (isSafeSelfTarget && isArrayIndex)
if (isSafeSelfTarget && CanUseFastAccess)
{
if (CanUseFastAccess)
if (IsArrayIndex(property, out var index))
{
SetIndexValue(index, value, updateLength: true);
return true;
}

if (property == CommonProperties.Length
&& _length is { Writable: true }
&& value is JsNumber jsNumber
&& jsNumber.IsInteger()
&& jsNumber._value <= MaxDenseArrayLength
&& jsNumber._value >= GetLength())
{
// we don't need explicit resize
_length.Value = jsNumber;
return true;
}
}

// slow path
Expand Down Expand Up @@ -696,15 +696,15 @@ internal bool Delete(uint index)
{
if (index < (uint) temp.Length)
{
if (!TryGetDescriptor(index, out var descriptor) || descriptor.Configurable)
if (!TryGetDescriptor(index, createIfMissing: false, out var descriptor) || descriptor.Configurable)
{
temp[index] = null;
return true;
}
}
}

if (!TryGetDescriptor(index, out var desc))
if (!TryGetDescriptor(index, createIfMissing: false, out var desc))
{
return true;
}
Expand Down Expand Up @@ -737,7 +737,7 @@ internal bool DeleteAt(uint index)
return false;
}

private bool TryGetDescriptor(uint index, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
private bool TryGetDescriptor(uint index, bool createIfMissing, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
{
descriptor = null;
var temp = _dense;
Expand All @@ -750,6 +750,10 @@ private bool TryGetDescriptor(uint index, [NotNullWhen(true)] out PropertyDescri
{
if (_sparse is null || !_sparse.TryGetValue(index, out descriptor) || descriptor is null)
{
if (!createIfMissing)
{
return false;
}
_sparse ??= new Dictionary<uint, PropertyDescriptor?>();
_sparse[index] = descriptor = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
}
Expand Down
11 changes: 0 additions & 11 deletions Jint/Native/JsArray.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Jint.Native.Array;
using Jint.Runtime.Descriptors;

namespace Jint.Native;

Expand All @@ -22,14 +21,4 @@ public JsArray(Engine engine, uint capacity = 0, uint length = 0) : base(engine,
public JsArray(Engine engine, JsValue[] items) : base(engine, items)
{
}

internal static JsArray CreateEmpty(Engine engine)
{
return new JsArray(engine)
{
_prototype = engine.Realm.Intrinsics.Array.PrototypeObject,
_dense = System.Array.Empty<JsValue>(),
_length = new PropertyDescriptor(JsNumber.PositiveZero, PropertyFlag.OnlyWritable)
};
}
}
4 changes: 2 additions & 2 deletions Jint/Runtime/Interpreter/Expressions/JintArrayExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ private JintEmptyArrayExpression(Expression expression) : base(expression)

protected override object EvaluateInternal(EvaluationContext context)
{
return JsArray.CreateEmpty(context.Engine);
return new JsArray(context.Engine, Array.Empty<JsValue>());
}

public override JsValue GetValue(EvaluationContext context)
{
return JsArray.CreateEmpty(context.Engine);
return new JsArray(context.Engine, Array.Empty<JsValue>());
}
}
}
Expand Down

0 comments on commit 943e59e

Please sign in to comment.