Skip to content

Commit ce6b6dc

Browse files
committed
Use more explicit magic values for array methods
1 parent c0e67c4 commit ce6b6dc

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

quickjs.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39763,10 +39763,10 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val,
3976339763
}
3976439764

3976539765
enum {
39766-
special_find,
39767-
special_findIndex,
39768-
special_findLast,
39769-
special_findLastIndex,
39766+
ArrayFind,
39767+
ArrayFindIndex,
39768+
ArrayFindLast,
39769+
ArrayFindLastIndex,
3977039770
};
3977139771

3977239772
static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
@@ -39792,14 +39792,13 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
3979239792
if (argc > 1)
3979339793
this_arg = argv[1];
3979439794

39795-
if (mode == special_findLast || mode == special_findLastIndex) {
39795+
k = 0;
39796+
dir = 1;
39797+
end = len;
39798+
if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
3979639799
k = len - 1;
3979739800
dir = -1;
3979839801
end = -1;
39799-
} else {
39800-
k = 0;
39801-
dir = 1;
39802-
end = len;
3980339802
}
3980439803

3980539804
// TODO(bnoordhuis) add fast path for fast arrays
@@ -39817,7 +39816,7 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
3981739816
if (JS_IsException(res))
3981839817
goto exception;
3981939818
if (JS_ToBoolFree(ctx, res)) {
39820-
if (mode == special_findIndex || mode == special_findLastIndex) {
39819+
if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
3982139820
JS_FreeValue(ctx, val);
3982239821
JS_FreeValue(ctx, obj);
3982339822
return index_val;
@@ -39831,7 +39830,7 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
3983139830
JS_FreeValue(ctx, index_val);
3983239831
}
3983339832
JS_FreeValue(ctx, obj);
39834-
if (mode == special_findIndex || mode == special_findLastIndex)
39833+
if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
3983539834
return JS_NewInt32(ctx, -1);
3983639835
else
3983739836
return JS_UNDEFINED;
@@ -40858,10 +40857,10 @@ static const JSCFunctionListEntry js_array_proto_funcs[] = {
4085840857
JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce ),
4085940858
JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight ),
4086040859
JS_CFUNC_DEF("fill", 1, js_array_fill ),
40861-
JS_CFUNC_MAGIC_DEF("find", 1, js_array_find, special_find ),
40862-
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_array_find, special_findIndex ),
40863-
JS_CFUNC_MAGIC_DEF("findLast", 1, js_array_find, special_findLast ),
40864-
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_array_find, special_findLastIndex ),
40860+
JS_CFUNC_MAGIC_DEF("find", 1, js_array_find, ArrayFind ),
40861+
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_array_find, ArrayFindIndex ),
40862+
JS_CFUNC_MAGIC_DEF("findLast", 1, js_array_find, ArrayFindLast ),
40863+
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_array_find, ArrayFindLastIndex ),
4086540864
JS_CFUNC_DEF("indexOf", 1, js_array_indexOf ),
4086640865
JS_CFUNC_DEF("lastIndexOf", 1, js_array_lastIndexOf ),
4086740866
JS_CFUNC_DEF("includes", 1, js_array_includes ),
@@ -53909,14 +53908,13 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
5390953908
if (argc > 1)
5391053909
this_arg = argv[1];
5391153910

53912-
if (mode == special_findLast || mode == special_findLastIndex) {
53911+
k = 0;
53912+
dir = 1;
53913+
end = len;
53914+
if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
5391353915
k = len - 1;
5391453916
dir = -1;
5391553917
end = -1;
53916-
} else {
53917-
k = 0;
53918-
dir = 1;
53919-
end = len;
5392053918
}
5392153919

5392253920
for(; k != end; k += dir) {
@@ -53931,7 +53929,7 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
5393153929
if (JS_IsException(res))
5393253930
goto exception;
5393353931
if (JS_ToBoolFree(ctx, res)) {
53934-
if (mode == special_findIndex || mode == special_findLastIndex) {
53932+
if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
5393553933
JS_FreeValue(ctx, val);
5393653934
return index_val;
5393753935
} else {
@@ -53940,7 +53938,7 @@ static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
5394053938
}
5394153939
JS_FreeValue(ctx, val);
5394253940
}
53943-
if (mode == special_findIndex || mode == special_findLastIndex)
53941+
if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
5394453942
return JS_NewInt32(ctx, -1);
5394553943
else
5394653944
return JS_UNDEFINED;
@@ -54784,10 +54782,10 @@ static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
5478454782
JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce | special_TA ),
5478554783
JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight | special_TA ),
5478654784
JS_CFUNC_DEF("fill", 1, js_typed_array_fill ),
54787-
JS_CFUNC_MAGIC_DEF("find", 1, js_typed_array_find, special_find ),
54788-
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_typed_array_find, special_findIndex ),
54789-
JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, special_findLast ),
54790-
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, special_findLastIndex ),
54785+
JS_CFUNC_MAGIC_DEF("find", 1, js_typed_array_find, ArrayFind ),
54786+
JS_CFUNC_MAGIC_DEF("findIndex", 1, js_typed_array_find, ArrayFindIndex ),
54787+
JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, ArrayFindLast ),
54788+
JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, ArrayFindLastIndex ),
5479154789
JS_CFUNC_DEF("reverse", 0, js_typed_array_reverse ),
5479254790
JS_CFUNC_DEF("toReversed", 0, js_typed_array_toReversed ),
5479354791
JS_CFUNC_DEF("slice", 2, js_typed_array_slice ),

0 commit comments

Comments
 (0)