Use more explicit magic values for array methods

This commit is contained in:
Charlie Gordon 2024-03-23 09:52:23 +01:00
parent c0e67c47cd
commit ce6b6dcacd

View File

@ -39763,10 +39763,10 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val,
} }
enum { enum {
special_find, ArrayFind,
special_findIndex, ArrayFindIndex,
special_findLast, ArrayFindLast,
special_findLastIndex, ArrayFindLastIndex,
}; };
static JSValue js_array_find(JSContext *ctx, JSValueConst this_val, static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
@ -39792,14 +39792,13 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
if (argc > 1) if (argc > 1)
this_arg = argv[1]; this_arg = argv[1];
if (mode == special_findLast || mode == special_findLastIndex) { k = 0;
dir = 1;
end = len;
if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
k = len - 1; k = len - 1;
dir = -1; dir = -1;
end = -1; end = -1;
} else {
k = 0;
dir = 1;
end = len;
} }
// TODO(bnoordhuis) add fast path for fast arrays // TODO(bnoordhuis) add fast path for fast arrays
@ -39817,7 +39816,7 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
if (JS_IsException(res)) if (JS_IsException(res))
goto exception; goto exception;
if (JS_ToBoolFree(ctx, res)) { if (JS_ToBoolFree(ctx, res)) {
if (mode == special_findIndex || mode == special_findLastIndex) { if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
JS_FreeValue(ctx, obj); JS_FreeValue(ctx, obj);
return index_val; return index_val;
@ -39831,7 +39830,7 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
JS_FreeValue(ctx, index_val); JS_FreeValue(ctx, index_val);
} }
JS_FreeValue(ctx, obj); JS_FreeValue(ctx, obj);
if (mode == special_findIndex || mode == special_findLastIndex) if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
return JS_NewInt32(ctx, -1); return JS_NewInt32(ctx, -1);
else else
return JS_UNDEFINED; return JS_UNDEFINED;
@ -40858,10 +40857,10 @@ static const JSCFunctionListEntry js_array_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce ), JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce ),
JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight ), JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight ),
JS_CFUNC_DEF("fill", 1, js_array_fill ), JS_CFUNC_DEF("fill", 1, js_array_fill ),
JS_CFUNC_MAGIC_DEF("find", 1, js_array_find, special_find ), JS_CFUNC_MAGIC_DEF("find", 1, js_array_find, ArrayFind ),
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_array_find, special_findIndex ), JS_CFUNC_MAGIC_DEF("findIndex", 1, js_array_find, ArrayFindIndex ),
JS_CFUNC_MAGIC_DEF("findLast", 1, js_array_find, special_findLast ), JS_CFUNC_MAGIC_DEF("findLast", 1, js_array_find, ArrayFindLast ),
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_array_find, special_findLastIndex ), JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_array_find, ArrayFindLastIndex ),
JS_CFUNC_DEF("indexOf", 1, js_array_indexOf ), JS_CFUNC_DEF("indexOf", 1, js_array_indexOf ),
JS_CFUNC_DEF("lastIndexOf", 1, js_array_lastIndexOf ), JS_CFUNC_DEF("lastIndexOf", 1, js_array_lastIndexOf ),
JS_CFUNC_DEF("includes", 1, js_array_includes ), JS_CFUNC_DEF("includes", 1, js_array_includes ),
@ -53909,14 +53908,13 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
if (argc > 1) if (argc > 1)
this_arg = argv[1]; this_arg = argv[1];
if (mode == special_findLast || mode == special_findLastIndex) { k = 0;
dir = 1;
end = len;
if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
k = len - 1; k = len - 1;
dir = -1; dir = -1;
end = -1; end = -1;
} else {
k = 0;
dir = 1;
end = len;
} }
for(; k != end; k += dir) { for(; k != end; k += dir) {
@ -53931,7 +53929,7 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
if (JS_IsException(res)) if (JS_IsException(res))
goto exception; goto exception;
if (JS_ToBoolFree(ctx, res)) { if (JS_ToBoolFree(ctx, res)) {
if (mode == special_findIndex || mode == special_findLastIndex) { if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
return index_val; return index_val;
} else { } else {
@ -53940,7 +53938,7 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
} }
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
} }
if (mode == special_findIndex || mode == special_findLastIndex) if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
return JS_NewInt32(ctx, -1); return JS_NewInt32(ctx, -1);
else else
return JS_UNDEFINED; return JS_UNDEFINED;
@ -54784,10 +54782,10 @@ static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce | special_TA ), JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce | special_TA ),
JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight | special_TA ), JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight | special_TA ),
JS_CFUNC_DEF("fill", 1, js_typed_array_fill ), JS_CFUNC_DEF("fill", 1, js_typed_array_fill ),
JS_CFUNC_MAGIC_DEF("find", 1, js_typed_array_find, special_find ), JS_CFUNC_MAGIC_DEF("find", 1, js_typed_array_find, ArrayFind ),
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_typed_array_find, special_findIndex ), JS_CFUNC_MAGIC_DEF("findIndex", 1, js_typed_array_find, ArrayFindIndex ),
JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, special_findLast ), JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, ArrayFindLast ),
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, special_findLastIndex ), JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, ArrayFindLastIndex ),
JS_CFUNC_DEF("reverse", 0, js_typed_array_reverse ), JS_CFUNC_DEF("reverse", 0, js_typed_array_reverse ),
JS_CFUNC_DEF("toReversed", 0, js_typed_array_toReversed ), JS_CFUNC_DEF("toReversed", 0, js_typed_array_toReversed ),
JS_CFUNC_DEF("slice", 2, js_typed_array_slice ), JS_CFUNC_DEF("slice", 2, js_typed_array_slice ),