added Array.prototype.{with,toReversed,toSpliced,toSorted} and TypedArray.prototype.{with,toReversed,toSorted} (initial patch by bnoordhuis)

This commit is contained in:
Fabrice Bellard 2023-12-27 17:19:00 +01:00
parent c2c773e135
commit feefdb1742
3 changed files with 381 additions and 2 deletions

2
TODO
View file

@ -63,5 +63,5 @@ Optimization ideas:
Test262o: 0/11262 errors, 463 excluded
Test262o commit: 7da91bceb9ce7613f87db47ddd1292a2dda58b42 (es5-tests branch)
Result: 16/76471 errors, 1497 excluded, 8355 skipped
Result: 16/76707 errors, 1497 excluded, 8237 skipped
Test262 commit: 6cbb6da9473c56d95358d8e679c5a6d2b4574efb

379
quickjs.c
View file

@ -1192,6 +1192,10 @@ static JSValue js_typed_array_constructor(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv,
int classid);
static JSValue js_typed_array_constructor_ta(JSContext *ctx,
JSValueConst new_target,
JSValueConst src_obj,
int classid);
static BOOL typed_array_is_detached(JSContext *ctx, JSObject *p);
static uint32_t typed_array_get_length(JSContext *ctx, JSObject *p);
static JSValue JS_ThrowTypeErrorDetachedArrayBuffer(JSContext *ctx);
@ -8319,6 +8323,30 @@ static int add_fast_array_element(JSContext *ctx, JSObject *p,
return TRUE;
}
/* Allocate a new fast array. Its 'length' property is set to zero. It
maximum size is 2^31-1 elements. For convenience, 'len' is a 64 bit
integer. WARNING: the content of the array is not initialized. */
static JSValue js_allocate_fast_array(JSContext *ctx, int64_t len)
{
JSValue arr;
JSObject *p;
if (len > INT32_MAX)
return JS_ThrowRangeError(ctx, "invalid array length");
arr = JS_NewArray(ctx);
if (JS_IsException(arr))
return arr;
if (len > 0) {
p = JS_VALUE_GET_OBJ(arr);
if (expand_fast_array(ctx, p, len) < 0) {
JS_FreeValue(ctx, arr);
return JS_EXCEPTION;
}
p->u.array.count = len;
}
return arr;
}
static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc)
{
JS_FreeValue(ctx, desc->getter);
@ -38612,6 +38640,71 @@ static JSValue js_array_at(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
static JSValue js_array_with(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, obj, ret, *arrp, *pval;
JSObject *p;
int64_t i, len, idx;
uint32_t count32;
ret = JS_EXCEPTION;
arr = JS_UNDEFINED;
obj = JS_ToObject(ctx, this_val);
if (js_get_length64(ctx, &len, obj))
goto exception;
if (JS_ToInt64Sat(ctx, &idx, argv[0]))
goto exception;
if (idx < 0)
idx = len + idx;
if (idx < 0 || idx >= len) {
JS_ThrowRangeError(ctx, "out of bound");
goto exception;
}
arr = js_allocate_fast_array(ctx, len);
if (JS_IsException(arr))
goto exception;
p = JS_VALUE_GET_OBJ(arr);
i = 0;
pval = p->u.array.u.values;
if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
for (; i < idx; i++, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
*pval = JS_DupValue(ctx, argv[1]);
for (i++, pval++; i < len; i++, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
} else {
for (; i < idx; i++, pval++)
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
goto fill_and_fail;
*pval = JS_DupValue(ctx, argv[1]);
for (i++, pval++; i < len; i++, pval++) {
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) {
fill_and_fail:
for (; i < len; i++, pval++)
*pval = JS_UNDEFINED;
goto exception;
}
}
}
if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, len)) < 0)
goto exception;
ret = arr;
arr = JS_UNDEFINED;
exception:
JS_FreeValue(ctx, arr);
JS_FreeValue(ctx, obj);
return ret;
}
static JSValue js_array_concat(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@ -39430,6 +39523,61 @@ static JSValue js_array_reverse(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
// Note: a.toReversed() is a.slice().reverse() with the twist that a.slice()
// leaves holes in sparse arrays intact whereas a.toReversed() replaces them
// with undefined, thus in effect creating a dense array.
// Does not use Array[@@species], always returns a base Array.
static JSValue js_array_toReversed(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, obj, ret, *arrp, *pval;
JSObject *p;
int64_t i, len;
uint32_t count32;
ret = JS_EXCEPTION;
arr = JS_UNDEFINED;
obj = JS_ToObject(ctx, this_val);
if (js_get_length64(ctx, &len, obj))
goto exception;
arr = js_allocate_fast_array(ctx, len);
if (JS_IsException(arr))
goto exception;
if (len > 0) {
p = JS_VALUE_GET_OBJ(arr);
i = len - 1;
pval = p->u.array.u.values;
if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
for (; i >= 0; i--, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
} else {
// Query order is observable; test262 expects descending order.
for (; i >= 0; i--, pval++) {
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) {
// Exception; initialize remaining elements.
for (; i >= 0; i--, pval++)
*pval = JS_UNDEFINED;
goto exception;
}
}
}
if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, len)) < 0)
goto exception;
}
ret = arr;
arr = JS_UNDEFINED;
exception:
JS_FreeValue(ctx, arr);
JS_FreeValue(ctx, obj);
return ret;
}
static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int splice)
{
@ -39537,6 +39685,92 @@ static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, obj, ret, *arrp, *pval, *last;
JSObject *p;
int64_t i, j, len, newlen, start, add, del;
uint32_t count32;
pval = NULL;
last = NULL;
ret = JS_EXCEPTION;
arr = JS_UNDEFINED;
obj = JS_ToObject(ctx, this_val);
if (js_get_length64(ctx, &len, obj))
goto exception;
start = 0;
if (argc > 0)
if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len))
goto exception;
del = 0;
if (argc > 0)
del = len - start;
if (argc > 1)
if (JS_ToInt64Clamp(ctx, &del, argv[1], 0, del, 0))
goto exception;
add = 0;
if (argc > 2)
add = argc - 2;
newlen = len + add - del;
if (newlen > MAX_SAFE_INTEGER) {
JS_ThrowTypeError(ctx, "invalid array length");
goto exception;
}
arr = js_allocate_fast_array(ctx, newlen);
if (JS_IsException(arr))
goto exception;
if (newlen <= 0)
goto done;
p = JS_VALUE_GET_OBJ(arr);
pval = &p->u.array.u.values[0];
last = &p->u.array.u.values[newlen];
if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
for (i = 0; i < start; i++, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
for (j = 0; j < add; j++, pval++)
*pval = JS_DupValue(ctx, argv[2 + j]);
for (i += del; i < len; i++, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
} else {
for (i = 0; i < start; i++, pval++)
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
goto exception;
for (j = 0; j < add; j++, pval++)
*pval = JS_DupValue(ctx, argv[2 + j]);
for (i += del; i < len; i++, pval++)
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
goto exception;
}
assert(pval == last);
if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, newlen)) < 0)
goto exception;
done:
ret = arr;
arr = JS_UNDEFINED;
exception:
while (pval != last)
*pval++ = JS_UNDEFINED;
JS_FreeValue(ctx, arr);
JS_FreeValue(ctx, obj);
return ret;
}
static JSValue js_array_copyWithin(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@ -39840,6 +40074,68 @@ fail:
return JS_EXCEPTION;
}
// Note: a.toSorted() is a.slice().sort() with the twist that a.slice()
// leaves holes in sparse arrays intact whereas a.toSorted() replaces them
// with undefined, thus in effect creating a dense array.
// Does not use Array[@@species], always returns a base Array.
static JSValue js_array_toSorted(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, obj, ret, *arrp, *pval;
JSObject *p;
int64_t i, len;
uint32_t count32;
int ok;
ok = JS_IsUndefined(argv[0]) || JS_IsFunction(ctx, argv[0]);
if (!ok)
return JS_ThrowTypeError(ctx, "not a function");
ret = JS_EXCEPTION;
arr = JS_UNDEFINED;
obj = JS_ToObject(ctx, this_val);
if (js_get_length64(ctx, &len, obj))
goto exception;
arr = js_allocate_fast_array(ctx, len);
if (JS_IsException(arr))
goto exception;
if (len > 0) {
p = JS_VALUE_GET_OBJ(arr);
i = 0;
pval = p->u.array.u.values;
if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
for (; i < len; i++, pval++)
*pval = JS_DupValue(ctx, arrp[i]);
} else {
for (; i < len; i++, pval++) {
if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) {
for (; i < len; i++, pval++)
*pval = JS_UNDEFINED;
goto exception;
}
}
}
if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, len)) < 0)
goto exception;
}
ret = js_array_sort(ctx, arr, argc, argv);
if (JS_IsException(ret))
goto exception;
JS_FreeValue(ctx, ret);
ret = arr;
arr = JS_UNDEFINED;
exception:
JS_FreeValue(ctx, arr);
JS_FreeValue(ctx, obj);
return ret;
}
typedef struct JSArrayIteratorData {
JSValue obj;
JSIteratorKindEnum kind;
@ -39993,6 +40289,7 @@ static const JSCFunctionListEntry js_iterator_proto_funcs[] = {
static const JSCFunctionListEntry js_array_proto_funcs[] = {
JS_CFUNC_DEF("at", 1, js_array_at ),
JS_CFUNC_DEF("with", 2, js_array_with ),
JS_CFUNC_DEF("concat", 1, js_array_concat ),
JS_CFUNC_MAGIC_DEF("every", 1, js_array_every, special_every ),
JS_CFUNC_MAGIC_DEF("some", 1, js_array_every, special_some ),
@ -40017,9 +40314,12 @@ static const JSCFunctionListEntry js_array_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("shift", 0, js_array_pop, 1 ),
JS_CFUNC_MAGIC_DEF("unshift", 1, js_array_push, 1 ),
JS_CFUNC_DEF("reverse", 0, js_array_reverse ),
JS_CFUNC_DEF("toReversed", 0, js_array_toReversed ),
JS_CFUNC_DEF("sort", 1, js_array_sort ),
JS_CFUNC_DEF("toSorted", 1, js_array_toSorted ),
JS_CFUNC_MAGIC_DEF("slice", 2, js_array_slice, 0 ),
JS_CFUNC_MAGIC_DEF("splice", 2, js_array_slice, 1 ),
JS_CFUNC_DEF("toSpliced", 2, js_array_toSpliced ),
JS_CFUNC_DEF("copyWithin", 2, js_array_copyWithin ),
JS_CFUNC_MAGIC_DEF("flatMap", 1, js_array_flatten, 1 ),
JS_CFUNC_MAGIC_DEF("flat", 0, js_array_flatten, 0 ),
@ -51649,6 +51949,9 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
"flatMap" "\0"
"includes" "\0"
"keys" "\0"
"toReversed" "\0"
"toSorted" "\0"
"toSpliced" "\0"
"values" "\0";
const char *p = unscopables;
obj1 = JS_NewObjectProto(ctx, JS_NULL);
@ -52375,6 +52678,43 @@ static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val,
return JS_GetPropertyInt64(ctx, this_val, idx);
}
static JSValue js_typed_array_with(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, val;
JSObject *p;
int64_t idx, len;
p = get_typed_array(ctx, this_val, /*is_dataview*/0);
if (!p)
return JS_EXCEPTION;
if (JS_ToInt64Sat(ctx, &idx, argv[0]))
return JS_EXCEPTION;
len = p->u.array.count;
if (idx < 0)
idx = len + idx;
if (idx < 0 || idx >= len)
return JS_ThrowRangeError(ctx, "out of bound");
val = JS_ToPrimitive(ctx, argv[1], HINT_NUMBER);
if (JS_IsException(val))
return JS_EXCEPTION;
arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
p->class_id);
if (JS_IsException(arr)) {
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
if (JS_SetPropertyInt64(ctx, arr, idx, val) < 0) {
JS_FreeValue(ctx, arr);
return JS_EXCEPTION;
}
return arr;
}
static JSValue js_typed_array_set(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv)
@ -53153,6 +53493,24 @@ static JSValue js_typed_array_reverse(JSContext *ctx, JSValueConst this_val,
return JS_DupValue(ctx, this_val);
}
static JSValue js_typed_array_toReversed(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, ret;
JSObject *p;
p = get_typed_array(ctx, this_val, /*is_dataview*/0);
if (!p)
return JS_EXCEPTION;
arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
p->class_id);
if (JS_IsException(arr))
return JS_EXCEPTION;
ret = js_typed_array_reverse(ctx, arr, argc, argv);
JS_FreeValue(ctx, arr);
return ret;
}
static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@ -53556,6 +53914,24 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValueConst this_val,
return JS_DupValue(ctx, this_val);
}
static JSValue js_typed_array_toSorted(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue arr, ret;
JSObject *p;
p = get_typed_array(ctx, this_val, /*is_dataview*/0);
if (!p)
return JS_EXCEPTION;
arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
p->class_id);
if (JS_IsException(arr))
return JS_EXCEPTION;
ret = js_typed_array_sort(ctx, arr, argc, argv);
JS_FreeValue(ctx, arr);
return ret;
}
static const JSCFunctionListEntry js_typed_array_base_funcs[] = {
JS_CFUNC_DEF("from", 1, js_typed_array_from ),
JS_CFUNC_DEF("of", 0, js_typed_array_of ),
@ -53568,6 +53944,7 @@ static const JSCFunctionListEntry js_typed_array_base_funcs[] = {
static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
JS_CGETSET_DEF("length", js_typed_array_get_length, NULL ),
JS_CFUNC_DEF("at", 1, js_typed_array_at ),
JS_CFUNC_DEF("with", 2, js_typed_array_with ),
JS_CGETSET_MAGIC_DEF("buffer", js_typed_array_get_buffer, NULL, 0 ),
JS_CGETSET_MAGIC_DEF("byteLength", js_typed_array_get_byteLength, NULL, 0 ),
JS_CGETSET_MAGIC_DEF("byteOffset", js_typed_array_get_byteOffset, NULL, 0 ),
@ -53591,9 +53968,11 @@ static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, special_findLast ),
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, special_findLastIndex ),
JS_CFUNC_DEF("reverse", 0, js_typed_array_reverse ),
JS_CFUNC_DEF("toReversed", 0, js_typed_array_toReversed ),
JS_CFUNC_DEF("slice", 2, js_typed_array_slice ),
JS_CFUNC_DEF("subarray", 2, js_typed_array_subarray ),
JS_CFUNC_DEF("sort", 1, js_typed_array_sort ),
JS_CFUNC_DEF("toSorted", 1, js_typed_array_toSorted ),
JS_CFUNC_MAGIC_DEF("join", 1, js_typed_array_join, 0 ),
JS_CFUNC_MAGIC_DEF("toLocaleString", 0, js_typed_array_join, 1 ),
JS_CFUNC_MAGIC_DEF("indexOf", 1, js_typed_array_indexOf, special_indexOf ),

View file

@ -73,7 +73,7 @@ Atomics
Atomics.waitAsync=skip
BigInt
caller
change-array-by-copy=skip
change-array-by-copy
class
class-fields-private
class-fields-private-in=skip