Skip to content
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
24 changes: 6 additions & 18 deletions Jint/Native/Array/ArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private JsArray ConstructFromIterator(JsValue items, ICallable usingIterator, IC
var iterator = items.GetIterator(_realm, method: usingIterator);

var builder = new JsValueListBuilder(16);
var args = callable is not null ? _engine._jsValueArrayPool.RentArray(2) : null;
var invoker = callable is not null ? CallbackInvoker.Rent(_engine, callable, 2) : default;
try
{
var iterations = 0;
Expand All @@ -119,9 +119,7 @@ private JsArray ConstructFromIterator(JsValue items, ICallable usingIterator, IC

if (callable is not null)
{
args![0] = jsValue;
args[1] = index;
jsValue = callable.Call(thisArg, args);
jsValue = invoker.Call(thisArg, jsValue, index);
}

builder.Add(jsValue);
Expand All @@ -138,10 +136,7 @@ private JsArray ConstructFromIterator(JsValue items, ICallable usingIterator, IC
finally
{
builder.Dispose();
if (args is not null)
{
_engine._jsValueArrayPool.ReturnArray(args);
}
invoker.Return();
}
}

Expand Down Expand Up @@ -665,9 +660,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
a = ArrayCreate(length);
}

var args = callable is not null
? _engine._jsValueArrayPool.RentArray(2)
: null;
var invoker = callable is not null ? CallbackInvoker.Rent(_engine, callable, 2) : default;

var target = ArrayOperations.For(a, forWrite: true);
uint n = 0;
Expand All @@ -682,9 +675,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
var value = source.Get(i);
if (callable is not null)
{
args![0] = value;
args[1] = i;
value = callable.Call(thisArg, args);
value = invoker.Call(thisArg, value, i);

// function can alter data
length = source.GetLength();
Expand All @@ -694,10 +685,7 @@ private ObjectInstance ConstructArrayFromArrayLike(
n++;
}

if (callable is not null)
{
_engine._jsValueArrayPool.ReturnArray(args!);
}
invoker.Return();

target.SetLength(length);
return a;
Expand Down
37 changes: 10 additions & 27 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,17 +1667,12 @@ internal JsArray Map(JsCallArguments arguments)

var callable = GetCallable(callbackfn);
var a = _engine.Realm.Intrinsics.Array.ArrayCreate(len);
// args is rented from the pool whose factory allocates new JsValue[3], so it is an exact
// JsValue[]; the per-element fills below bypass the covariant array store type check.
var args = _engine._jsValueArrayPool.RentArray(3);
Arguments.WriteNoTypeCheck(args, 2, this);
var invoker = CallbackInvoker.Rent(_engine, callable, 3, this);
for (uint k = 0; k < len; k++)
{
if (TryGetValue(k, out var kvalue))
{
Arguments.WriteNoTypeCheck(args, 0, kvalue);
Arguments.WriteNoTypeCheck(args, 1, k);
var mappedValue = callable.Call(thisArg, args);
var mappedValue = invoker.Call(thisArg, kvalue, k);
if (a._dense != null && k < (uint) a._dense.Length)
{
a._dense[k] = mappedValue;
Expand All @@ -1689,7 +1684,7 @@ internal JsArray Map(JsCallArguments arguments)
}
}

_engine._jsValueArrayPool.ReturnArray(args);
invoker.Return();
return a;
}

Expand All @@ -1706,10 +1701,7 @@ internal JsArray Filter(JsCallArguments arguments)
// materialize an exact-size result instead of growing the result's dense backing by
// doubling. Capped initial rent so a large low-selectivity source doesn't rent ~len slots.
var builder = new JsValueListBuilder((int) System.Math.Min(len, 1024));
// args is rented from the pool whose factory allocates new JsValue[3], so it is an exact
// JsValue[]; the per-element fills below bypass the covariant array store type check.
var args = _engine._jsValueArrayPool.RentArray(3);
Arguments.WriteNoTypeCheck(args, 2, this);
var invoker = CallbackInvoker.Rent(_engine, callable, 3, this);

// try/finally so the rented args array and the pooled builder buffer are released
// when the callback or a periodic Check() throws.
Expand All @@ -1724,9 +1716,7 @@ internal JsArray Filter(JsCallArguments arguments)

if (TryGetValue(k, out var kvalue))
{
Arguments.WriteNoTypeCheck(args, 0, kvalue);
Arguments.WriteNoTypeCheck(args, 1, k);
var selected = callable.Call(thisArg, args);
var selected = invoker.Call(thisArg, kvalue, k);
if (TypeConverter.ToBoolean(selected))
{
builder.Add(kvalue);
Expand All @@ -1739,7 +1729,7 @@ internal JsArray Filter(JsCallArguments arguments)
finally
{
builder.Dispose();
_engine._jsValueArrayPool.ReturnArray(args);
invoker.Return();
}
}

Expand All @@ -1762,10 +1752,7 @@ internal sealed override bool FindWithCallback(
return false;
}

// args is rented from the pool whose factory allocates new JsValue[3], so it is an exact
// JsValue[]; the per-element fills below bypass the covariant array store type check.
var args = _engine._jsValueArrayPool.RentArray(3);
Arguments.WriteNoTypeCheck(args, 2, this);
var invoker = CallbackInvoker.Rent(_engine, callable, 3, this);

// try/finally so the rented pool array is returned on every exit path: the early
// return-on-match below and a periodic Check() throw both previously leaked it.
Expand All @@ -1783,9 +1770,7 @@ internal sealed override bool FindWithCallback(
if (TryGetValue(k, out var kvalue) || visitUnassigned)
{
kvalue ??= Undefined;
Arguments.WriteNoTypeCheck(args, 0, kvalue);
Arguments.WriteNoTypeCheck(args, 1, k);
var testResult = callable.Call(thisArg, args);
var testResult = invoker.Call(thisArg, kvalue, k);
if (TypeConverter.ToBoolean(testResult))
{
index = k;
Expand All @@ -1808,9 +1793,7 @@ internal sealed override bool FindWithCallback(
if (TryGetValue(idx, out var kvalue) || visitUnassigned)
{
kvalue ??= Undefined;
Arguments.WriteNoTypeCheck(args, 0, kvalue);
Arguments.WriteNoTypeCheck(args, 1, idx);
var testResult = callable.Call(thisArg, args);
var testResult = invoker.Call(thisArg, kvalue, idx);
if (TypeConverter.ToBoolean(testResult))
{
index = idx;
Expand All @@ -1823,7 +1806,7 @@ internal sealed override bool FindWithCallback(
}
finally
{
_engine._jsValueArrayPool.ReturnArray(args);
invoker.Return();
}

index = 0;
Expand Down
Loading