This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to branch update-quickjs-2026-09-26 in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 7d00d0d49a277652233926aa2ddd1b11f2e666ec Author: Nick Vatamaniuc <[email protected]> AuthorDate: Sat Sep 26 12:52:48 2026 -0400 Update QuickJS * Fix memory leaks in Atomics.store https://github.com/bellard/quickjs/commit/c8ef88981b9ef4e791ea0e4c7fc7a0687ed3bc1c * Reject out-of-range limits on Iterator.prototype.take/drop https://github.com/bellard/quickjs/commit/d88b1ed67e83e294b1c5f30453022689e61fed1d * Fix BigInt.AsUintN() https://github.com/bellard/quickjs/commit/c17f7fc1db6ee691bb73cb3aad82ea4c08ba66b8 * Fix regex unicode set parsing https://github.com/bellard/quickjs/commit/a479eff9e24a6608a7c587c6b3a5a3ac690b26f1 * Some C API additions, we don't use them https://github.com/bellard/quickjs/commit/87dc7cbaa8b50fabf7f1432101d68bfb71156964 https://github.com/bellard/quickjs/commit/21cbd86b016c925e6b01a7e0ad2cfa7889142290 * Test262 update, we had to update our patch for test262 https://github.com/bellard/quickjs/commit/f835c1d76204ab688b1a72cca5422a4e394fed2b https://github.com/bellard/quickjs/commit/a38171d37357edff256c28a73eaeb41d2466d1d5 --- .../patches/01-spidermonkey-185-mode.patch | 6 +- src/couch_quickjs/patches/02-test262-errors.patch | 13 +- src/couch_quickjs/quickjs/Makefile | 6 +- src/couch_quickjs/quickjs/libregexp.c | 4 +- src/couch_quickjs/quickjs/quickjs.c | 657 +++++++++++++++------ src/couch_quickjs/quickjs/quickjs.h | 2 + src/couch_quickjs/quickjs/run-test262.c | 154 +++-- src/couch_quickjs/quickjs/test262.conf | 4 + src/couch_quickjs/quickjs/test262_errors.txt | 16 +- 9 files changed, 609 insertions(+), 253 deletions(-) diff --git a/src/couch_quickjs/patches/01-spidermonkey-185-mode.patch b/src/couch_quickjs/patches/01-spidermonkey-185-mode.patch index 19e9316f9..6a44d2f3d 100644 --- a/src/couch_quickjs/patches/01-spidermonkey-185-mode.patch +++ b/src/couch_quickjs/patches/01-spidermonkey-185-mode.patch @@ -1,6 +1,6 @@ ---- quickjs-master/quickjs.c 2026-06-04 08:53:29 -+++ quickjs/quickjs.c 2026-06-04 13:06:09 -@@ -31922,10 +31922,24 @@ +--- quickjs-master/quickjs.c 2026-09-26 10:44:20.000000000 -0400 ++++ quickjs/quickjs.c 2026-09-26 12:50:22.209803231 -0400 +@@ -32088,10 +32088,24 @@ if (s->token.val == TOK_FUNCTION || (token_is_pseudo_keyword(s, JS_ATOM_async) && peek_token(s, TRUE) == TOK_FUNCTION)) { diff --git a/src/couch_quickjs/patches/02-test262-errors.patch b/src/couch_quickjs/patches/02-test262-errors.patch index a41cc408f..c9b000940 100644 --- a/src/couch_quickjs/patches/02-test262-errors.patch +++ b/src/couch_quickjs/patches/02-test262-errors.patch @@ -1,11 +1,10 @@ ---- quickjs-master/test262_errors.txt 2026-06-04 08:53:29 -+++ quickjs/test262_errors.txt 2026-06-04 13:06:09 -@@ -23,6 +23,8 @@ - test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from-and-import-star-as-and-export.js:74: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js' is ambiguous +--- quickjs-master/test262_errors.txt 2026-09-26 10:44:20.000000000 -0400 ++++ quickjs/test262_errors.txt 2026-09-26 12:50:22.212803072 -0400 +@@ -26,6 +26,7 @@ test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from.js:75: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from.js' is ambiguous test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js:74: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js' is ambiguous -+test262/test/language/statements/expression/S12.4_A1.js:15: unexpected error type: Test262: This statement should not be evaluated. -+test262/test/language/statements/expression/S12.4_A1.js:15: strict mode: unexpected error type: Test262: This statement should not be evaluated. + test262/test/language/module-code/namespace/internals/super-access-to-tdz-binding.js:32: Test262Error: Expected a ReferenceError but got a TypeError ++test262/test/language/statements/expression/S12.4_A1.js:15: unexpected error type: Test262: This statement should not be evaluated test262/test/staging/sm/Function/arguments-parameter-shadowing.js:14: Test262Error: Expected SameValue(«true», «false») to be true test262/test/staging/sm/Function/constructor-binding.js:11: Test262Error: Expected SameValue(«"function"», «"undefined"») to be true - test262/test/staging/sm/Function/constructor-binding.js:11: strict mode: Test262Error: Expected SameValue(«"function"», «"undefined"») to be true + test262/test/staging/sm/Function/function-bind.js:24: Test262Error: Conforms to NativeFunction Syntax: "function bound unbound() {\n [native code]\n}" diff --git a/src/couch_quickjs/quickjs/Makefile b/src/couch_quickjs/quickjs/Makefile index 61e70a57a..93d41aebe 100644 --- a/src/couch_quickjs/quickjs/Makefile +++ b/src/couch_quickjs/quickjs/Makefile @@ -57,7 +57,7 @@ PREFIX?=/usr/local #CONFIG_TSAN=y # TEST262 bootstrap config: commit id and shallow "since" parameter -TEST262_COMMIT?=5c8206929d81b2d3d727ca6aac56c18358c8d790 +TEST262_COMMIT?=7ab7fafa0003f73fc85c1b95d88094d33f7eb8bd TEST262_SINCE?=2025-09-01 OBJDIR=.obj @@ -493,7 +493,7 @@ test2o: run-test262 time ./run-test262 -t -m -c test262o.conf test2o-update: run-test262 - ./run-test262 -t -u -c test262o.conf -T 1 + ./run-test262 -t -u -c test262o.conf endif ifeq ($(wildcard test262/features.txt),) @@ -508,7 +508,7 @@ test2: run-test262 time ./run-test262 -t -m -c test262.conf -a test2-update: run-test262 - ./run-test262 -t -u -c test262.conf -a -T 1 + ./run-test262 -t -u -c test262.conf -a test2-check: run-test262 time ./run-test262 -t -m -c test262.conf -E -a diff --git a/src/couch_quickjs/quickjs/libregexp.c b/src/couch_quickjs/quickjs/libregexp.c index c387f0043..b231e9534 100644 --- a/src/couch_quickjs/quickjs/libregexp.c +++ b/src/couch_quickjs/quickjs/libregexp.c @@ -1183,7 +1183,7 @@ static int get_class_atom(REParseState *s, REStringList *cr, case '^': case '`': case '~': - if (s->unicode_sets && p[1] == c) { + if (inclass && s->unicode_sets && p[1] == c) { /* forbidden double characters */ return re_parse_error(s, "invalid class set operation in regular expression"); } @@ -1198,7 +1198,7 @@ static int get_class_atom(REParseState *s, REStringList *cr, case '/': case '-': case '|': - if (s->unicode_sets) { + if (inclass && s->unicode_sets) { /* invalid characters in unicode sets */ return re_parse_error(s, "invalid character in class in regular expression"); } diff --git a/src/couch_quickjs/quickjs/quickjs.c b/src/couch_quickjs/quickjs/quickjs.c index 40bb08c1b..4be06ff93 100644 --- a/src/couch_quickjs/quickjs/quickjs.c +++ b/src/couch_quickjs/quickjs/quickjs.c @@ -1265,7 +1265,7 @@ typedef enum JSStrictEqModeEnum { JS_EQ_SAME_VALUE_ZERO, } JSStrictEqModeEnum; -static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, +static BOOL js_strict_eq2(JSContext *ctx, JSValueConst op1, JSValueConst op2, JSStrictEqModeEnum eq_mode); static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2); static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2); @@ -3514,7 +3514,7 @@ static JSAtom JS_NewAtomInt64(JSContext *ctx, int64_t n) } /* 'p' is freed */ -static JSValue JS_NewSymbol(JSContext *ctx, JSString *p, int atom_type) +static JSValue JS_NewSymbolInternal(JSContext *ctx, JSString *p, int atom_type) { JSRuntime *rt = ctx->rt; JSAtom atom; @@ -3524,6 +3524,28 @@ static JSValue JS_NewSymbol(JSContext *ctx, JSString *p, int atom_type) return JS_MKPTR(JS_TAG_SYMBOL, rt->atom_array[atom]); } +/* description is UTF-8 encoded or NULL */ +JSValue JS_NewSymbol(JSContext *ctx, const char *description, BOOL is_global) +{ + JSValue str; + int atom_type; + + if (description == NULL) { + if (!is_global) { + /* Local symbol without description: Symbol() */ + return JS_NewSymbolInternal(ctx, NULL, JS_ATOM_TYPE_SYMBOL); + } + /* Global symbol without description: Symbol.for() + Per ES spec, ToString(undefined) becomes "undefined" */ + description = "undefined"; + } + str = JS_NewString(ctx, description); + if (JS_IsException(str)) + return JS_EXCEPTION; + atom_type = is_global ? JS_ATOM_TYPE_GLOBAL_SYMBOL : JS_ATOM_TYPE_SYMBOL; + return JS_NewSymbolInternal(ctx, JS_VALUE_GET_STRING(str), atom_type); +} + /* descr must be a non-numeric string atom */ static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr, int atom_type) @@ -3535,7 +3557,7 @@ static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr, assert(descr < rt->atom_size); p = rt->atom_array[descr]; JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p)); - return JS_NewSymbol(ctx, p, atom_type); + return JS_NewSymbolInternal(ctx, p, atom_type); } #define ATOM_GET_STR_BUF_SIZE 64 @@ -13188,19 +13210,29 @@ int JS_ToInt32Clamp(JSContext *ctx, int *pres, JSValueConst val, return res; } -static int JS_ToInt64SatFree(JSContext *ctx, int64_t *pres, JSValue val) +#define JS_TO_INT64_SAT_INF 1 /* result was +/-Infinity */ +#define JS_TO_INT64_SAT_NAN 2 /* result was NaN */ + +static int JS_ToInt64SatFree(JSContext *ctx, int64_t *pres, JSValue val, + BOOL ret_flags) { uint32_t tag; - + int ret; + redo: tag = JS_VALUE_GET_NORM_TAG(val); switch(tag) { case JS_TAG_INT: case JS_TAG_BOOL: case JS_TAG_NULL: - case JS_TAG_UNDEFINED: *pres = JS_VALUE_GET_INT(val); return 0; + case JS_TAG_UNDEFINED: + *pres = 0; + if (ret_flags) + return JS_TO_INT64_SAT_NAN; + else + return 0; case JS_TAG_EXCEPTION: *pres = 0; return -1; @@ -13209,16 +13241,26 @@ static int JS_ToInt64SatFree(JSContext *ctx, int64_t *pres, JSValue val) double d = JS_VALUE_GET_FLOAT64(val); if (isnan(d)) { *pres = 0; + ret = JS_TO_INT64_SAT_NAN; } else { - if (d < INT64_MIN) + ret = 0; + if (d < INT64_MIN) { *pres = INT64_MIN; - else if (d >= 0x1p63) /* must use INT64_MAX + 1 because INT64_MAX cannot be exactly represented as a double */ + if (!isfinite(d)) + ret = JS_TO_INT64_SAT_INF; + } else if (d >= 0x1p63) { /* must use INT64_MAX + 1 because INT64_MAX cannot be exactly represented as a double */ *pres = INT64_MAX; - else + if (!isfinite(d)) + ret = JS_TO_INT64_SAT_INF; + } else { *pres = (int64_t)d; + } } } - return 0; + if (ret_flags) + return ret; + else + return 0; default: val = JS_ToNumberFree(ctx, val); if (JS_IsException(val)) { @@ -13231,13 +13273,20 @@ static int JS_ToInt64SatFree(JSContext *ctx, int64_t *pres, JSValue val) int JS_ToInt64Sat(JSContext *ctx, int64_t *pres, JSValueConst val) { - return JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val)); + return JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val), FALSE); +} + +/* same as JS_ToInt64Sat, but return additional flags */ +static int JS_ToInt64SatF(JSContext *ctx, int64_t *pres, JSValueConst val) +{ + return JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val), TRUE); } + int JS_ToInt64Clamp(JSContext *ctx, int64_t *pres, JSValueConst val, int64_t min, int64_t max, int64_t neg_offset) { - int res = JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val)); + int res = JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val), FALSE); if (res == 0) { if (*pres < 0) *pres += neg_offset; @@ -15642,12 +15691,16 @@ static no_inline __exception int js_eq_slow(JSContext *ctx, JSValue *sp, } } else if (tag1 == tag2) { res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT); + JS_FreeValue(ctx, op1); + JS_FreeValue(ctx, op2); } else if ((tag1 == JS_TAG_NULL && tag2 == JS_TAG_UNDEFINED) || (tag2 == JS_TAG_NULL && tag1 == JS_TAG_UNDEFINED)) { res = TRUE; } else if (tag_is_string(tag1) && tag_is_string(tag2)) { /* needed when comparing strings and ropes */ res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT); + JS_FreeValue(ctx, op1); + JS_FreeValue(ctx, op2); } else if ((tag_is_string(tag1) && tag_is_number(tag2)) || (tag_is_string(tag2) && tag_is_number(tag1))) { @@ -15683,6 +15736,8 @@ static no_inline __exception int js_eq_slow(JSContext *ctx, JSValue *sp, } } res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT); + JS_FreeValue(ctx, op1); + JS_FreeValue(ctx, op2); } else if (tag1 == JS_TAG_BOOL) { op1 = JS_NewInt32(ctx, JS_VALUE_GET_INT(op1)); goto redo; @@ -15764,8 +15819,7 @@ static no_inline int js_shr_slow(JSContext *ctx, JSValue *sp) return -1; } -/* XXX: Should take JSValueConst arguments */ -static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, +static BOOL js_strict_eq2(JSContext *ctx, JSValueConst op1, JSValueConst op2, JSStrictEqModeEnum eq_mode) { BOOL res; @@ -15780,7 +15834,6 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, res = FALSE; } else { res = JS_VALUE_GET_INT(op1) == JS_VALUE_GET_INT(op2); - goto done_no_free; } break; case JS_TAG_NULL: @@ -15856,7 +15909,7 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, } else { res = (d1 == d2); /* if NaN return false and +0 == -0 */ } - goto done_no_free; + break; case JS_TAG_SHORT_BIG_INT: case JS_TAG_BIG_INT: { @@ -15884,17 +15937,12 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2, res = FALSE; break; } - JS_FreeValue(ctx, op1); - JS_FreeValue(ctx, op2); - done_no_free: return res; } static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2) { - return js_strict_eq2(ctx, - JS_DupValue(ctx, op1), JS_DupValue(ctx, op2), - JS_EQ_STRICT); + return js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT); } BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2) @@ -15904,9 +15952,7 @@ BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2) static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2) { - return js_strict_eq2(ctx, - JS_DupValue(ctx, op1), JS_DupValue(ctx, op2), - JS_EQ_SAME_VALUE); + return js_strict_eq2(ctx, op1, op2, JS_EQ_SAME_VALUE); } BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2) @@ -15916,9 +15962,7 @@ BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2) static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2) { - return js_strict_eq2(ctx, - JS_DupValue(ctx, op1), JS_DupValue(ctx, op2), - JS_EQ_SAME_VALUE_ZERO); + return js_strict_eq2(ctx, op1, op2, JS_EQ_SAME_VALUE_ZERO); } BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2) @@ -15926,15 +15970,6 @@ BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2) return js_same_value_zero(ctx, op1, op2); } -static no_inline int js_strict_eq_slow(JSContext *ctx, JSValue *sp, - BOOL is_neq) -{ - BOOL res; - res = js_strict_eq2(ctx, sp[-2], sp[-1], JS_EQ_STRICT); - sp[-2] = JS_NewBool(ctx, res ^ is_neq); - return 0; -} - static __exception int js_operator_in(JSContext *ctx, JSValue *sp) { JSValue op1, op2; @@ -16809,18 +16844,6 @@ static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val, static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic); -static BOOL js_is_fast_array(JSContext *ctx, JSValueConst obj) -{ - /* Try and handle fast arrays explicitly */ - if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) { - JSObject *p = JS_VALUE_GET_OBJ(obj); - if (p->class_id == JS_CLASS_ARRAY && p->fast_array) { - return TRUE; - } - } - return FALSE; -} - /* Access an Array's internal JSValue array if available */ static BOOL js_get_fast_array(JSContext *ctx, JSValueConst obj, JSValue **arrpp, uint32_t *countp) @@ -20253,16 +20276,36 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj, BREAK; -#define OP_CMP(opcode, binary_op, slow_call) \ - CASE(opcode): \ - { \ - JSValue op1, op2; \ - op1 = sp[-2]; \ - op2 = sp[-1]; \ +#define OP_CMP(opcode, binary_op, slow_call) \ + CASE(opcode): \ + { \ + JSValue op1, op2; \ + op1 = sp[-2]; \ + op2 = sp[-1]; \ if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) { \ sp[-2] = JS_NewBool(ctx, JS_VALUE_GET_INT(op1) binary_op JS_VALUE_GET_INT(op2)); \ sp--; \ + } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) || \ + JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { \ + double d1, d2; \ + if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1))) { \ + d1 = JS_VALUE_GET_FLOAT64(op1); \ + } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) { \ + d1 = JS_VALUE_GET_INT(op1); \ + } else { \ + goto opcode ## _slow_case; \ + } \ + if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { \ + d2 = JS_VALUE_GET_FLOAT64(op2); \ + } else if (JS_VALUE_GET_TAG(op2) == JS_TAG_INT) { \ + d2 = JS_VALUE_GET_INT(op2); \ + } else { \ + goto opcode ## _slow_case; \ + } \ + sp[-2] = JS_NewBool(ctx, d1 binary_op d2); \ + sp--; \ } else { \ + opcode ## _slow_case: \ sf->cur_pc = pc; \ if (slow_call) \ goto exception; \ @@ -20275,10 +20318,133 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj, OP_CMP(OP_lte, <=, js_relational_slow(ctx, sp, opcode)); OP_CMP(OP_gt, >, js_relational_slow(ctx, sp, opcode)); OP_CMP(OP_gte, >=, js_relational_slow(ctx, sp, opcode)); - OP_CMP(OP_eq, ==, js_eq_slow(ctx, sp, 0)); - OP_CMP(OP_neq, !=, js_eq_slow(ctx, sp, 1)); - OP_CMP(OP_strict_eq, ==, js_strict_eq_slow(ctx, sp, 0)); - OP_CMP(OP_strict_neq, !=, js_strict_eq_slow(ctx, sp, 1)); + +#define OP_CMP_EQ(opcode, inv) \ + CASE(opcode): \ + { \ + JSValue op1, op2; \ + int res; \ + uint32_t tag1, tag2; \ + op1 = sp[-2]; \ + op2 = sp[-1]; \ + tag1 = JS_VALUE_GET_TAG(op1); \ + tag2 = JS_VALUE_GET_TAG(op2); \ + if (likely(tag1 == JS_TAG_INT)) { \ + if (tag2 == JS_TAG_INT) { \ + res = JS_VALUE_GET_INT(op1) == JS_VALUE_GET_INT(op2); \ + } else if (JS_TAG_IS_FLOAT64(tag2)) { \ + res = (JS_VALUE_GET_INT(op1) == JS_VALUE_GET_FLOAT64(op2)); \ + } else { \ + goto slow_eq ## inv; \ + } \ + } else if (JS_TAG_IS_FLOAT64(tag1)) { \ + if (tag2 == JS_TAG_INT) { \ + res = JS_VALUE_GET_FLOAT64(op1) == JS_VALUE_GET_INT(op2); \ + } else if (JS_TAG_IS_FLOAT64(tag2)) { \ + res = (JS_VALUE_GET_FLOAT64(op1) == JS_VALUE_GET_FLOAT64(op2)); \ + } else { \ + goto slow_eq ## inv; \ + } \ + } else if (tag1 == JS_TAG_OBJECT) { \ + if (tag2 == JS_TAG_NULL || tag2 == JS_TAG_UNDEFINED) { \ + JSObject *p = JS_VALUE_GET_OBJ(op1); \ + res = p->is_HTMLDDA; \ + JS_FreeValue(ctx, op1); \ + } else if (tag2 == JS_TAG_OBJECT) { \ + res = JS_VALUE_GET_OBJ(op1) == JS_VALUE_GET_OBJ(op2); \ + JS_FreeValue(ctx, op1); \ + JS_FreeValue(ctx, op2); \ + } else { \ + goto slow_eq ## inv; \ + } \ + } else if (tag1 == JS_TAG_NULL || tag1 == JS_TAG_UNDEFINED) { \ + if (tag2 == JS_TAG_NULL || tag2 == JS_TAG_UNDEFINED) { \ + res = TRUE; \ + } else if (tag2 == JS_TAG_OBJECT) { \ + JSObject *p = JS_VALUE_GET_OBJ(op2); \ + res = p->is_HTMLDDA; \ + JS_FreeValue(ctx, op2); \ + } else { \ + goto slow_eq ## inv; \ + } \ + } else if (tag1 == JS_TAG_STRING && tag2 == JS_TAG_STRING) { \ + res = js_string_eq(ctx, JS_VALUE_GET_STRING(op1), \ + JS_VALUE_GET_STRING(op2)); \ + JS_FreeValue(ctx, op1); \ + JS_FreeValue(ctx, op2); \ + } else { \ + slow_eq ## inv: \ + sf->cur_pc = pc; \ + if (js_eq_slow(ctx, sp, inv)) \ + goto exception; \ + sp--; \ + goto slow_eq_done ## inv; \ + } \ + sp[-2] = JS_NewBool(ctx, res ^ inv); \ + sp--; \ + slow_eq_done ## inv: ; \ + } \ + BREAK + + OP_CMP_EQ(OP_eq, 0); + OP_CMP_EQ(OP_neq, 1); + +#define OP_CMP_STRICT_EQ(opcode, inv) \ + CASE(opcode): \ + { \ + JSValue op1, op2; \ + int res; \ + uint32_t tag1, tag2; \ + op1 = sp[-2]; \ + op2 = sp[-1]; \ + tag1 = JS_VALUE_GET_TAG(op1); \ + tag2 = JS_VALUE_GET_TAG(op2); \ + if (likely(tag1 == JS_TAG_INT)) { \ + if (tag2 == JS_TAG_INT) { \ + res = JS_VALUE_GET_INT(op1) == JS_VALUE_GET_INT(op2); \ + } else if (JS_TAG_IS_FLOAT64(tag2)) { \ + res = (JS_VALUE_GET_INT(op1) == JS_VALUE_GET_FLOAT64(op2)); \ + } else { \ + JS_FreeValue(ctx, op2); \ + res = FALSE; \ + } \ + } else if (JS_TAG_IS_FLOAT64(tag1)) { \ + if (tag2 == JS_TAG_INT) { \ + res = JS_VALUE_GET_FLOAT64(op1) == JS_VALUE_GET_INT(op2); \ + } else if (JS_TAG_IS_FLOAT64(tag2)) { \ + res = (JS_VALUE_GET_FLOAT64(op1) == JS_VALUE_GET_FLOAT64(op2)); \ + } else { \ + JS_FreeValue(ctx, op2); \ + res = FALSE; \ + } \ + } else if (tag1 == JS_TAG_OBJECT) { \ + if (tag2 == JS_TAG_OBJECT) { \ + res = JS_VALUE_GET_OBJ(op1) == JS_VALUE_GET_OBJ(op2); \ + } else { \ + res = FALSE; \ + } \ + JS_FreeValue(ctx, op1); \ + JS_FreeValue(ctx, op2); \ + } else if (tag1 == JS_TAG_NULL || tag1 == JS_TAG_UNDEFINED) { \ + res = (tag1 == tag2); \ + JS_FreeValue(ctx, op2); \ + } else if (tag1 == JS_TAG_STRING && tag2 == JS_TAG_STRING) { \ + res = js_string_eq(ctx, JS_VALUE_GET_STRING(op1), \ + JS_VALUE_GET_STRING(op2)); \ + JS_FreeValue(ctx, op1); \ + JS_FreeValue(ctx, op2); \ + } else { \ + res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT); \ + JS_FreeValue(ctx, op1); \ + JS_FreeValue(ctx, op2); \ + } \ + sp[-2] = JS_NewBool(ctx, res ^ inv); \ + sp--; \ + } \ + BREAK + + OP_CMP_STRICT_EQ(OP_strict_eq, 0); + OP_CMP_STRICT_EQ(OP_strict_neq, 1); CASE(OP_in): sf->cur_pc = pc; @@ -41754,10 +41920,10 @@ static JSValue js_get_this(JSContext *ctx, return JS_DupValue(ctx, this_val); } -static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj, - JSValueConst len_val) +/* XXX: optimize */ +static JSValue JS_ArraySpeciesGetCtor(JSContext *ctx, JSValueConst obj) { - JSValue ctor, ret, species; + JSValue ctor, species; int res; JSContext *realm; @@ -41765,7 +41931,7 @@ static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj, if (res < 0) return JS_EXCEPTION; if (!res) - return js_array_constructor(ctx, JS_UNDEFINED, 1, &len_val); + return JS_UNDEFINED; ctor = JS_GetProperty(ctx, obj, JS_ATOM_constructor); if (JS_IsException(ctor)) return ctor; @@ -41791,13 +41957,39 @@ static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj, if (JS_IsNull(ctor)) ctor = JS_UNDEFINED; } + if (!JS_IsUndefined(ctor) && + js_same_value(ctx, ctor, ctx->array_ctor)) { + JS_FreeValue(ctx, ctor); + ctor = JS_UNDEFINED; + } + return ctor; +} + +static JSValue JS_ArrayCreateFromCtor(JSContext *ctx, JSValueConst ctor, int64_t len) +{ + JSValue len_val, ret; + + len_val = JS_NewInt64(ctx, len); if (JS_IsUndefined(ctor)) { - return js_array_constructor(ctx, JS_UNDEFINED, 1, &len_val); + ret = js_array_constructor(ctx, JS_UNDEFINED, 1, (JSValueConst *)&len_val); } else { - ret = JS_CallConstructor(ctx, ctor, 1, &len_val); - JS_FreeValue(ctx, ctor); - return ret; + ret = JS_CallConstructor(ctx, ctor, 1, (JSValueConst *)&len_val); } + JS_FreeValue(ctx, len_val); + return ret; +} + +/* len must be >= 0 */ +static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj, int64_t len) +{ + JSValue ctor, ret; + + ctor = JS_ArraySpeciesGetCtor(ctx, obj); + if (JS_IsException(ctor)) + return ctor; + ret = JS_ArrayCreateFromCtor(ctx, ctor, len); + JS_FreeValue(ctx, ctor); + return ret; } static const JSCFunctionListEntry js_array_funcs[] = { @@ -41927,7 +42119,7 @@ static JSValue js_array_concat(JSContext *ctx, JSValueConst this_val, if (JS_IsException(obj)) goto exception; - arr = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0)); + arr = JS_ArraySpeciesCreate(ctx, obj, 0); if (JS_IsException(arr)) goto exception; n = 0; @@ -42030,13 +42222,12 @@ static JSValue js_array_every(JSContext *ctx, JSValueConst this_val, ret = JS_FALSE; break; case special_map: - /* XXX: JS_ArraySpeciesCreate should take int64_t */ - ret = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt64(ctx, len)); + ret = JS_ArraySpeciesCreate(ctx, obj, len); if (JS_IsException(ret)) goto exception; break; case special_filter: - ret = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0)); + ret = JS_ArraySpeciesCreate(ctx, obj, 0); if (JS_IsException(ret)) goto exception; break; @@ -42297,8 +42488,7 @@ static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val, } if (js_get_fast_array(ctx, obj, &arrp, &count)) { for (; n < count; n++) { - if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), - JS_DupValue(ctx, arrp[n]), + if (js_strict_eq2(ctx, argv[0], arrp[n], JS_EQ_SAME_VALUE_ZERO)) { res = TRUE; goto done; @@ -42309,11 +42499,13 @@ static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val, val = JS_GetPropertyInt64(ctx, obj, n); if (JS_IsException(val)) goto exception; - if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val, + if (js_strict_eq2(ctx, argv[0], val, JS_EQ_SAME_VALUE_ZERO)) { + JS_FreeValue(ctx, val); res = TRUE; break; } + JS_FreeValue(ctx, val); } } done: @@ -42346,8 +42538,7 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValueConst this_val, } if (js_get_fast_array(ctx, obj, &arrp, &count)) { for (; n < count; n++) { - if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), - JS_DupValue(ctx, arrp[n]), JS_EQ_STRICT)) { + if (js_strict_eq2(ctx, argv[0], arrp[n], JS_EQ_STRICT)) { res = n; goto done; } @@ -42358,10 +42549,12 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValueConst this_val, if (present < 0) goto exception; if (present) { - if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val, JS_EQ_STRICT)) { + if (js_strict_eq2(ctx, argv[0], val, JS_EQ_STRICT)) { + JS_FreeValue(ctx, val); res = n; break; } + JS_FreeValue(ctx, val); } } } @@ -42398,10 +42591,12 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val, if (present < 0) goto exception; if (present) { - if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val, JS_EQ_STRICT)) { + if (js_strict_eq2(ctx, argv[0], val, JS_EQ_STRICT)) { + JS_FreeValue(ctx, val); res = n; break; } + JS_FreeValue(ctx, val); } } } @@ -42804,13 +42999,13 @@ exception: } static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv, int splice) + int argc, JSValueConst *argv) { - JSValue obj, arr, val, len_val; - int64_t len, start, k, final, n, count, del_count, new_len; + JSValue obj, arr, val, ctor; + int64_t len, start, k, final, n, count; int kPresent; JSValue *arrp; - uint32_t count32, i, item_count; + uint32_t count32; arr = JS_UNDEFINED; obj = JS_ToObject(ctx, this_val); @@ -42820,69 +43015,150 @@ static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val, if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len)) goto exception; - if (splice) { - if (argc == 0) { - item_count = 0; - del_count = 0; - } else - if (argc == 1) { - item_count = 0; - del_count = len - start; - } else { - item_count = argc - 2; - if (JS_ToInt64Clamp(ctx, &del_count, argv[1], 0, len - start, 0)) - goto exception; - } - if (len + item_count - del_count > MAX_SAFE_INTEGER) { - JS_ThrowTypeError(ctx, "Array loo long"); + final = len; + if (!JS_IsUndefined(argv[1])) { + if (JS_ToInt64Clamp(ctx, &final, argv[1], 0, len, len)) goto exception; - } - count = del_count; - } else { - item_count = 0; /* avoid warning */ - final = len; - if (!JS_IsUndefined(argv[1])) { - if (JS_ToInt64Clamp(ctx, &final, argv[1], 0, len, len)) - goto exception; - } - count = max_int64(final - start, 0); } - len_val = JS_NewInt64(ctx, count); - arr = JS_ArraySpeciesCreate(ctx, obj, len_val); - JS_FreeValue(ctx, len_val); - if (JS_IsException(arr)) + count = max_int64(final - start, 0); + + ctor = JS_ArraySpeciesGetCtor(ctx, obj); + if (JS_IsException(ctor)) goto exception; - k = start; final = start + count; - n = 0; - /* The fast array test on arr ensures that - JS_CreateDataPropertyUint32() won't modify obj in case arr is - an exotic object */ - /* Special case fast arrays */ - if (js_get_fast_array(ctx, obj, &arrp, &count32) && - js_is_fast_array(ctx, arr)) { - /* XXX: should share code with fast array constructor */ - for (; k < final && k < count32; k++, n++) { - if (JS_CreateDataPropertyUint32(ctx, arr, n, JS_DupValue(ctx, arrp[k]), JS_PROP_THROW) < 0) + if (JS_IsUndefined(ctor) && + js_get_fast_array(ctx, obj, &arrp, &count32) && + final <= count32) { + /* fast case */ + arr = js_create_array(ctx, count, (JSValueConst *)arrp + start); + } else { + arr = JS_ArrayCreateFromCtor(ctx, ctor, count); + JS_FreeValue(ctx, ctor); + if (JS_IsException(arr)) + goto exception; + + n = 0; + for (k = start; k < final; k++, n++) { + kPresent = JS_TryGetPropertyInt64(ctx, obj, k, &val); + if (kPresent < 0) goto exception; + if (kPresent) { + if (JS_CreateDataPropertyUint32(ctx, arr, n, val, JS_PROP_THROW) < 0) + goto exception; + } } + if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0) + goto exception; } - /* Copy the remaining elements if any (handle case of inherited properties) */ - for (; k < final; k++, n++) { - kPresent = JS_TryGetPropertyInt64(ctx, obj, k, &val); - if (kPresent < 0) + JS_FreeValue(ctx, obj); + return arr; + + exception: + JS_FreeValue(ctx, obj); + JS_FreeValue(ctx, arr); + return JS_EXCEPTION; +} + +static JSValue js_array_splice(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue obj, arr, val, ctor; + int64_t len, start, k, final, n, del_count, new_len; + int kPresent; + uint32_t i, item_count; + JSObject *p; + + arr = JS_UNDEFINED; + obj = JS_ToObject(ctx, this_val); + if (js_get_length64(ctx, &len, obj)) + goto exception; + + if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len)) + goto exception; + + if (argc == 0) { + item_count = 0; + del_count = 0; + } else if (argc == 1) { + item_count = 0; + del_count = len - start; + } else { + item_count = argc - 2; + if (JS_ToInt64Clamp(ctx, &del_count, argv[1], 0, len - start, 0)) goto exception; - if (kPresent) { - if (JS_CreateDataPropertyUint32(ctx, arr, n, val, JS_PROP_THROW) < 0) - goto exception; - } } - if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0) + if (len + item_count - del_count > MAX_SAFE_INTEGER) { + JS_ThrowTypeError(ctx, "Array loo long"); + goto exception; + } + final = start + del_count; + /* warning: 'len' may be different from the actual array length + because it may have been modified */ + new_len = len + item_count - del_count; + + ctor = JS_ArraySpeciesGetCtor(ctx, obj); + if (JS_IsException(ctor)) goto exception; - if (splice) { - new_len = len + item_count - del_count; + p = JS_VALUE_GET_PTR(obj); + if (JS_IsUndefined(ctor) && + p->class_id == JS_CLASS_ARRAY && + p->fast_array && + final <= p->u.array.count && + (get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE) && /* writable array length */ + can_extend_fast_array(p)) { + uint32_t count32 = p->u.array.count; + JSValue *arrp = p->u.array.u.values; + + /* fast case */ + arr = js_create_array(ctx, del_count, (JSValueConst *)arrp + start); + if (JS_IsException(arr)) + goto exception; + + if (item_count != del_count) { + /* resize */ + uint32_t new_count32; + new_count32 = count32 + item_count - del_count; + if (del_count > item_count) { + for(i = 0; i < del_count - item_count; i++) + JS_FreeValue(ctx, arrp[start + item_count + i]); + memmove(arrp + start + item_count, arrp + final, + (count32 - final) * sizeof(arrp[0])); + } else { + if (unlikely(new_count32 > p->u.array.u1.size)) { + if (expand_fast_array(ctx, p, new_count32)) + goto exception; + arrp = p->u.array.u.values; + } + memmove(arrp + start + item_count, arrp + final, + (count32 - final) * sizeof(arrp[0])); + for(i = 0; i < item_count - del_count; i++) + arrp[start + del_count + i] = JS_UNDEFINED; + } + p->u.array.count = new_count32; + } + for(i = 0; i < item_count; i++) + set_value(ctx, &arrp[start + i], JS_DupValue(ctx, argv[i + 2])); + } else { + arr = JS_ArrayCreateFromCtor(ctx, ctor, del_count); + JS_FreeValue(ctx, ctor); + if (JS_IsException(arr)) + goto exception; + + n = 0; + for (k = start; k < final; k++, n++) { + kPresent = JS_TryGetPropertyInt64(ctx, obj, k, &val); + if (kPresent < 0) + goto exception; + if (kPresent) { + if (JS_CreateDataPropertyUint32(ctx, arr, n, val, JS_PROP_THROW) < 0) + goto exception; + } + } + if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0) + goto exception; + if (item_count != del_count) { if (JS_CopySubArray(ctx, obj, start + item_count, start + del_count, len - (start + del_count), @@ -42898,9 +43174,9 @@ static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val, if (JS_SetPropertyInt64(ctx, obj, start + i, JS_DupValue(ctx, argv[i + 2])) < 0) goto exception; } - if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, new_len)) < 0) - goto exception; } + if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, new_len)) < 0) + goto exception; JS_FreeValue(ctx, obj); return arr; @@ -43116,7 +43392,7 @@ static JSValue js_array_flatten(JSContext *ctx, JSValueConst this_val, goto exception; } } - arr = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0)); + arr = JS_ArraySpeciesCreate(ctx, obj, 0); if (JS_IsException(arr)) goto exception; if (JS_FlattenIntoArray(ctx, arr, obj, sourceLen, 0, depthNum, @@ -43855,35 +44131,19 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, case JS_ITERATOR_HELPER_KIND_DROP: case JS_ITERATOR_HELPER_KIND_TAKE: { - JSValue v; - double dlimit; - v = JS_ToNumber(ctx, argv[0]); - if (JS_IsException(v)) - goto fail; - // Check for Infinity. - if (JS_ToFloat64(ctx, &dlimit, v)) { - JS_FreeValue(ctx, v); + int ret; + ret = JS_ToInt64SatF(ctx, &count, argv[0]); + if (ret < 0) goto fail; - } - if (isnan(dlimit)) { - JS_FreeValue(ctx, v); + if (ret == JS_TO_INT64_SAT_NAN || count < 0) goto range_error; - } - if (!isfinite(dlimit)) { - JS_FreeValue(ctx, v); - if (dlimit < 0) + if (count > MAX_SAFE_INTEGER) { + /* XXX: not strictly compliant e.g. for 2**31-1 + 0.5 */ + if (ret != JS_TO_INT64_SAT_INF) goto range_error; else count = MAX_SAFE_INTEGER; - } else { - v = JS_ToIntegerFree(ctx, v); - if (JS_IsException(v)) - goto fail; - if (JS_ToInt64Free(ctx, &count, v)) - goto fail; } - if (count < 0) - goto range_error; } break; case JS_ITERATOR_HELPER_KIND_FILTER: @@ -44579,8 +44839,8 @@ static const JSCFunctionListEntry js_array_proto_funcs[] = { 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("slice", 2, js_array_slice ), + JS_CFUNC_DEF("splice", 2, js_array_splice ), 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 ), @@ -46162,27 +46422,30 @@ static JSValue js_string_repeat(JSContext *ctx, JSValueConst this_val, StringBuffer b_s, *b = &b_s; JSString *p; int64_t val; - int n, len; + int n, len, ret; str = JS_ToStringCheckObject(ctx, this_val); if (JS_IsException(str)) goto fail; - if (JS_ToInt64Sat(ctx, &val, argv[0])) + ret = JS_ToInt64SatF(ctx, &val, argv[0]); + if (ret < 0) goto fail; - if (val < 0 || val > 2147483647) { + if (val < 0 || ret == JS_TO_INT64_SAT_INF) { JS_ThrowRangeError(ctx, "invalid repeat count"); goto fail; } - n = val; p = JS_VALUE_GET_STRING(str); len = p->len; - if (len == 0 || n == 1) + if (len == 0 || val == 1) return str; - // XXX: potential arithmetic overflow + if (val > INT32_MAX) + goto string_too_long; if (val * len > JS_STRING_LEN_MAX) { - JS_ThrowRangeError(ctx, "invalid string length"); + string_too_long: + JS_ThrowRangeError(ctx, "string too long"); goto fail; } + n = val; if (string_buffer_init2(ctx, b, n * len, p->is_wide_char)) goto fail; if (len == 1) { @@ -51368,7 +51631,7 @@ static JSValue js_symbol_constructor(JSContext *ctx, JSValueConst new_target, return JS_EXCEPTION; p = JS_VALUE_GET_STRING(str); } - return JS_NewSymbol(ctx, p, JS_ATOM_TYPE_SYMBOL); + return JS_NewSymbolInternal(ctx, p, JS_ATOM_TYPE_SYMBOL); } static JSValue js_thisSymbolValue(JSContext *ctx, JSValueConst this_val) @@ -51440,7 +51703,7 @@ static JSValue js_symbol_for(JSContext *ctx, JSValueConst this_val, str = JS_ToString(ctx, argv[0]); if (JS_IsException(str)) return JS_EXCEPTION; - return JS_NewSymbol(ctx, JS_VALUE_GET_STRING(str), JS_ATOM_TYPE_GLOBAL_SYMBOL); + return JS_NewSymbolInternal(ctx, JS_VALUE_GET_STRING(str), JS_ATOM_TYPE_GLOBAL_SYMBOL); } static JSValue js_symbol_keyFor(JSContext *ctx, JSValueConst this_val, @@ -56077,6 +56340,8 @@ static JSValue js_bigint_asUintN(JSContext *ctx, { uint64_t bits; JSValue res, a; + JSBigInt *p; + JSBigIntBuf buf; if (JS_ToIndex(ctx, &bits, argv[0])) return JS_EXCEPTION; @@ -56088,13 +56353,18 @@ static JSValue js_bigint_asUintN(JSContext *ctx, res = __JS_NewShortBigInt(ctx, 0); } else if (JS_VALUE_GET_TAG(a) == JS_TAG_SHORT_BIG_INT) { /* fast case */ + js_slimb_t sv = JS_VALUE_GET_SHORT_BIG_INT(a); if (bits >= JS_SHORT_BIG_INT_BITS) { - res = a; + if (!asIntN && sv < 0) { + p = js_bigint_set_short(&buf, a); + goto slow_case; + } else { + res = a; + } } else { - uint64_t v; + uint64_t v = sv; int shift; shift = 64 - bits; - v = JS_VALUE_GET_SHORT_BIG_INT(a); v = v << shift; if (asIntN) v = (int64_t)v >> shift; @@ -56103,30 +56373,49 @@ static JSValue js_bigint_asUintN(JSContext *ctx, res = __JS_NewShortBigInt(ctx, v); } } else { - JSBigInt *r, *p = JS_VALUE_GET_PTR(a); - if (bits >= p->len * JS_LIMB_BITS) { + JSBigInt *r; + p = JS_VALUE_GET_PTR(a); + if (bits >= p->len * JS_LIMB_BITS && (asIntN || !js_bigint_sign(p))) { res = a; } else { - int len, shift, i; + uint64_t len64; + int len, shift, i, l, is_neg; js_limb_t v; - len = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS; + slow_case: + is_neg = js_bigint_sign(p); + len64 = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS; + len = min_int64(len64, INT32_MAX); r = js_bigint_new(ctx, len); if (!r) { JS_FreeValue(ctx, a); return JS_EXCEPTION; } + /* sign extend */ r->len = len; - for(i = 0; i < len - 1; i++) + l = min_int(len, p->len); + for(i = 0; i < l; i++) r->tab[i] = p->tab[i]; + for(i = l; i < len; i++) + r->tab[i] = -is_neg; + shift = (-bits) & (JS_LIMB_BITS - 1); /* 0 <= shift <= JS_LIMB_BITS - 1 */ - v = p->tab[len - 1] << shift; + v = r->tab[len - 1] << shift; if (asIntN) v = (js_slimb_t)v >> shift; else v = v >> shift; r->tab[len - 1] = v; - r = js_bigint_normalize(ctx, r); + + if (!asIntN) { + r = js_bigint_extend(ctx, r, 0); + if (!r) { + JS_FreeValue(ctx, a); + return JS_EXCEPTION; + } + } else { + r = js_bigint_normalize(ctx, r); + } JS_FreeValue(ctx, a); res = JS_CompactBigInt(ctx, r); } @@ -60555,10 +60844,14 @@ static JSValue js_atomics_store(JSContext *ctx, } v = v32; } - if (typed_array_is_oob(p)) + if (typed_array_is_oob(p)) { + JS_FreeValue(ctx, ret); return JS_ThrowTypeErrorDetachedArrayBuffer(ctx); - if (idx >= p->u.array.count) + } + if (idx >= p->u.array.count) { + JS_FreeValue(ctx, ret); return JS_ThrowRangeError(ctx, "out-of-bound access"); + } ptr = p->u.array.u.uint8_ptr + ((uintptr_t)idx << size_log2); diff --git a/src/couch_quickjs/quickjs/quickjs.h b/src/couch_quickjs/quickjs/quickjs.h index 476d73513..d349d95b3 100644 --- a/src/couch_quickjs/quickjs/quickjs.h +++ b/src/couch_quickjs/quickjs/quickjs.h @@ -759,6 +759,8 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1) } void JS_FreeCString(JSContext *ctx, const char *ptr); +JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global); + JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto, JSClassID class_id); JSValue JS_NewObjectClass(JSContext *ctx, int class_id); JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto); diff --git a/src/couch_quickjs/quickjs/run-test262.c b/src/couch_quickjs/quickjs/run-test262.c index 81811b58d..c7d9c8282 100644 --- a/src/couch_quickjs/quickjs/run-test262.c +++ b/src/couch_quickjs/quickjs/run-test262.c @@ -87,6 +87,8 @@ typedef struct { namelist_t test_list; namelist_t exclude_list; namelist_t exclude_dir_list; +namelist_t error_list; +pthread_mutex_t error_list_mutex; int nthreads; pthread_t progress_thread; @@ -121,7 +123,6 @@ char *harness_skip_features; int *harness_skip_features_count; char *error_filename; char *error_file; -FILE *error_out; char *report_filename; int update_errors; int slow_test_threshold; @@ -390,20 +391,22 @@ int namelist_cmp_indirect(const void *a, const void *b) return namelist_cmp(*(const char **)a, *(const char **)b); } -void namelist_sort(namelist_t *lp) +void namelist_sort(namelist_t *lp, BOOL remove_duplicates) { int i, count; if (lp->count > 1) { qsort(lp->array, lp->count, sizeof(*lp->array), namelist_cmp_indirect); /* remove duplicates */ - for (count = i = 1; i < lp->count; i++) { - if (namelist_cmp(lp->array[count - 1], lp->array[i]) == 0) { - free(lp->array[i]); - } else { - lp->array[count++] = lp->array[i]; + if (remove_duplicates) { + for (count = i = 1; i < lp->count; i++) { + if (namelist_cmp(lp->array[count - 1], lp->array[i]) == 0) { + free(lp->array[i]); + } else { + lp->array[count++] = lp->array[i]; + } } + lp->count = count; } - lp->count = count; } } @@ -1077,7 +1080,7 @@ void update_exclude_dirs(void) } ep->count = count; - namelist_sort(dp); + namelist_sort(dp, TRUE); /* filter out excluded directories */ for (count = i = 0; i < lp->count; i++) { @@ -1358,6 +1361,36 @@ int longest_match(const char *str, const char *find, int pos, int *ppos, int lin return maxlen; } +static __attribute__((__format__(__printf__, 1, 2))) void print_error(const char *fmt, ...) +{ + va_list ap; + if (update_errors) { + char buf[256], *str; + int len; + + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + if (len >= sizeof(buf)) { + str = malloc(len + 1); + va_start(ap, fmt); + vsnprintf(str, len + 1, fmt, ap); + va_end(ap); + } else { + str = buf; + } + pthread_mutex_lock(&error_list_mutex); + namelist_add(&error_list, NULL, str); + pthread_mutex_unlock(&error_list_mutex); + if (str != buf) + free(str); + } else { + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + } +} + static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len, const char *filename, int is_test, int is_negative, const char *error_type, FILE *outfile, int eval_flags, @@ -1505,11 +1538,11 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len, } else { if (!s) { // not yet reported if (msg) { - fprintf(error_out, "%s:%d: %sunexpected error type: %s\n", - filename, error_line, strict_mode, msg); + print_error("%s:%d: %sunexpected error type: %s\n", + filename, error_line, strict_mode, msg); } else { - fprintf(error_out, "%s:%d: %sexpected error\n", - filename, error_line, strict_mode); + print_error("%s:%d: %sexpected error\n", + filename, error_line, strict_mode); } new_errors++; } @@ -1525,8 +1558,8 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len, longest_match(buf, p, pos, &pos, pos_line, &error_line); } } - fprintf(error_out, "%s:%d: %s%s%s\n", filename, error_line, strict_mode, - error_file ? "unexpected error: " : "", msg); + print_error("%s:%d: %s%s%s\n", filename, error_line, strict_mode, + error_file ? "unexpected error: " : "", msg); if (s && (!str_equal(s, msg) || error_line != s_line)) { printf("%s:%d: %sprevious error: %s\n", filename, s_line, strict_mode, s); @@ -1757,15 +1790,6 @@ int run_test_buf(ThreadLocalStorage *tls, js_agent_free(ctx); JS_FreeContext(ctx); JS_FreeRuntime(rt); - - atomic_inc(&test_count); - if (ret) { - atomic_inc(&test_failed); - if (outfile) { - /* do not output a failure number to minimize diff */ - fprintf(outfile, " FAILED\n"); - } - } return ret; } @@ -1992,12 +2016,25 @@ int run_test(ThreadLocalStorage *tls, const char *filename, int index) error_type, eval_flags, is_negative, is_async, can_block); } - if (use_strict) { + /* we avoid running the strict mode test if there was an error + in the nostrict case to have a single error report per + test */ + if (use_strict && ret == 0) { ret |= run_test_buf(tls, filename, harness, ip, buf, buf_len, error_type, eval_flags | JS_EVAL_FLAG_STRICT, is_negative, is_async, can_block); } clocks = clock() - clocks; + + atomic_inc(&test_count); + if (ret) { + atomic_inc(&test_failed); + if (outfile) { + /* do not output a failure number to minimize diff */ + fprintf(outfile, " FAILED\n"); + } + } + if (outfile && index >= 0 && clocks >= CLOCKS_PER_SEC / 10) { /* output timings for tests that take more than 100 ms */ fprintf(outfile, " time: %d ms\n", (int)(clocks * 1000LL / CLOCKS_PER_SEC)); @@ -2205,6 +2242,7 @@ void help(void) "-s run tests in strict mode, skip @nostrict tests\n" "-E only run tests from the error file\n" "-C use compact progress indicator\n" + "-q no progress indicator\n" "-t show timings\n" "-u update error file\n" "-v verbose: output error messages\n" @@ -2240,13 +2278,14 @@ int main(int argc, char **argv) BOOL is_module = FALSE; BOOL can_block = TRUE; BOOL count_skipped_features = FALSE; + int enable_progress = -1; clock_t clocks; init_thread_local_storage(tls); pthread_mutex_init(&stats_mutex, NULL); + pthread_mutex_init(&error_list_mutex, NULL); #if !defined(_WIN32) - compact = !isatty(STDERR_FILENO); /* Date tests assume California local time */ setenv("TZ", "America/Los_Angeles", 1); #endif @@ -2289,7 +2328,10 @@ int main(int argc, char **argv) } else if (str_equal(arg, "-v")) { verbose++; } else if (str_equal(arg, "-C")) { + enable_progress = 1; compact = 1; + } else if (str_equal(arg, "-q")) { + enable_progress = 0; } else if (str_equal(arg, "-c")) { load_config(get_opt_arg(arg, argv[optind++]), ignore); } else if (str_equal(arg, "-d")) { @@ -2338,7 +2380,6 @@ int main(int argc, char **argv) } nthreads = max_int(nthreads, 1); - error_out = stdout; if (error_filename) { error_file = load_file(error_filename, NULL); if (only_check_errors && error_file) { @@ -2348,15 +2389,19 @@ int main(int argc, char **argv) if (update_errors) { free(error_file); error_file = NULL; - error_out = fopen(error_filename, "w"); - if (!error_out) { - perror_exit(1, error_filename); - } } } update_exclude_dirs(); + if (enable_progress < 0) { +#if defined(_WIN32) + enable_progress = 1; +#else + enable_progress = (isatty(STDERR_FILENO) != 0); +#endif + } + clocks = clock(); if (count_skipped_features) { @@ -2397,8 +2442,8 @@ int main(int argc, char **argv) } // exclude_dir_list has already been sorted by update_exclude_dirs() - namelist_sort(&test_list); - namelist_sort(&exclude_list); + namelist_sort(&test_list, TRUE); + namelist_sort(&exclude_list, TRUE); for (i = 0; i < test_list.count; i++) { switch (include_exclude_or_skip(i)) { @@ -2413,8 +2458,10 @@ int main(int argc, char **argv) pthread_cond_init(&progress_cond, NULL); pthread_mutex_init(&progress_mutex, NULL); - pthread_create(&progress_thread, NULL, show_progress, NULL); - + if (enable_progress) { + pthread_create(&progress_thread, NULL, show_progress, NULL); + } + threads = malloc(sizeof(threads[0]) * nthreads); for (i = 0; i < nthreads; i++) { RunTestDirThread *th = &threads[i]; @@ -2431,12 +2478,13 @@ int main(int argc, char **argv) pthread_join(threads[i].tid, NULL); free(threads); - pthread_mutex_lock(&progress_mutex); - progress_exit_request = TRUE; - pthread_cond_signal(&progress_cond); - pthread_mutex_unlock(&progress_mutex); - pthread_join(progress_thread, NULL); - + if (enable_progress) { + pthread_mutex_lock(&progress_mutex); + progress_exit_request = TRUE; + pthread_cond_signal(&progress_cond); + pthread_mutex_unlock(&progress_mutex); + pthread_join(progress_thread, NULL); + } pthread_mutex_destroy(&progress_mutex); pthread_cond_destroy(&progress_cond); @@ -2444,6 +2492,9 @@ int main(int argc, char **argv) fclose(outfile); outfile = NULL; } + /* useful to have the report at the end in case stdout and + stderr are redirected to a single file */ + fflush(stdout); } else { outfile = stdout; while (optind < argc) { @@ -2509,14 +2560,25 @@ int main(int argc, char **argv) fprintf(stderr, "Total user time: %.3fs (nthreads=%d)\n", (double)clocks / CLOCKS_PER_SEC, nthreads); } - if (error_out && error_out != stdout) { + if (update_errors) { + FILE *error_out = fopen(error_filename, "w"); + int i; + if (!error_out) { + perror_exit(1, error_filename); + } + /* sort the error list so that its order does not depend on + the thread scheduling */ + namelist_sort(&error_list, FALSE); + for (i = 0; i < error_list.count; i++) { + fputs(error_list.array[i], error_out); + } fclose(error_out); - error_out = NULL; } namelist_free(&test_list); namelist_free(&exclude_list); namelist_free(&exclude_dir_list); + namelist_free(&error_list); free(harness_dir); free(harness_skip_features); free(harness_skip_features_count); @@ -2525,5 +2587,9 @@ int main(int argc, char **argv) free(error_file); /* Signal that the error file is out of date. */ - return new_errors || changed_errors || fixed_errors; + if (update_errors) { + return 0; + } else { + return new_errors || changed_errors || fixed_errors; + } } diff --git a/src/couch_quickjs/quickjs/test262.conf b/src/couch_quickjs/quickjs/test262.conf index af5cd6474..81fd8546c 100644 --- a/src/couch_quickjs/quickjs/test262.conf +++ b/src/couch_quickjs/quickjs/test262.conf @@ -105,6 +105,7 @@ destructuring-binding dynamic-import error-cause Error.isError +error-stack-accessor=skip explicit-resource-management=skip exponentiation export-star-as-namespace-from-module @@ -147,7 +148,10 @@ Intl.NumberFormat-v3=skip Intl.RelativeTimeFormat=skip Intl.Segmenter=skip IsHTMLDDA +Iterator.prototype.join=skip +iterator-chunking=skip iterator-helpers +iterator-includes=skip iterator-sequencing json-modules json-parse-with-source diff --git a/src/couch_quickjs/quickjs/test262_errors.txt b/src/couch_quickjs/quickjs/test262_errors.txt index dd1cceea6..0c983cfbb 100644 --- a/src/couch_quickjs/quickjs/test262_errors.txt +++ b/src/couch_quickjs/quickjs/test262_errors.txt @@ -5,6 +5,8 @@ test262/test/annexB/language/expressions/assignmenttargettype/callexpression-in- test262/test/annexB/language/expressions/assignmenttargettype/callexpression-in-prefix-update.js:27: SyntaxError: invalid increment/decrement operand test262/test/annexB/language/expressions/assignmenttargettype/callexpression.js:33: SyntaxError: invalid assignment left-hand side test262/test/annexB/language/expressions/assignmenttargettype/cover-callexpression-and-asyncarrowhead.js:20: SyntaxError: invalid assignment left-hand side +test262/test/built-ins/Promise/try/avoids-wrap-for-subclass.js:10: Test262Error: Expected SameValue(«[object Promise]», «[object Promise]») to be true +test262/test/built-ins/Promise/try/avoids-wrap.js:10: Test262Error: Expected SameValue(«[object Promise]», «[object Promise]») to be true test262/test/language/expressions/assignment/S11.13.1_A6_T1.js:23: Test262Error: #1: innerX === undefined. Actual: 1 test262/test/language/expressions/assignment/S11.13.1_A6_T2.js:23: Test262Error: #1: innerX === 2. Actual: 1 test262/test/language/expressions/compound-assignment/S11.13.2_A6.1_T1.js:24: Test262Error: #1: innerX === 2. Actual: 12 @@ -23,25 +25,19 @@ test262/test/language/module-code/ambiguous-export-bindings/import-and-export-pr test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from-and-import-star-as-and-export.js:74: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js' is ambiguous test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from.js:75: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-export-star-as-from.js' is ambiguous test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js:74: SyntaxError: export 'foo' in module 'test262/test/language/module-code/ambiguous-export-bindings/namespace-unambiguous-if-import-star-as-and-export.js' is ambiguous -test262/test/language/statements/expression/S12.4_A1.js:15: unexpected error type: Test262: This statement should not be evaluated. -test262/test/language/statements/expression/S12.4_A1.js:15: strict mode: unexpected error type: Test262: This statement should not be evaluated. +test262/test/language/module-code/namespace/internals/super-access-to-tdz-binding.js:32: Test262Error: Expected a ReferenceError but got a TypeError +test262/test/language/statements/expression/S12.4_A1.js:15: unexpected error type: Test262: This statement should not be evaluated test262/test/staging/sm/Function/arguments-parameter-shadowing.js:14: Test262Error: Expected SameValue(«true», «false») to be true test262/test/staging/sm/Function/constructor-binding.js:11: Test262Error: Expected SameValue(«"function"», «"undefined"») to be true -test262/test/staging/sm/Function/constructor-binding.js:11: strict mode: Test262Error: Expected SameValue(«"function"», «"undefined"») to be true test262/test/staging/sm/Function/function-bind.js:24: Test262Error: Conforms to NativeFunction Syntax: "function bound unbound() {\n [native code]\n}" test262/test/staging/sm/Function/function-name-for.js:13: Test262Error: Expected SameValue(«""», «"forInHead"») to be true test262/test/staging/sm/Function/implicit-this-in-parameter-expression.js:12: Test262Error: Expected SameValue(«[object Object]», «undefined») to be true test262/test/staging/sm/Function/invalid-parameter-list.js:13: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all -test262/test/staging/sm/Function/invalid-parameter-list.js:13: strict mode: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all test262/test/staging/sm/async-functions/async-contains-unicode-escape.js:11: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all -test262/test/staging/sm/async-functions/async-contains-unicode-escape.js:11: strict mode: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all test262/test/staging/sm/async-functions/await-in-arrow-parameters.js:10: Test262Error: AsyncFunction:(a = (b = await/r/g) => {}) => {} Expected a SyntaxError to be thrown but no exception was thrown at all -test262/test/staging/sm/async-functions/await-in-arrow-parameters.js:10: strict mode: Test262Error: AsyncFunction:(a = (b = await/r/g) => {}) => {} Expected a SyntaxError to be thrown but no exception was thrown at all test262/test/staging/sm/class/boundFunctionSubclassing.js:9: Test262Error: Expected SameValue(«false», «true») to be true -test262/test/staging/sm/class/boundFunctionSubclassing.js:9: strict mode: Test262Error: Expected SameValue(«false», «true») to be true test262/test/staging/sm/class/strictExecution.js:13: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all test262/test/staging/sm/class/superPropOrdering.js:17: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all -test262/test/staging/sm/class/superPropOrdering.js:17: strict mode: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all test262/test/staging/sm/generators/syntax.js:50: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all test262/test/staging/sm/lexical-environment/block-scoped-functions-annex-b-arguments.js:13: Test262Error: Expected SameValue(«"object"», «"function"») to be true test262/test/staging/sm/lexical-environment/block-scoped-functions-annex-b-eval.js:11: Test262Error: Expected SameValue(«"outer-gouter-geval-gtruefalseq"», «"outer-geval-gwith-gtruefalseq"») to be true @@ -50,11 +46,7 @@ test262/test/staging/sm/lexical-environment/block-scoped-functions-annex-b-notap test262/test/staging/sm/lexical-environment/block-scoped-functions-deprecated-redecl.js:23: Test262Error: Expected SameValue(«3», «4») to be true test262/test/staging/sm/lexical-environment/var-in-catch-body-annex-b-eval.js:16: Test262Error: Expected SameValue(«"g"», «"global-x"») to be true test262/test/staging/sm/object/defineProperties-order.js:11: Test262Error: Expected SameValue(«"ownKeys,getOwnPropertyDescriptor,getOwnPropertyDescriptor,get,get"», «"ownKeys,getOwnPropertyDescriptor,get,getOwnPropertyDescriptor,get"») to be true -test262/test/staging/sm/object/defineProperties-order.js:11: strict mode: Test262Error: Expected SameValue(«"ownKeys,getOwnPropertyDescriptor,getOwnPropertyDescriptor,get,get"», «"ownKeys,getOwnPropertyDescriptor,get,getOwnPropertyDescriptor,get"») to be true test262/test/staging/sm/regress/regress-602621.js:13: Test262Error: function sub-statement must override arguments Expected SameValue(«"function"», «"object"») to be true test262/test/staging/sm/regress/regress-1383630.js:28: Test262Error: proxy must report the same value for the non-writable, non-configurable property Expected a TypeError to be thrown but no exception was thrown at all -test262/test/staging/sm/regress/regress-1383630.js:28: strict mode: Test262Error: proxy must report the same value for the non-writable, non-configurable property Expected a TypeError to be thrown but no exception was thrown at all test262/test/staging/sm/statements/arrow-function-in-for-statement-head.js:13: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all -test262/test/staging/sm/statements/arrow-function-in-for-statement-head.js:13: strict mode: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all test262/test/staging/sm/statements/try-completion.js:11: Test262Error: Expected SameValue(«"try"», «undefined») to be true -test262/test/staging/sm/statements/try-completion.js:11: strict mode: Test262Error: Expected SameValue(«"try"», «undefined») to be true
