From 3849bcbeff8ca582b426a3c713b1477e2d59943c Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Tue, 10 Nov 2026 01:10:35 -0800 Subject: [PATCH] Bump embedded QuickJS-ng to v0.18.0 Picks up upstream fixes since v0.14.0, including integer overflows in Proxy ownKeys() enumeration and TypedArray sort, and a use-after-free of a suspended coroutine reachable via a closure. Also update update.sh's default version. --- src/quickjs/quickjs.c | 11707 ++++++++++++++++++++++++++++++---------- src/quickjs/quickjs.h | 170 +- src/quickjs/update.sh | 4 +- 3 files changed, 9048 insertions(+), 2833 deletions(-) diff --git a/src/quickjs/quickjs.c b/src/quickjs/quickjs.c index 3f7363fe..feb50838 100644 --- a/src/quickjs/quickjs.c +++ b/src/quickjs/quickjs.c @@ -109,7 +109,7 @@ extern "C" { #endif #if defined(__APPLE__) #include -#elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__GLIBC__) +#elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__GLIBC__) || defined(__wasi__) #include #elif defined(__FreeBSD__) #include @@ -263,6 +263,20 @@ static inline uint32_t hash32(uint32_t a) return a * 0x61c88647; } +static inline uint64_t hash64(uint64_t a) +{ + return a * 0x61c8864680b583ebULL; +} + +static inline uintptr_t hash_uintptr(uintptr_t a) +{ + if (sizeof(uintptr_t) == sizeof(uint32_t)) { + return hash32(a); + } else { + return hash64(a); + } +} + /* WARNING: undefined if a = 0 */ static inline int clz32(unsigned int a) { @@ -294,6 +308,15 @@ static inline int clz64(uint64_t a) #endif } +static inline uintptr_t clz_uintptr(uintptr_t a) +{ + if (sizeof(uintptr_t) == sizeof(uint32_t)) { + return clz32(a); + } else { + return clz64(a); + } +} + /* WARNING: undefined if a = 0 */ static inline int ctz32(unsigned int a) { @@ -661,7 +684,7 @@ static inline size_t js__malloc_usable_size(const void *ptr) return malloc_size(ptr); #elif defined(_WIN32) return _msize((void *)ptr); -#elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__GLIBC__) +#elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__wasi__) return malloc_usable_size((void *)ptr); #else return 0; @@ -725,6 +748,7 @@ static inline int js_thread_join(js_thread_t thrd); // Note that `*&cw` in the asm constraints looks redundant but isn't. #if defined(__i386__) && !defined(_MSC_VER) #define JS_X87_FPCW_SAVE_AND_ADJUST(cw) \ + (void)0; \ unsigned short cw; \ __asm__ __volatile__("fnstcw %0" : "=m"(*&cw)); \ do { \ @@ -2308,6 +2332,7 @@ typedef enum { CR_OP_UNION, CR_OP_INTER, CR_OP_XOR, + CR_OP_SUB, } CharRangeOpEnum; void cr_init(CharRange *cr, void *mem_opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size)); @@ -2336,14 +2361,14 @@ static inline int cr_add_interval(CharRange *cr, uint32_t c1, uint32_t c2) return 0; } -int cr_union1(CharRange *cr, const uint32_t *b_pt, int b_len); +int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op); static inline int cr_union_interval(CharRange *cr, uint32_t c1, uint32_t c2) { uint32_t b_pt[2]; b_pt[0] = c1; b_pt[1] = c2 + 1; - return cr_union1(cr, b_pt, 2); + return cr_op1(cr, b_pt, 2, CR_OP_UNION); } int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len, @@ -2367,6 +2392,51 @@ int unicode_script(CharRange *cr, int unicode_general_category(CharRange *cr, const char *gc_name); int unicode_prop(CharRange *cr, const char *prop_name); +typedef void UnicodeSequencePropCB(void *opaque, const uint32_t *buf, int len); +int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque, + CharRange *cr); + +/* Code point type categories (ASCII fast path table) */ +enum { + UNICODE_C_SPACE = (1 << 0), + UNICODE_C_DIGIT = (1 << 1), + UNICODE_C_UPPER = (1 << 2), + UNICODE_C_LOWER = (1 << 3), + UNICODE_C_UNDER = (1 << 4), + UNICODE_C_DOLLAR = (1 << 5), + UNICODE_C_XDIGIT = (1 << 6), +}; +extern uint8_t const lre_ctype_bits[256]; + +static inline int lre_is_space_byte(uint8_t c) { + return lre_ctype_bits[c] & UNICODE_C_SPACE; +} + +static inline int lre_is_id_start_byte(uint8_t c) { + return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER | + UNICODE_C_UNDER | UNICODE_C_DOLLAR); +} + +static inline int lre_is_id_continue_byte(uint8_t c) { + return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER | + UNICODE_C_UNDER | UNICODE_C_DOLLAR | + UNICODE_C_DIGIT); +} + +static inline int lre_is_word_byte(uint8_t c) { + return lre_ctype_bits[c] & (UNICODE_C_UPPER | UNICODE_C_LOWER | + UNICODE_C_UNDER | UNICODE_C_DIGIT); +} + +int lre_is_space_non_ascii(uint32_t c); + +static inline int lre_is_space(uint32_t c) { + if (c < 256) + return lre_is_space_byte(c); + else + return lre_is_space_non_ascii(c); +} + #ifdef __cplusplus } /* extern "C" { */ #endif @@ -2421,9 +2491,14 @@ extern "C" { #define LRE_RET_TIMEOUT (-2) #define LRE_RET_BYTECODE_ERROR (-3) +/* trailer length after the group name including the trailing '\0' */ +#define LRE_GROUP_NAME_TRAILER_LEN 2 + uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size, const char *buf, size_t buf_len, int re_flags, void *opaque); +int lre_get_alloc_count(const uint8_t *bc_buf); +int lre_check_bytecode(const uint8_t *bc_buf, int bc_buf_len); int lre_get_capture_count(const uint8_t *bc_buf); int lre_get_flags(const uint8_t *bc_buf); const char *lre_get_groupnames(const uint8_t *bc_buf); @@ -2432,7 +2507,7 @@ int lre_exec(uint8_t **capture, int cbuf_type, void *opaque); int lre_parse_escape(const uint8_t **pp, int allow_utf16); -bool lre_is_space(int c); +/* lre_is_space() is provided as an inline in libunicode.h */ /* must be provided by the user */ bool lre_check_stack_overflow(void *opaque, size_t alloca_size); @@ -2471,6 +2546,48 @@ static inline int lre_js_is_ident_next(int c) /* Compressed unicode tables */ /* Automatically generated file - do not edit */ +/* +UNICODE LICENSE V3 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © 1991-2026 Unicode, Inc. + +NOTICE TO USER: Carefully read the following legal agreement. BY +DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR +SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE +TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT +DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. +*/ + #include static const uint32_t case_conv_table1[378] = { @@ -6495,6 +6612,76 @@ static const uint8_t unicode_prop_Changes_When_NFKC_Casefolded1_table[449] = { 0xff, }; +static const uint8_t unicode_prop_Basic_Emoji1_table[144] = { + 0x60, 0x23, 0x19, 0x81, 0x40, 0xcc, 0x1a, 0x01, + 0x80, 0x42, 0x08, 0x81, 0x94, 0x81, 0xb1, 0x8b, + 0xaa, 0x80, 0x92, 0x80, 0x8c, 0x07, 0x81, 0x90, + 0x0c, 0x0f, 0x04, 0x80, 0x94, 0x06, 0x08, 0x03, + 0x01, 0x06, 0x03, 0x81, 0x9b, 0x80, 0xa2, 0x00, + 0x03, 0x10, 0x80, 0xbc, 0x82, 0x97, 0x80, 0x8d, + 0x80, 0x43, 0x5a, 0x81, 0xb2, 0x03, 0x80, 0x61, + 0xc4, 0xad, 0x80, 0x40, 0xc9, 0x80, 0x40, 0xbd, + 0x01, 0x89, 0xe5, 0x80, 0x97, 0x80, 0x93, 0x01, + 0x20, 0x82, 0x94, 0x81, 0x40, 0xad, 0xa0, 0x8b, + 0x88, 0x80, 0xc5, 0x80, 0x95, 0x8b, 0xaa, 0x1c, + 0x8b, 0x90, 0x10, 0x82, 0xc6, 0x00, 0x80, 0x40, + 0xba, 0x81, 0xbe, 0x8c, 0x18, 0x97, 0x91, 0x80, + 0x99, 0x81, 0x8c, 0x80, 0xd5, 0xd4, 0xaf, 0xc5, + 0x28, 0x12, 0x0b, 0x13, 0x8a, 0x0e, 0x88, 0x40, + 0xe2, 0x8b, 0x18, 0x41, 0x1a, 0xae, 0x80, 0x89, + 0x80, 0x40, 0xb8, 0xef, 0x8c, 0x82, 0x8a, 0x82, + 0xb8, 0x00, 0x83, 0x8f, 0x81, 0x8b, 0x83, 0x89, +}; + +static const uint8_t unicode_prop_Basic_Emoji2_table[183] = { + 0x40, 0xa8, 0x03, 0x80, 0x5f, 0x8c, 0x80, 0x8b, + 0x80, 0x40, 0xd7, 0x80, 0x95, 0x80, 0xd9, 0x85, + 0x8e, 0x81, 0x41, 0x7c, 0x80, 0x40, 0xa5, 0x80, + 0x9c, 0x10, 0x0c, 0x82, 0x40, 0xc6, 0x80, 0x40, + 0xe6, 0x81, 0x89, 0x80, 0x88, 0x80, 0xb9, 0x0a, + 0x84, 0x88, 0x01, 0x05, 0x03, 0x01, 0x00, 0x09, + 0x02, 0x02, 0x0f, 0x14, 0x00, 0x80, 0x9b, 0x09, + 0x00, 0x08, 0x80, 0x91, 0x01, 0x80, 0x92, 0x00, + 0x18, 0x00, 0x0a, 0x05, 0x07, 0x81, 0x95, 0x05, + 0x00, 0x00, 0x80, 0x94, 0x05, 0x09, 0x01, 0x17, + 0x04, 0x09, 0x08, 0x01, 0x00, 0x00, 0x05, 0x02, + 0x80, 0x90, 0x81, 0x8e, 0x01, 0x80, 0x9a, 0x81, + 0xbb, 0x80, 0x41, 0x91, 0x81, 0x41, 0xce, 0x82, + 0x45, 0x27, 0x80, 0x8b, 0x80, 0x42, 0x58, 0x00, + 0x80, 0x61, 0xbe, 0xd5, 0x81, 0x8b, 0x81, 0x40, + 0x81, 0x80, 0xb3, 0x80, 0x40, 0xe8, 0x01, 0x88, + 0x88, 0x80, 0xc5, 0x80, 0x97, 0x08, 0x11, 0x81, + 0xaa, 0x1c, 0x8b, 0x92, 0x00, 0x00, 0x80, 0xc6, + 0x00, 0x80, 0x40, 0xba, 0x80, 0xca, 0x81, 0xa3, + 0x09, 0x86, 0x8c, 0x01, 0x19, 0x80, 0x93, 0x01, + 0x07, 0x81, 0x88, 0x04, 0x82, 0x8b, 0x17, 0x11, + 0x00, 0x03, 0x05, 0x02, 0x05, 0x80, 0x40, 0xcf, + 0x00, 0x82, 0x8f, 0x2a, 0x05, 0x01, 0x80, +}; + +static const uint8_t unicode_prop_RGI_Emoji_Flag_Sequence_table[128] = { + 0x0c, 0x00, 0x09, 0x00, 0x04, 0x01, 0x02, 0x06, + 0x03, 0x03, 0x01, 0x02, 0x01, 0x03, 0x07, 0x0d, + 0x18, 0x00, 0x09, 0x00, 0x00, 0x89, 0x08, 0x00, + 0x00, 0x81, 0x88, 0x83, 0x8c, 0x10, 0x00, 0x01, + 0x07, 0x08, 0x29, 0x10, 0x28, 0x00, 0x80, 0x8a, + 0x00, 0x0a, 0x00, 0x0e, 0x15, 0x18, 0x83, 0x89, + 0x06, 0x00, 0x81, 0x8d, 0x00, 0x12, 0x08, 0x00, + 0x03, 0x00, 0x24, 0x00, 0x05, 0x21, 0x00, 0x00, + 0x29, 0x90, 0x00, 0x02, 0x00, 0x08, 0x09, 0x00, + 0x08, 0x18, 0x8b, 0x80, 0x8c, 0x02, 0x19, 0x1a, + 0x11, 0x00, 0x00, 0x80, 0x9c, 0x80, 0x88, 0x02, + 0x00, 0x00, 0x02, 0x20, 0x88, 0x0a, 0x00, 0x03, + 0x01, 0x02, 0x05, 0x08, 0x00, 0x01, 0x09, 0x20, + 0x21, 0x18, 0x22, 0x00, 0x00, 0x00, 0x00, 0x18, + 0x28, 0x89, 0x80, 0x8b, 0x80, 0x90, 0x80, 0x92, + 0x80, 0x8d, 0x05, 0x80, 0x8a, 0x80, 0x88, 0x80, +}; + +static const uint8_t unicode_prop_Emoji_Keycap_Sequence_table[4] = { + 0xa2, 0x05, 0x04, 0x89, +}; + static const uint8_t unicode_prop_ASCII_Hex_Digit_table[5] = { 0xaf, 0x89, 0x35, 0x99, 0x85, }; @@ -6943,6 +7130,10 @@ typedef enum { UNICODE_PROP_Changes_When_Titlecased1, UNICODE_PROP_Changes_When_Casefolded1, UNICODE_PROP_Changes_When_NFKC_Casefolded1, + UNICODE_PROP_Basic_Emoji1, + UNICODE_PROP_Basic_Emoji2, + UNICODE_PROP_RGI_Emoji_Flag_Sequence, + UNICODE_PROP_Emoji_Keycap_Sequence, UNICODE_PROP_ASCII_Hex_Digit, UNICODE_PROP_Bidi_Control, UNICODE_PROP_Dash, @@ -7082,6 +7273,10 @@ static const uint8_t * const unicode_prop_table[] = { unicode_prop_Changes_When_Titlecased1_table, unicode_prop_Changes_When_Casefolded1_table, unicode_prop_Changes_When_NFKC_Casefolded1_table, + unicode_prop_Basic_Emoji1_table, + unicode_prop_Basic_Emoji2_table, + unicode_prop_RGI_Emoji_Flag_Sequence_table, + unicode_prop_Emoji_Keycap_Sequence_table, unicode_prop_ASCII_Hex_Digit_table, unicode_prop_Bidi_Control_table, unicode_prop_Dash_table, @@ -7137,6 +7332,10 @@ static const uint16_t unicode_prop_len_table[] = { countof(unicode_prop_Changes_When_Titlecased1_table), countof(unicode_prop_Changes_When_Casefolded1_table), countof(unicode_prop_Changes_When_NFKC_Casefolded1_table), + countof(unicode_prop_Basic_Emoji1_table), + countof(unicode_prop_Basic_Emoji2_table), + countof(unicode_prop_RGI_Emoji_Flag_Sequence_table), + countof(unicode_prop_Emoji_Keycap_Sequence_table), countof(unicode_prop_ASCII_Hex_Digit_table), countof(unicode_prop_Bidi_Control_table), countof(unicode_prop_Dash_table), @@ -7175,6 +7374,335 @@ static const uint16_t unicode_prop_len_table[] = { countof(unicode_prop_Case_Ignorable_table), }; +typedef enum { + UNICODE_SEQUENCE_PROP_Basic_Emoji, + UNICODE_SEQUENCE_PROP_Emoji_Keycap_Sequence, + UNICODE_SEQUENCE_PROP_RGI_Emoji_Modifier_Sequence, + UNICODE_SEQUENCE_PROP_RGI_Emoji_Flag_Sequence, + UNICODE_SEQUENCE_PROP_RGI_Emoji_Tag_Sequence, + UNICODE_SEQUENCE_PROP_RGI_Emoji_ZWJ_Sequence, + UNICODE_SEQUENCE_PROP_RGI_Emoji, + UNICODE_SEQUENCE_PROP_COUNT, +} UnicodeSequencePropertyEnum; + +static const char unicode_sequence_prop_name_table[] = + "Basic_Emoji" "\0" + "Emoji_Keycap_Sequence" "\0" + "RGI_Emoji_Modifier_Sequence" "\0" + "RGI_Emoji_Flag_Sequence" "\0" + "RGI_Emoji_Tag_Sequence" "\0" + "RGI_Emoji_ZWJ_Sequence" "\0" + "RGI_Emoji" "\0" +; + +static const uint8_t unicode_rgi_emoji_tag_sequence[18] = { + 0x67, 0x62, 0x65, 0x6e, 0x67, 0x00, 0x67, 0x62, + 0x73, 0x63, 0x74, 0x00, 0x67, 0x62, 0x77, 0x6c, + 0x73, 0x00, +}; + +static const uint8_t unicode_rgi_emoji_zwj_sequence[2392] = { + 0x02, 0xb8, 0x19, 0x40, 0x86, 0x02, 0xd1, 0x39, + 0xb0, 0x19, 0x02, 0x26, 0x39, 0x42, 0x86, 0x02, + 0xb4, 0x36, 0x42, 0x86, 0x03, 0x68, 0x54, 0x64, + 0x87, 0x68, 0x54, 0x02, 0xdc, 0x39, 0x42, 0x86, + 0x02, 0xd1, 0x39, 0x73, 0x13, 0x02, 0x39, 0x39, + 0x40, 0x86, 0x02, 0x69, 0x34, 0xbd, 0x19, 0x03, + 0xb6, 0x36, 0x40, 0x86, 0xa1, 0x87, 0x03, 0x68, + 0x74, 0x1d, 0x19, 0x68, 0x74, 0x03, 0x68, 0x34, + 0xbd, 0x19, 0xa1, 0x87, 0x02, 0xf1, 0x7a, 0xf2, + 0x7a, 0x02, 0xca, 0x33, 0x42, 0x86, 0x02, 0x69, + 0x34, 0xb0, 0x19, 0x04, 0x68, 0x14, 0x68, 0x14, + 0x67, 0x14, 0x66, 0x14, 0x02, 0xf9, 0x26, 0x42, + 0x86, 0x03, 0x69, 0x74, 0x1d, 0x19, 0x69, 0x74, + 0x03, 0xd1, 0x19, 0xbc, 0x19, 0xa1, 0x87, 0x02, + 0x3c, 0x19, 0x40, 0x86, 0x02, 0x68, 0x34, 0xeb, + 0x13, 0x02, 0xc3, 0x33, 0xa1, 0x87, 0x02, 0x70, + 0x34, 0x40, 0x86, 0x02, 0xd4, 0x39, 0x42, 0x86, + 0x02, 0xcf, 0x39, 0x42, 0x86, 0x03, 0xd1, 0x79, + 0xef, 0x1a, 0xd1, 0x79, 0x03, 0x68, 0x74, 0xef, + 0x1a, 0x68, 0x74, 0x03, 0x69, 0x74, 0xef, 0x1a, + 0x69, 0x74, 0x02, 0x47, 0x36, 0x40, 0x86, 0x03, + 0x68, 0x74, 0x30, 0x14, 0x68, 0x74, 0x02, 0x39, + 0x39, 0x42, 0x86, 0x04, 0xd1, 0x79, 0x64, 0x87, + 0x8b, 0x14, 0xd1, 0x79, 0x03, 0x69, 0x74, 0x30, + 0x14, 0x69, 0x74, 0x02, 0xd1, 0x39, 0x95, 0x86, + 0x02, 0x68, 0x34, 0x93, 0x13, 0x02, 0x69, 0x34, + 0xed, 0x13, 0x02, 0xda, 0x39, 0x40, 0x86, 0x03, + 0x69, 0x34, 0xaf, 0x19, 0xa1, 0x87, 0x02, 0xd1, + 0x39, 0x93, 0x13, 0x03, 0xce, 0x39, 0x42, 0x86, + 0xa1, 0x87, 0x03, 0xd1, 0x79, 0x64, 0x87, 0xd1, + 0x79, 0x03, 0xc3, 0x33, 0x42, 0x86, 0xa1, 0x87, + 0x03, 0x69, 0x74, 0x1d, 0x19, 0x68, 0x74, 0x02, + 0x69, 0x34, 0x92, 0x16, 0x02, 0xd1, 0x39, 0x96, + 0x86, 0x04, 0x69, 0x14, 0x64, 0x87, 0x8b, 0x14, + 0x68, 0x14, 0x02, 0x47, 0x36, 0x42, 0x86, 0x02, + 0x68, 0x34, 0x7c, 0x13, 0x02, 0x86, 0x34, 0x42, + 0x86, 0x02, 0xd1, 0x39, 0x7c, 0x13, 0x02, 0x69, + 0x14, 0xa4, 0x13, 0x02, 0xda, 0x39, 0x42, 0x86, + 0x02, 0x37, 0x39, 0x40, 0x86, 0x02, 0xd1, 0x39, + 0x08, 0x87, 0x04, 0x68, 0x54, 0x64, 0x87, 0x8b, + 0x14, 0x68, 0x54, 0x02, 0x4d, 0x36, 0x40, 0x86, + 0x02, 0x68, 0x34, 0x2c, 0x15, 0x02, 0x69, 0x34, + 0xaf, 0x19, 0x02, 0x6e, 0x34, 0x40, 0x86, 0x02, + 0xcd, 0x39, 0x42, 0x86, 0x02, 0xd1, 0x39, 0x2c, + 0x15, 0x02, 0x6f, 0x14, 0x40, 0x86, 0x03, 0xd1, + 0x39, 0xbc, 0x19, 0xa1, 0x87, 0x02, 0x68, 0x34, + 0xa8, 0x13, 0x02, 0x69, 0x34, 0x73, 0x13, 0x04, + 0x69, 0x54, 0x64, 0x87, 0x8b, 0x14, 0x68, 0x54, + 0x02, 0x71, 0x34, 0x42, 0x86, 0x02, 0x45, 0x36, + 0x40, 0x86, 0x02, 0xd1, 0x39, 0xa8, 0x13, 0x03, + 0x69, 0x54, 0x64, 0x87, 0x68, 0x54, 0x03, 0x69, + 0x54, 0x64, 0x87, 0x69, 0x54, 0x03, 0xce, 0x39, + 0x40, 0x86, 0xa1, 0x87, 0x02, 0xd8, 0x39, 0x40, + 0x86, 0x03, 0xc3, 0x33, 0x40, 0x86, 0xa1, 0x87, + 0x02, 0x4d, 0x36, 0x42, 0x86, 0x02, 0xd1, 0x19, + 0x92, 0x16, 0x02, 0xd1, 0x39, 0xeb, 0x13, 0x02, + 0x68, 0x34, 0xbc, 0x14, 0x02, 0xd1, 0x39, 0xbc, + 0x14, 0x02, 0x3d, 0x39, 0x40, 0x86, 0x02, 0xb8, + 0x39, 0x42, 0x86, 0x02, 0xa3, 0x36, 0x40, 0x86, + 0x02, 0x75, 0x35, 0x40, 0x86, 0x02, 0xd8, 0x39, + 0x42, 0x86, 0x02, 0x69, 0x34, 0x93, 0x13, 0x02, + 0x35, 0x39, 0x40, 0x86, 0x02, 0x4b, 0x36, 0x40, + 0x86, 0x02, 0x3d, 0x39, 0x42, 0x86, 0x02, 0x38, + 0x39, 0x42, 0x86, 0x02, 0xa3, 0x36, 0x42, 0x86, + 0x03, 0x69, 0x14, 0x67, 0x14, 0x67, 0x14, 0x02, + 0xb6, 0x36, 0x40, 0x86, 0x02, 0x69, 0x34, 0x7c, + 0x13, 0x02, 0x75, 0x35, 0x42, 0x86, 0x02, 0xcc, + 0x93, 0x40, 0x86, 0x02, 0x6f, 0x34, 0x42, 0x86, + 0x02, 0xcc, 0x33, 0x40, 0x86, 0x03, 0xd1, 0x39, + 0xbd, 0x19, 0xa1, 0x87, 0x02, 0x87, 0x34, 0x40, + 0x86, 0x02, 0x82, 0x34, 0x40, 0x86, 0x02, 0x69, + 0x14, 0x3e, 0x13, 0x02, 0xd6, 0x39, 0x40, 0x86, + 0x02, 0x68, 0x14, 0xbd, 0x19, 0x02, 0x4b, 0x36, + 0x42, 0x86, 0x02, 0x46, 0x36, 0x42, 0x86, 0x02, + 0x69, 0x34, 0x2c, 0x15, 0x03, 0xb6, 0x36, 0x42, + 0x86, 0xa1, 0x87, 0x02, 0xc4, 0x33, 0x40, 0x86, + 0x02, 0x26, 0x19, 0x40, 0x86, 0x02, 0x69, 0x14, + 0xb0, 0x19, 0x02, 0xde, 0x19, 0x42, 0x86, 0x02, + 0x69, 0x34, 0xa8, 0x13, 0x02, 0xcc, 0x33, 0x42, + 0x86, 0x02, 0x82, 0x34, 0x42, 0x86, 0x02, 0xd1, + 0x19, 0x93, 0x13, 0x02, 0x81, 0x14, 0x42, 0x86, + 0x03, 0xd1, 0x79, 0x30, 0x14, 0xd1, 0x79, 0x02, + 0x68, 0x34, 0xbb, 0x14, 0x02, 0x69, 0x34, 0x95, + 0x86, 0x02, 0xd1, 0x39, 0xbb, 0x14, 0x02, 0x69, + 0x34, 0xeb, 0x13, 0x02, 0xd1, 0x39, 0x84, 0x13, + 0x02, 0x69, 0x34, 0xbc, 0x14, 0x04, 0x69, 0x54, + 0x64, 0x87, 0x8b, 0x14, 0x69, 0x54, 0x02, 0x26, + 0x39, 0x40, 0x86, 0x02, 0xb4, 0x36, 0x40, 0x86, + 0x02, 0x47, 0x16, 0x42, 0x86, 0x02, 0xdc, 0x39, + 0x40, 0x86, 0x02, 0xca, 0x33, 0x40, 0x86, 0x02, + 0xf9, 0x26, 0x40, 0x86, 0x02, 0x69, 0x34, 0x08, + 0x87, 0x03, 0x69, 0x14, 0x69, 0x14, 0x66, 0x14, + 0x03, 0xd1, 0x59, 0x1d, 0x19, 0xd1, 0x59, 0x02, + 0xd4, 0x39, 0x40, 0x86, 0x02, 0xcf, 0x39, 0x40, + 0x86, 0x02, 0x68, 0x34, 0xa4, 0x13, 0x02, 0xd1, + 0x39, 0xa4, 0x13, 0x02, 0xd1, 0x19, 0xa8, 0x13, + 0x02, 0xd7, 0x39, 0x42, 0x86, 0x03, 0x69, 0x34, + 0xbc, 0x19, 0xa1, 0x87, 0x02, 0x68, 0x14, 0xb0, + 0x19, 0x02, 0x3c, 0x39, 0x42, 0x86, 0x02, 0x68, + 0x14, 0x73, 0x13, 0x04, 0x69, 0x14, 0x69, 0x14, + 0x66, 0x14, 0x66, 0x14, 0x03, 0x68, 0x34, 0xaf, + 0x19, 0xa1, 0x87, 0x02, 0x68, 0x34, 0x80, 0x16, + 0x02, 0x73, 0x34, 0x42, 0x86, 0x02, 0xd1, 0x39, + 0x80, 0x16, 0x02, 0x68, 0x34, 0xb0, 0x19, 0x02, + 0x86, 0x34, 0x40, 0x86, 0x02, 0x38, 0x19, 0x42, + 0x86, 0x02, 0x69, 0x34, 0xbb, 0x14, 0x02, 0xb5, + 0x36, 0x42, 0x86, 0x02, 0xcd, 0x39, 0x40, 0x86, + 0x02, 0x68, 0x34, 0x27, 0x15, 0x02, 0x68, 0x34, + 0x95, 0x86, 0x03, 0x68, 0x14, 0x68, 0x14, 0x66, + 0x14, 0x02, 0x71, 0x34, 0x40, 0x86, 0x02, 0xd1, + 0x39, 0x27, 0x15, 0x02, 0x2e, 0x16, 0xa8, 0x14, + 0x02, 0xc3, 0x33, 0x42, 0x86, 0x02, 0x69, 0x14, + 0x66, 0x14, 0x02, 0x68, 0x34, 0x96, 0x86, 0x02, + 0xd1, 0x39, 0x70, 0x1a, 0x03, 0x69, 0x14, 0x64, + 0x87, 0x68, 0x14, 0x02, 0x69, 0x34, 0xa4, 0x13, + 0x02, 0xb8, 0x39, 0x40, 0x86, 0x02, 0x68, 0x34, + 0x3e, 0x13, 0x03, 0xd1, 0x19, 0xaf, 0x19, 0xa1, + 0x87, 0x02, 0xd1, 0x39, 0x3e, 0x13, 0x02, 0x68, + 0x34, 0xbd, 0x19, 0x02, 0xd1, 0x19, 0xbb, 0x14, + 0x02, 0xd1, 0x19, 0x95, 0x86, 0x02, 0xdb, 0x39, + 0x42, 0x86, 0x02, 0x38, 0x39, 0x40, 0x86, 0x02, + 0x69, 0x34, 0x80, 0x16, 0x02, 0x69, 0x14, 0xeb, + 0x13, 0x04, 0x68, 0x14, 0x69, 0x14, 0x67, 0x14, + 0x67, 0x14, 0x02, 0x6f, 0x34, 0x40, 0x86, 0x02, + 0x77, 0x34, 0x42, 0x86, 0x02, 0x46, 0x36, 0x40, + 0x86, 0x02, 0x68, 0x34, 0x92, 0x16, 0x02, 0x4e, + 0x36, 0x42, 0x86, 0x03, 0x69, 0x14, 0xbd, 0x19, + 0xa1, 0x87, 0x02, 0xde, 0x19, 0x40, 0x86, 0x02, + 0x69, 0x34, 0x27, 0x15, 0x03, 0xc3, 0x13, 0x40, + 0x86, 0xa1, 0x87, 0x02, 0x81, 0x14, 0x40, 0x86, + 0x03, 0xd1, 0x39, 0xaf, 0x19, 0xa1, 0x87, 0x02, + 0x68, 0x34, 0xbc, 0x19, 0x02, 0xd1, 0x19, 0x80, + 0x16, 0x02, 0xd9, 0x39, 0x42, 0x86, 0x02, 0xd1, + 0x39, 0xbc, 0x19, 0x02, 0xdc, 0x19, 0x42, 0x86, + 0x02, 0x68, 0x34, 0x73, 0x13, 0x02, 0x69, 0x34, + 0x3e, 0x13, 0x02, 0x47, 0x16, 0x40, 0x86, 0x02, + 0xd1, 0x39, 0xbd, 0x19, 0x02, 0x3e, 0x39, 0x42, + 0x86, 0x02, 0x69, 0x14, 0x95, 0x86, 0x02, 0x68, + 0x14, 0x96, 0x86, 0x03, 0x69, 0x34, 0xbd, 0x19, + 0xa1, 0x87, 0x02, 0xd7, 0x39, 0x40, 0x86, 0x02, + 0x45, 0x16, 0x42, 0x86, 0x02, 0x68, 0x34, 0xed, + 0x13, 0x03, 0x68, 0x34, 0xbc, 0x19, 0xa1, 0x87, + 0x02, 0xd1, 0x39, 0xed, 0x13, 0x02, 0x3c, 0x39, + 0x40, 0x86, 0x02, 0xd1, 0x19, 0x70, 0x1a, 0x02, + 0xd1, 0x39, 0x92, 0x16, 0x02, 0x73, 0x34, 0x40, + 0x86, 0x02, 0x38, 0x19, 0x40, 0x86, 0x02, 0xb5, + 0x36, 0x40, 0x86, 0x02, 0x68, 0x34, 0xaf, 0x19, + 0x02, 0xd1, 0x39, 0xaf, 0x19, 0x02, 0x69, 0x34, + 0xbc, 0x19, 0x02, 0xb6, 0x16, 0x42, 0x86, 0x02, + 0x26, 0x14, 0x25, 0x15, 0x02, 0xc3, 0x33, 0x40, + 0x86, 0x02, 0xdd, 0x39, 0x42, 0x86, 0x02, 0xcb, + 0x93, 0x42, 0x86, 0x02, 0xcb, 0x33, 0x42, 0x86, + 0x02, 0x81, 0x34, 0x42, 0x86, 0x02, 0xce, 0x39, + 0xa1, 0x87, 0x02, 0xdb, 0x39, 0x40, 0x86, 0x02, + 0x68, 0x34, 0x08, 0x87, 0x02, 0xd1, 0x19, 0xb0, + 0x19, 0x02, 0x77, 0x34, 0x40, 0x86, 0x02, 0x4e, + 0x36, 0x40, 0x86, 0x02, 0xce, 0x39, 0x42, 0x86, + 0x02, 0x4e, 0x16, 0x42, 0x86, 0x02, 0xd9, 0x39, + 0x40, 0x86, 0x02, 0xdc, 0x19, 0x40, 0x86, 0x02, + 0x3e, 0x39, 0x40, 0x86, 0x02, 0xb9, 0x39, 0x42, + 0x86, 0x02, 0xda, 0x19, 0x42, 0x86, 0x02, 0x42, + 0x16, 0x94, 0x81, 0x02, 0x45, 0x16, 0x40, 0x86, + 0x02, 0x69, 0x14, 0xbd, 0x19, 0x02, 0x70, 0x34, + 0x42, 0x86, 0x02, 0xce, 0x19, 0xa1, 0x87, 0x02, + 0xc3, 0x13, 0x42, 0x86, 0x02, 0x68, 0x14, 0x08, + 0x87, 0x02, 0xd1, 0x19, 0x7c, 0x13, 0x02, 0x68, + 0x14, 0x92, 0x16, 0x02, 0xb6, 0x16, 0x40, 0x86, + 0x02, 0x37, 0x39, 0x42, 0x86, 0x03, 0xce, 0x19, + 0x42, 0x86, 0xa1, 0x87, 0x03, 0x68, 0x14, 0x67, + 0x14, 0x67, 0x14, 0x02, 0xdd, 0x39, 0x40, 0x86, + 0x02, 0xcf, 0x19, 0x42, 0x86, 0x02, 0xd1, 0x19, + 0x2c, 0x15, 0x02, 0x4b, 0x13, 0xe9, 0x17, 0x02, + 0x68, 0x14, 0x67, 0x14, 0x02, 0xcb, 0x93, 0x40, + 0x86, 0x02, 0x6e, 0x34, 0x42, 0x86, 0x02, 0xcb, + 0x33, 0x40, 0x86, 0x02, 0x81, 0x34, 0x40, 0x86, + 0x02, 0xb6, 0x36, 0xa1, 0x87, 0x02, 0x45, 0x36, + 0x42, 0x86, 0x02, 0xb4, 0x16, 0x42, 0x86, 0x02, + 0x69, 0x14, 0x73, 0x13, 0x04, 0x69, 0x14, 0x69, + 0x14, 0x67, 0x14, 0x66, 0x14, 0x02, 0x35, 0x39, + 0x42, 0x86, 0x02, 0x68, 0x14, 0x93, 0x13, 0x02, + 0xb6, 0x36, 0x42, 0x86, 0x03, 0x68, 0x14, 0x69, + 0x14, 0x66, 0x14, 0x02, 0xce, 0x39, 0x40, 0x86, + 0x02, 0x4e, 0x16, 0x40, 0x86, 0x02, 0x87, 0x34, + 0x42, 0x86, 0x02, 0x86, 0x14, 0x42, 0x86, 0x02, + 0xd6, 0x39, 0x42, 0x86, 0x02, 0xc4, 0x33, 0x42, + 0x86, 0x02, 0x69, 0x34, 0x96, 0x86, 0x02, 0xb9, + 0x39, 0x40, 0x86, 0x02, 0x68, 0x14, 0xa8, 0x13, + 0x02, 0xd1, 0x19, 0x84, 0x13, 0x02, 0xda, 0x19, + 0x40, 0x86, 0x02, 0xd8, 0x19, 0x42, 0x86, 0x02, + 0xc3, 0x13, 0x40, 0x86, 0x02, 0xb9, 0x19, 0x42, + 0x86, 0x02, 0x3d, 0x19, 0x42, 0x86, 0x02, 0xcf, + 0x19, 0x40, 0x86, 0x04, 0x68, 0x14, 0x68, 0x14, + 0x67, 0x14, 0x67, 0x14, 0x03, 0xd1, 0x19, 0xd1, + 0x19, 0xd2, 0x19, 0x02, 0x68, 0x14, 0xbb, 0x14, + 0x02, 0x3b, 0x14, 0x44, 0x87, 0x02, 0xd1, 0x19, + 0x27, 0x15, 0x02, 0xb4, 0x16, 0x40, 0x86, 0x02, + 0xcd, 0x19, 0x42, 0x86, 0x02, 0xd3, 0x86, 0xa5, + 0x14, 0x02, 0x70, 0x14, 0x42, 0x86, 0x03, 0xb6, + 0x16, 0x42, 0x86, 0xa1, 0x87, 0x04, 0x69, 0x14, + 0x64, 0x87, 0x8b, 0x14, 0x69, 0x14, 0x02, 0x36, + 0x16, 0x2b, 0x93, 0x02, 0x68, 0x14, 0x80, 0x16, + 0x02, 0x86, 0x14, 0x40, 0x86, 0x02, 0x08, 0x14, + 0x1b, 0x0b, 0x02, 0xd1, 0x19, 0xbc, 0x19, 0x02, + 0xca, 0x13, 0x42, 0x86, 0x02, 0x41, 0x94, 0xe8, + 0x95, 0x02, 0xd8, 0x19, 0x40, 0x86, 0x02, 0xb9, + 0x19, 0x40, 0x86, 0x02, 0xd1, 0x19, 0xed, 0x13, + 0x02, 0xf9, 0x86, 0x42, 0x86, 0x03, 0xd1, 0x19, + 0xbd, 0x19, 0xa1, 0x87, 0x02, 0x3d, 0x19, 0x40, + 0x86, 0x02, 0xd6, 0x19, 0x42, 0x86, 0x03, 0x69, + 0x14, 0x66, 0x14, 0x66, 0x14, 0x02, 0xd1, 0x19, + 0xaf, 0x19, 0x03, 0x69, 0x14, 0x69, 0x14, 0x67, + 0x14, 0x02, 0xcd, 0x19, 0x40, 0x86, 0x02, 0x70, + 0x14, 0x40, 0x86, 0x03, 0x68, 0x14, 0xbc, 0x19, + 0xa1, 0x87, 0x02, 0x6e, 0x14, 0x42, 0x86, 0x02, + 0x69, 0x14, 0x92, 0x16, 0x03, 0x68, 0x14, 0x68, + 0x14, 0x67, 0x14, 0x02, 0x69, 0x14, 0x67, 0x14, + 0x02, 0x75, 0x95, 0x42, 0x86, 0x03, 0x69, 0x14, + 0x64, 0x87, 0x69, 0x14, 0x02, 0xd1, 0x19, 0xbc, + 0x14, 0x02, 0xdf, 0x19, 0x42, 0x86, 0x02, 0xca, + 0x13, 0x40, 0x86, 0x02, 0x82, 0x14, 0x42, 0x86, + 0x02, 0x69, 0x14, 0x93, 0x13, 0x02, 0x68, 0x14, + 0x7c, 0x13, 0x02, 0xf9, 0x86, 0x40, 0x86, 0x02, + 0xd6, 0x19, 0x40, 0x86, 0x02, 0x68, 0x14, 0x2c, + 0x15, 0x02, 0x69, 0x14, 0xa8, 0x13, 0x02, 0xd4, + 0x19, 0x42, 0x86, 0x04, 0x68, 0x14, 0x69, 0x14, + 0x66, 0x14, 0x66, 0x14, 0x02, 0x77, 0x14, 0x42, + 0x86, 0x02, 0x39, 0x19, 0x42, 0x86, 0x02, 0xd1, + 0x19, 0xa4, 0x13, 0x02, 0x6e, 0x14, 0x40, 0x86, + 0x03, 0xd1, 0x19, 0xd2, 0x19, 0xd2, 0x19, 0x02, + 0x69, 0x14, 0xbb, 0x14, 0x02, 0xd1, 0x19, 0x96, + 0x86, 0x02, 0x75, 0x95, 0x40, 0x86, 0x04, 0x68, + 0x14, 0x64, 0x87, 0x8b, 0x14, 0x68, 0x14, 0x02, + 0xd1, 0x19, 0x3e, 0x13, 0x02, 0xdf, 0x19, 0x40, + 0x86, 0x02, 0x82, 0x14, 0x40, 0x86, 0x02, 0x44, + 0x13, 0xeb, 0x17, 0x02, 0xdd, 0x19, 0x42, 0x86, + 0x03, 0x68, 0x14, 0xaf, 0x19, 0xa1, 0x87, 0x02, + 0x69, 0x14, 0x80, 0x16, 0x02, 0xa3, 0x16, 0x42, + 0x86, 0x02, 0x69, 0x14, 0x96, 0x86, 0x02, 0x46, + 0x16, 0x42, 0x86, 0x02, 0xb6, 0x16, 0xa1, 0x87, + 0x02, 0x68, 0x14, 0x27, 0x15, 0x02, 0x26, 0x14, + 0x1b, 0x0b, 0x02, 0xd4, 0x19, 0x40, 0x86, 0x02, + 0x77, 0x14, 0x40, 0x86, 0x02, 0x39, 0x19, 0x40, + 0x86, 0x02, 0x37, 0x19, 0x42, 0x86, 0x03, 0x69, + 0x14, 0x67, 0x14, 0x66, 0x14, 0x03, 0xc3, 0x13, + 0x42, 0x86, 0xa1, 0x87, 0x02, 0x68, 0x14, 0xbc, + 0x19, 0x02, 0xd1, 0x19, 0xeb, 0x13, 0x04, 0x69, + 0x14, 0x69, 0x14, 0x67, 0x14, 0x67, 0x14, 0x02, + 0xd1, 0x19, 0x08, 0x87, 0x02, 0x68, 0x14, 0xed, + 0x13, 0x03, 0x69, 0x14, 0xbc, 0x19, 0xa1, 0x87, + 0x02, 0xdd, 0x19, 0x40, 0x86, 0x02, 0xc3, 0x13, + 0xa1, 0x87, 0x03, 0x68, 0x14, 0x66, 0x14, 0x66, + 0x14, 0x03, 0x68, 0x14, 0x69, 0x14, 0x67, 0x14, + 0x02, 0xa3, 0x16, 0x40, 0x86, 0x02, 0xdb, 0x19, + 0x42, 0x86, 0x02, 0x68, 0x14, 0xaf, 0x19, 0x02, + 0x46, 0x16, 0x40, 0x86, 0x02, 0x35, 0x16, 0xab, + 0x14, 0x02, 0x68, 0x14, 0x95, 0x86, 0x02, 0x42, + 0x16, 0x95, 0x81, 0x02, 0xc4, 0x13, 0x42, 0x86, + 0x02, 0x15, 0x14, 0xba, 0x19, 0x03, 0xd1, 0x19, + 0x1d, 0x19, 0xd1, 0x19, 0x02, 0x69, 0x14, 0x08, + 0x87, 0x02, 0x69, 0x14, 0x7c, 0x13, 0x02, 0x37, + 0x19, 0x40, 0x86, 0x02, 0x73, 0x14, 0x42, 0x86, + 0x02, 0x69, 0x14, 0x2c, 0x15, 0x02, 0xb5, 0x16, + 0x42, 0x86, 0x02, 0x35, 0x19, 0x42, 0x86, 0x04, + 0x68, 0x14, 0x69, 0x14, 0x67, 0x14, 0x66, 0x14, + 0x02, 0x64, 0x87, 0x25, 0x15, 0x02, 0x64, 0x87, + 0x79, 0x1a, 0x02, 0x68, 0x14, 0xbc, 0x14, 0x03, + 0xce, 0x19, 0x40, 0x86, 0xa1, 0x87, 0x02, 0x87, + 0x14, 0x42, 0x86, 0x02, 0x4d, 0x16, 0x42, 0x86, + 0x04, 0x68, 0x14, 0x68, 0x14, 0x66, 0x14, 0x66, + 0x14, 0x02, 0xdb, 0x19, 0x40, 0x86, 0x02, 0xd9, + 0x19, 0x42, 0x86, 0x02, 0xc4, 0x13, 0x40, 0x86, + 0x02, 0xd1, 0x19, 0xbd, 0x19, 0x02, 0x68, 0x14, + 0xa4, 0x13, 0x02, 0x3e, 0x19, 0x42, 0x86, 0x02, + 0xf3, 0x93, 0xa7, 0x86, 0x03, 0x69, 0x14, 0xaf, + 0x19, 0xa1, 0x87, 0x02, 0xf3, 0x93, 0x08, 0x13, + 0x02, 0xd1, 0x19, 0xd2, 0x19, 0x02, 0x73, 0x14, + 0x40, 0x86, 0x02, 0xb5, 0x16, 0x40, 0x86, 0x02, + 0x35, 0x19, 0x40, 0x86, 0x02, 0x69, 0x14, 0x27, + 0x15, 0x02, 0xce, 0x19, 0x42, 0x86, 0x02, 0x71, + 0x14, 0x42, 0x86, 0x02, 0xd1, 0x19, 0x73, 0x13, + 0x02, 0x68, 0x14, 0x3e, 0x13, 0x02, 0xf4, 0x13, + 0x20, 0x86, 0x02, 0x87, 0x14, 0x40, 0x86, 0x03, + 0xb6, 0x16, 0x40, 0x86, 0xa1, 0x87, 0x02, 0x4d, + 0x16, 0x40, 0x86, 0x02, 0x69, 0x14, 0xbc, 0x19, + 0x02, 0x4b, 0x16, 0x42, 0x86, 0x02, 0xd9, 0x19, + 0x40, 0x86, 0x02, 0x3e, 0x19, 0x40, 0x86, 0x02, + 0x69, 0x14, 0xed, 0x13, 0x02, 0xd7, 0x19, 0x42, + 0x86, 0x02, 0xb8, 0x19, 0x42, 0x86, 0x03, 0x68, + 0x14, 0x67, 0x14, 0x66, 0x14, 0x02, 0x3c, 0x19, + 0x42, 0x86, 0x02, 0x68, 0x14, 0x66, 0x14, 0x03, + 0x68, 0x14, 0x64, 0x87, 0x68, 0x14, 0x02, 0x69, + 0x14, 0xaf, 0x19, 0x02, 0xce, 0x19, 0x40, 0x86, + 0x02, 0x71, 0x14, 0x40, 0x86, 0x02, 0x68, 0x14, + 0xeb, 0x13, 0x03, 0x68, 0x14, 0xbd, 0x19, 0xa1, + 0x87, 0x02, 0x6f, 0x14, 0x42, 0x86, 0x04, 0xd1, + 0x19, 0xd1, 0x19, 0xd2, 0x19, 0xd2, 0x19, 0x02, + 0x69, 0x14, 0xbc, 0x14, 0x02, 0xcc, 0x93, 0x42, + 0x86, 0x02, 0x4b, 0x16, 0x40, 0x86, 0x02, 0x26, + 0x19, 0x42, 0x86, 0x02, 0xd7, 0x19, 0x40, 0x86, +}; + /* * QuickJS Javascript Engine * @@ -7245,7 +7773,7 @@ extern "C" { # define JS_EXTERN /* nothing */ # endif #else -# ifdef QUICKJS_NG_CC_GNULIKE +# if defined(BUILDING_QJS_SHARED) && defined(QUICKJS_NG_CC_GNULIKE) # define JS_EXTERN __attribute__((visibility("default"))) # else # define JS_EXTERN /* nothing */ @@ -7286,7 +7814,7 @@ extern "C" { # define JS_MODULE_EXTERN __declspec(dllimport) # endif #else -# ifdef QUICKJS_NG_CC_GNULIKE +# if defined(QUICKJS_NG_MODULE_BUILD) && defined(QUICKJS_NG_CC_GNULIKE) # define JS_MODULE_EXTERN __attribute__((visibility("default"))) # else # define JS_MODULE_EXTERN /* nothing */ @@ -7312,13 +7840,31 @@ extern "C" { #endif #endif +/* + * `JS_CALL_X` -- macro tree for calling a function with a variable number of arguments. + * This is used for bulk operations such as freeing values or atoms. + */ +#define JS_COUNT_ARGS_(_0, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N +#define JS_COUNT_ARGS(...) JS_COUNT_ARGS_(0, __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +#define JS_CALLX1(F,X,a) do { F(X,a); } while (0) +#define JS_CALLX2(F,X,a,b) do { F(X,a); F(X,b); } while (0) +#define JS_CALLX3(F,X,a,b,c) do { F(X,a); F(X,b); F(X,c); } while (0) +#define JS_CALLX4(F,X,a,b,c,d) do { F(X,a); F(X,b); F(X,c); F(X,d); } while (0) +#define JS_CALLX5(F,X,a,b,c,d,e) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); } while (0) +#define JS_CALLX6(F,X,a,b,c,d,e,f) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); } while (0) +#define JS_CALLX7(F,X,a,b,c,d,e,f,g) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); F(X,g); } while (0) +#define JS_CALLX8(F,X,a,b,c,d,e,f,g,h) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); F(X,g); F(X,h); } while (0) + +#define JS_CALLX__(F, X, N, ...) JS_CALLX##N(F, X, __VA_ARGS__) +#define JS_CALLX_(F, X, N, ...) JS_CALLX__(F, X, N, __VA_ARGS__) + #undef QUICKJS_NG_CC_GNULIKE #undef QUICKJS_NG_PLAT_WIN32 typedef struct JSRuntime JSRuntime; typedef struct JSContext JSContext; typedef struct JSObject JSObject; -typedef struct JSClass JSClass; typedef uint32_t JSClassID; typedef uint32_t JSAtom; @@ -7401,12 +7947,6 @@ static inline JSValue __JS_NewFloat64(double d) return JS_MKVAL(JS_TAG_FLOAT64, (int)d); } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d) -{ - (void)&ctx; - return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d); -} - static inline bool JS_VALUE_IS_NAN(JSValue v) { (void)&v; @@ -7457,12 +7997,6 @@ static inline JSValue __JS_NewFloat64(double d) return v; } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d) -{ - (void)&ctx; - return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d); -} - #define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST)) /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */ @@ -7486,7 +8020,10 @@ static inline bool JS_VALUE_IS_NAN(JSValue v) #else /* !JS_NAN_BOXING */ typedef union JSValueUnion { - int32_t int32; + /* JS_MKVAL() writes all 64 bits even though only 32 are needed: + leaving the upper half uninitialized makes GCC 14+ generate much + slower code on x86-64 */ + uint64_t uint64; double float64; void *ptr; int32_t short_big_int; @@ -7500,10 +8037,10 @@ typedef struct JSValue { #define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag) /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */ #define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v) -#define JS_VALUE_GET_INT(v) ((v).u.int32) -#define JS_VALUE_GET_BOOL(v) ((v).u.int32) +#define JS_VALUE_GET_INT(v) ((int)(v).u.uint64) +#define JS_VALUE_GET_BOOL(v) ((int)(v).u.uint64) #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64) -#define JS_VALUE_GET_SHORT_BIG_INT(v) ((v).u.short_big_int) +#define JS_VALUE_GET_SHORT_BIG_INT(v) ((int32_t)(v).u.uint64) #define JS_VALUE_GET_PTR(v) ((v).u.ptr) /* msvc doesn't understand designated initializers without /std:c++20 */ @@ -7518,7 +8055,7 @@ static inline JSValue JS_MKPTR(int64_t tag, void *ptr) static inline JSValue JS_MKVAL(int64_t tag, int32_t int32) { JSValue v; - v.u.int32 = int32; + v.u.uint64 = (uint32_t)int32; v.tag = tag; return v; } @@ -7535,7 +8072,7 @@ static inline JSValue JS_MKNAN(void) #define JS_NAN JS_MKNAN() /* alas, not a constant expression */ #else #define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag } -#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag } +#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .uint64 = (uint32_t)(val) }, tag } #define JS_NAN (JSValue){ (JSValueUnion){ .float64 = NAN }, JS_TAG_FLOAT64 } #endif @@ -7549,15 +8086,6 @@ static inline JSValue __JS_NewFloat64(double d) return v; } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d) -{ - (void)&ctx; - JSValue v; - v.tag = JS_TAG_SHORT_BIG_INT; - v.u.short_big_int = d; - return v; -} - static inline bool JS_VALUE_IS_NAN(JSValue v) { union { @@ -7676,6 +8204,7 @@ typedef struct JSMallocFunctions { #define JS_DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */ #define JS_DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */ #define JS_DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */ +#define JS_ABORT_ON_LEAKS 0x10C000 /* abort on atom/object/string leaks; for testing */ // Finalizers run in LIFO order at the very end of JS_FreeRuntime. // Intended for cleanup of associated resources; the runtime itself @@ -7737,6 +8266,7 @@ JS_EXTERN int JS_AddIntrinsicBigInt(JSContext *ctx); JS_EXTERN int JS_AddIntrinsicWeakRef(JSContext *ctx); JS_EXTERN int JS_AddPerformance(JSContext *ctx); JS_EXTERN int JS_AddIntrinsicDOMException(JSContext *ctx); +JS_EXTERN int JS_AddIntrinsicAToB(JSContext *ctx); /* for equality comparisons and sameness */ JS_EXTERN int JS_IsEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2); @@ -7761,7 +8291,6 @@ JS_EXTERN void *js_malloc(JSContext *ctx, size_t size); JS_EXTERN void js_free(JSContext *ctx, void *ptr); JS_EXTERN void *js_realloc(JSContext *ctx, void *ptr, size_t size); JS_EXTERN size_t js_malloc_usable_size(JSContext *ctx, const void *ptr); -JS_EXTERN void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack); JS_EXTERN void *js_mallocz(JSContext *ctx, size_t size); JS_EXTERN char *js_strdup(JSContext *ctx, const char *str); JS_EXTERN char *js_strndup(JSContext *ctx, const char *s, size_t n); @@ -7795,10 +8324,12 @@ JS_EXTERN JSAtom JS_DupAtom(JSContext *ctx, JSAtom v); JS_EXTERN JSAtom JS_DupAtomRT(JSRuntime *rt, JSAtom v); JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v); JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v); +#define JS_FreeAtoms(ctx, ...) JS_CALLX_(JS_FreeAtom, ctx, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) +#define JS_FreeAtomsRT(rt, ...) JS_CALLX_(JS_FreeAtomRT, rt, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom); JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom); JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom); -static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) +static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) { return JS_AtomToCStringLen(ctx, NULL, atom); } @@ -7937,6 +8468,17 @@ static inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val) return v; } +static inline JSValue JS_NewUint64(JSContext *ctx, uint64_t val) +{ + JSValue v; + if (val <= INT32_MAX) { + v = JS_NewInt32(ctx, (int32_t)val); + } else { + v = JS_NewFloat64(ctx, (double)val); + } + return v; +} + JS_EXTERN JSValue JS_NewNumber(JSContext *ctx, double d); JS_EXTERN JSValue JS_NewBigInt64(JSContext *ctx, int64_t v); JS_EXTERN JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v); @@ -8012,12 +8554,14 @@ JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValueConst val); // JS_Throw(ctx, exc); JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx); JS_EXTERN JSValue JS_NewError(JSContext *ctx); +// InternalError cannot be caught by JS code JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); +// InternalError cannot be caught by JS code JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); @@ -8028,6 +8572,8 @@ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowDOMException(JSContext *ct JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx); JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v); JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v); +#define JS_FreeValues(ctx, ...) JS_CALLX_(JS_FreeValue, ctx, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) +#define JS_FreeValuesRT(rt, ...) JS_CALLX_(JS_FreeValueRT, rt, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValueConst v); JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v); JS_EXTERN int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */ @@ -8141,6 +8687,8 @@ JS_EXTERN JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst this_obj, int64_t idx); JS_EXTERN JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj, const char *prop); +JS_EXTERN JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop); JS_EXTERN int JS_SetProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop, JSValue val); @@ -8150,6 +8698,9 @@ JS_EXTERN int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj, int64_t idx, JSValue val); JS_EXTERN int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj, const char *prop, JSValue val); +// |flags| is a mix of JS_PROP_* values; when in doubt use JS_PROP_C_W_E +JS_EXTERN int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop, JSValue val, int flags); JS_EXTERN int JS_HasProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop); JS_EXTERN int JS_IsExtensible(JSContext *ctx, JSValueConst obj); JS_EXTERN int JS_PreventExtensions(JSContext *ctx, JSValueConst obj); @@ -8232,10 +8783,24 @@ JS_EXTERN JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, JS_EXTERN JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj, JSValueConst replacer, JSValueConst space0); -typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque, void *ptr); +/* Manages the backing memory of an externally created ArrayBuffer. + When 'size' is zero, 'ptr' must be freed and NULL returned. Otherwise the + block must be resized to 'size' bytes and the new pointer returned, or NULL + if that is not possible, indicating a memory error, in which case 'ptr' must + stay valid and the ArrayBuffer keeps its current size. */ +typedef void *JSReallocArrayBufferDataFunc(JSRuntime *rt, void *opaque, + void *ptr, size_t size); +/* Creates an ArrayBuffer backed by 'buf'. Pass a zero 'max_len' for a regular, + fixed length ArrayBuffer; a non-zero 'max_len' makes it resizable and is its + maxByteLength. 'realloc_func' may be NULL if the memory must not be managed + by quickjs at all; such an ArrayBuffer can neither be resized nor + transferred to a different length. It is required for resizable + ArrayBuffers, except for SharedArrayBuffers: those commit their maximum size + upfront and therefore need 'buf' to be at least 'max_len' bytes big. */ JS_EXTERN JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared); + size_t max_len, + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared); JS_EXTERN JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len); JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj); JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj); @@ -8268,8 +8833,8 @@ JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_length, size_t *pbytes_per_element); JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared); + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared); /* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */ JS_EXTERN int JS_GetTypedArrayType(JSValueConst obj); JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len); @@ -8290,13 +8855,20 @@ typedef enum JSPromiseStateEnum { } JSPromiseStateEnum; JS_EXTERN JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs); +JS_EXTERN JSValue JS_PromiseThen(JSContext *ctx, JSValueConst promise, + JSValueConst on_fulfilled, + JSValueConst on_rejected); JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValueConst promise); JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValueConst promise); JS_EXTERN bool JS_IsPromise(JSValueConst val); +JS_EXTERN void JS_PromiseMarkAsHandled(JSContext *ctx, JSValueConst promise); JS_EXTERN JSValue JS_NewSettledPromise(JSContext *ctx, bool is_reject, JSValueConst value); +// a symbol that is visible to JavaScript code JS_EXTERN JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global); +// a symbol that is only visible to C code +JS_EXTERN JSValue JS_NewPrivateSymbol(JSContext *ctx, const char *description); typedef enum JSPromiseHookType { JS_PROMISE_HOOK_INIT, // emitted when a new promise is created @@ -8322,7 +8894,7 @@ typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise, JS_EXTERN void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque); /* return != 0 if the JS code needs to be interrupted */ -typedef int JSInterruptHandler(JSRuntime *rt, void *opaque); +typedef int JSInterruptHandler(JSContext *ctx, void *opaque); JS_EXTERN void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque); /* if can_block is true, Atomics.wait() can be used */ JS_EXTERN void JS_SetCanBlock(JSRuntime *rt, bool can_block); @@ -8344,6 +8916,35 @@ typedef char *JSModuleNormalizeFunc2(JSContext *ctx, typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx, const char *module_name, void *opaque); +// Dynamic module loading in detail: +// +// 1. JS code calls `import(url, {with: /*...*/})` +// +// 2. If there are import attributes, JSModuleCheckSupportedImportAttributes +// runs synchronously (with the |opaque| passed to JS_SetModuleLoaderFunc2) +// +// 3. An unfulfilled promise is returned to the JS caller +// +// 4. A job is posted to the microtask queue that runs when something +// calls JS_ExecutePendingJob or js_std_await +// +// 5. The JSDynamicImportFunc callback is called +// +// 6. You, the embedder, load the resource at your convenience and eventually +// call JS_LoadModule(). Pass the result to JS_FinishDynamicImport(). +// +// Step 6 means the non-dynamic module loader needs to be aware of resources +// loaded by the dynamic module loader. How to implement that is up to you. +// +// Ownership of |token| passes to JSDynamicImportFunc. If you need to hold +// on to |basename|, |filename| or |attributes|, strdup or JS_DupValue them. +// +// You must *always* call JS_FinishDynamicImport(), even on error, otherwise +// the resources associated with |token| leak. +typedef void JSDynamicImportFunc(JSContext *ctx, const char *basename, + const char *filename, JSValue token, + JSValueConst attributes, void *opaque); + /* module loader with import attributes support */ typedef JSModuleDef *JSModuleLoaderFunc2(JSContext *ctx, const char *module_name, void *opaque, @@ -8366,10 +8967,18 @@ JS_EXTERN void JS_SetModuleLoaderFunc2(JSRuntime *rt, JSModuleCheckSupportedImportAttributes *module_check_attrs, void *opaque); +JS_EXTERN void JS_SetDynamicImportFunc(JSRuntime *rt, JSDynamicImportFunc *func, + void *opaque); + /* Set an attributes-aware module normalizer. Call after JS_SetModuleLoaderFunc2. */ JS_EXTERN void JS_SetModuleNormalizeFunc2(JSRuntime *rt, JSModuleNormalizeFunc2 *module_normalize); +// pass promise=JS_EXCEPTION and exception= if an exception +// occurred during module loading; pass exception=JS_UNDEFINED otherwise +JS_EXTERN int JS_FinishDynamicImport(JSContext *ctx, JSValue token, + JSValue promise, JSValue exception); + /* return the import.meta object of a module */ JS_EXTERN JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m); JS_EXTERN JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m); @@ -8406,8 +9015,14 @@ JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst ob JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj, int flags, JSSABTab *psab_tab); +/* WARNING: only enable JS_READ_OBJ_BYTECODE on input from a trusted + writer. The bytecode format is not designed to resist a hostile + producer; loading adversarial bytecode can lead to memory corruption. */ #define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */ #define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */ +/* WARNING: serialized SharedArrayBuffers carry a literal host pointer in + the blob; only enable JS_READ_OBJ_SAB on input produced by a trusted + writer in the same process (e.g. another Worker on the same runtime). */ #define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */ #define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */ JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags); @@ -8422,9 +9037,9 @@ JS_EXTERN int JS_ResolveModule(JSContext *ctx, JSValueConst obj); /* only exported for os.Worker() */ JS_EXTERN JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels); -/* only exported for os.Worker() */ +/* don't call this recursively */ JS_EXTERN JSValue JS_LoadModule(JSContext *ctx, const char *basename, - const char *filename); + const char *filename, JSValueConst attributes); /* C function definition */ typedef enum JSCFunctionEnum { /* XXX: should rename for namespace isolation */ @@ -8552,6 +9167,7 @@ typedef struct JSCFunctionListEntry { #define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } } #define JS_CGETSET_DEF2(name, fgetter, fsetter, prop_flags) { name, prop_flags, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } } #define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } } +#define JS_CGETSET_MAGIC_DEF2(name, fgetter, fsetter, magic, prop_flags) { name, prop_flags, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } } #define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } } #define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } } #define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } } @@ -8587,7 +9203,7 @@ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m, /* Version */ #define QJS_VERSION_MAJOR 0 -#define QJS_VERSION_MINOR 14 +#define QJS_VERSION_MINOR 18 #define QJS_VERSION_PATCH 0 #define QJS_VERSION_SUFFIX "" @@ -8779,6 +9395,7 @@ enum { JS_CLASS_STRING_ITERATOR, /* u.array_iterator_data */ JS_CLASS_REGEXP_STRING_ITERATOR, /* u.regexp_string_iterator_data */ JS_CLASS_GENERATOR, /* u.generator_data */ + JS_CLASS_DISPOSABLE_STACK, JS_CLASS_PROXY, /* u.proxy_data */ JS_CLASS_PROMISE, /* u.promise_data */ JS_CLASS_PROMISE_RESOLVE_FUNCTION, /* u.promise_function_data */ @@ -8789,6 +9406,7 @@ enum { JS_CLASS_ASYNC_FROM_SYNC_ITERATOR, /* u.async_from_sync_iterator_data */ JS_CLASS_ASYNC_GENERATOR_FUNCTION, /* u.func */ JS_CLASS_ASYNC_GENERATOR, /* u.async_generator_data */ + JS_CLASS_ASYNC_DISPOSABLE_STACK, JS_CLASS_WEAK_REF, JS_CLASS_FINALIZATION_REGISTRY, JS_CLASS_DOM_EXCEPTION, @@ -8812,6 +9430,7 @@ typedef enum JSErrorEnum { JS_URI_ERROR, JS_INTERNAL_ERROR, JS_AGGREGATE_ERROR, + JS_SUPPRESSED_ERROR, JS_NATIVE_ERROR_COUNT, /* number of different NativeError objects */ JS_PLAIN_ERROR = JS_NATIVE_ERROR_COUNT @@ -8856,6 +9475,63 @@ typedef struct JSMallocState { void *opaque; /* user opaque */ } JSMallocState; +/* Small-block "arena" allocator. + js_{malloc,free,realloc,calloc}_rt serve allocations up to ~512 + bytes from size-classed 4KB arenas carved out of the underlying + JSMallocFunctions allocator, so the backing allocator (system malloc, + mimalloc, ...) sees roughly one request per arena refill instead of one per + object. */ + +#define JS_ARENA_ALIGN 8 +#define JS_ARENA_SIZE 4096 +#define JS_ARENA_BLOCK_SIZE_COUNT 31 +#define JS_ARENA_MAX_SMALL_SIZE 512 +#define JS_ARENA_FREE_NIL 0xffff + +#if defined(__SANITIZE_ADDRESS__) || defined(FORCE_GC_AT_MALLOC) +/* Route every allocation through the backing malloc so each block is seen + individually: ASan can then poison a freed block, and a GC-stress build + (FORCE_GC_AT_MALLOC) exposes prematurely-freed objects instead of the pool + silently recycling them. */ +#define JS_ARENA_LARGE_BLOCKS_ONLY 1 +#else +#define JS_ARENA_LARGE_BLOCKS_ONLY 0 +#endif + +/* 8-byte header preceding every user allocation. It carries the allocator + bookkeeping (block_idx/free_next + block_size_idx) and, the + GC/refcount fields (gc_obj_type/mark/ref_count) that would otherwise sit in + the object body. The 8-byte size keeps user_data JS_ARENA_ALIGN-aligned. */ +typedef struct JSMallocBlockHeader { + union { + uint16_t block_idx; /* JS_ARENA_FREE_NIL => large or zero-size block */ + uint16_t free_next; /* next free block index while on a free list */ + } u; + uint8_t block_size_idx; + /* GC/refcount header merged into the allocator header: + the object body keeps only JSGCObjectHeader.link (no ref_count/flags). */ + uint8_t gc_obj_type : 7; /* JSGCObjectTypeEnum for GC objects */ + uint8_t mark : 1; /* used by the cycle collector */ + int ref_count; + _Alignas(JS_ARENA_ALIGN) uint8_t user_data[]; +} JSMallocBlockHeader; + +typedef struct JSArena { + struct list_head free_link; /* in free_arena_list[idx] while not full */ + struct list_head link; /* in arena_list[idx] for the whole lifetime */ + uint8_t block_size_idx; + uint16_t n_used_blocks; + uint16_t n_blocks; + uint16_t first_free_block; /* JS_ARENA_FREE_NIL if none */ + _Alignas(JS_ARENA_ALIGN) uint8_t blocks[]; +} JSArena; + +typedef struct JSArenaState { + struct list_head arena_list[JS_ARENA_BLOCK_SIZE_COUNT]; + struct list_head free_arena_list[JS_ARENA_BLOCK_SIZE_COUNT]; + _Alignas(JS_ARENA_ALIGN) uint8_t zero_size_block[sizeof(JSMallocBlockHeader)]; +} JSArenaState; + typedef struct JSRuntimeFinalizerState { struct JSRuntimeFinalizerState *next; JSRuntimeFinalizer *finalizer; @@ -8867,9 +9543,20 @@ typedef struct JSValueLink { JSValueConst value; } JSValueLink; +typedef struct JSClass { + uint32_t class_id; /* 0 means free entry */ + JSAtom class_name; + JSClassFinalizer *finalizer; + JSClassGCMark *gc_mark; + JSClassCall *call; + /* pointers for exotic behavior, can be NULL if none are present */ + const JSClassExoticMethods *exotic; +} JSClass; + struct JSRuntime { JSMallocFunctions mf; JSMallocState malloc_state; + JSArenaState arena_state; const char *rt_info; int atom_hash_size; /* power of two */ @@ -8937,6 +9624,8 @@ struct JSRuntime { } u; JSModuleCheckSupportedImportAttributes *module_check_attrs; void *module_loader_opaque; + JSDynamicImportFunc *dynamic_import; + void *dynamic_import_opaque; /* timestamp for internal use in module evaluation */ int64_t module_async_evaluation_next_timestamp; @@ -8956,16 +9645,6 @@ struct JSRuntime { JSRuntimeFinalizerState *finalizers; }; -struct JSClass { - uint32_t class_id; /* 0 means free entry */ - JSAtom class_name; - JSClassFinalizer *finalizer; - JSClassGCMark *gc_mark; - JSClassCall *call; - /* pointers for exotic behavior, can be NULL if none are present */ - const JSClassExoticMethods *exotic; -}; - typedef struct JSStackFrame { struct JSStackFrame *prev_frame; /* NULL if first stack frame */ JSValue cur_func; /* current function, JS_UNDEFINED if the frame is detached */ @@ -8977,9 +9656,16 @@ typedef struct JSStackFrame { uint16_t var_ref_count; /* number of var refs */ uint16_t arg_count; bool is_strict_mode; + bool is_constructor; /* true if invoked as a constructor (new) */ /* only used in generators. Current stack pointer value. NULL if the function is running. */ JSValue *cur_sp; + /* only set for coroutine frames (async function / generator / + async generator): the GC object owning this heap-allocated frame, + NULL for ordinary C-stack frames. Lets a var_ref capturing one of + the coroutine's locals keep the (suspended) coroutine reachable by + the cycle collector. */ + struct JSGCObjectHeader *cur_gc_obj; } JSStackFrame; typedef enum { @@ -8995,26 +9681,31 @@ typedef enum { reference count that can reference other GC objects. JS Objects are a particular type of GC object. */ struct JSGCObjectHeader { - int ref_count; /* must come first, 32-bit */ - JSGCObjectTypeEnum gc_obj_type : 4; - uint8_t mark : 1; /* used by the GC */ - uint8_t dummy0 : 3; - uint8_t dummy1; /* not used by the GC */ - uint16_t dummy2; /* not used by the GC */ + /* ref_count/gc_obj_type/mark live in the allocator block header (js_rc(p), + the 8 bytes before every allocation); only the GC list link remains in + the object body. */ struct list_head link; }; typedef struct JSVarRef { - union { - JSGCObjectHeader header; /* must come first */ - struct { - int __gc_ref_count; /* corresponds to header.ref_count */ - uint8_t __gc_mark; /* corresponds to header.mark/gc_obj_type */ - uint8_t is_detached; - uint8_t is_lexical; /* only used with global variables */ - uint8_t is_const; /* only used with global variables */ - }; - }; + JSGCObjectHeader header; /* {link}; must come first so &p->header == p */ + uint8_t is_detached; + uint8_t is_lexical; /* only used with global variables */ + uint8_t is_const; /* only used with global variables */ + /* Set at creation for an open var_ref that captures a local of a + coroutine (async function / generator / async generator): such a + var_ref holds a counted reference to that coroutine's GC object and + is itself a GC object, so the cycle collector can see the closure -> + var_ref -> coroutine edge and keep the suspended coroutine (hence the + captured variable it points into) alive. The coroutine is recovered + as stack_frame->cur_gc_obj (valid while open). This is a *snapshot* of + cur_gc_obj != NULL taken at creation, NOT rederived at read time: + cur_gc_obj transitions NULL -> owner (it is set only after a + generator's initial resume), so var_refs created during the prologue + (e.g. mapped `arguments`) must stay non-coro even though cur_gc_obj is + later set. Cleared when the var_ref is detached; the ref is released on + detach/free. */ + uint8_t is_coro; JSValue *pvalue; /* pointer to the value, either on the stack or to 'value' */ union { @@ -9026,9 +9717,17 @@ typedef struct JSVarRef { }; } JSVarRef; -typedef struct JSRefCountHeader { - int ref_count; -} JSRefCountHeader; +/* Accessors for the reference count and GC mark/type. These fields live in the + arena block header (the 8 bytes before every allocation, reached via + js_rc()), not in the object body. The macros yield lvalues, and the argument + is always the object/header pointer (whose address equals the GC header's, + since the header is the first member). */ +static inline JSMallocBlockHeader *js_rc(const void *p) { + return (JSMallocBlockHeader *)((uint8_t *)(uintptr_t)p - offsetof(JSMallocBlockHeader, user_data)); +} +#define JS_REF_COUNT(p) (js_rc(p)->ref_count) +#define JS_GC_TYPE(p) (js_rc(p)->gc_obj_type) +#define JS_GC_MARK(p) (js_rc(p)->mark) /* bigint */ typedef int32_t js_slimb_t; @@ -9047,7 +9746,6 @@ typedef uint64_t js_dlimb_t; typedef struct JSBigInt { - JSRefCountHeader header; /* must come first, 32-bit */ uint32_t len; /* number of limbs, >= 1 */ js_limb_t tab[]; /* two's complement representation, always normalized so that 'len' is the minimum @@ -9125,6 +9823,7 @@ struct JSContext { double time_origin; uint64_t random_state; + uint32_t hash_seed; /* when the counter reaches zero, JSRutime.interrupt_handler is called */ int interrupt_counter; @@ -9216,7 +9915,6 @@ typedef enum { #define JS_ATOM_HASH_MASK ((1 << 28) - 1) struct JSString { - JSRefCountHeader header; /* must come first, 32-bit */ uint32_t len : 31; uint32_t is_wide_char : 1; /* 0 = 8 bits, 1 = 16 bits characters */ /* for JS_ATOM_TYPE_SYMBOL: hash = 0, atom_type = 3, @@ -9238,7 +9936,6 @@ typedef struct JSStringSlice { } JSStringSlice; struct JSStringRope { - JSRefCountHeader header; /* must come first, 32-bit */ uint32_t len; uint8_t is_wide_char; /* 0 = 8 bits, 1 = 16 bits characters */ uint8_t depth; /* max depth of the rope tree */ @@ -9305,6 +10002,10 @@ typedef struct JSClosureVar { typedef struct JSVarScope { int parent; /* index into fd->scopes of the enclosing scope */ int first; /* index into fd->vars of the last variable in this scope */ + uint8_t has_using : 1; /* scope has using declarations */ + uint8_t is_await_using : 1; /* scope has await using declarations */ + int using_label_catch; /* label for catch handler (-1 if none) */ + int using_label_end; /* label for end of disposal block (-1 if none) */ } JSVarScope; typedef enum { @@ -9320,6 +10021,10 @@ typedef enum { JS_VAR_PRIVATE_GETTER, JS_VAR_PRIVATE_SETTER, /* must come after JS_VAR_PRIVATE_GETTER */ JS_VAR_PRIVATE_GETTER_SETTER, /* must come after JS_VAR_PRIVATE_SETTER */ + JS_VAR_USING, /* using declaration variable */ + JS_VAR_USING_METHOD, /* hidden local holding the cached dispose method + for the preceding JS_VAR_USING var (always + allocated immediately after it). */ } JSVarKindEnum; /* XXX: could use a different structure in bytecode functions to save @@ -9420,6 +10125,7 @@ typedef enum JSIteratorKindEnum { } JSIteratorKindEnum; typedef enum JSIteratorHelperKindEnum { + JS_ITERATOR_HELPER_KIND_CHUNKS, JS_ITERATOR_HELPER_KIND_DROP, JS_ITERATOR_HELPER_KIND_EVERY, JS_ITERATOR_HELPER_KIND_FILTER, @@ -9429,6 +10135,7 @@ typedef enum JSIteratorHelperKindEnum { JS_ITERATOR_HELPER_KIND_MAP, JS_ITERATOR_HELPER_KIND_SOME, JS_ITERATOR_HELPER_KIND_TAKE, + JS_ITERATOR_HELPER_KIND_WINDOWS, } JSIteratorHelperKindEnum; typedef struct JSForInIterator { @@ -9459,7 +10166,7 @@ typedef struct JSArrayBuffer { uint8_t *data; /* NULL if detached */ struct list_head array_list; void *opaque; - JSFreeArrayBufferDataFunc *free_func; + JSReallocArrayBufferDataFunc *realloc_func; } JSArrayBuffer; typedef struct JSTypedArray { @@ -9532,7 +10239,6 @@ typedef enum { } JSModuleStatus; struct JSModuleDef { - JSRefCountHeader header; /* must come first, 32-bit */ JSAtom module_name; struct list_head link; @@ -9552,6 +10258,11 @@ struct JSModuleDef { int import_entries_count; int import_entries_size; + /* import attributes the module was requested with, JS_UNDEFINED if + none; the same module name can be loaded with different attributes + (e.g. as both a JS module and a text module) */ + JSValue attributes; + JSValue module_ns; JSValue func_obj; /* only used for JS modules */ JSModuleInitFunc *init_func; /* only used for C modules */ @@ -9629,28 +10340,23 @@ struct JSShape { int deleted_prop_count; JSShape *shape_hash_next; /* in JSRuntime.shape_hash[h] list */ JSObject *proto; - JSShapeProperty prop[]; /* prop_size elements */ + uint32_t hash_table[]; /* prop_hash_mask + 1 elements, then prop[prop_size] */ }; struct JSObject { - union { - JSGCObjectHeader header; - struct { - int __gc_ref_count; /* corresponds to header.ref_count */ - uint8_t __gc_mark : 7; /* corresponds to header.mark/gc_obj_type */ - uint8_t is_prototype : 1; /* object may be used as prototype */ - - uint8_t extensible : 1; - uint8_t free_mark : 1; /* only used when freeing objects with cycles */ - uint8_t is_exotic : 1; /* true if object has exotic property handlers */ - uint8_t fast_array : 1; /* true if u.array is used for get/put (for JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS, JS_CLASS_MAPPED_ARGUMENTS and typed arrays) */ - uint8_t is_constructor : 1; /* true if object is a constructor function */ - uint8_t is_uncatchable_error : 1; /* if true, error is not catchable */ - uint8_t tmp_mark : 1; /* used in JS_WriteObjectRec() */ - uint8_t is_HTMLDDA : 1; /* specific annex B IsHtmlDDA behavior */ - uint16_t class_id; /* see JS_CLASS_x */ - }; - }; + /* ref_count/gc_obj_type/mark live in the allocator block header; the object + body keeps only the GC list link plus the object's own flags. */ + JSGCObjectHeader header; /* {link}; must come first so &p->header == p */ + uint8_t is_prototype : 1; /* object may be used as prototype */ + uint8_t extensible : 1; + uint8_t free_mark : 1; /* only used when freeing objects with cycles */ + uint8_t is_exotic : 1; /* true if object has exotic property handlers */ + uint8_t fast_array : 1; /* true if u.array is used for get/put (for JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS, JS_CLASS_MAPPED_ARGUMENTS and typed arrays) */ + uint8_t is_constructor : 1; /* true if object is a constructor function */ + uint8_t is_uncatchable_error : 1; /* if true, error is not catchable */ + uint8_t tmp_mark : 1; /* used in JS_WriteObjectRec() */ + uint8_t is_HTMLDDA : 1; /* specific annex B IsHtmlDDA behavior */ + uint16_t class_id; /* see JS_CLASS_x */ /* byte offsets: 16/24 */ JSShape *shape; /* prototype and property names + flag */ JSProperty *prop; /* array of properties */ @@ -9727,6 +10433,7 @@ typedef struct JSCallSiteData { JSValue func; JSValue func_name; bool native; + bool constructor; int line_num; int col_num; } JSCallSiteData; @@ -9799,6 +10506,7 @@ DEF(export, "export") DEF(extends, "extends") DEF(import, "import") DEF(super, "super") +DEF(using, "using") /* FutureReservedWords when parsing strict mode code */ DEF(implements, "implements") DEF(interface, "interface") @@ -9820,6 +10528,8 @@ DEF(length, "length") DEF(message, "message") DEF(cause, "cause") DEF(errors, "errors") +DEF(error, "error") +DEF(suppressed, "suppressed") DEF(stack, "stack") DEF(name, "name") DEF(toString, "toString") @@ -9852,6 +10562,10 @@ DEF(_ret_, "") DEF(_var_, "") DEF(_arg_var_, "") DEF(_with_, "") +DEF(_using_dispose_, "") +DEF(use, "use") +DEF(dispose, "dispose") +DEF(disposeAsync, "disposeAsync") DEF(lastIndex, "lastIndex") DEF(target, "target") DEF(index, "index") @@ -9859,7 +10573,6 @@ DEF(input, "input") DEF(defineProperties, "defineProperties") DEF(apply, "apply") DEF(join, "join") -DEF(concat, "concat") DEF(split, "split") DEF(construct, "construct") DEF(getPrototypeOf, "getPrototypeOf") @@ -9984,6 +10697,9 @@ DEF(URIError, "URIError") DEF(InternalError, "InternalError") DEF(DOMException, "DOMException") DEF(CallSite, "CallSite") +DEF(DisposableStack, "DisposableStack") +DEF(AsyncDisposableStack, "AsyncDisposableStack") +DEF(SuppressedError, "SuppressedError") /* private symbols */ DEF(Private_brand, "") /* symbols */ @@ -10000,13 +10716,15 @@ DEF(Symbol_hasInstance, "Symbol.hasInstance") DEF(Symbol_species, "Symbol.species") DEF(Symbol_unscopables, "Symbol.unscopables") DEF(Symbol_asyncIterator, "Symbol.asyncIterator") +DEF(Symbol_dispose, "Symbol.dispose") +DEF(Symbol_asyncDispose, "Symbol.asyncDispose") #endif /* DEF */ #undef DEF JS_ATOM_END, }; -#define JS_ATOM_LAST_KEYWORD JS_ATOM_super +#define JS_ATOM_LAST_KEYWORD JS_ATOM_using #define JS_ATOM_LAST_STRICT_KEYWORD JS_ATOM_yield static const char js_atom_init[] = @@ -10076,6 +10794,7 @@ DEF(export, "export") DEF(extends, "extends") DEF(import, "import") DEF(super, "super") +DEF(using, "using") /* FutureReservedWords when parsing strict mode code */ DEF(implements, "implements") DEF(interface, "interface") @@ -10097,6 +10816,8 @@ DEF(length, "length") DEF(message, "message") DEF(cause, "cause") DEF(errors, "errors") +DEF(error, "error") +DEF(suppressed, "suppressed") DEF(stack, "stack") DEF(name, "name") DEF(toString, "toString") @@ -10129,6 +10850,10 @@ DEF(_ret_, "") DEF(_var_, "") DEF(_arg_var_, "") DEF(_with_, "") +DEF(_using_dispose_, "") +DEF(use, "use") +DEF(dispose, "dispose") +DEF(disposeAsync, "disposeAsync") DEF(lastIndex, "lastIndex") DEF(target, "target") DEF(index, "index") @@ -10136,7 +10861,6 @@ DEF(input, "input") DEF(defineProperties, "defineProperties") DEF(apply, "apply") DEF(join, "join") -DEF(concat, "concat") DEF(split, "split") DEF(construct, "construct") DEF(getPrototypeOf, "getPrototypeOf") @@ -10261,6 +10985,9 @@ DEF(URIError, "URIError") DEF(InternalError, "InternalError") DEF(DOMException, "DOMException") DEF(CallSite, "CallSite") +DEF(DisposableStack, "DisposableStack") +DEF(AsyncDisposableStack, "AsyncDisposableStack") +DEF(SuppressedError, "SuppressedError") /* private symbols */ DEF(Private_brand, "") /* symbols */ @@ -10277,6 +11004,8 @@ DEF(Symbol_hasInstance, "Symbol.hasInstance") DEF(Symbol_species, "Symbol.species") DEF(Symbol_unscopables, "Symbol.unscopables") DEF(Symbol_asyncIterator, "Symbol.asyncIterator") +DEF(Symbol_dispose, "Symbol.dispose") +DEF(Symbol_asyncDispose, "Symbol.asyncDispose") #endif /* DEF */ @@ -10475,6 +11204,7 @@ DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */ DEF( ret, 1, 1, 0, none) /* used to return from the finally block */ DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */ +DEF( check_object, 1, 1, 1, none) DEF( to_object, 1, 1, 1, none) //DEF( to_string, 1, 1, 1, none) DEF( to_propkey, 1, 1, 1, none) @@ -10497,7 +11227,6 @@ DEF( for_of_start, 1, 1, 3, none) DEF(for_await_of_start, 1, 1, 3, none) DEF( for_in_next, 1, 1, 3, none) DEF( for_of_next, 2, 3, 5, u8) -DEF(iterator_check_object, 1, 1, 1, none) DEF(iterator_get_value_done, 1, 1, 2, none) DEF( iterator_close, 1, 3, 0, none) DEF( iterator_next, 1, 4, 4, none) @@ -10551,6 +11280,12 @@ DEF( strict_neq, 1, 2, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none) DEF( private_in, 1, 2, 1, none) DEF(push_bigint_i32, 5, 0, 1, i32) +DEF( using_dispose_init, 1, 0, 1, none) +DEF( using_dispose, 3, 1, 1, loc) +DEF(using_dispose_async, 3, 0, 1, loc) +DEF(using_dispose_merge, 1, 2, 1, none) +DEF( using_dispose_end, 1, 1, 0, none) +DEF( using_check, 2, 1, 2, u8) /* must be the last non short and non temporary opcode */ DEF( nop, 1, 0, 0, none) @@ -10576,6 +11311,8 @@ def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ +def( dispose_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */ + def( source_loc, 9, 0, 0, u32x2) /* emitted in phase 1, removed in phase 3 */ DEF( push_minus1, 1, 0, 1, none_int) @@ -10853,6 +11590,7 @@ DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */ DEF( ret, 1, 1, 0, none) /* used to return from the finally block */ DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */ +DEF( check_object, 1, 1, 1, none) DEF( to_object, 1, 1, 1, none) //DEF( to_string, 1, 1, 1, none) DEF( to_propkey, 1, 1, 1, none) @@ -10875,7 +11613,6 @@ DEF( for_of_start, 1, 1, 3, none) DEF(for_await_of_start, 1, 1, 3, none) DEF( for_in_next, 1, 1, 3, none) DEF( for_of_next, 2, 3, 5, u8) -DEF(iterator_check_object, 1, 1, 1, none) DEF(iterator_get_value_done, 1, 1, 2, none) DEF( iterator_close, 1, 3, 0, none) DEF( iterator_next, 1, 4, 4, none) @@ -10929,6 +11666,12 @@ DEF( strict_neq, 1, 2, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none) DEF( private_in, 1, 2, 1, none) DEF(push_bigint_i32, 5, 0, 1, i32) +DEF( using_dispose_init, 1, 0, 1, none) +DEF( using_dispose, 3, 1, 1, loc) +DEF(using_dispose_async, 3, 0, 1, loc) +DEF(using_dispose_merge, 1, 2, 1, none) +DEF( using_dispose_end, 1, 1, 0, none) +DEF( using_check, 2, 1, 2, u8) /* must be the last non short and non temporary opcode */ DEF( nop, 1, 0, 0, none) @@ -10954,6 +11697,8 @@ def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ +def( dispose_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */ + def( source_loc, 9, 0, 0, u32x2) /* emitted in phase 1, removed in phase 3 */ DEF( push_minus1, 1, 0, 1, none_int) @@ -11233,6 +11978,7 @@ DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */ DEF( ret, 1, 1, 0, none) /* used to return from the finally block */ DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */ +DEF( check_object, 1, 1, 1, none) DEF( to_object, 1, 1, 1, none) //DEF( to_string, 1, 1, 1, none) DEF( to_propkey, 1, 1, 1, none) @@ -11255,7 +12001,6 @@ DEF( for_of_start, 1, 1, 3, none) DEF(for_await_of_start, 1, 1, 3, none) DEF( for_in_next, 1, 1, 3, none) DEF( for_of_next, 2, 3, 5, u8) -DEF(iterator_check_object, 1, 1, 1, none) DEF(iterator_get_value_done, 1, 1, 2, none) DEF( iterator_close, 1, 3, 0, none) DEF( iterator_next, 1, 4, 4, none) @@ -11309,6 +12054,12 @@ DEF( strict_neq, 1, 2, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none) DEF( private_in, 1, 2, 1, none) DEF(push_bigint_i32, 5, 0, 1, i32) +DEF( using_dispose_init, 1, 0, 1, none) +DEF( using_dispose, 3, 1, 1, loc) +DEF(using_dispose_async, 3, 0, 1, loc) +DEF(using_dispose_merge, 1, 2, 1, none) +DEF( using_dispose_end, 1, 1, 0, none) +DEF( using_check, 2, 1, 2, u8) /* must be the last non short and non temporary opcode */ DEF( nop, 1, 0, 0, none) @@ -11334,6 +12085,8 @@ def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ +def( dispose_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */ + def( source_loc, 9, 0, 0, u32x2) /* emitted in phase 1, removed in phase 3 */ DEF( push_minus1, 1, 0, 1, none_int) @@ -11446,6 +12199,8 @@ static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen, JSValue val, bool is_array_ctor); static JSValue JS_EvalObject(JSContext *ctx, JSValueConst this_obj, JSValueConst val, int flags, int scope_idx); +static JSValue js_new_suppressed_error(JSContext *ctx, JSValueConst error, + JSValueConst suppressed); static __maybe_unused void JS_DumpString(JSRuntime *rt, JSString *p); static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt); static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p); @@ -11516,6 +12271,9 @@ static void js_promise_mark(JSRuntime *rt, JSValueConst val, static void js_promise_resolve_function_finalizer(JSRuntime *rt, JSValueConst val); static void js_promise_resolve_function_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func); +static void js_disposable_stack_finalizer(JSRuntime *rt, JSValueConst val); +static void js_disposable_stack_mark(JSRuntime *rt, JSValueConst val, + JS_MarkFunc *mark_func); #define HINT_STRING 0 #define HINT_NUMBER 1 @@ -11545,6 +12303,7 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic); static JSValue js_object_defineProperty(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic); +static uint64_t xorshift64star(uint64_t *pstate); typedef enum JSStrictEqModeEnum { JS_EQ_STRICT, @@ -11584,9 +12343,10 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, uint64_t len, uint64_t *max_len, JSClassID class_id, uint8_t *buf, - JSFreeArrayBufferDataFunc *free_func, + JSReallocArrayBufferDataFunc *realloc_func, void *opaque, bool alloc_flag); -static void js_array_buffer_free(JSRuntime *rt, void *opaque, void *ptr); +static void *js_array_buffer_realloc(JSRuntime *rt, void *opaque, void *ptr, + size_t size); static JSArrayBuffer *js_get_array_buffer(JSContext *ctx, JSValueConst obj); static bool array_buffer_is_resizable(const JSArrayBuffer *abuf); static JSValue js_typed_array_constructor(JSContext *ctx, @@ -11621,12 +12381,16 @@ static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, const char *input, size_t input_len, const char *filename, int line, int flags, int scope_idx); static void js_free_module_def(JSContext *ctx, JSModuleDef *m); +static int js_module_attributes_equal(JSContext *ctx, JSValueConst attr1, + JSValueConst attr2); static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m, JS_MarkFunc *mark_func); static JSValue js_import_meta(JSContext *ctx); static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier, JSValueConst options); static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref); +static void js_async_function_free(JSRuntime *rt, JSAsyncFunctionData *s); +static void js_release_coro(JSRuntime *rt, JSGCObjectHeader *coro); static JSValue js_new_promise_capability(JSContext *ctx, JSValue *resolving_funcs, JSValueConst ctor); @@ -11642,8 +12406,6 @@ static JSValue js_promise_resolve_thenable_job(JSContext *ctx, int argc, JSValueConst *argv); static bool js_string_eq(JSString *p1, JSString *p2); static int js_string_compare(JSString *p1, JSString *p2); -static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj, - JSValue prop, JSValue val, int flags); static int JS_NumberIsInteger(JSContext *ctx, JSValueConst val); static bool JS_NumberIsNegativeOrMinusZero(JSContext *ctx, JSValueConst val); static JSValue JS_ToNumberFree(JSContext *ctx, JSValue val); @@ -11702,8 +12464,7 @@ static JSValue JS_InstantiateFunctionListItem2(JSContext *ctx, JSObject *p, static JSValue JS_NewObjectProtoList(JSContext *ctx, JSValueConst proto, const JSCFunctionListEntry *fields, int n_fields); -static void js_set_uncatchable_error(JSContext *ctx, JSValueConst val, - bool flag); +static void js_set_uncatchable_error(JSValueConst val, bool flag); static JSValue js_new_callsite(JSContext *ctx, JSCallSiteData *csd); static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFrame *sf); @@ -11775,6 +12536,27 @@ static JSValue js_number(double d) return js_float64(d); } +/* If v is a number (int or float64), store it as a double and return true. + Used by the interpreter arithmetic fast paths to handle mixed int/float + operands inline instead of falling back to the slow path. */ +static inline bool js_arith_to_float64(JSValue v, double *pd) +{ + uint32_t tag = JS_VALUE_GET_TAG(v); + if (JS_TAG_IS_FLOAT64(tag)) + *pd = JS_VALUE_GET_FLOAT64(v); + else if (tag == JS_TAG_INT) + *pd = JS_VALUE_GET_INT(v); + else + return false; + return true; +} + +static JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d) +{ + (void)&ctx; + return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d); +} + JSValue JS_NewNumber(JSContext *ctx, double d) { return js_number(d); @@ -11788,8 +12570,8 @@ static JSValue js_bool(bool v) static JSValue js_dup(JSValueConst v) { if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - p->ref_count++; + void *p = JS_VALUE_GET_PTR(v); + JS_REF_COUNT(p)++; } return unsafe_unconst(v); } @@ -11830,6 +12612,279 @@ static size_t js_malloc_usable_size_unknown(const void *ptr) return 0; } +/* max overhead for size >= 64: 12.5% */ +static const uint16_t arena_block_sizes[JS_ARENA_BLOCK_SIZE_COUNT] = { + 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, + 144, 160, 176, 192, 208, 224, 240, 256, + 288, 320, 352, 384, 416, 448, 480, 512, +}; + +static int arena_get_size_index(size_t size) +{ + if (size <= 16) + return 0; + else if (size <= 128) + return (size + 7) / 8 - 2; + else if (size <= 256) + return (size + 15) / 16 + 6; + else if (size <= 512) + return (size + 31) / 32 + 14; + else + return JS_ARENA_BLOCK_SIZE_COUNT; +} + +static inline JSMallocBlockHeader *arena_zero_block(JSRuntime *rt) +{ + return (JSMallocBlockHeader *)rt->arena_state.zero_size_block; +} + +static void js_arena_init(JSRuntime *rt) +{ + JSArenaState *s = &rt->arena_state; + int i; + arena_zero_block(rt)->u.block_idx = JS_ARENA_FREE_NIL; + for (i = 0; i < JS_ARENA_BLOCK_SIZE_COUNT; i++) { + init_list_head(&s->arena_list[i]); + init_list_head(&s->free_arena_list[i]); + } +} + +static inline void *arena_get_block(JSArena *ar, unsigned int idx, + unsigned int block_size) +{ + return ar->blocks + (size_t)idx * block_size; +} + +static no_inline JSArena *arena_new(JSRuntime *rt, int block_size_idx) +{ + JSMallocBlockHeader *b; + JSArena *ar; + int n_blocks, block_size, i; + + block_size = arena_block_sizes[block_size_idx]; + n_blocks = (JS_ARENA_SIZE - sizeof(JSArena)) / block_size; + ar = rt->mf.js_malloc(rt->malloc_state.opaque, + sizeof(JSArena) + (size_t)n_blocks * block_size); + if (!ar) + return NULL; + ar->block_size_idx = block_size_idx; + ar->n_blocks = n_blocks; + ar->n_used_blocks = 0; + ar->first_free_block = 0; + for (i = 0; i < n_blocks - 1; i++) { + b = arena_get_block(ar, i, block_size); + b->u.free_next = i + 1; + b->block_size_idx = block_size_idx; + } + b = arena_get_block(ar, n_blocks - 1, block_size); + b->u.free_next = JS_ARENA_FREE_NIL; + b->block_size_idx = block_size_idx; + list_add(&ar->link, &rt->arena_state.arena_list[block_size_idx]); + list_add(&ar->free_link, &rt->arena_state.free_arena_list[block_size_idx]); + return ar; +} + +static no_inline void *arena_malloc_large(JSRuntime *rt, size_t size) +{ + JSMallocBlockHeader *b; + b = rt->mf.js_malloc(rt->malloc_state.opaque, + sizeof(JSMallocBlockHeader) + size); + if (!b) + return NULL; + b->u.block_idx = JS_ARENA_FREE_NIL; + b->block_size_idx = 0xff; /* fail safe */ + return b->user_data; +} + +static no_inline void *arena_calloc_large(JSRuntime *rt, size_t size) +{ + JSMallocBlockHeader *b; + b = rt->mf.js_calloc(rt->malloc_state.opaque, 1, + sizeof(JSMallocBlockHeader) + size); + if (!b) + return NULL; + b->u.block_idx = JS_ARENA_FREE_NIL; + b->block_size_idx = 0xff; /* fail safe */ + return b->user_data; +} + +static void *js_arena_malloc(JSRuntime *rt, size_t size) +{ + size_t total_size; + + if (unlikely(size == 0)) + return arena_zero_block(rt)->user_data; + total_size = ((size + JS_ARENA_ALIGN - 1) & ~(size_t)(JS_ARENA_ALIGN - 1)) + + sizeof(JSMallocBlockHeader); + if (!JS_ARENA_LARGE_BLOCKS_ONLY && total_size <= JS_ARENA_MAX_SMALL_SIZE) { + int block_size_idx; + unsigned int block_idx, block_size; + JSMallocBlockHeader *b; + JSArena *ar; + struct list_head *el, *head; + + block_size_idx = arena_get_size_index(total_size); + block_size = arena_block_sizes[block_size_idx]; + head = &rt->arena_state.free_arena_list[block_size_idx]; + el = head->next; + if (unlikely(el == head)) { + ar = arena_new(rt, block_size_idx); + if (!ar) + return NULL; + } else { + ar = list_entry(el, JSArena, free_link); + } + block_idx = ar->first_free_block; + b = arena_get_block(ar, block_idx, block_size); + ar->first_free_block = b->u.free_next; + b->u.block_idx = block_idx; + ar->n_used_blocks++; + if (unlikely(ar->n_used_blocks == ar->n_blocks)) + list_del(&ar->free_link); + return b->user_data; + } else { + return arena_malloc_large(rt, size); + } +} + +static void js_arena_free(JSRuntime *rt, void *ptr) +{ + JSMallocBlockHeader *b; + + if (!ptr) + return; + b = container_of(ptr, JSMallocBlockHeader, user_data); + if (unlikely(b->u.block_idx == JS_ARENA_FREE_NIL)) { + /* large or zero-size block */ + if (b == arena_zero_block(rt)) { + /* nothing to do */ + } else { + rt->mf.js_free(rt->malloc_state.opaque, b); + } + } else { + unsigned int block_idx = b->u.block_idx; + unsigned int block_size_idx = b->block_size_idx; + unsigned int block_size = arena_block_sizes[block_size_idx]; + JSArena *ar = (JSArena *)((uint8_t *)b - + (size_t)block_size * block_idx - + sizeof(JSArena)); + b->u.free_next = ar->first_free_block; + ar->first_free_block = block_idx; + if (unlikely(ar->n_used_blocks == ar->n_blocks)) + list_add(&ar->free_link, + &rt->arena_state.free_arena_list[block_size_idx]); + ar->n_used_blocks--; + if (unlikely(ar->n_used_blocks == 0)) { + list_del(&ar->link); + list_del(&ar->free_link); + rt->mf.js_free(rt->malloc_state.opaque, ar); + } + } +} + +static size_t js_arena_usable_size(JSRuntime *rt, const void *ptr) +{ + const JSMallocBlockHeader *b; + + if (!ptr) + return 0; + b = container_of(ptr, JSMallocBlockHeader, user_data); + if (b->u.block_idx == JS_ARENA_FREE_NIL) { + if (b == arena_zero_block(rt)) { + return 0; + } else { + size_t size = rt->mf.js_malloc_usable_size(b); + if (size != 0) + size -= sizeof(JSMallocBlockHeader); + return size; + } + } else { + return arena_block_sizes[b->block_size_idx] - sizeof(JSMallocBlockHeader); + } +} + +static void *js_arena_realloc(JSRuntime *rt, void *ptr, size_t size) +{ + JSMallocBlockHeader *b; + + /* js_realloc_rt already handles ptr == NULL and size == 0 */ + b = container_of(ptr, JSMallocBlockHeader, user_data); + if (b->u.block_idx == JS_ARENA_FREE_NIL) { + if (b == arena_zero_block(rt)) { + return js_arena_malloc(rt, size); + } else { + JSMallocBlockHeader *nb; + nb = rt->mf.js_realloc(rt->malloc_state.opaque, b, + sizeof(JSMallocBlockHeader) + size); + if (!nb) + return NULL; + nb->u.block_idx = JS_ARENA_FREE_NIL; + nb->block_size_idx = 0xff; + return nb->user_data; + } + } else { + unsigned int block_size = arena_block_sizes[b->block_size_idx]; + size_t total_size, old_usable; + void *new_ptr; + + total_size = ((size + JS_ARENA_ALIGN - 1) & ~(size_t)(JS_ARENA_ALIGN - 1)) + + sizeof(JSMallocBlockHeader); + if (total_size <= block_size) + return ptr; /* still fits the current size class */ + new_ptr = js_arena_malloc(rt, size); + if (!new_ptr) + return NULL; + { + /* carry the merged GC/refcount fields to the relocated block */ + JSMallocBlockHeader *nb = container_of(new_ptr, JSMallocBlockHeader, user_data); + nb->gc_obj_type = b->gc_obj_type; + nb->mark = b->mark; + nb->ref_count = b->ref_count; + } + old_usable = block_size - sizeof(JSMallocBlockHeader); + if (size > old_usable) + size = old_usable; + memcpy(new_ptr, ptr, size); + js_arena_free(rt, ptr); + return new_ptr; + } +} + +static void *js_arena_calloc(JSRuntime *rt, size_t count, size_t size) +{ + size_t n = count * size; /* overflow already checked by js_calloc_rt */ + size_t total_size = ((n + JS_ARENA_ALIGN - 1) & ~(size_t)(JS_ARENA_ALIGN - 1)) + + sizeof(JSMallocBlockHeader); + if (!JS_ARENA_LARGE_BLOCKS_ONLY && total_size <= JS_ARENA_MAX_SMALL_SIZE) { + /* Small blocks are carved from recycled (dirty) arena memory, so they + must be zeroed explicitly. */ + void *ptr = js_arena_malloc(rt, n); + if (unlikely(!ptr)) + return NULL; + return memset(ptr, 0, n); + } + /* Large blocks come straight from the backing allocator, so let js_calloc + do the zeroing. */ + return arena_calloc_large(rt, n); +} + +/* free any arenas still mapped at runtime teardown (normally none: empty + arenas are released eagerly as their last block is freed) */ +static void js_arena_free_all(JSRuntime *rt) +{ + JSArenaState *s = &rt->arena_state; + struct list_head *el, *el1; + int i; + for (i = 0; i < JS_ARENA_BLOCK_SIZE_COUNT; i++) { + list_for_each_safe(el, el1, &s->arena_list[i]) { + JSArena *ar = list_entry(el, JSArena, link); + rt->mf.js_free(rt->malloc_state.opaque, ar); + } + init_list_head(&s->arena_list[i]); + init_list_head(&s->free_arena_list[i]); + } +} + void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size) { void *ptr; @@ -11847,12 +12902,12 @@ void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size) if (unlikely(s->malloc_size + (count * size) > s->malloc_limit - 1)) return NULL; - ptr = rt->mf.js_calloc(s->opaque, count, size); + ptr = js_arena_calloc(rt, count, size); if (!ptr) return NULL; s->malloc_count++; - s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size += js_arena_usable_size(rt, ptr) + MALLOC_OVERHEAD; return ptr; } @@ -11870,12 +12925,12 @@ void *js_malloc_rt(JSRuntime *rt, size_t size) if (unlikely(s->malloc_size + size > s->malloc_limit - 1)) return NULL; - ptr = rt->mf.js_malloc(s->opaque, size); + ptr = js_arena_malloc(rt, size); if (!ptr) return NULL; s->malloc_count++; - s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + s->malloc_size += js_arena_usable_size(rt, ptr) + MALLOC_OVERHEAD; return ptr; } @@ -11887,14 +12942,14 @@ void js_free_rt(JSRuntime *rt, void *ptr) return; s = &rt->malloc_state; - size_t free_size = rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD; + size_t free_size = js_arena_usable_size(rt, ptr) + MALLOC_OVERHEAD; if (unlikely(free_size > s->malloc_size)) { printf("js_free_rt: malloc_size underflow: freeing %zu but only %zu tracked\n", free_size, s->malloc_size); abort(); } s->malloc_count--; s->malloc_size -= free_size; - rt->mf.js_free(s->opaque, ptr); + js_arena_free(rt, ptr); } void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size) @@ -11911,23 +12966,23 @@ void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size) js_free_rt(rt, ptr); return NULL; } - old_size = rt->mf.js_malloc_usable_size(ptr); + old_size = js_arena_usable_size(rt, ptr); s = &rt->malloc_state; /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ if (s->malloc_size + size - old_size > s->malloc_limit - 1) return NULL; - ptr = rt->mf.js_realloc(s->opaque, ptr, size); + ptr = js_arena_realloc(rt, ptr, size); if (!ptr) return NULL; - s->malloc_size += rt->mf.js_malloc_usable_size(ptr) - old_size; + s->malloc_size += js_arena_usable_size(rt, ptr) - old_size; return ptr; } size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr) { - return rt->mf.js_malloc_usable_size(ptr); + return js_arena_usable_size(rt, ptr); } /** @@ -11995,22 +13050,6 @@ void *js_realloc(JSContext *ctx, void *ptr, size_t size) return ret; } -/* store extra allocated size in *pslack if successful */ -void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack) -{ - void *ret; - ret = js_realloc_rt(ctx->rt, ptr, size); - if (unlikely(!ret && size != 0)) { - JS_ThrowOutOfMemory(ctx); - return NULL; - } - if (pslack) { - size_t new_size = js_malloc_usable_size_rt(ctx->rt, ret); - *pslack = (new_size > size) ? new_size - size : 0; - } - return ret; -} - size_t js_malloc_usable_size(JSContext *ctx, const void *ptr) { return js_malloc_usable_size_rt(ctx->rt, ptr); @@ -12037,14 +13076,13 @@ static no_inline int js_realloc_array(JSContext *ctx, void **parray, int elem_size, int *psize, int req_size) { int new_size; - size_t slack; void *new_array; /* XXX: potential arithmetic overflow */ new_size = max_int(req_size, *psize * 3 / 2); - new_array = js_realloc2(ctx, *parray, new_size * elem_size, &slack); + assert(elem_size > 0 && new_size > 0); + new_array = js_realloc(ctx, *parray, (size_t)new_size * (size_t)elem_size); if (!new_array) return -1; - new_size += slack / elem_size; *psize = new_size; *parray = new_array; return 0; @@ -12087,7 +13125,7 @@ typedef struct JSClassShortDef { static JSClassShortDef const js_std_class_def[] = { { JS_ATOM_Object, NULL, NULL }, /* JS_CLASS_OBJECT */ { JS_ATOM_Array, js_array_finalizer, js_array_mark }, /* JS_CLASS_ARRAY */ - { JS_ATOM_Error, NULL, NULL }, /* JS_CLASS_ERROR */ + { JS_ATOM_Error, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_ERROR */ { JS_ATOM_Number, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_NUMBER */ { JS_ATOM_String, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_STRING */ { JS_ATOM_Boolean, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_BOOLEAN */ @@ -12134,6 +13172,7 @@ static JSClassShortDef const js_std_class_def[] = { { JS_ATOM_String_Iterator, js_array_iterator_finalizer, js_array_iterator_mark }, /* JS_CLASS_STRING_ITERATOR */ { JS_ATOM_RegExp_String_Iterator, js_regexp_string_iterator_finalizer, js_regexp_string_iterator_mark }, /* JS_CLASS_REGEXP_STRING_ITERATOR */ { JS_ATOM_Generator, js_generator_finalizer, js_generator_mark }, /* JS_CLASS_GENERATOR */ + { JS_ATOM_DisposableStack, js_disposable_stack_finalizer, js_disposable_stack_mark }, /* JS_CLASS_DISPOSABLE_STACK */ }; static int init_class_range(JSRuntime *rt, JSClassShortDef const *tab, @@ -12200,6 +13239,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque) ms.malloc_count++; ms.malloc_size += rt->mf.js_malloc_usable_size(rt) + MALLOC_OVERHEAD; rt->malloc_state = ms; + js_arena_init(rt); rt->malloc_gc_threshold = 256 * 1024; init_list_head(&rt->context_list); @@ -12235,9 +13275,6 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque) rt->js_class_id_alloc = JS_CLASS_INIT_COUNT; rt->stack_size = JS_DEFAULT_STACK_SIZE; -#ifdef __wasi__ - rt->stack_size = 0; -#endif JS_UpdateStackTop(rt); @@ -12446,7 +13483,7 @@ static JSString *js_alloc_string_rt(JSRuntime *rt, int max_len, int is_wide_char str = js_malloc_rt(rt, sizeof(JSString) + (max_len << is_wide_char) + 1 - is_wide_char); if (unlikely(!str)) return NULL; - str->header.ref_count = 1; + JS_REF_COUNT(str) = 1; str->is_wide_char = is_wide_char; str->len = max_len; str->kind = JS_STRING_KIND_NORMAL; @@ -12475,7 +13512,7 @@ static inline void js_free_string0(JSRuntime *rt, JSString *str); /* same as JS_FreeValueRT() but faster */ static inline void js_free_string(JSRuntime *rt, JSString *str) { - if (--str->header.ref_count <= 0) + if (--JS_REF_COUNT(str) <= 0) js_free_string0(rt, str); } @@ -12511,6 +13548,7 @@ void JS_SetRuntimeInfo(JSRuntime *rt, const char *s) void JS_FreeRuntime(JSRuntime *rt) { struct list_head *el, *el1; + bool leak = false; int i; rt->in_free = true; @@ -12537,27 +13575,28 @@ void JS_FreeRuntime(JSRuntime *rt) referenced externally */ list_for_each(el, &rt->gc_obj_list) { p = list_entry(el, JSGCObjectHeader, link); - p->mark = 0; + JS_GC_MARK(p) = 0; } gc_decref(rt); header_done = false; list_for_each(el, &rt->gc_obj_list) { p = list_entry(el, JSGCObjectHeader, link); - if (p->ref_count != 0) { + if (JS_REF_COUNT(p) != 0) { if (!header_done) { printf("Object leaks:\n"); JS_DumpObjectHeader(rt); header_done = true; } JS_DumpGCObject(rt, p); + leak = true; } } count = 0; list_for_each(el, &rt->gc_obj_list) { p = list_entry(el, JSGCObjectHeader, link); - if (p->ref_count == 0) { + if (JS_REF_COUNT(p) == 0) { count++; } } @@ -12585,7 +13624,7 @@ void JS_FreeRuntime(JSRuntime *rt) for(i = 0; i < rt->atom_size; i++) { JSAtomStruct *p = rt->atom_array[i]; if (!atom_is_free(p) /* && p->str*/) { - if (i >= JS_ATOM_END || p->header.ref_count != 1) { + if (i >= JS_ATOM_END || JS_REF_COUNT(p) != 1) { if (!header_done) { header_done = true; if (rt->rt_info) { @@ -12599,7 +13638,7 @@ void JS_FreeRuntime(JSRuntime *rt) if (rt->rt_info) { printf(" "); } else { - printf(" %6u %6u ", i, p->header.ref_count); + printf(" %6u %6u ", i, JS_REF_COUNT(p)); } switch (p->atom_type) { case JS_ATOM_TYPE_STRING: @@ -12623,10 +13662,11 @@ void JS_FreeRuntime(JSRuntime *rt) break; } if (rt->rt_info) { - printf(":%u", p->header.ref_count); + printf(":%u", JS_REF_COUNT(p)); } else { printf("\n"); } + leak = true; } } } @@ -12662,11 +13702,11 @@ void JS_FreeRuntime(JSRuntime *rt) if (rt->rt_info) { printf(" "); } else { - printf(" %6u ", str->header.ref_count); + printf(" %6u ", JS_REF_COUNT(str)); } JS_DumpString(rt, str); if (rt->rt_info) { - printf(":%u", str->header.ref_count); + printf(":%u", JS_REF_COUNT(str)); } else { printf("\n"); } @@ -12675,6 +13715,7 @@ void JS_FreeRuntime(JSRuntime *rt) } if (rt->rt_info) printf("\n"); + leak = true; } #endif @@ -12694,14 +13735,22 @@ void JS_FreeRuntime(JSRuntime *rt) printf("Memory leak: %zd bytes lost in %zd block%s\n", s->malloc_size - sizeof(JSRuntime), s->malloc_count - 1, &"s"[s->malloc_count == 2]); + leak = true; } } #endif + leak &= check_dump_flag(rt, JS_ABORT_ON_LEAKS); + + js_arena_free_all(rt); + { JSMallocState *ms = &rt->malloc_state; rt->mf.js_free(ms->opaque, rt); } + + if (leak) + abort(); } JSContext *JS_NewContextRaw(JSRuntime *rt) @@ -12712,12 +13761,13 @@ JSContext *JS_NewContextRaw(JSRuntime *rt) ctx = js_mallocz_rt(rt, sizeof(JSContext)); if (!ctx) return NULL; - ctx->header.ref_count = 1; + JS_REF_COUNT(ctx) = 1; add_gc_object(rt, &ctx->header, JS_GC_OBJ_TYPE_JS_CONTEXT); ctx->class_proto = js_malloc_rt(rt, sizeof(ctx->class_proto[0]) * rt->class_count); if (!ctx->class_proto) { + remove_gc_object(&ctx->header); js_free_rt(rt, ctx); return NULL; } @@ -12735,6 +13785,12 @@ JSContext *JS_NewContextRaw(JSRuntime *rt) ctx->error_prepare_stack = JS_UNDEFINED; ctx->error_stack_trace_limit = js_int32(10); init_list_head(&ctx->loaded_modules); + // TODO(bnoordhuis) use getrandom() etc. + ctx->random_state = js__gettimeofday_us(); + // the state must be non zero + if (ctx->random_state == 0) + ctx->random_state = 1; + ctx->hash_seed = xorshift64star(&ctx->random_state); if (JS_AddIntrinsicBasicObjects(ctx)) { JS_FreeContext(ctx); @@ -12746,7 +13802,6 @@ JSContext *JS_NewContextRaw(JSRuntime *rt) JSContext *JS_NewContext(JSRuntime *rt) { JSContext *ctx; - ctx = JS_NewContextRaw(rt); if (!ctx) return NULL; @@ -12761,7 +13816,7 @@ JSContext *JS_NewContext(JSRuntime *rt) JS_AddIntrinsicTypedArrays(ctx) || JS_AddIntrinsicPromise(ctx) || JS_AddIntrinsicWeakRef(ctx) || - JS_AddIntrinsicDOMException(ctx) || + JS_AddIntrinsicAToB(ctx) || JS_AddPerformance(ctx)) { JS_FreeContext(ctx); return NULL; @@ -12827,7 +13882,7 @@ static void js_free_modules(JSContext *ctx, JSFreeModuleEnum flag) JSContext *JS_DupContext(JSContext *ctx) { - ctx->header.ref_count++; + JS_REF_COUNT(ctx)++; return ctx; } @@ -12892,9 +13947,9 @@ void JS_FreeContext(JSContext *ctx) JSRuntime *rt = ctx->rt; int i; - if (--ctx->header.ref_count > 0) + if (--JS_REF_COUNT(ctx) > 0) return; - assert(ctx->header.ref_count == 0); + assert(JS_REF_COUNT(ctx) == 0); #ifdef ENABLE_DUMPS // JS_DUMP_ATOMS if (check_dump_flag(rt, JS_DUMP_ATOMS)) @@ -12972,15 +14027,11 @@ JSRuntime *JS_GetRuntime(JSContext *ctx) static void update_stack_limit(JSRuntime *rt) { -#if defined(__wasi__) - rt->stack_limit = 0; /* no limit */ -#else - if (rt->stack_size == 0) { + if (rt->stack_size == 0 || rt->stack_top < rt->stack_size) { rt->stack_limit = 0; /* no limit */ } else { rt->stack_limit = rt->stack_top - rt->stack_size; } -#endif } void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size) @@ -13077,7 +14128,7 @@ static inline uint32_t hash_string8(const uint8_t *str, size_t len, uint32_t h) for(i = 0; i < len; i++) h = h * 263 + str[i]; - return h; + return h ^ hash32(len); } static inline uint32_t hash_string16(const uint16_t *str, @@ -13087,7 +14138,7 @@ static inline uint32_t hash_string16(const uint16_t *str, for(i = 0; i < len; i++) h = h * 263 + str[i]; - return h; + return h ^ hash32(len); } static uint32_t hash_string(JSString *str, uint32_t h) @@ -13118,8 +14169,8 @@ static __maybe_unused void JS_DumpString(JSRuntime *rt, JSString *p) printf(""); return; } - if (p->header.ref_count != 1) - printf("%d", p->header.ref_count); + if (JS_REF_COUNT(p) != 1) + printf("%d", JS_REF_COUNT(p)); if (p->is_wide_char) putchar('L'); sep = '\"'; @@ -13216,7 +14267,7 @@ static int JS_InitAtoms(JSRuntime *rt) rt->atom_count = 0; rt->atom_size = 0; rt->atom_free_index = 0; - if (JS_ResizeAtomHash(rt, 512)) /* there are at least 504 predefined atoms */ + if (JS_ResizeAtomHash(rt, 1024)) // there are at least 587 predefined atoms return -1; p = js_atom_init; @@ -13241,7 +14292,7 @@ JSAtom JS_DupAtomRT(JSRuntime *rt, JSAtom v) if (!__JS_AtomIsConst(v)) { p = rt->atom_array[v]; - p->header.ref_count++; + JS_REF_COUNT(p)++; } return v; } @@ -13254,7 +14305,7 @@ JSAtom JS_DupAtom(JSContext *ctx, JSAtom v) if (!__JS_AtomIsConst(v)) { rt = ctx->rt; p = rt->atom_array[v]; - p->header.ref_count++; + JS_REF_COUNT(p)++; } return v; } @@ -13320,7 +14371,7 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) i = js_get_atom_index(rt, str); /* reduce string refcount and increase atom's unless constant */ if (__JS_AtomIsConst(i)) - str->header.ref_count--; + JS_REF_COUNT(str)--; return i; } /* try and locate an already registered atom */ @@ -13336,7 +14387,7 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) p->len == len && js_string_memcmp(p, str, len) == 0) { if (!__JS_AtomIsConst(i)) - p->header.ref_count++; + JS_REF_COUNT(p)++; goto done; } i = p->hash_next; @@ -13363,7 +14414,6 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) new_size = max_int(711, rt->atom_size * 3 / 2); if (new_size > JS_ATOM_MAX) goto fail; - /* XXX: should use realloc2 to use slack space */ new_array = js_realloc_rt(rt, rt->atom_array, sizeof(*new_array) * new_size); if (!new_array) goto fail; @@ -13376,7 +14426,7 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) js_free_rt(rt, new_array); goto fail; } - p->header.ref_count = 1; /* not refcounted */ + JS_REF_COUNT(p) = 1; /* not refcounted */ p->atom_type = JS_ATOM_TYPE_SYMBOL; #ifdef ENABLE_DUMPS // JS_DUMP_LEAKS list_add_tail(&p->link, &rt->string_list); @@ -13408,7 +14458,7 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) 1 - str->is_wide_char); if (unlikely(!p)) goto fail; - p->header.ref_count = 1; + JS_REF_COUNT(p) = 1; p->is_wide_char = str->is_wide_char; p->len = str->len; p->kind = JS_STRING_KIND_NORMAL; @@ -13423,7 +14473,7 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type) p = js_malloc_rt(rt, sizeof(JSAtomStruct)); /* empty wide string */ if (!p) return JS_ATOM_NULL; - p->header.ref_count = 1; + JS_REF_COUNT(p) = 1; p->is_wide_char = 1; /* Hack to represent NULL as a JSString */ p->len = 0; p->kind = JS_STRING_KIND_NORMAL; @@ -13495,7 +14545,7 @@ static JSAtom __JS_FindAtom(JSRuntime *rt, const char *str, size_t len, p->is_wide_char == 0 && memcmp(str8(p), str, len) == 0) { if (!__JS_AtomIsConst(i)) - p->header.ref_count++; + JS_REF_COUNT(p)++; return i; } i = p->hash_next; @@ -13548,7 +14598,7 @@ static void __JS_FreeAtom(JSRuntime *rt, uint32_t i) JSAtomStruct *p; p = rt->atom_array[i]; - if (--p->header.ref_count > 0) + if (--JS_REF_COUNT(p) > 0) return; JS_FreeAtomStruct(rt, p); } @@ -13648,12 +14698,39 @@ static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr, } /* `description` may be pure ASCII or UTF-8 encoded */ -JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global) +static JSValue js_new_symbol(JSContext *ctx, const char *description, + int atom_type) { + if (description == NULL) { + if (atom_type != JS_ATOM_TYPE_GLOBAL_SYMBOL) { + /* Local symbol without description: Symbol() */ + return JS_NewSymbolInternal(ctx, NULL, atom_type); + } + /* Global symbol without description: Symbol.for() + Per ES spec, ToString(undefined) becomes "undefined" */ + description = "undefined"; + } JSAtom atom = JS_NewAtom(ctx, description); if (atom == JS_ATOM_NULL) return JS_EXCEPTION; - return JS_NewSymbolFromAtom(ctx, atom, is_global ? JS_ATOM_TYPE_GLOBAL_SYMBOL : JS_ATOM_TYPE_SYMBOL); + JSValue symbol = JS_NewSymbolFromAtom(ctx, atom, atom_type); + JS_FreeAtom(ctx, atom); + return symbol; +} + +JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global) +{ + int atom_type; + + atom_type = JS_ATOM_TYPE_SYMBOL; + if (is_global) + atom_type = JS_ATOM_TYPE_GLOBAL_SYMBOL; + return js_new_symbol(ctx, description, atom_type); +} + +JSValue JS_NewPrivateSymbol(JSContext *ctx, const char *description) +{ + return js_new_symbol(ctx, description, JS_ATOM_TYPE_PRIVATE); } #define ATOM_GET_STR_BUF_SIZE 64 @@ -14016,7 +15093,7 @@ bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id) JSAtom JS_GetClassName(JSRuntime *rt, JSClassID class_id) { if (JS_IsRegisteredClass(rt, class_id)) { - return JS_DupAtomRT(rt, rt->class_array[class_id].class_id); + return JS_DupAtomRT(rt, rt->class_array[class_id].class_name); } else { return JS_ATOM_NULL; } @@ -14168,7 +15245,7 @@ static JSValue js_sub_string(JSContext *ctx, JSString *p, int start, int end) slice = (void *)&q[1]; slice->parent = p; slice->start = start << p->is_wide_char; // chars -> bytes - p->header.ref_count++; + JS_REF_COUNT(p)++; return JS_MKPTR(JS_TAG_STRING, q); } if (p->is_wide_char) { @@ -14250,16 +15327,14 @@ static int string_buffer_set_error(StringBuffer *s) static no_inline int string_buffer_widen(StringBuffer *s, int size) { JSString *str; - size_t slack; int i; if (s->error_status) return -1; - str = js_realloc2(s->ctx, s->str, sizeof(JSString) + (size << 1), &slack); + str = js_realloc(s->ctx, s->str, sizeof(JSString) + (size << 1)); if (!str) return string_buffer_set_error(s); - size += slack >> 1; for(i = s->len; i-- > 0;) { str16(str)[i] = str8(str)[i]; } @@ -14273,7 +15348,7 @@ static no_inline int string_buffer_realloc(StringBuffer *s, int new_len, int c) { JSString *new_str; int new_size; - size_t new_size_bytes, slack; + size_t new_size_bytes; if (s->error_status) return -1; @@ -14287,10 +15362,9 @@ static no_inline int string_buffer_realloc(StringBuffer *s, int new_len, int c) return string_buffer_widen(s, new_size); } new_size_bytes = sizeof(JSString) + (new_size << s->is_wide_char) + 1 - s->is_wide_char; - new_str = js_realloc2(s->ctx, s->str, new_size_bytes, &slack); + new_str = js_realloc(s->ctx, s->str, new_size_bytes); if (!new_str) return string_buffer_set_error(s); - new_size = min_int(new_size + (slack >> s->is_wide_char), JS_STRING_LEN_MAX); s->size = new_size; s->str = new_str; return 0; @@ -14575,18 +15649,18 @@ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) size_t len; int kind; - if (buf_len <= 0) { + if (unlikely(buf_len <= 0)) return js_empty_string(ctx->rt); - } + /* Compute string kind and length: 7-bit, 8-bit, 16-bit, 16-bit UTF-16 */ kind = utf8_scan(buf, buf_len, &len); - if (len > JS_STRING_LEN_MAX) + if (unlikely(len > JS_STRING_LEN_MAX)) return JS_ThrowRangeError(ctx, "invalid string length"); switch (kind) { case UTF8_PLAIN_ASCII: str = js_alloc_string(ctx, len, 0); - if (!str) + if (unlikely(!str)) return JS_EXCEPTION; memcpy(str8(str), buf, len); str8(str)[len] = '\0'; @@ -14594,7 +15668,7 @@ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) case UTF8_NON_ASCII: /* buf contains non-ASCII code-points, but limited to 8-bit values */ str = js_alloc_string(ctx, len, 0); - if (!str) + if (unlikely(!str)) return JS_EXCEPTION; utf8_decode_buf8(str8(str), len + 1, buf, buf_len); break; @@ -14603,7 +15677,7 @@ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len) //if (kind & UTF8_HAS_ERRORS) // return JS_ThrowRangeError(ctx, "invalid UTF-8 sequence"); str = js_alloc_string(ctx, len, 1); - if (!str) + if (unlikely(!str)) return JS_EXCEPTION; utf8_decode_buf16(str16(str), len, buf, buf_len); break; @@ -14615,10 +15689,13 @@ JSValue JS_NewStringUTF16(JSContext *ctx, const uint16_t *buf, size_t len) { JSString *str; - if (!len) + if (unlikely(!len)) return js_empty_string(ctx->rt); + if (unlikely(len > JS_STRING_LEN_MAX)) + return JS_ThrowRangeError(ctx, "invalid string length"); + str = js_alloc_string(ctx, len, 1); - if (!str) + if (unlikely(!str)) return JS_EXCEPTION; memcpy(str16(str), buf, len * sizeof(*buf)); return JS_MKPTR(JS_TAG_STRING, str); @@ -14788,23 +15865,34 @@ const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, return NULL; } +static void copy_str16(uint16_t *dst, JSString *p, int offset, int len) +{ + if (p->is_wide_char) { + memcpy(dst, str16(p) + offset, len * 2); + } else { + const uint8_t *src1 = str8(p) + offset; + int i; + + for(i = 0; i < len; i++) + dst[i] = src1[i]; + } +} + const uint16_t *JS_ToCStringLenUTF16(JSContext *ctx, size_t *plen, JSValueConst val1) { JSString *p, *q; - uint32_t i; JSValue v; v = js_force_tostring(ctx, val1); if (JS_IsException(v)) goto fail; p = JS_VALUE_GET_STRING(v); - if (!p->is_wide_char) { + if (!(p->is_wide_char && p->kind == JS_STRING_KIND_NORMAL)) { q = js_alloc_string(ctx, p->len, /*is_wide_char*/true); if (!q) goto fail; - for (i = 0; i < p->len; i++) - str16(q)[i] = str8(p)[i]; + copy_str16(str16(q), p, 0, p->len); JS_FreeValue(ctx, v); p = q; } @@ -15075,7 +16163,7 @@ static JSValue js_new_string_rope(JSContext *ctx, JSValue op1, JSValue op2) r = js_malloc(ctx, sizeof(*r)); if (!r) goto fail; - r->header.ref_count = 1; + JS_REF_COUNT(r) = 1; r->len = len; r->is_wide_char = is_wide_char; r->depth = depth + 1; @@ -15234,7 +16322,7 @@ static JSValue js_linearize_string_rope(JSContext *ctx, JSValueConst rope) if (string_buffer_concat_value(b, rope)) goto fail; ret = string_buffer_end(b); - if (r->header.ref_count > 1) { + if (JS_REF_COUNT(r) > 1) { /* update the rope so that it won't need to be linearized again */ JS_FreeValue(ctx, r->left); JS_FreeValue(ctx, r->right); @@ -15249,19 +16337,6 @@ static JSValue js_linearize_string_rope(JSContext *ctx, JSValueConst rope) /* flat string concatenation - used by rope when concatenating short strings */ static JSValue JS_ConcatString2(JSContext *ctx, JSValue op1, JSValue op2); -static void copy_str16(uint16_t *dst, JSString *p, int offset, int len) -{ - if (p->is_wide_char) { - memcpy(dst, str16(p) + offset, len * 2); - } else { - const uint8_t *src1 = str8(p) + offset; - int i; - - for(i = 0; i < len; i++) - dst[i] = src1[i]; - } -} - static JSValue JS_ConcatString1(JSContext *ctx, JSString *p1, JSString *p2) { JSString *p; @@ -15299,7 +16374,7 @@ static JSValue JS_ConcatString2(JSContext *ctx, JSValue op1, JSValue op2) if (p2->len == 0) { goto ret_op1; } - if (p1->header.ref_count == 1 && p1->is_wide_char == p2->is_wide_char + if (JS_REF_COUNT(p1) == 1 && p1->is_wide_char == p2->is_wide_char && js_malloc_usable_size(ctx, p1) >= sizeof(*p1) + ((p1->len + p2->len) << p2->is_wide_char) + 1 - p1->is_wide_char) { /* Concatenate in place in available space at the end of p1 */ if (p1->is_wide_char) { @@ -15407,17 +16482,26 @@ static inline size_t get_shape_size(size_t hash_size, size_t prop_size) static inline JSShape *get_shape_from_alloc(void *sh_alloc, size_t hash_size) { - return (JSShape *)(void *)((uint32_t *)sh_alloc + hash_size); + (void)hash_size; + return (JSShape *)sh_alloc; /* shape sits at the allocation start */ } +/* one-past-the-end of the hash bucket array; buckets are addressed as + prop_hash_end(sh)[-h - 1] for h in [0, prop_hash_mask], in BOTH layouts. */ static inline uint32_t *prop_hash_end(JSShape *sh) { - return (uint32_t *)sh; + return sh->hash_table + sh->prop_hash_mask + 1; +} + +/* the JSShapeProperty array */ +static inline JSShapeProperty *get_shape_prop(JSShape *sh) +{ + return (JSShapeProperty *)(void *)(sh->hash_table + sh->prop_hash_mask + 1); } static inline void *get_alloc_from_shape(JSShape *sh) { - return prop_hash_end(sh) - ((intptr_t)sh->prop_hash_mask + 1); + return sh; /* shape sits at the allocation start */ } static int init_shape_hash(JSRuntime *rt) @@ -15432,7 +16516,12 @@ static int init_shape_hash(JSRuntime *rt) return 0; } -/* same magic hash multiplier as the Linux kernel */ +static uintptr_t hash_atom(JSAtom atom, uintptr_t mask) +{ + int shift = 1 + clz_uintptr(mask+1); + return hash_uintptr(atom) >> shift; +} + static uint32_t shape_hash(uint32_t h, uint32_t val) { return hash32(h + val); @@ -15513,14 +16602,16 @@ static inline JSShape *js_new_shape_nohash(JSContext *ctx, JSObject *proto, if (!sh_alloc) return NULL; sh = get_shape_from_alloc(sh_alloc, hash_size); - sh->header.ref_count = 1; + JS_REF_COUNT(sh) = 1; add_gc_object(rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE); if (proto) js_dup(JS_MKPTR(JS_TAG_OBJECT, proto)); sh->proto = proto; + /* prop_hash_mask must be set before prop_hash_end(sh) is used, as the hash + location depends on it in the merged-header layout. */ + sh->prop_hash_mask = hash_size - 1; memset(prop_hash_end(sh) - hash_size, 0, sizeof(prop_hash_end(sh)[0]) * hash_size); - sh->prop_hash_mask = hash_size - 1; sh->prop_size = prop_size; sh->prop_count = 0; sh->deleted_prop_count = 0; @@ -15609,13 +16700,13 @@ static JSShape *js_clone_shape(JSContext *ctx, JSShape *sh1) sh_alloc1 = get_alloc_from_shape(sh1); memcpy(sh_alloc, sh_alloc1, size); sh = get_shape_from_alloc(sh_alloc, hash_size); - sh->header.ref_count = 1; + JS_REF_COUNT(sh) = 1; add_gc_object(ctx->rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE); sh->is_hashed = false; if (sh->proto) { js_dup(JS_MKPTR(JS_TAG_OBJECT, sh->proto)); } - for(i = 0, pr = sh->prop; i < sh->prop_count; i++, pr++) { + for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) { JS_DupAtom(ctx, pr->atom); } return sh; @@ -15623,7 +16714,7 @@ static JSShape *js_clone_shape(JSContext *ctx, JSShape *sh1) static JSShape *js_dup_shape(JSShape *sh) { - sh->header.ref_count++; + JS_REF_COUNT(sh)++; return sh; } @@ -15632,13 +16723,13 @@ static void js_free_shape0(JSRuntime *rt, JSShape *sh) uint32_t i; JSShapeProperty *pr; - assert(sh->header.ref_count == 0); + assert(JS_REF_COUNT(sh) == 0); if (sh->is_hashed) js_shape_hash_unlink(rt, sh); if (sh->proto != NULL) { JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, sh->proto)); } - pr = sh->prop; + pr = get_shape_prop(sh); for(i = 0; i < sh->prop_count; i++) { JS_FreeAtomRT(rt, pr->atom); pr++; @@ -15649,7 +16740,7 @@ static void js_free_shape0(JSRuntime *rt, JSShape *sh) static void js_free_shape(JSRuntime *rt, JSShape *sh) { - if (unlikely(--sh->header.ref_count <= 0)) { + if (unlikely(--JS_REF_COUNT(sh) <= 0)) { js_free_shape0(rt, sh); } } @@ -15693,17 +16784,24 @@ static no_inline int resize_properties(JSContext *ctx, JSShape **psh, return -1; sh = get_shape_from_alloc(sh_alloc, new_hash_size); list_del(&old_sh->header.link); - /* copy all the fields and the properties */ - memcpy(sh, old_sh, - sizeof(JSShape) + sizeof(sh->prop[0]) * old_sh->prop_count); + /* copy the shape header, then the properties. Their location relative + to the struct differs by layout, so copy via get_shape_prop(). */ + memcpy(sh, old_sh, sizeof(JSShape)); list_add_tail(&sh->header.link, &ctx->rt->gc_obj_list); + /* the GC/refcount fields live in the block header, not the struct, so + the memcpy above did not carry them: transfer them explicitly */ + JS_REF_COUNT(sh) = JS_REF_COUNT(old_sh); + JS_GC_TYPE(sh) = JS_GC_TYPE(old_sh); + JS_GC_MARK(sh) = JS_GC_MARK(old_sh); new_hash_mask = new_hash_size - 1; sh->prop_hash_mask = new_hash_mask; + memcpy(get_shape_prop(sh), get_shape_prop(old_sh), + sizeof(JSShapeProperty) * old_sh->prop_count); memset(prop_hash_end(sh) - new_hash_size, 0, sizeof(prop_hash_end(sh)[0]) * new_hash_size); - for(i = 0, pr = sh->prop; i < sh->prop_count; i++, pr++) { + for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) { if (pr->atom != JS_ATOM_NULL) { - h = ((uintptr_t)pr->atom & new_hash_mask); + h = hash_atom(pr->atom, new_hash_mask); pr->hash_next = prop_hash_end(sh)[-h - 1]; prop_hash_end(sh)[-h - 1] = i + 1; } @@ -15758,19 +16856,27 @@ static int compact_properties(JSContext *ctx, JSObject *p) list_del(&old_sh->header.link); memcpy(sh, old_sh, sizeof(JSShape)); list_add_tail(&sh->header.link, &ctx->rt->gc_obj_list); - + /* the GC/refcount fields live in the block header, not the struct, so the + memcpy above did not carry them: transfer them explicitly */ + JS_REF_COUNT(sh) = JS_REF_COUNT(old_sh); + JS_GC_TYPE(sh) = JS_GC_TYPE(old_sh); + JS_GC_MARK(sh) = JS_GC_MARK(old_sh); + + /* set the new hash mask before prop_hash_end()/get_shape_prop() are used, + as their locations depend on it in the merged-header layout */ + sh->prop_hash_mask = new_hash_mask; memset(prop_hash_end(sh) - new_hash_size, 0, sizeof(prop_hash_end(sh)[0]) * new_hash_size); j = 0; - old_pr = old_sh->prop; - pr = sh->prop; + old_pr = get_shape_prop(old_sh); + pr = get_shape_prop(sh); prop = p->prop; for(i = 0; i < sh->prop_count; i++) { if (old_pr->atom != JS_ATOM_NULL) { pr->atom = old_pr->atom; pr->flags = old_pr->flags; - h = ((uintptr_t)old_pr->atom & new_hash_mask); + h = hash_atom(old_pr->atom, new_hash_mask); pr->hash_next = prop_hash_end(sh)[-h - 1]; prop_hash_end(sh)[-h - 1] = j + 1; prop[j] = prop[i]; @@ -15826,13 +16932,13 @@ static int add_shape_property(JSContext *ctx, JSShape **psh, } /* Initialize the new shape property. The object property at p->prop[sh->prop_count] is uninitialized */ - prop = sh->prop; + prop = get_shape_prop(sh); pr = &prop[sh->prop_count++]; pr->atom = JS_DupAtom(ctx, atom); pr->flags = prop_flags; /* add in hash table */ hash_mask = sh->prop_hash_mask; - h = atom & hash_mask; + h = hash_atom(atom, hash_mask); pr->hash_next = prop_hash_end(sh)[-h - 1]; prop_hash_end(sh)[-h - 1] = sh->prop_count; return 0; @@ -15876,12 +16982,12 @@ static JSShape *find_hashed_shape_prop(JSRuntime *rt, JSShape *sh, sh1->proto == sh->proto && sh1->prop_count == ((n = sh->prop_count) + 1)) { for(i = 0; i < n; i++) { - if (unlikely(sh1->prop[i].atom != sh->prop[i].atom) || - unlikely(sh1->prop[i].flags != sh->prop[i].flags)) + if (unlikely(get_shape_prop(sh1)[i].atom != get_shape_prop(sh)[i].atom) || + unlikely(get_shape_prop(sh1)[i].flags != get_shape_prop(sh)[i].flags)) goto next; } - if (unlikely(sh1->prop[n].atom != atom) || - unlikely(sh1->prop[n].flags != prop_flags)) + if (unlikely(get_shape_prop(sh1)[n].atom != atom) || + unlikely(get_shape_prop(sh1)[n].flags != prop_flags)) goto next; return sh1; } @@ -15897,11 +17003,11 @@ static __maybe_unused void JS_DumpShape(JSRuntime *rt, int i, JSShape *sh) /* XXX: should output readable class prototype */ printf("%5d %3d%c %14p %5d %5d", i, - sh->header.ref_count, " *"[sh->is_hashed], + JS_REF_COUNT(sh), " *"[sh->is_hashed], (void *)sh->proto, sh->prop_size, sh->prop_count); for(j = 0; j < sh->prop_count; j++) { printf(" %s", JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf), - sh->prop[j].atom)); + get_shape_prop(sh)[j].atom)); } printf("\n"); } @@ -15925,7 +17031,7 @@ static __maybe_unused void JS_DumpShapes(JSRuntime *rt) /* dump non-hashed shapes */ list_for_each(el, &rt->gc_obj_list) { gp = list_entry(el, JSGCObjectHeader, link); - if (gp->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) { + if (JS_GC_TYPE(gp) == JS_GC_OBJ_TYPE_JS_OBJECT) { p = (JSObject *)gp; if (!p->shape->is_hashed) { JS_DumpShape(rt, -1, p->shape); @@ -15943,7 +17049,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas JSObject *p; int i; - js_trigger_gc(ctx->rt, sizeof(JSObject)); + js_trigger_gc(ctx->rt, sizeof(JSObject) + sizeof(JSProperty) * sh->prop_size); p = js_malloc(ctx, sizeof(JSObject)); if (unlikely(!p)) goto fail; @@ -15965,7 +17071,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas js_free(ctx, p); fail: if (props) { - JSShapeProperty *prs = sh->prop; + JSShapeProperty *prs = get_shape_prop(sh); for(i = 0; i < sh->prop_count; i++) { free_property(ctx->rt, &props[i], prs->flags); prs++; @@ -16027,6 +17133,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas p->u.array.u.ptr = NULL; p->u.array.count = 0; break; + case JS_CLASS_ERROR: case JS_CLASS_NUMBER: case JS_CLASS_STRING: case JS_CLASS_BOOLEAN: @@ -16046,7 +17153,7 @@ static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID clas } break; } - p->header.ref_count = 1; + JS_REF_COUNT(p) = 1; add_gc_object(ctx->rt, &p->header, JS_GC_OBJ_TYPE_JS_OBJECT); if (props) { for(i = 0; i < sh->prop_count; i++) @@ -16135,48 +17242,21 @@ JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto) JSValue JS_NewObjectFrom(JSContext *ctx, int count, const JSAtom *props, const JSValue *values) { - JSShapeProperty *pr; - uint32_t *hash; - JSRuntime *rt; - JSObject *p; - JSShape *sh; JSValue obj; - JSAtom atom; - intptr_t h; int i; - rt = ctx->rt; obj = JS_NewObject(ctx); if (JS_IsException(obj)) return JS_EXCEPTION; - if (count > 0) { - p = JS_VALUE_GET_OBJ(obj); - sh = p->shape; - assert(sh->is_hashed); - assert(sh->header.ref_count == 1); - js_shape_hash_unlink(rt, sh); - if (resize_properties(ctx, &sh, p, count)) { - js_shape_hash_link(rt, sh); - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - p->shape = sh; - for (i = 0; i < count; i++) { - atom = props[i]; - pr = &sh->prop[i]; - sh->hash = shape_hash(shape_hash(sh->hash, atom), JS_PROP_C_W_E); - h = atom & sh->prop_hash_mask; - hash = &prop_hash_end(sh)[-h - 1]; - pr->hash_next = *hash; - *hash = i + 1; - pr->atom = JS_DupAtom(ctx, atom); - pr->flags = JS_PROP_C_W_E; - p->prop[i].u.value = values[i]; - } - js_shape_hash_link(rt, sh); - sh->prop_count = count; - } + for (i = 0; i < count; i++) + if (JS_SetProperty(ctx, obj, props[i], values[i]) < 0) + goto fail; return obj; +fail: + for (/*empty*/; i < count; i++) + JS_FreeValue(ctx, values[i]); + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; } JSValue JS_NewObjectFromStr(JSContext *ctx, int count, const char **props, @@ -16455,6 +17535,7 @@ static JSValue js_call_c_function_data(JSContext *ctx, JSValueConst func_obj, rt->current_stack_frame = sf; // TODO(bnoordhuis) switch realms like js_call_c_function does sf->is_strict_mode = false; + sf->is_constructor = (flags & JS_CALL_FLAG_CONSTRUCTOR) != 0; sf->cur_func = unsafe_unconst(func_obj); sf->arg_count = argc; ret = s->func(ctx, this_val, argc, arg_buf, s->magic, vc(s->data)); @@ -16580,6 +17661,7 @@ static JSValue js_call_c_closure(JSContext *ctx, JSValueConst func_obj, rt->current_stack_frame = sf; // TODO(bnoordhuis) switch realms like js_call_c_function does sf->is_strict_mode = false; + sf->is_constructor = (flags & JS_CALL_FLAG_CONSTRUCTOR) != 0; sf->cur_func = unsafe_unconst(func_obj); sf->arg_count = argc; ret = s->func(ctx, this_val, argc, arg_buf, s->magic, s->opaque); @@ -16648,9 +17730,9 @@ static inline JSShapeProperty *find_own_property1(JSObject *p, JSAtom atom) JSShapeProperty *pr, *prop; intptr_t h; sh = p->shape; - h = (uintptr_t)atom & sh->prop_hash_mask; + h = hash_atom(atom, sh->prop_hash_mask); h = prop_hash_end(sh)[-h - 1]; - prop = sh->prop; + prop = get_shape_prop(sh); while (h) { pr = &prop[h - 1]; if (likely(pr->atom == atom)) { @@ -16669,9 +17751,9 @@ static inline JSShapeProperty *find_own_property(JSProperty **ppr, JSShapeProperty *pr, *prop; intptr_t h; sh = p->shape; - h = (uintptr_t)atom & sh->prop_hash_mask; + h = hash_atom(atom, sh->prop_hash_mask); h = prop_hash_end(sh)[-h - 1]; - prop = sh->prop; + prop = get_shape_prop(sh); while (h) { pr = &prop[h - 1]; if (likely(pr->atom == atom)) { @@ -16685,11 +17767,28 @@ static inline JSShapeProperty *find_own_property(JSProperty **ppr, return NULL; } +/* Release a counted reference an open var_ref held on the coroutine + (async function / generator / async generator) owning the frame it + points into. */ +static void js_release_coro(JSRuntime *rt, JSGCObjectHeader *coro) +{ + switch (JS_GC_TYPE(coro)) { + case JS_GC_OBJ_TYPE_ASYNC_FUNCTION: + js_async_function_free(rt, (JSAsyncFunctionData *)coro); + break; + case JS_GC_OBJ_TYPE_JS_OBJECT: /* generator / async generator */ + JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, (JSObject *)coro)); + break; + default: + abort(); + } +} + static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref) { if (var_ref) { - assert(var_ref->header.ref_count > 0); - if (--var_ref->header.ref_count == 0) { + assert(JS_REF_COUNT(var_ref) > 0); + if (--JS_REF_COUNT(var_ref) == 0) { if (var_ref->is_detached) { JS_FreeValueRT(rt, var_ref->value); remove_gc_object(&var_ref->header); @@ -16697,6 +17796,12 @@ static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref) JSStackFrame *sf = var_ref->stack_frame; assert(sf->var_refs[var_ref->var_ref_idx] == var_ref); sf->var_refs[var_ref->var_ref_idx] = NULL; + /* an open coroutine var_ref is itself a GC object and holds + a counted ref to its coroutine (sf->cur_gc_obj) */ + if (var_ref->is_coro) { + js_release_coro(rt, sf->cur_gc_obj); + remove_gc_object(&var_ref->header); + } } js_free_rt(rt, var_ref); } @@ -16706,7 +17811,7 @@ static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref) static void js_array_finalizer(JSRuntime *rt, JSValueConst val) { JSObject *p = JS_VALUE_GET_OBJ(val); - int i; + uint32_t i; for(i = 0; i < p->u.array.count; i++) { JS_FreeValueRT(rt, p->u.array.u.values[i]); @@ -16718,7 +17823,7 @@ static void js_array_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func) { JSObject *p = JS_VALUE_GET_OBJ(val); - int i; + uint32_t i; for(i = 0; i < p->u.array.count; i++) { JS_MarkValue(rt, p->u.array.u.values[i], mark_func); @@ -16795,7 +17900,10 @@ static void js_bytecode_function_mark(JSRuntime *rt, JSValueConst val, if (var_refs) { for(i = 0; i < b->closure_var_count; i++) { JSVarRef *var_ref = var_refs[i]; - if (var_ref && var_ref->is_detached) { + /* Detached var_refs are GC objects; open var_refs are GC + objects only when they capture a coroutine local (see + get_var_ref). Only those may be marked. */ + if (var_ref && (var_ref->is_detached || var_ref->is_coro)) { mark_func(rt, &var_ref->header); } } @@ -16860,7 +17968,7 @@ static void free_object(JSRuntime *rt, JSObject *p) freeing cycles */ /* free all the fields */ sh = p->shape; - pr = sh->prop; + pr = get_shape_prop(sh); for(i = 0; i < sh->prop_count; i++) { free_property(rt, &p->prop[i], pr->flags); pr++; @@ -16889,7 +17997,7 @@ static void free_object(JSRuntime *rt, JSObject *p) p->u.func.home_object = NULL; remove_gc_object(&p->header); - if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && p->header.ref_count != 0) { + if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && JS_REF_COUNT(p) != 0) { list_add_tail(&p->header.link, &rt->gc_zero_ref_count_list); } else { js_free_rt(rt, p); @@ -16898,7 +18006,7 @@ static void free_object(JSRuntime *rt, JSObject *p) static void free_gc_object(JSRuntime *rt, JSGCObjectHeader *gp) { - switch(gp->gc_obj_type) { + switch(JS_GC_TYPE(gp)) { case JS_GC_OBJ_TYPE_JS_OBJECT: free_object(rt, (JSObject *)gp); break; @@ -16921,7 +18029,7 @@ static void free_zero_refcount(JSRuntime *rt) if (el == &rt->gc_zero_ref_count_list) break; p = list_entry(el, JSGCObjectHeader, link); - assert(p->ref_count == 0); + assert(JS_REF_COUNT(p) == 0); free_gc_object(rt, p); } rt->gc_phase = JS_GC_PHASE_NONE; @@ -16997,8 +18105,8 @@ static void js_free_value_rt(JSRuntime *rt, JSValue v) void JS_FreeValueRT(JSRuntime *rt, JSValue v) { if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - if (--p->ref_count <= 0) { + void *p = JS_VALUE_GET_PTR(v); + if (--JS_REF_COUNT(p) <= 0) { js_free_value_rt(rt, v); } } @@ -17014,8 +18122,8 @@ void JS_FreeValue(JSContext *ctx, JSValue v) static void add_gc_object(JSRuntime *rt, JSGCObjectHeader *h, JSGCObjectTypeEnum type) { - h->mark = 0; - h->gc_obj_type = type; + JS_GC_MARK(h) = 0; + JS_GC_TYPE(h) = type; list_add_tail(&h->link, &rt->gc_obj_list); } @@ -17057,7 +18165,7 @@ static void mark_weak_map_value(JSRuntime *rt, JSWeakRefRecord *first_weak_ref, static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp, JS_MarkFunc *mark_func) { - switch(gp->gc_obj_type) { + switch(JS_GC_TYPE(gp)) { case JS_GC_OBJ_TYPE_JS_OBJECT: { JSObject *p = (JSObject *)gp; @@ -17067,7 +18175,7 @@ static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp, sh = p->shape; mark_func(rt, &sh->header); /* mark all the fields */ - prs = sh->prop; + prs = get_shape_prop(sh); for(i = 0; i < sh->prop_count; i++) { JSProperty *pr = &p->prop[i]; if (prs->atom != JS_ATOM_NULL) { @@ -17078,7 +18186,8 @@ static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp, if (pr->u.getset.setter) mark_func(rt, &pr->u.getset.setter->header); } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) { - if (pr->u.var_ref->is_detached) { + if (pr->u.var_ref->is_detached || + pr->u.var_ref->is_coro) { /* Note: the tag does not matter provided it is a GC object */ mark_func(rt, &pr->u.var_ref->header); @@ -17120,9 +18229,17 @@ static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp, case JS_GC_OBJ_TYPE_VAR_REF: { JSVarRef *var_ref = (JSVarRef *)gp; - /* only detached variable referenced are taken into account */ - assert(var_ref->is_detached); - JS_MarkValue(rt, *var_ref->pvalue, mark_func); + if (var_ref->is_detached) { + /* the var_ref owns its value */ + JS_MarkValue(rt, *var_ref->pvalue, mark_func); + } else { + /* open var_ref: the value lives in the coroutine's frame + and is marked by the coroutine itself; only keep that + coroutine reachable. (Open var_refs are GC objects only + when they capture a coroutine local.) */ + assert(var_ref->is_coro); + mark_func(rt, var_ref->stack_frame->cur_gc_obj); + } } break; case JS_GC_OBJ_TYPE_ASYNC_FUNCTION: @@ -17155,9 +18272,9 @@ static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp, static void gc_decref_child(JSRuntime *rt, JSGCObjectHeader *p) { - assert(p->ref_count > 0); - p->ref_count--; - if (p->ref_count == 0 && p->mark == 1) { + assert(JS_REF_COUNT(p) > 0); + JS_REF_COUNT(p)--; + if (JS_REF_COUNT(p) == 0 && JS_GC_MARK(p) == 1) { list_del(&p->link); list_add_tail(&p->link, &rt->tmp_obj_list); } @@ -17175,10 +18292,10 @@ static void gc_decref(JSRuntime *rt) tmp_obj_list */ list_for_each_safe(el, el1, &rt->gc_obj_list) { p = list_entry(el, JSGCObjectHeader, link); - assert(p->mark == 0); + assert(JS_GC_MARK(p) == 0); mark_children(rt, p, gc_decref_child); - p->mark = 1; - if (p->ref_count == 0) { + JS_GC_MARK(p) = 1; + if (JS_REF_COUNT(p) == 0) { list_del(&p->link); list_add_tail(&p->link, &rt->tmp_obj_list); } @@ -17187,19 +18304,19 @@ static void gc_decref(JSRuntime *rt) static void gc_scan_incref_child(JSRuntime *rt, JSGCObjectHeader *p) { - p->ref_count++; - if (p->ref_count == 1) { + JS_REF_COUNT(p)++; + if (JS_REF_COUNT(p) == 1) { /* ref_count was 0: remove from tmp_obj_list and add at the end of gc_obj_list */ list_del(&p->link); list_add_tail(&p->link, &rt->gc_obj_list); - p->mark = 0; /* reset the mark for the next GC call */ + JS_GC_MARK(p) = 0; /* reset the mark for the next GC call */ } } static void gc_scan_incref_child2(JSRuntime *rt, JSGCObjectHeader *p) { - p->ref_count++; + JS_REF_COUNT(p)++; } static void gc_scan(JSRuntime *rt) @@ -17210,8 +18327,8 @@ static void gc_scan(JSRuntime *rt) /* keep the objects with a refcount > 0 and their children. */ list_for_each(el, &rt->gc_obj_list) { p = list_entry(el, JSGCObjectHeader, link); - assert(p->ref_count > 0); - p->mark = 0; /* reset the mark for the next GC call */ + assert(JS_REF_COUNT(p) > 0); + JS_GC_MARK(p) = 0; /* reset the mark for the next GC call */ mark_children(rt, p, gc_scan_incref_child); } @@ -17240,7 +18357,7 @@ static void gc_free_cycles(JSRuntime *rt) /* Only need to free the GC object associated with JS values. The rest will be automatically removed because they must be referenced by them. */ - switch(p->gc_obj_type) { + switch(JS_GC_TYPE(p)) { case JS_GC_OBJ_TYPE_JS_OBJECT: case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE: #ifdef ENABLE_DUMPS // JS_DUMP_GC_FREE @@ -17265,8 +18382,8 @@ static void gc_free_cycles(JSRuntime *rt) list_for_each_safe(el, el1, &rt->gc_zero_ref_count_list) { p = list_entry(el, JSGCObjectHeader, link); - assert(p->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT || - p->gc_obj_type == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE); + assert(JS_GC_TYPE(p) == JS_GC_OBJ_TYPE_JS_OBJECT || + JS_GC_TYPE(p) == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE); js_free_rt(rt, p); } @@ -17316,7 +18433,7 @@ static void compute_value_size(JSValue val, JSMemoryUsage_helper *hp); static void compute_jsstring_size(JSString *str, JSMemoryUsage_helper *hp) { if (!str->atom_type) { /* atoms are handled separately */ - double s_ref_count = str->header.ref_count; + double s_ref_count = JS_REF_COUNT(str); hp->str_count += 1 / s_ref_count; hp->str_size += ((sizeof(*str) + (str->len << str->is_wide_char) + 1 - str->is_wide_char) / s_ref_count); @@ -17438,10 +18555,10 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s) JSShapeProperty *prs; /* XXX: could count the other GC object types too */ - if (gp->gc_obj_type == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE) { + if (JS_GC_TYPE(gp) == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE) { compute_bytecode_size((JSFunctionBytecode *)gp, hp); continue; - } else if (gp->gc_obj_type != JS_GC_OBJ_TYPE_JS_OBJECT) { + } else if (JS_GC_TYPE(gp) != JS_GC_OBJ_TYPE_JS_OBJECT) { continue; } p = (JSObject *)gp; @@ -17451,7 +18568,7 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s) s->memory_used_count++; s->prop_size += sh->prop_size * sizeof(*p->prop); s->prop_count += sh->prop_count; - prs = sh->prop; + prs = get_shape_prop(sh); for(i = 0; i < sh->prop_count; i++) { JSProperty *pr = &p->prop[i]; if (prs->atom != JS_ATOM_NULL && !(prs->flags & JS_PROP_TMASK)) { @@ -17484,6 +18601,7 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s) } } break; + case JS_CLASS_ERROR: /* u.object_data */ case JS_CLASS_NUMBER: /* u.object_data */ case JS_CLASS_STRING: /* u.object_data */ case JS_CLASS_BOOLEAN: /* u.object_data */ @@ -17505,7 +18623,7 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s) s->js_func_size += b->closure_var_count * sizeof(*var_refs); for (i = 0; i < b->closure_var_count; i++) { if (var_refs[i]) { - double ref_count = var_refs[i]->header.ref_count; + double ref_count = JS_REF_COUNT(var_refs[i]); s->memory_used_count += 1 / ref_count; s->js_func_size += sizeof(*var_refs[i]) / ref_count; /* handle non object closed values */ @@ -17699,7 +18817,7 @@ void JS_DumpMemoryUsage(FILE *fp, const JSMemoryUsage *s, JSRuntime *rt) list_for_each(el, &rt->gc_obj_list) { JSGCObjectHeader *gp = list_entry(el, JSGCObjectHeader, link); JSObject *p; - if (gp->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) { + if (JS_GC_TYPE(gp) == JS_GC_OBJ_TYPE_JS_OBJECT) { p = (JSObject *)gp; obj_classes[min_uint32(p->class_id, JS_CLASS_INIT_COUNT)]++; } @@ -17871,7 +18989,8 @@ static int find_line_num(JSContext *ctx, JSFunctionBytecode *b, uint32_t pc_value, int *col) { const uint8_t *p_end, *p; - int new_line_num, new_col_num, line_num, col_num, pc, v, ret; + int new_line_num, new_col_num, line_num, col_num, pc, ret; + int32_t v; unsigned int op; *col = 1; @@ -17947,7 +19066,23 @@ static bool can_add_backtrace(JSValueConst obj) if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) return false; p = JS_VALUE_GET_OBJ(obj); - if (p->class_id != JS_CLASS_ERROR && p->class_id != JS_CLASS_DOM_EXCEPTION) + if (p->class_id != JS_CLASS_DOM_EXCEPTION) + return false; + if (find_own_property1(p, JS_ATOM_stack)) + return false; + return true; +} + +/* Note: it is important that no exception is returned by this function */ +static bool can_store_error_stack(JSValueConst obj) +{ + JSObject *p; + if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) + return false; + p = JS_VALUE_GET_OBJ(obj); + if (p->class_id != JS_CLASS_ERROR) + return false; + if (!JS_IsUndefined(p->u.object_data)) return false; if (find_own_property1(p, JS_ATOM_stack)) return false; @@ -17966,7 +19101,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val, int line_num, int col_num, int backtrace_flags) { JSStackFrame *sf, *sf_start; - JSValue stack, prepare, saved_exception; + JSValue stack, prepare, saved_exception, error_obj; DynBuf dbuf; const char *func_name_str; const char *str1; @@ -17983,6 +19118,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val, if (rt->in_build_stack_trace) return; rt->in_build_stack_trace = true; + error_obj = js_dup(error_val); // Save exception because conversion to double may fail. saved_exception = JS_GetException(ctx); @@ -18115,10 +19251,8 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val, JSValue v = js_new_callsite(ctx, &csd[j]); if (JS_IsException(v)) break; - if (JS_DefinePropertyValueUint32(ctx, stack, j, v, JS_PROP_C_W_E) < 0) { - JS_FreeValue(ctx, v); + if (JS_DefinePropertyValueUint32(ctx, stack, j, v, JS_PROP_C_W_E) < 0) break; - } } } // Clear the csd's we didn't use in case of error. @@ -18128,7 +19262,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val, JS_FreeValue(ctx, csd[k].func_name); } JSValueConst args[] = { - error_val, + error_obj, stack, }; JSValue stack2 = JS_Call(ctx, prepare, ctx->error_ctor, countof(args), args); @@ -18149,13 +19283,25 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val, if (JS_IsUndefined(ctx->error_back_trace)) ctx->error_back_trace = js_dup(stack); - if (has_filter_func || can_add_backtrace(error_val)) { - JS_DefinePropertyValue(ctx, error_val, JS_ATOM_stack, stack, + if (has_filter_func) { + /* Error.captureStackTrace(target, ...): install an own data property + on the (possibly non-Error) target, shadowing the accessor */ + JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, stack, + JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + } else if (can_store_error_stack(error_obj)) { + /* genuine Error instance: store as the [[ErrorData]] stack value */ + p = JS_VALUE_GET_OBJ(error_obj); + JS_FreeValue(ctx, p->u.object_data); + p->u.object_data = stack; + } else if (can_add_backtrace(error_obj)) { + /* DOMException and the like keep an own "stack" data property */ + JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, stack, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); } else { JS_FreeValue(ctx, stack); } + JS_FreeValue(ctx, error_obj); rt->in_build_stack_trace = false; } @@ -18242,31 +19388,35 @@ JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, X(Type, TYPE) \ #define X(lc, uc) \ - JSValue JS_PRINTF_FORMAT_ATTR(2, 3) \ - JS_New##lc##Error(JSContext *ctx, \ - JS_PRINTF_FORMAT const char *fmt, ...) \ - { \ - JSValue val; \ - va_list ap; \ - \ - va_start(ap, fmt); \ - val = JS_MakeError(ctx, JS_##uc##_ERROR, \ - /*add_backtrace*/true, fmt, ap); \ - va_end(ap); \ - return val; \ - } \ - JSValue JS_PRINTF_FORMAT_ATTR(2, 3) \ - JS_Throw##lc##Error(JSContext *ctx, \ - JS_PRINTF_FORMAT const char *fmt, ...) \ - { \ - JSValue val; \ - va_list ap; \ - \ - va_start(ap, fmt); \ - val = JS_ThrowError(ctx, JS_##uc##_ERROR, fmt, ap); \ - va_end(ap); \ - return val; \ - } \ + JSValue JS_PRINTF_FORMAT_ATTR(2, 3) \ + JS_New##lc##Error(JSContext *ctx, \ + JS_PRINTF_FORMAT const char *fmt, ...) \ + { \ + JSValue val; \ + va_list ap; \ + \ + va_start(ap, fmt); \ + val = JS_MakeError(ctx, JS_##uc##_ERROR, \ + /*add_backtrace*/true, fmt, ap); \ + va_end(ap); \ + if (JS_##uc##_ERROR == JS_INTERNAL_ERROR) \ + js_set_uncatchable_error(val, true); \ + return val; \ + } \ + JSValue JS_PRINTF_FORMAT_ATTR(2, 3) \ + JS_Throw##lc##Error(JSContext *ctx, \ + JS_PRINTF_FORMAT const char *fmt, ...) \ + { \ + JSValue val; \ + va_list ap; \ + \ + va_start(ap, fmt); \ + val = JS_ThrowError(ctx, JS_##uc##_ERROR, fmt, ap); \ + va_end(ap); \ + if (JS_##uc##_ERROR == JS_INTERNAL_ERROR) \ + js_set_uncatchable_error(ctx->rt->current_exception, true); \ + return val; \ + } \ JS_ERROR_MAP(X) @@ -18419,7 +19569,7 @@ static no_inline __exception int __js_poll_interrupts(JSContext *ctx) JSRuntime *rt = ctx->rt; ctx->interrupt_counter = JS_INTERRUPT_COUNTER_INIT; if (rt->interrupt_handler) { - if (rt->interrupt_handler(rt, rt->interrupt_opaque)) { + if (rt->interrupt_handler(ctx, rt->interrupt_opaque)) { JS_ThrowInterrupted(ctx); return -1; } @@ -18701,10 +19851,10 @@ int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj) #include -const uint32_t qjsc_builtin_array_fromasync_size = 875; +const uint32_t qjsc_builtin_array_fromasync_size = 890; -const uint8_t qjsc_builtin_array_fromasync[875] = { - 0x19, 0x26, 0x96, 0xb9, 0xe5, 0x0e, 0x01, 0x28, +const uint8_t qjsc_builtin_array_fromasync[890] = { + 0x1d, 0xd5, 0x99, 0xb7, 0x67, 0x0e, 0x01, 0x28, 0x53, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0xb7, 0x61, 0x73, 0x79, 0x6e, 0x63, 0x49, 0x74, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x01, 0x2a, 0x4f, 0x62, @@ -18712,108 +19862,110 @@ const uint8_t qjsc_builtin_array_fromasync[875] = { 0x69, 0x6e, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x01, 0x1e, 0x53, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0xb7, 0x69, 0x74, 0x65, 0x72, - 0x61, 0x74, 0x6f, 0x72, 0x01, 0x12, 0x61, 0x72, - 0x72, 0x61, 0x79, 0x4c, 0x69, 0x6b, 0x65, 0x01, - 0x0a, 0x6d, 0x61, 0x70, 0x46, 0x6e, 0x01, 0x0e, - 0x74, 0x68, 0x69, 0x73, 0x41, 0x72, 0x67, 0x01, - 0x0c, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x01, - 0x02, 0x69, 0x01, 0x1a, 0x69, 0x73, 0x43, 0x6f, - 0x6e, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x6f, - 0x72, 0x01, 0x08, 0x73, 0x79, 0x6e, 0x63, 0x01, - 0x0c, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x01, - 0x08, 0x69, 0x74, 0x65, 0x72, 0x01, 0x1c, 0x6e, - 0x6f, 0x74, 0x20, 0x61, 0x20, 0x66, 0x75, 0x6e, - 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x01, 0x08, 0x63, - 0x61, 0x6c, 0x6c, 0x0c, 0x00, 0x02, 0x00, 0xa2, + 0x61, 0x74, 0x6f, 0x72, 0x01, 0x08, 0x63, 0x61, + 0x6c, 0x6c, 0x01, 0x12, 0x61, 0x72, 0x72, 0x61, + 0x79, 0x4c, 0x69, 0x6b, 0x65, 0x01, 0x0a, 0x6d, + 0x61, 0x70, 0x46, 0x6e, 0x01, 0x0e, 0x74, 0x68, + 0x69, 0x73, 0x41, 0x72, 0x67, 0x01, 0x0c, 0x72, + 0x65, 0x73, 0x75, 0x6c, 0x74, 0x01, 0x02, 0x69, + 0x01, 0x1a, 0x69, 0x73, 0x43, 0x6f, 0x6e, 0x73, + 0x74, 0x72, 0x75, 0x63, 0x74, 0x6f, 0x72, 0x01, + 0x08, 0x73, 0x79, 0x6e, 0x63, 0x01, 0x0c, 0x6d, + 0x65, 0x74, 0x68, 0x6f, 0x64, 0x01, 0x08, 0x69, + 0x74, 0x65, 0x72, 0x01, 0x1c, 0x6e, 0x6f, 0x74, + 0x20, 0x61, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, + 0x69, 0x6f, 0x6e, 0x0c, 0x00, 0x02, 0x00, 0xa8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, - 0x04, 0x01, 0xa4, 0x01, 0x00, 0x00, 0x00, 0x0c, - 0x43, 0x02, 0x01, 0x00, 0x05, 0x00, 0x05, 0x01, - 0x05, 0x00, 0x01, 0x03, 0x05, 0xb0, 0x02, 0x00, - 0x01, 0x40, 0x03, 0xa6, 0x03, 0x00, 0x01, 0x40, - 0x00, 0xcc, 0x03, 0x00, 0x01, 0x40, 0x01, 0xce, - 0x03, 0x00, 0x01, 0x40, 0x04, 0xd0, 0x03, 0x00, - 0x01, 0x40, 0x02, 0x0c, 0x60, 0x02, 0x01, 0xfa, - 0x01, 0x03, 0x0e, 0x01, 0x06, 0x00, 0x05, 0x00, - 0x86, 0x04, 0x11, 0xd2, 0x03, 0x00, 0x01, 0x00, - 0xd4, 0x03, 0x00, 0x01, 0x00, 0xd6, 0x03, 0x00, - 0x01, 0x00, 0xd2, 0x03, 0x01, 0xff, 0xff, 0xff, - 0xff, 0x0f, 0x20, 0xd4, 0x03, 0x01, 0x01, 0x20, - 0xd6, 0x03, 0x01, 0x02, 0x20, 0xd8, 0x03, 0x02, - 0x00, 0x20, 0xda, 0x03, 0x02, 0x04, 0x20, 0xdc, - 0x03, 0x02, 0x05, 0x20, 0xde, 0x03, 0x02, 0x06, - 0x20, 0xe0, 0x03, 0x02, 0x07, 0x20, 0x64, 0x06, - 0x08, 0x20, 0x82, 0x01, 0x07, 0x09, 0x20, 0xe2, - 0x03, 0x0a, 0x08, 0x30, 0x82, 0x01, 0x0d, 0x0b, - 0x20, 0xd4, 0x01, 0x0d, 0x0c, 0x20, 0x10, 0x00, - 0x01, 0x00, 0xa6, 0x03, 0x01, 0x01, 0xcc, 0x03, - 0x02, 0x01, 0xd0, 0x03, 0x04, 0x01, 0xb0, 0x02, - 0x00, 0x01, 0xce, 0x03, 0x03, 0x01, 0x08, 0xc2, - 0x0d, 0x60, 0x02, 0x00, 0x60, 0x01, 0x00, 0x60, - 0x00, 0x00, 0xd1, 0xc9, 0xd2, 0x11, 0xf2, 0xea, - 0x08, 0x0e, 0x38, 0x46, 0x00, 0x00, 0x00, 0xda, - 0xca, 0xd3, 0x11, 0xf2, 0xea, 0x08, 0x0e, 0x38, - 0x46, 0x00, 0x00, 0x00, 0xdb, 0xcb, 0x60, 0x07, - 0x00, 0x60, 0x06, 0x00, 0x60, 0x05, 0x00, 0x60, - 0x04, 0x00, 0x60, 0x03, 0x00, 0xd2, 0x38, 0x46, - 0x00, 0x00, 0x00, 0xae, 0xea, 0x16, 0xd2, 0x96, - 0x04, 0x1b, 0x00, 0x00, 0x00, 0xae, 0xea, 0x0c, - 0xdd, 0x11, 0x04, 0xf2, 0x00, 0x00, 0x00, 0x21, - 0x01, 0x00, 0x30, 0x06, 0xcc, 0xb4, 0xc2, 0x04, - 0xc1, 0x0d, 0xf5, 0xc2, 0x05, 0x09, 0xc2, 0x06, - 0xd1, 0xde, 0x46, 0xc2, 0x07, 0x61, 0x07, 0x00, - 0x07, 0xab, 0xea, 0x0f, 0x0a, 0x11, 0x62, 0x06, - 0x00, 0x0e, 0xd1, 0xdf, 0x46, 0x11, 0x62, 0x07, - 0x00, 0x0e, 0x61, 0x07, 0x00, 0x07, 0xab, 0x68, - 0xa6, 0x00, 0x00, 0x00, 0x60, 0x08, 0x00, 0x06, - 0x11, 0xf2, 0xeb, 0x0c, 0x6f, 0x41, 0x32, 0x00, - 0x00, 0x00, 0xc2, 0x08, 0x0e, 0xec, 0x05, 0x0e, - 0xd1, 0xec, 0xf2, 0x61, 0x08, 0x00, 0x8c, 0x11, - 0xeb, 0x03, 0x0e, 0xb4, 0x11, 0x62, 0x08, 0x00, - 0x0e, 0x61, 0x05, 0x00, 0xea, 0x0c, 0xc1, 0x0d, - 0x11, 0x61, 0x08, 0x00, 0x21, 0x01, 0x00, 0xec, - 0x06, 0xe0, 0x61, 0x08, 0x00, 0xef, 0x11, 0x62, - 0x03, 0x00, 0x0e, 0x61, 0x04, 0x00, 0x61, 0x08, - 0x00, 0xa5, 0x68, 0x2a, 0x01, 0x00, 0x00, 0x60, - 0x09, 0x00, 0xd1, 0x61, 0x04, 0x00, 0x46, 0xc2, - 0x09, 0x61, 0x06, 0x00, 0xea, 0x0a, 0x61, 0x09, - 0x00, 0x8a, 0x11, 0x62, 0x09, 0x00, 0x0e, 0xd2, - 0xea, 0x17, 0xd2, 0x41, 0xf3, 0x00, 0x00, 0x00, - 0xd3, 0x61, 0x09, 0x00, 0x61, 0x04, 0x00, 0x24, - 0x03, 0x00, 0x8a, 0x11, 0x62, 0x09, 0x00, 0x0e, - 0x5d, 0x04, 0x00, 0x61, 0x03, 0x00, 0x61, 0x04, + 0x04, 0x01, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x0c, + 0x43, 0x02, 0x01, 0x00, 0x06, 0x00, 0x06, 0x01, + 0x06, 0x00, 0x01, 0x03, 0x06, 0xbc, 0x02, 0x00, + 0x01, 0x40, 0x03, 0xb2, 0x03, 0x00, 0x01, 0x40, + 0x00, 0xe2, 0x03, 0x00, 0x01, 0x40, 0x01, 0xe4, + 0x03, 0x00, 0x01, 0x40, 0x05, 0xe6, 0x03, 0x00, + 0x01, 0x40, 0x02, 0xe8, 0x03, 0x00, 0x01, 0x40, + 0x04, 0x0c, 0x60, 0x02, 0x01, 0x86, 0x02, 0x03, + 0x0e, 0x01, 0x06, 0x00, 0x06, 0x00, 0x8b, 0x04, + 0x11, 0xea, 0x03, 0x00, 0x01, 0x00, 0xec, 0x03, + 0x00, 0x01, 0x00, 0xee, 0x03, 0x00, 0x01, 0x00, + 0xea, 0x03, 0x01, 0xff, 0xff, 0xff, 0xff, 0x0f, + 0x20, 0xec, 0x03, 0x01, 0x01, 0x20, 0xee, 0x03, + 0x01, 0x02, 0x20, 0xf0, 0x03, 0x02, 0x00, 0x20, + 0xf2, 0x03, 0x02, 0x04, 0x20, 0xf4, 0x03, 0x02, + 0x05, 0x20, 0xf6, 0x03, 0x02, 0x06, 0x20, 0xf8, + 0x03, 0x02, 0x07, 0x20, 0x66, 0x06, 0x08, 0x20, + 0x88, 0x01, 0x07, 0x09, 0x20, 0xfa, 0x03, 0x0a, + 0x08, 0x30, 0x88, 0x01, 0x0d, 0x0b, 0x20, 0xe0, + 0x01, 0x0d, 0x0c, 0x20, 0x10, 0x00, 0x01, 0x00, + 0xb2, 0x03, 0x01, 0x01, 0xe2, 0x03, 0x02, 0x01, + 0xe6, 0x03, 0x04, 0x01, 0xbc, 0x02, 0x00, 0x01, + 0xe8, 0x03, 0x05, 0x01, 0xe4, 0x03, 0x03, 0x01, + 0x08, 0xc8, 0x0d, 0x60, 0x02, 0x00, 0x60, 0x01, + 0x00, 0x60, 0x00, 0x00, 0xd7, 0xcf, 0xd8, 0x11, + 0xf8, 0xf0, 0x08, 0x0e, 0x38, 0x49, 0x00, 0x00, + 0x00, 0xe0, 0xd0, 0xd9, 0x11, 0xf8, 0xf0, 0x08, + 0x0e, 0x38, 0x49, 0x00, 0x00, 0x00, 0xe1, 0xd1, + 0x60, 0x07, 0x00, 0x60, 0x06, 0x00, 0x60, 0x05, + 0x00, 0x60, 0x04, 0x00, 0x60, 0x03, 0x00, 0xd8, + 0x38, 0x49, 0x00, 0x00, 0x00, 0xae, 0xf0, 0x16, + 0xd8, 0x96, 0x04, 0x1b, 0x00, 0x00, 0x00, 0xae, + 0xf0, 0x0c, 0xe3, 0x11, 0x04, 0xfe, 0x00, 0x00, + 0x00, 0x21, 0x01, 0x00, 0x30, 0x06, 0xd2, 0xba, + 0xc8, 0x04, 0xc7, 0x0d, 0xfb, 0xc8, 0x05, 0x09, + 0xc8, 0x06, 0xd7, 0xe4, 0x46, 0xc8, 0x07, 0x61, + 0x07, 0x00, 0x07, 0xab, 0xf0, 0x0f, 0x0a, 0x11, + 0x62, 0x06, 0x00, 0x0e, 0xd7, 0xe5, 0x46, 0x11, + 0x62, 0x07, 0x00, 0x0e, 0x61, 0x07, 0x00, 0x07, + 0xab, 0x68, 0xaa, 0x00, 0x00, 0x00, 0x60, 0x08, + 0x00, 0x06, 0x11, 0xf8, 0xf1, 0x0c, 0x70, 0x41, + 0x33, 0x00, 0x00, 0x00, 0xc8, 0x08, 0x0e, 0xf2, + 0x05, 0x0e, 0xd7, 0xf2, 0xf2, 0x61, 0x08, 0x00, + 0x8c, 0x11, 0xf1, 0x03, 0x0e, 0xba, 0x11, 0x62, + 0x08, 0x00, 0x0e, 0x61, 0x05, 0x00, 0xf0, 0x0c, + 0xc7, 0x0d, 0x11, 0x61, 0x08, 0x00, 0x21, 0x01, + 0x00, 0xf2, 0x06, 0xe6, 0x61, 0x08, 0x00, 0xf5, + 0x11, 0x62, 0x03, 0x00, 0x0e, 0x61, 0x04, 0x00, + 0x61, 0x08, 0x00, 0xa5, 0x68, 0x2f, 0x01, 0x00, + 0x00, 0x60, 0x09, 0x00, 0xd7, 0x61, 0x04, 0x00, + 0x46, 0xc8, 0x09, 0x61, 0x06, 0x00, 0xf0, 0x0a, + 0x61, 0x09, 0x00, 0x8a, 0x11, 0x62, 0x09, 0x00, + 0x0e, 0xd8, 0xf0, 0x15, 0x5d, 0x04, 0x00, 0xd9, + 0xd8, 0x61, 0x09, 0x00, 0x61, 0x04, 0x00, 0x22, + 0x04, 0x00, 0x8a, 0x11, 0x62, 0x09, 0x00, 0x0e, + 0x5d, 0x05, 0x00, 0x61, 0x03, 0x00, 0x61, 0x04, 0x00, 0x90, 0x62, 0x04, 0x00, 0x0b, 0x61, 0x09, - 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, - 0x3e, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x3f, 0x00, - 0x00, 0x00, 0xf1, 0x0e, 0xec, 0x9e, 0x60, 0x0a, - 0x00, 0x61, 0x07, 0x00, 0x41, 0xf3, 0x00, 0x00, - 0x00, 0xd1, 0x24, 0x01, 0x00, 0xc2, 0x0a, 0x61, - 0x05, 0x00, 0xea, 0x09, 0xc1, 0x0d, 0x11, 0x21, - 0x00, 0x00, 0xec, 0x03, 0xe0, 0xee, 0x11, 0x62, - 0x03, 0x00, 0x0e, 0x6b, 0x8c, 0x00, 0x00, 0x00, - 0x60, 0x0c, 0x00, 0x60, 0x0b, 0x00, 0x06, 0x11, - 0xf2, 0xeb, 0x13, 0x6f, 0x41, 0x41, 0x00, 0x00, - 0x00, 0xc2, 0x0b, 0x41, 0x6a, 0x00, 0x00, 0x00, - 0xc2, 0x0c, 0x0e, 0xec, 0x10, 0x0e, 0x61, 0x0a, - 0x00, 0x41, 0x6b, 0x00, 0x00, 0x00, 0x24, 0x00, - 0x00, 0x8a, 0xec, 0xe0, 0x61, 0x0c, 0x00, 0xeb, - 0x4e, 0x61, 0x06, 0x00, 0xea, 0x0a, 0x61, 0x0b, - 0x00, 0x8a, 0x11, 0x62, 0x0b, 0x00, 0x0e, 0xd2, - 0xea, 0x17, 0xd2, 0x41, 0xf3, 0x00, 0x00, 0x00, - 0xd3, 0x61, 0x0b, 0x00, 0x61, 0x04, 0x00, 0x24, - 0x03, 0x00, 0x8a, 0x11, 0x62, 0x0b, 0x00, 0x0e, - 0x5d, 0x04, 0x00, 0x61, 0x03, 0x00, 0x61, 0x04, + 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, + 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x42, 0x00, + 0x00, 0x00, 0x0a, 0x4b, 0x43, 0x00, 0x00, 0x00, + 0xf7, 0x0e, 0xf2, 0x9a, 0x60, 0x0a, 0x00, 0x5d, + 0x04, 0x00, 0xd7, 0x61, 0x07, 0x00, 0xf6, 0xc8, + 0x0a, 0x61, 0x05, 0x00, 0xf0, 0x09, 0xc7, 0x0d, + 0x11, 0x21, 0x00, 0x00, 0xf2, 0x03, 0xe6, 0xf4, + 0x11, 0x62, 0x03, 0x00, 0x0e, 0x6b, 0x91, 0x00, + 0x00, 0x00, 0x60, 0x0c, 0x00, 0x60, 0x0b, 0x00, + 0x06, 0x11, 0xf8, 0xf1, 0x13, 0x70, 0x41, 0x44, + 0x00, 0x00, 0x00, 0xc8, 0x0b, 0x41, 0x70, 0x00, + 0x00, 0x00, 0xc8, 0x0c, 0x0e, 0xf2, 0x10, 0x0e, + 0x61, 0x0a, 0x00, 0x41, 0x71, 0x00, 0x00, 0x00, + 0x24, 0x00, 0x00, 0x8a, 0xf2, 0xe0, 0x61, 0x0c, + 0x00, 0xf1, 0x53, 0x61, 0x06, 0x00, 0xf0, 0x0a, + 0x61, 0x0b, 0x00, 0x8a, 0x11, 0x62, 0x0b, 0x00, + 0x0e, 0xd8, 0xf0, 0x15, 0x5d, 0x04, 0x00, 0xd9, + 0xd8, 0x61, 0x0b, 0x00, 0x61, 0x04, 0x00, 0x22, + 0x04, 0x00, 0x8a, 0x11, 0x62, 0x0b, 0x00, 0x0e, + 0x5d, 0x05, 0x00, 0x61, 0x03, 0x00, 0x61, 0x04, 0x00, 0x90, 0x62, 0x04, 0x00, 0x0b, 0x61, 0x0b, - 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, - 0x3e, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x3f, 0x00, - 0x00, 0x00, 0xf1, 0x0e, 0xec, 0x83, 0x0e, 0x06, - 0x6c, 0x0d, 0x00, 0x00, 0x00, 0x0e, 0xec, 0x1e, - 0x6c, 0x05, 0x00, 0x00, 0x00, 0x30, 0x61, 0x0a, - 0x00, 0x40, 0x06, 0x00, 0x00, 0x00, 0xea, 0x0d, - 0x61, 0x0a, 0x00, 0x41, 0x06, 0x00, 0x00, 0x00, - 0x24, 0x00, 0x00, 0x0e, 0x6d, 0x61, 0x03, 0x00, - 0x61, 0x04, 0x00, 0x42, 0x32, 0x00, 0x00, 0x00, - 0x61, 0x03, 0x00, 0x2f, 0xbf, 0x00, 0x28, 0xbf, - 0x00, 0xcd, 0x28, + 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, + 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x42, 0x00, + 0x00, 0x00, 0x0a, 0x4b, 0x43, 0x00, 0x00, 0x00, + 0xf7, 0x0e, 0xf3, 0x7f, 0xff, 0x0e, 0x06, 0x6c, + 0x0d, 0x00, 0x00, 0x00, 0x0e, 0xf2, 0x1e, 0x6c, + 0x05, 0x00, 0x00, 0x00, 0x30, 0x61, 0x0a, 0x00, + 0x40, 0x06, 0x00, 0x00, 0x00, 0xf0, 0x0d, 0x61, + 0x0a, 0x00, 0x41, 0x06, 0x00, 0x00, 0x00, 0x24, + 0x00, 0x00, 0x0e, 0x6d, 0x61, 0x03, 0x00, 0x61, + 0x04, 0x00, 0x42, 0x33, 0x00, 0x00, 0x00, 0x61, + 0x03, 0x00, 0x2f, 0xc5, 0x00, 0x28, 0xc5, 0x00, + 0xd3, 0x28, }; @@ -18821,10 +19973,10 @@ const uint8_t qjsc_builtin_array_fromasync[875] = { #include -const uint32_t qjsc_builtin_iterator_zip_keyed_size = 2582; +const uint32_t qjsc_builtin_iterator_zip_keyed_size = 2427; -const uint8_t qjsc_builtin_iterator_zip_keyed[2582] = { - 0x19, 0xcb, 0x4b, 0xae, 0xb1, 0x2b, 0x01, 0x1c, +const uint8_t qjsc_builtin_iterator_zip_keyed[2427] = { + 0x1d, 0x79, 0x0d, 0xf1, 0x8b, 0x2b, 0x01, 0x1c, 0x49, 0x74, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x48, 0x65, 0x6c, 0x70, 0x65, 0x72, 0x01, 0x08, 0x63, 0x61, 0x6c, 0x6c, 0x01, 0x24, 0x68, 0x61, @@ -18874,279 +20026,260 @@ const uint8_t qjsc_builtin_iterator_zip_keyed[2582] = { 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x01, 0x18, 0x62, 0x61, 0x64, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x0c, 0x00, 0x02, - 0x00, 0xa2, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, - 0x00, 0x01, 0x04, 0x01, 0xa4, 0x01, 0x00, 0x00, + 0x00, 0xa8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0x00, 0x01, 0x04, 0x01, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x02, 0x00, 0x00, 0x07, 0x03, - 0x07, 0x01, 0x0a, 0x00, 0x04, 0x0c, 0x0a, 0xcc, - 0x03, 0x00, 0x01, 0x40, 0x09, 0xaa, 0x03, 0x00, - 0x01, 0x40, 0x03, 0xa6, 0x03, 0x00, 0x01, 0x40, - 0x00, 0xce, 0x03, 0x00, 0x01, 0x40, 0x01, 0xd0, - 0x03, 0x00, 0x01, 0x40, 0x07, 0xd2, 0x03, 0x00, - 0x01, 0x40, 0x06, 0xd4, 0x03, 0x00, 0x01, 0x40, - 0x08, 0xd6, 0x03, 0x00, 0x00, 0x40, 0x05, 0xd8, - 0x03, 0x00, 0x01, 0x40, 0x02, 0xda, 0x03, 0x00, - 0x02, 0x40, 0x04, 0x0c, 0x43, 0x02, 0x00, 0xd6, + 0x07, 0x01, 0x0a, 0x00, 0x04, 0x0c, 0x0a, 0xe2, + 0x03, 0x00, 0x01, 0x40, 0x09, 0xb6, 0x03, 0x00, + 0x01, 0x40, 0x03, 0xb2, 0x03, 0x00, 0x01, 0x40, + 0x00, 0xe4, 0x03, 0x00, 0x01, 0x40, 0x01, 0xe6, + 0x03, 0x00, 0x01, 0x40, 0x07, 0xe8, 0x03, 0x00, + 0x01, 0x40, 0x06, 0xea, 0x03, 0x00, 0x01, 0x40, + 0x08, 0xec, 0x03, 0x00, 0x00, 0x40, 0x05, 0xee, + 0x03, 0x00, 0x01, 0x40, 0x02, 0xf0, 0x03, 0x00, + 0x02, 0x40, 0x04, 0x0c, 0x43, 0x02, 0x00, 0xec, 0x03, 0x02, 0x00, 0x02, 0x03, 0x00, 0x01, 0x00, - 0x17, 0x02, 0xdc, 0x03, 0x00, 0x01, 0x00, 0xde, - 0x03, 0x00, 0x01, 0x00, 0xa6, 0x03, 0x02, 0x01, - 0xd1, 0x96, 0x04, 0x4a, 0x00, 0x00, 0x00, 0xad, - 0xea, 0x07, 0xd1, 0x07, 0xae, 0xea, 0x02, 0x29, - 0xdd, 0x11, 0xd2, 0x21, 0x01, 0x00, 0x30, 0x0c, - 0x43, 0x02, 0x00, 0xd8, 0x03, 0x01, 0x02, 0x01, - 0x04, 0x00, 0x01, 0x00, 0x2e, 0x03, 0xe0, 0x03, - 0x00, 0x01, 0x00, 0xe2, 0x03, 0x02, 0x00, 0x20, - 0xe4, 0x03, 0x05, 0x00, 0x03, 0xce, 0x03, 0x03, - 0x01, 0x6b, 0x23, 0x00, 0x00, 0x00, 0x60, 0x00, - 0x00, 0xd1, 0x95, 0xea, 0x04, 0x06, 0x6e, 0x28, - 0xd1, 0x40, 0x06, 0x00, 0x00, 0x00, 0xc9, 0x61, - 0x00, 0x00, 0xea, 0x08, 0xdd, 0xd1, 0x61, 0x00, - 0x00, 0xf0, 0x0e, 0x0e, 0x29, 0xca, 0x6b, 0x07, - 0x00, 0x00, 0x00, 0xc6, 0x6e, 0x28, 0x30, 0x0c, - 0x43, 0x02, 0x00, 0xda, 0x03, 0x02, 0x04, 0x02, - 0x03, 0x00, 0x01, 0x00, 0x55, 0x06, 0xe6, 0x03, - 0x00, 0x01, 0x00, 0xe8, 0x03, 0x00, 0x01, 0x00, - 0xea, 0x03, 0x01, 0x00, 0x20, 0xec, 0x03, 0x02, - 0x01, 0x20, 0xe0, 0x03, 0x03, 0x02, 0x20, 0xe4, - 0x03, 0x03, 0x03, 0x20, 0xd8, 0x03, 0x01, 0x00, - 0x60, 0x00, 0x00, 0x38, 0x46, 0x00, 0x00, 0x00, - 0xc9, 0x60, 0x01, 0x00, 0xd2, 0xca, 0x61, 0x01, - 0x00, 0x8f, 0x62, 0x01, 0x00, 0xb4, 0xa7, 0xea, - 0x39, 0x60, 0x03, 0x00, 0x60, 0x02, 0x00, 0xd1, - 0x61, 0x01, 0x00, 0x46, 0xcb, 0xd1, 0x61, 0x01, - 0x00, 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, - 0x1b, 0x1b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x1b, - 0x70, 0x1b, 0x48, 0xdd, 0x61, 0x02, 0x00, 0xef, - 0xcc, 0x61, 0x00, 0x00, 0x95, 0xea, 0xc8, 0x61, - 0x03, 0x00, 0x11, 0x62, 0x00, 0x00, 0x0e, 0xec, - 0xbe, 0x61, 0x00, 0x00, 0x28, 0x0c, 0x41, 0x02, - 0x00, 0xac, 0x02, 0x02, 0x15, 0x01, 0x06, 0x08, - 0x09, 0x02, 0xc9, 0x05, 0x17, 0xee, 0x03, 0x00, - 0x01, 0x00, 0xf0, 0x03, 0x00, 0x01, 0x00, 0xee, - 0x03, 0x01, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x20, - 0xf0, 0x03, 0x01, 0x01, 0x20, 0xf2, 0x03, 0x02, - 0x00, 0x60, 0x04, 0xf4, 0x03, 0x02, 0x03, 0x20, - 0x60, 0x02, 0x04, 0x60, 0x02, 0xe8, 0x03, 0x02, - 0x05, 0x60, 0x01, 0xe6, 0x03, 0x02, 0x06, 0x60, - 0x03, 0xf6, 0x03, 0x02, 0x07, 0x60, 0x06, 0xf8, - 0x03, 0x02, 0x08, 0x60, 0x05, 0xfa, 0x03, 0x09, - 0x15, 0x20, 0xec, 0x03, 0x0b, 0x15, 0x20, 0xfc, - 0x03, 0x0c, 0x0b, 0x20, 0xfa, 0x03, 0x0c, 0x0c, - 0x20, 0xe0, 0x03, 0x0e, 0x0d, 0x20, 0xe2, 0x03, - 0x10, 0x0e, 0x20, 0xfe, 0x03, 0x14, 0x0d, 0x20, - 0xec, 0x03, 0x19, 0x15, 0x20, 0xec, 0x03, 0x1b, - 0x15, 0x20, 0xe4, 0x03, 0x1c, 0x15, 0x03, 0x80, - 0x04, 0x02, 0x09, 0x60, 0x00, 0x82, 0x04, 0x02, - 0x14, 0x60, 0x07, 0xa6, 0x03, 0x02, 0x01, 0xaa, - 0x03, 0x01, 0x01, 0xce, 0x03, 0x03, 0x01, 0xda, - 0x03, 0x02, 0x00, 0xd6, 0x03, 0x00, 0x00, 0xd2, - 0x03, 0x05, 0x01, 0xd0, 0x03, 0x04, 0x01, 0xd4, - 0x03, 0x06, 0x01, 0xcc, 0x03, 0x00, 0x01, 0x0c, - 0x42, 0x03, 0x00, 0x00, 0x00, 0x09, 0x00, 0x05, - 0x00, 0x0c, 0x00, 0xf7, 0x04, 0x09, 0x84, 0x04, - 0x01, 0x00, 0x20, 0xd8, 0x01, 0x01, 0x01, 0x20, - 0x86, 0x04, 0x01, 0x02, 0x20, 0xec, 0x03, 0x03, - 0x03, 0x20, 0xfa, 0x03, 0x04, 0x04, 0x20, 0xe0, - 0x03, 0x04, 0x05, 0x20, 0x88, 0x04, 0x04, 0x06, - 0x20, 0xe4, 0x03, 0x09, 0x07, 0x03, 0xea, 0x03, - 0x10, 0x07, 0x20, 0x80, 0x04, 0x13, 0x10, 0xa6, - 0x03, 0x00, 0x02, 0xaa, 0x03, 0x01, 0x02, 0xe8, - 0x03, 0x05, 0x10, 0x60, 0x04, 0x10, 0xe6, 0x03, - 0x06, 0x10, 0xf2, 0x03, 0x02, 0x10, 0xf8, 0x03, - 0x08, 0x10, 0xce, 0x03, 0x02, 0x02, 0xf6, 0x03, - 0x07, 0x10, 0x82, 0x04, 0x14, 0x10, 0xda, 0x03, - 0x03, 0x02, 0x60, 0x02, 0x00, 0x60, 0x01, 0x00, - 0x60, 0x00, 0x00, 0x64, 0x00, 0x00, 0x11, 0xb4, - 0xad, 0xeb, 0x06, 0x11, 0xb5, 0xad, 0xea, 0x09, - 0xb6, 0x11, 0x65, 0x00, 0x00, 0x0e, 0xec, 0x33, - 0x11, 0xb6, 0xad, 0xea, 0x0c, 0xde, 0x11, 0x04, - 0x05, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, - 0x11, 0xb7, 0xad, 0xea, 0x13, 0x0b, 0x38, 0x46, - 0x00, 0x00, 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, - 0x0a, 0x4b, 0x6a, 0x00, 0x00, 0x00, 0x28, 0xdf, - 0x11, 0x04, 0x06, 0x01, 0x00, 0x00, 0x21, 0x01, - 0x00, 0x30, 0x0e, 0xb4, 0xc9, 0xb4, 0xca, 0x0c, - 0x07, 0xcb, 0x60, 0x03, 0x00, 0xb4, 0xcc, 0x61, - 0x03, 0x00, 0x64, 0x03, 0x00, 0xa5, 0x68, 0xdd, - 0x01, 0x00, 0x00, 0x60, 0x06, 0x00, 0x60, 0x05, - 0x00, 0x60, 0x04, 0x00, 0x64, 0x04, 0x00, 0x61, - 0x03, 0x00, 0x46, 0xc2, 0x04, 0x64, 0x05, 0x00, - 0x61, 0x03, 0x00, 0x46, 0xc2, 0x05, 0x61, 0x05, - 0x00, 0x95, 0xea, 0x34, 0x64, 0x06, 0x00, 0x04, - 0x07, 0x01, 0x00, 0x00, 0xae, 0xea, 0x0c, 0xdf, - 0x11, 0x04, 0x06, 0x01, 0x00, 0x00, 0x21, 0x01, - 0x00, 0x30, 0x61, 0x02, 0x00, 0x61, 0x04, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x64, 0x07, 0x00, 0x61, 0x03, 0x00, 0x46, - 0x1b, 0x70, 0x1b, 0x48, 0xed, 0x7c, 0x01, 0x06, - 0xc2, 0x06, 0x6b, 0x1a, 0x00, 0x00, 0x00, 0x5d, - 0x08, 0x00, 0x61, 0x05, 0x00, 0x64, 0x09, 0x00, - 0x61, 0x03, 0x00, 0x46, 0xf0, 0x11, 0x62, 0x06, - 0x00, 0x0e, 0x0e, 0xec, 0x35, 0xc2, 0x07, 0x6b, - 0x30, 0x00, 0x00, 0x00, 0xb4, 0x11, 0x65, 0x0a, - 0x00, 0x0e, 0x64, 0x05, 0x00, 0x61, 0x03, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x1b, 0x70, - 0x1b, 0x48, 0x5d, 0x0b, 0x00, 0x64, 0x05, 0x00, - 0x64, 0x03, 0x00, 0xf0, 0x0e, 0xc1, 0x07, 0x30, - 0x30, 0x61, 0x06, 0x00, 0x40, 0x6a, 0x00, 0x00, - 0x00, 0x95, 0xea, 0x4f, 0x64, 0x06, 0x00, 0x04, - 0x08, 0x01, 0x00, 0x00, 0xad, 0xea, 0x1e, 0x61, - 0x00, 0x00, 0xb4, 0xa7, 0xea, 0x17, 0x5d, 0x0b, - 0x00, 0x64, 0x05, 0x00, 0x64, 0x03, 0x00, 0xf0, - 0x0e, 0xde, 0x11, 0x04, 0x09, 0x01, 0x00, 0x00, + 0x17, 0x02, 0xf2, 0x03, 0x00, 0x01, 0x00, 0xf4, + 0x03, 0x00, 0x01, 0x00, 0xb2, 0x03, 0x02, 0x01, + 0xd7, 0x96, 0x04, 0x4d, 0x00, 0x00, 0x00, 0xad, + 0xf0, 0x07, 0xd7, 0x07, 0xae, 0xf0, 0x02, 0x29, + 0xe3, 0x11, 0xd8, 0x21, 0x01, 0x00, 0x30, 0x0c, + 0x43, 0x02, 0x00, 0xee, 0x03, 0x01, 0x02, 0x01, + 0x04, 0x00, 0x01, 0x00, 0x2d, 0x03, 0xf6, 0x03, + 0x00, 0x01, 0x00, 0xf8, 0x03, 0x02, 0x00, 0x20, + 0xfa, 0x03, 0x05, 0x00, 0x03, 0xe4, 0x03, 0x03, + 0x01, 0x6b, 0x22, 0x00, 0x00, 0x00, 0x60, 0x00, + 0x00, 0xd7, 0xf1, 0x04, 0x06, 0x6e, 0x28, 0xd7, + 0x40, 0x06, 0x00, 0x00, 0x00, 0xcf, 0x61, 0x00, + 0x00, 0xf0, 0x08, 0xe3, 0xd7, 0x61, 0x00, 0x00, + 0xf6, 0x0e, 0x0e, 0x29, 0xd0, 0x6b, 0x07, 0x00, + 0x00, 0x00, 0xcc, 0x6e, 0x28, 0x30, 0x0c, 0x43, + 0x02, 0x00, 0xf0, 0x03, 0x02, 0x04, 0x02, 0x03, + 0x00, 0x01, 0x00, 0x48, 0x06, 0xfc, 0x03, 0x00, + 0x01, 0x00, 0xfe, 0x03, 0x00, 0x01, 0x00, 0x80, + 0x04, 0x01, 0x00, 0x20, 0x82, 0x04, 0x02, 0x01, + 0x20, 0xf6, 0x03, 0x03, 0x02, 0x20, 0xfa, 0x03, + 0x03, 0x03, 0x20, 0xee, 0x03, 0x01, 0x00, 0x60, + 0x00, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, 0xcf, + 0x60, 0x01, 0x00, 0xd8, 0xd0, 0x61, 0x01, 0x00, + 0x8f, 0x62, 0x01, 0x00, 0xba, 0xa7, 0xf0, 0x2c, + 0x60, 0x03, 0x00, 0x60, 0x02, 0x00, 0xd7, 0x61, + 0x01, 0x00, 0x46, 0xd1, 0xd7, 0x61, 0x01, 0x00, + 0x38, 0x49, 0x00, 0x00, 0x00, 0x48, 0xe3, 0x61, + 0x02, 0x00, 0xf5, 0xd2, 0x61, 0x00, 0x00, 0xf1, + 0xd5, 0x61, 0x03, 0x00, 0x11, 0x62, 0x00, 0x00, + 0x0e, 0xf2, 0xcb, 0x61, 0x00, 0x00, 0x28, 0x0c, + 0x41, 0x02, 0x00, 0xb8, 0x02, 0x02, 0x15, 0x01, + 0x06, 0x08, 0x09, 0x02, 0x80, 0x05, 0x17, 0x84, + 0x04, 0x00, 0x01, 0x00, 0x86, 0x04, 0x00, 0x01, + 0x00, 0x84, 0x04, 0x01, 0xff, 0xff, 0xff, 0xff, + 0x0f, 0x20, 0x86, 0x04, 0x01, 0x01, 0x20, 0x88, + 0x04, 0x02, 0x00, 0x60, 0x04, 0x8a, 0x04, 0x02, + 0x03, 0x20, 0x62, 0x02, 0x04, 0x60, 0x02, 0xfe, + 0x03, 0x02, 0x05, 0x60, 0x01, 0xfc, 0x03, 0x02, + 0x06, 0x60, 0x03, 0x8c, 0x04, 0x02, 0x07, 0x60, + 0x06, 0x8e, 0x04, 0x02, 0x08, 0x60, 0x05, 0x90, + 0x04, 0x09, 0x15, 0x20, 0x82, 0x04, 0x0b, 0x15, + 0x20, 0x92, 0x04, 0x0c, 0x0b, 0x20, 0x90, 0x04, + 0x0c, 0x0c, 0x20, 0xf6, 0x03, 0x0e, 0x0d, 0x20, + 0xf8, 0x03, 0x10, 0x0e, 0x20, 0x94, 0x04, 0x14, + 0x0d, 0x20, 0x82, 0x04, 0x19, 0x15, 0x20, 0x82, + 0x04, 0x1b, 0x15, 0x20, 0xfa, 0x03, 0x1c, 0x15, + 0x03, 0x96, 0x04, 0x02, 0x09, 0x60, 0x00, 0x98, + 0x04, 0x02, 0x14, 0x60, 0x07, 0xb2, 0x03, 0x02, + 0x01, 0xb6, 0x03, 0x01, 0x01, 0xe4, 0x03, 0x03, + 0x01, 0xf0, 0x03, 0x02, 0x00, 0xec, 0x03, 0x00, + 0x00, 0xe8, 0x03, 0x05, 0x01, 0xe6, 0x03, 0x04, + 0x01, 0xea, 0x03, 0x06, 0x01, 0xe2, 0x03, 0x00, + 0x01, 0x0c, 0x42, 0x03, 0x00, 0x00, 0x00, 0x09, + 0x00, 0x05, 0x00, 0x0c, 0x00, 0xb9, 0x04, 0x09, + 0x9a, 0x04, 0x01, 0x00, 0x20, 0xe4, 0x01, 0x01, + 0x01, 0x20, 0x9c, 0x04, 0x01, 0x02, 0x20, 0x82, + 0x04, 0x03, 0x03, 0x20, 0x90, 0x04, 0x04, 0x04, + 0x20, 0xf6, 0x03, 0x04, 0x05, 0x20, 0x9e, 0x04, + 0x04, 0x06, 0x20, 0xfa, 0x03, 0x09, 0x07, 0x03, + 0x80, 0x04, 0x10, 0x07, 0x20, 0x96, 0x04, 0x13, + 0x10, 0xb2, 0x03, 0x00, 0x02, 0xb6, 0x03, 0x01, + 0x02, 0xfe, 0x03, 0x05, 0x10, 0x62, 0x04, 0x10, + 0xfc, 0x03, 0x06, 0x10, 0x88, 0x04, 0x02, 0x10, + 0x8e, 0x04, 0x08, 0x10, 0xe4, 0x03, 0x02, 0x02, + 0x8c, 0x04, 0x07, 0x10, 0x98, 0x04, 0x14, 0x10, + 0xf0, 0x03, 0x03, 0x02, 0x60, 0x02, 0x00, 0x60, + 0x01, 0x00, 0x60, 0x00, 0x00, 0x64, 0x00, 0x00, + 0x11, 0xba, 0xad, 0xf1, 0x06, 0x11, 0xbb, 0xad, + 0xf0, 0x09, 0xbc, 0x11, 0x65, 0x00, 0x00, 0x0e, + 0xf2, 0x33, 0x11, 0xbc, 0xad, 0xf0, 0x0c, 0xe4, + 0x11, 0x04, 0x10, 0x01, 0x00, 0x00, 0x21, 0x01, + 0x00, 0x30, 0x11, 0xbd, 0xad, 0xf0, 0x13, 0x0b, + 0x38, 0x49, 0x00, 0x00, 0x00, 0x4b, 0x44, 0x00, + 0x00, 0x00, 0x0a, 0x4b, 0x70, 0x00, 0x00, 0x00, + 0x28, 0xe5, 0x11, 0x04, 0x11, 0x01, 0x00, 0x00, + 0x21, 0x01, 0x00, 0x30, 0x0e, 0xba, 0xcf, 0xba, + 0xd0, 0x0c, 0x07, 0xd1, 0x60, 0x03, 0x00, 0xba, + 0xd2, 0x61, 0x03, 0x00, 0x64, 0x03, 0x00, 0xa5, + 0x68, 0x9f, 0x01, 0x00, 0x00, 0x60, 0x06, 0x00, + 0x60, 0x05, 0x00, 0x60, 0x04, 0x00, 0x64, 0x04, + 0x00, 0x61, 0x03, 0x00, 0x46, 0xc8, 0x04, 0x64, + 0x05, 0x00, 0x61, 0x03, 0x00, 0x46, 0xc8, 0x05, + 0x61, 0x05, 0x00, 0xf1, 0x28, 0x64, 0x06, 0x00, + 0x04, 0x12, 0x01, 0x00, 0x00, 0xae, 0xf0, 0x0c, + 0xe5, 0x11, 0x04, 0x11, 0x01, 0x00, 0x00, 0x21, + 0x01, 0x00, 0x30, 0x61, 0x02, 0x00, 0x61, 0x04, + 0x00, 0x64, 0x07, 0x00, 0x61, 0x03, 0x00, 0x46, + 0x48, 0xf3, 0x4b, 0x01, 0x06, 0xc8, 0x06, 0x6b, + 0x1a, 0x00, 0x00, 0x00, 0x5d, 0x08, 0x00, 0x61, + 0x05, 0x00, 0x64, 0x09, 0x00, 0x61, 0x03, 0x00, + 0x46, 0xf6, 0x11, 0x62, 0x06, 0x00, 0x0e, 0x0e, + 0xf2, 0x29, 0xc8, 0x07, 0x6b, 0x24, 0x00, 0x00, + 0x00, 0xba, 0x11, 0x65, 0x0a, 0x00, 0x0e, 0x64, + 0x05, 0x00, 0x61, 0x03, 0x00, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x48, 0x5d, 0x0b, 0x00, 0x64, 0x05, + 0x00, 0x64, 0x03, 0x00, 0xf6, 0x0e, 0xc7, 0x07, + 0x30, 0x30, 0x61, 0x06, 0x00, 0x40, 0x70, 0x00, + 0x00, 0x00, 0xf1, 0x43, 0x64, 0x06, 0x00, 0x04, + 0x13, 0x01, 0x00, 0x00, 0xad, 0xf0, 0x1e, 0x61, + 0x00, 0x00, 0xba, 0xa7, 0xf0, 0x17, 0x5d, 0x0b, + 0x00, 0x64, 0x05, 0x00, 0x64, 0x03, 0x00, 0xf6, + 0x0e, 0xe4, 0x11, 0x04, 0x14, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x61, 0x02, 0x00, 0x61, - 0x04, 0x00, 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, - 0x70, 0x1b, 0x1b, 0x61, 0x06, 0x00, 0x40, 0x41, - 0x00, 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, 0x61, - 0x01, 0x00, 0x90, 0x62, 0x01, 0x00, 0x0e, 0xed, - 0xd1, 0x00, 0x64, 0x0a, 0x00, 0x8f, 0x65, 0x0a, - 0x00, 0x0e, 0x61, 0x00, 0x00, 0x90, 0x62, 0x00, - 0x00, 0x0e, 0x64, 0x05, 0x00, 0x61, 0x03, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x1b, 0x70, - 0x1b, 0x48, 0x64, 0x06, 0x00, 0x60, 0x08, 0x00, - 0x11, 0x04, 0x0a, 0x01, 0x00, 0x00, 0xad, 0xea, + 0x04, 0x00, 0x61, 0x06, 0x00, 0x40, 0x44, 0x00, + 0x00, 0x00, 0x48, 0x61, 0x01, 0x00, 0x90, 0x62, + 0x01, 0x00, 0x0e, 0xf3, 0xb9, 0x00, 0x64, 0x0a, + 0x00, 0x8f, 0x65, 0x0a, 0x00, 0x0e, 0x61, 0x00, + 0x00, 0x90, 0x62, 0x00, 0x00, 0x0e, 0x64, 0x05, + 0x00, 0x61, 0x03, 0x00, 0x38, 0x49, 0x00, 0x00, + 0x00, 0x48, 0x64, 0x06, 0x00, 0x60, 0x08, 0x00, + 0x11, 0x04, 0x15, 0x01, 0x00, 0x00, 0xad, 0xf0, 0x2e, 0x5d, 0x0b, 0x00, 0x64, 0x05, 0x00, 0x64, - 0x03, 0x00, 0xf0, 0xc2, 0x08, 0x61, 0x08, 0x00, - 0xea, 0x05, 0x61, 0x08, 0x00, 0x30, 0xb7, 0x11, - 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x46, 0x00, - 0x00, 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, - 0x4b, 0x6a, 0x00, 0x00, 0x00, 0x28, 0x11, 0x04, - 0x07, 0x01, 0x00, 0x00, 0xad, 0xea, 0x3c, 0x64, - 0x0a, 0x00, 0xb5, 0xa5, 0xea, 0x19, 0xb7, 0x11, - 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x46, 0x00, - 0x00, 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, - 0x4b, 0x6a, 0x00, 0x00, 0x00, 0x28, 0x61, 0x02, - 0x00, 0x61, 0x04, 0x00, 0x1b, 0x11, 0xaf, 0xeb, - 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x64, 0x07, 0x00, - 0x61, 0x03, 0x00, 0x46, 0x1b, 0x70, 0x1b, 0x48, - 0xec, 0x27, 0x11, 0x04, 0x08, 0x01, 0x00, 0x00, - 0xad, 0xea, 0x1e, 0x61, 0x01, 0x00, 0xb4, 0xa7, - 0xea, 0x17, 0x5d, 0x0b, 0x00, 0x64, 0x05, 0x00, - 0x64, 0x03, 0x00, 0xf0, 0x0e, 0xde, 0x11, 0x04, - 0x09, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, - 0x0e, 0x61, 0x03, 0x00, 0x90, 0x62, 0x03, 0x00, - 0x0e, 0xed, 0x1d, 0xfe, 0x61, 0x01, 0x00, 0xb4, - 0xad, 0xea, 0x19, 0xb7, 0x11, 0x65, 0x00, 0x00, - 0x0e, 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x4b, - 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, 0x00, - 0x00, 0x00, 0x28, 0xb5, 0x11, 0x65, 0x00, 0x00, - 0x0e, 0x0b, 0x61, 0x02, 0x00, 0x4b, 0x41, 0x00, - 0x00, 0x00, 0x09, 0x4b, 0x6a, 0x00, 0x00, 0x00, - 0x28, 0x0c, 0x42, 0x03, 0x00, 0x00, 0x00, 0x01, - 0x00, 0x06, 0x00, 0x06, 0x00, 0x88, 0x01, 0x01, - 0xea, 0x03, 0x01, 0x00, 0x20, 0x80, 0x04, 0x13, - 0x10, 0xa6, 0x03, 0x00, 0x02, 0xaa, 0x03, 0x01, - 0x02, 0xda, 0x03, 0x03, 0x02, 0xe6, 0x03, 0x06, - 0x10, 0xe8, 0x03, 0x05, 0x10, 0x60, 0x00, 0x00, - 0x64, 0x00, 0x00, 0x11, 0xb4, 0xad, 0xea, 0x09, - 0xb7, 0x11, 0x65, 0x00, 0x00, 0x0e, 0xec, 0x4b, - 0x11, 0xb5, 0xad, 0xea, 0x09, 0xb6, 0x11, 0x65, - 0x00, 0x00, 0x0e, 0xec, 0x3e, 0x11, 0xb6, 0xad, - 0xea, 0x0c, 0xde, 0x11, 0x04, 0x05, 0x01, 0x00, - 0x00, 0x21, 0x01, 0x00, 0x30, 0x11, 0xb7, 0xad, - 0xea, 0x13, 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, - 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, - 0x00, 0x00, 0x00, 0x28, 0xdf, 0x11, 0x04, 0x0b, - 0x01, 0x00, 0x00, 0x41, 0x5d, 0x00, 0x00, 0x00, - 0x64, 0x00, 0x00, 0x24, 0x01, 0x00, 0x21, 0x01, - 0x00, 0x30, 0x0e, 0xe0, 0x64, 0x04, 0x00, 0x64, - 0x05, 0x00, 0xf0, 0xc9, 0x61, 0x00, 0x00, 0xea, - 0x05, 0x61, 0x00, 0x00, 0x30, 0xb7, 0x11, 0x65, - 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x46, 0x00, 0x00, - 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, - 0x6a, 0x00, 0x00, 0x00, 0x28, 0x60, 0x01, 0x00, - 0x60, 0x00, 0x00, 0xd1, 0xc9, 0xd2, 0x11, 0xf2, - 0xea, 0x08, 0x0e, 0x38, 0x46, 0x00, 0x00, 0x00, - 0xda, 0xca, 0x60, 0x14, 0x00, 0x60, 0x13, 0x00, - 0x60, 0x08, 0x00, 0x60, 0x07, 0x00, 0x60, 0x06, - 0x00, 0x60, 0x05, 0x00, 0x60, 0x04, 0x00, 0x60, - 0x03, 0x00, 0x60, 0x02, 0x00, 0x5d, 0x04, 0x00, - 0xd1, 0x04, 0x0c, 0x01, 0x00, 0x00, 0xf0, 0x0e, - 0xd2, 0x38, 0x46, 0x00, 0x00, 0x00, 0xad, 0xea, - 0x06, 0x0c, 0x07, 0xd6, 0xec, 0x0c, 0x5d, 0x04, - 0x00, 0xd2, 0x04, 0x0d, 0x01, 0x00, 0x00, 0xf0, - 0x0e, 0xd2, 0x40, 0xf9, 0x00, 0x00, 0x00, 0xcb, - 0x61, 0x02, 0x00, 0x38, 0x46, 0x00, 0x00, 0x00, - 0xad, 0xea, 0x0b, 0x04, 0x0a, 0x01, 0x00, 0x00, - 0x11, 0x62, 0x02, 0x00, 0x0e, 0x61, 0x02, 0x00, - 0x04, 0x08, 0x01, 0x00, 0x00, 0xad, 0x11, 0xeb, - 0x18, 0x0e, 0x61, 0x02, 0x00, 0x04, 0x07, 0x01, - 0x00, 0x00, 0xad, 0x11, 0xeb, 0x0b, 0x0e, 0x61, - 0x02, 0x00, 0x04, 0x0a, 0x01, 0x00, 0x00, 0xad, - 0x95, 0xea, 0x0c, 0xdd, 0x11, 0x04, 0x0e, 0x01, - 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x38, 0x46, - 0x00, 0x00, 0x00, 0xcc, 0x61, 0x02, 0x00, 0x04, - 0x07, 0x01, 0x00, 0x00, 0xad, 0xea, 0x24, 0xd2, - 0x40, 0xfa, 0x00, 0x00, 0x00, 0x11, 0x62, 0x03, - 0x00, 0x0e, 0x61, 0x03, 0x00, 0x38, 0x46, 0x00, - 0x00, 0x00, 0xae, 0xea, 0x0e, 0x5d, 0x04, 0x00, - 0x61, 0x03, 0x00, 0x04, 0x0f, 0x01, 0x00, 0x00, - 0xf0, 0x0e, 0x26, 0x00, 0x00, 0xc2, 0x04, 0xb4, - 0xc2, 0x05, 0x26, 0x00, 0x00, 0xc2, 0x06, 0x26, - 0x00, 0x00, 0xc2, 0x07, 0x26, 0x00, 0x00, 0xc2, - 0x08, 0x60, 0x09, 0x00, 0x5d, 0x05, 0x00, 0xd1, - 0xef, 0x7d, 0xec, 0x1d, 0xc2, 0x09, 0x61, 0x04, - 0x00, 0x61, 0x05, 0x00, 0x90, 0x62, 0x05, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x61, 0x09, 0x00, 0x1b, 0x70, 0x1b, 0x48, - 0x80, 0x00, 0xea, 0xe1, 0x0e, 0x83, 0x6b, 0x7d, - 0x01, 0x00, 0x00, 0x60, 0x0a, 0x00, 0xb4, 0xc2, - 0x0a, 0x61, 0x0a, 0x00, 0x61, 0x05, 0x00, 0xa5, - 0x68, 0xf0, 0x00, 0x00, 0x00, 0x60, 0x0c, 0x00, - 0x60, 0x0b, 0x00, 0x0a, 0xc2, 0x0b, 0x61, 0x04, - 0x00, 0x61, 0x0a, 0x00, 0x46, 0xc2, 0x0c, 0x5d, - 0x06, 0x00, 0xd1, 0x61, 0x0c, 0x00, 0xf0, 0xea, - 0x78, 0x60, 0x0d, 0x00, 0xd1, 0x61, 0x0c, 0x00, - 0x46, 0xc2, 0x0d, 0x61, 0x0d, 0x00, 0x38, 0x46, - 0x00, 0x00, 0x00, 0xae, 0xea, 0x63, 0x60, 0x0e, - 0x00, 0x5d, 0x04, 0x00, 0x61, 0x0d, 0x00, 0x04, - 0x10, 0x01, 0x00, 0x00, 0xf0, 0x0e, 0x61, 0x0d, - 0x00, 0x5d, 0x07, 0x00, 0x46, 0xc2, 0x0e, 0x61, - 0x0e, 0x00, 0xea, 0x0e, 0xdf, 0x61, 0x0d, 0x00, - 0x61, 0x0e, 0x00, 0xf0, 0x11, 0x62, 0x0d, 0x00, - 0x0e, 0x61, 0x06, 0x00, 0x61, 0x0a, 0x00, 0x1b, - 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, - 0x61, 0x0d, 0x00, 0x1b, 0x70, 0x1b, 0x48, 0x61, - 0x07, 0x00, 0x61, 0x0a, 0x00, 0x1b, 0x11, 0xaf, - 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x61, 0x0d, - 0x00, 0x40, 0x6b, 0x00, 0x00, 0x00, 0x1b, 0x70, - 0x1b, 0x48, 0x09, 0x11, 0x62, 0x0b, 0x00, 0x0e, - 0x61, 0x0b, 0x00, 0xea, 0x4a, 0x60, 0x0f, 0x00, - 0x61, 0x0a, 0x00, 0xb5, 0x9c, 0xc2, 0x0f, 0x61, - 0x0f, 0x00, 0x61, 0x05, 0x00, 0xa5, 0xea, 0x27, - 0x61, 0x04, 0x00, 0x61, 0x0f, 0x00, 0xb5, 0x9d, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x61, 0x04, 0x00, 0x61, 0x0f, 0x00, 0x46, - 0x1b, 0x70, 0x1b, 0x48, 0x61, 0x0f, 0x00, 0x90, - 0x62, 0x0f, 0x00, 0x0e, 0xec, 0xd2, 0x61, 0x05, - 0x00, 0x8f, 0x62, 0x05, 0x00, 0x0e, 0x61, 0x0a, - 0x00, 0x8f, 0x62, 0x0a, 0x00, 0x0e, 0x61, 0x0a, - 0x00, 0x90, 0x62, 0x0a, 0x00, 0x0e, 0xed, 0x0a, - 0xff, 0x61, 0x02, 0x00, 0x04, 0x07, 0x01, 0x00, - 0x00, 0xad, 0xea, 0x6e, 0x61, 0x03, 0x00, 0xea, - 0x38, 0x60, 0x10, 0x00, 0xb4, 0xc2, 0x10, 0x61, - 0x10, 0x00, 0x61, 0x05, 0x00, 0xa5, 0xea, 0x5a, - 0x61, 0x08, 0x00, 0x61, 0x10, 0x00, 0x1b, 0x11, - 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x61, - 0x03, 0x00, 0x61, 0x04, 0x00, 0x61, 0x10, 0x00, - 0x46, 0x46, 0x1b, 0x70, 0x1b, 0x48, 0x61, 0x10, - 0x00, 0x90, 0x62, 0x10, 0x00, 0x0e, 0xec, 0xd0, - 0x60, 0x11, 0x00, 0xb4, 0xc2, 0x11, 0x61, 0x11, - 0x00, 0x61, 0x05, 0x00, 0xa5, 0xea, 0x23, 0x61, - 0x08, 0x00, 0x61, 0x11, 0x00, 0x1b, 0x11, 0xaf, - 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x38, 0x46, - 0x00, 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, 0x61, - 0x11, 0x00, 0x90, 0x62, 0x11, 0x00, 0x0e, 0xec, - 0xd6, 0x0e, 0xec, 0x15, 0xc2, 0x12, 0x6b, 0x10, - 0x00, 0x00, 0x00, 0xe0, 0x61, 0x06, 0x00, 0x61, - 0x05, 0x00, 0xf0, 0x0e, 0xc1, 0x12, 0x30, 0x30, - 0xb4, 0xc2, 0x13, 0x61, 0x05, 0x00, 0xc2, 0x14, - 0x0b, 0x5d, 0x08, 0x00, 0x4e, 0xbf, 0x00, 0x53, - 0x6b, 0x00, 0x00, 0x00, 0x04, 0xbf, 0x01, 0x53, - 0x06, 0x00, 0x00, 0x00, 0x04, 0x28, 0xbf, 0x00, - 0xc9, 0xbf, 0x01, 0xca, 0xbf, 0x02, 0xcb, 0xbf, - 0x03, 0x28, 0xbf, 0x00, 0xcd, 0x28, + 0x03, 0x00, 0xf6, 0xc8, 0x08, 0x61, 0x08, 0x00, + 0xf0, 0x05, 0x61, 0x08, 0x00, 0x30, 0xbd, 0x11, + 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, + 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, 0x11, 0x04, + 0x12, 0x01, 0x00, 0x00, 0xad, 0xf0, 0x30, 0x64, + 0x0a, 0x00, 0xbb, 0xa5, 0xf0, 0x19, 0xbd, 0x11, + 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, + 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, 0x61, 0x02, + 0x00, 0x61, 0x04, 0x00, 0x64, 0x07, 0x00, 0x61, + 0x03, 0x00, 0x46, 0x48, 0xf2, 0x27, 0x11, 0x04, + 0x13, 0x01, 0x00, 0x00, 0xad, 0xf0, 0x1e, 0x61, + 0x01, 0x00, 0xba, 0xa7, 0xf0, 0x17, 0x5d, 0x0b, + 0x00, 0x64, 0x05, 0x00, 0x64, 0x03, 0x00, 0xf6, + 0x0e, 0xe4, 0x11, 0x04, 0x14, 0x01, 0x00, 0x00, + 0x21, 0x01, 0x00, 0x30, 0x0e, 0x61, 0x03, 0x00, + 0x90, 0x62, 0x03, 0x00, 0x0e, 0xf3, 0x5b, 0xfe, + 0x61, 0x01, 0x00, 0xba, 0xad, 0xf0, 0x19, 0xbd, + 0x11, 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x49, + 0x00, 0x00, 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, + 0x0a, 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, 0xbb, + 0x11, 0x65, 0x00, 0x00, 0x0e, 0x0b, 0x61, 0x02, + 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x09, 0x4b, + 0x70, 0x00, 0x00, 0x00, 0x28, 0x0c, 0x42, 0x03, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x06, + 0x00, 0x82, 0x01, 0x01, 0x80, 0x04, 0x01, 0x00, + 0x20, 0x96, 0x04, 0x13, 0x10, 0xb2, 0x03, 0x00, + 0x02, 0xb6, 0x03, 0x01, 0x02, 0xf0, 0x03, 0x03, + 0x02, 0xfc, 0x03, 0x06, 0x10, 0xfe, 0x03, 0x05, + 0x10, 0x60, 0x00, 0x00, 0x64, 0x00, 0x00, 0x11, + 0xba, 0xad, 0xf0, 0x09, 0xbd, 0x11, 0x65, 0x00, + 0x00, 0x0e, 0xf2, 0x45, 0x11, 0xbb, 0xad, 0xf0, + 0x09, 0xbc, 0x11, 0x65, 0x00, 0x00, 0x0e, 0xf2, + 0x38, 0x11, 0xbc, 0xad, 0xf0, 0x0c, 0xe4, 0x11, + 0x04, 0x10, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, + 0x30, 0x11, 0xbd, 0xad, 0xf0, 0x13, 0x0b, 0x38, + 0x49, 0x00, 0x00, 0x00, 0x4b, 0x44, 0x00, 0x00, + 0x00, 0x0a, 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, + 0xe5, 0x11, 0x04, 0x16, 0x01, 0x00, 0x00, 0x64, + 0x00, 0x00, 0x71, 0x9c, 0x21, 0x01, 0x00, 0x30, + 0x0e, 0xe6, 0x64, 0x04, 0x00, 0x64, 0x05, 0x00, + 0xf6, 0xcf, 0x61, 0x00, 0x00, 0xf0, 0x05, 0x61, + 0x00, 0x00, 0x30, 0xbd, 0x11, 0x65, 0x00, 0x00, + 0x0e, 0x0b, 0x38, 0x49, 0x00, 0x00, 0x00, 0x4b, + 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x70, 0x00, + 0x00, 0x00, 0x28, 0x60, 0x01, 0x00, 0x60, 0x00, + 0x00, 0xd7, 0xcf, 0xd8, 0x11, 0xf8, 0xf0, 0x08, + 0x0e, 0x38, 0x49, 0x00, 0x00, 0x00, 0xe0, 0xd0, + 0x60, 0x14, 0x00, 0x60, 0x13, 0x00, 0x60, 0x08, + 0x00, 0x60, 0x07, 0x00, 0x60, 0x06, 0x00, 0x60, + 0x05, 0x00, 0x60, 0x04, 0x00, 0x60, 0x03, 0x00, + 0x60, 0x02, 0x00, 0x5d, 0x04, 0x00, 0xd7, 0x04, + 0x17, 0x01, 0x00, 0x00, 0xf6, 0x0e, 0xd8, 0x38, + 0x49, 0x00, 0x00, 0x00, 0xad, 0xf0, 0x06, 0x0c, + 0x07, 0xdc, 0xf2, 0x0c, 0x5d, 0x04, 0x00, 0xd8, + 0x04, 0x18, 0x01, 0x00, 0x00, 0xf6, 0x0e, 0xd8, + 0x40, 0x04, 0x01, 0x00, 0x00, 0xd1, 0x61, 0x02, + 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, 0xad, 0xf0, + 0x0b, 0x04, 0x15, 0x01, 0x00, 0x00, 0x11, 0x62, + 0x02, 0x00, 0x0e, 0x61, 0x02, 0x00, 0x04, 0x13, + 0x01, 0x00, 0x00, 0xad, 0x11, 0xf1, 0x18, 0x0e, + 0x61, 0x02, 0x00, 0x04, 0x12, 0x01, 0x00, 0x00, + 0xad, 0x11, 0xf1, 0x0b, 0x0e, 0x61, 0x02, 0x00, + 0x04, 0x15, 0x01, 0x00, 0x00, 0xad, 0xf1, 0x0c, + 0xe3, 0x11, 0x04, 0x19, 0x01, 0x00, 0x00, 0x21, + 0x01, 0x00, 0x30, 0x38, 0x49, 0x00, 0x00, 0x00, + 0xd2, 0x61, 0x02, 0x00, 0x04, 0x12, 0x01, 0x00, + 0x00, 0xad, 0xf0, 0x24, 0xd8, 0x40, 0x05, 0x01, + 0x00, 0x00, 0x11, 0x62, 0x03, 0x00, 0x0e, 0x61, + 0x03, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, 0xae, + 0xf0, 0x0e, 0x5d, 0x04, 0x00, 0x61, 0x03, 0x00, + 0x04, 0x1a, 0x01, 0x00, 0x00, 0xf6, 0x0e, 0x26, + 0x00, 0x00, 0xc8, 0x04, 0xba, 0xc8, 0x05, 0x26, + 0x00, 0x00, 0xc8, 0x06, 0x26, 0x00, 0x00, 0xc8, + 0x07, 0x26, 0x00, 0x00, 0xc8, 0x08, 0x60, 0x09, + 0x00, 0x5d, 0x05, 0x00, 0xd7, 0xf5, 0x7e, 0xf2, + 0x11, 0xc8, 0x09, 0x61, 0x04, 0x00, 0x61, 0x05, + 0x00, 0x90, 0x62, 0x05, 0x00, 0x61, 0x09, 0x00, + 0x48, 0x81, 0x00, 0xf0, 0xed, 0x0e, 0x83, 0x6b, + 0x41, 0x01, 0x00, 0x00, 0x60, 0x0a, 0x00, 0xba, + 0xc8, 0x0a, 0x61, 0x0a, 0x00, 0x61, 0x05, 0x00, + 0xa5, 0x68, 0xcc, 0x00, 0x00, 0x00, 0x60, 0x0c, + 0x00, 0x60, 0x0b, 0x00, 0x0a, 0xc8, 0x0b, 0x61, + 0x04, 0x00, 0x61, 0x0a, 0x00, 0x46, 0xc8, 0x0c, + 0x5d, 0x06, 0x00, 0xd7, 0x61, 0x0c, 0x00, 0xf6, + 0xf0, 0x60, 0x60, 0x0d, 0x00, 0xd7, 0x61, 0x0c, + 0x00, 0x46, 0xc8, 0x0d, 0x61, 0x0d, 0x00, 0x38, + 0x49, 0x00, 0x00, 0x00, 0xae, 0xf0, 0x4b, 0x60, + 0x0e, 0x00, 0x5d, 0x04, 0x00, 0x61, 0x0d, 0x00, + 0x04, 0x1b, 0x01, 0x00, 0x00, 0xf6, 0x0e, 0x61, + 0x0d, 0x00, 0x5d, 0x07, 0x00, 0x46, 0xc8, 0x0e, + 0x61, 0x0e, 0x00, 0xf0, 0x0e, 0xe5, 0x61, 0x0d, + 0x00, 0x61, 0x0e, 0x00, 0xf6, 0x11, 0x62, 0x0d, + 0x00, 0x0e, 0x61, 0x06, 0x00, 0x61, 0x0a, 0x00, + 0x61, 0x0d, 0x00, 0x48, 0x61, 0x07, 0x00, 0x61, + 0x0a, 0x00, 0x61, 0x0d, 0x00, 0x40, 0x71, 0x00, + 0x00, 0x00, 0x48, 0x09, 0x11, 0x62, 0x0b, 0x00, + 0x0e, 0x61, 0x0b, 0x00, 0xf0, 0x3e, 0x60, 0x0f, + 0x00, 0x61, 0x0a, 0x00, 0xbb, 0x9c, 0xc8, 0x0f, + 0x61, 0x0f, 0x00, 0x61, 0x05, 0x00, 0xa5, 0xf0, + 0x1b, 0x61, 0x04, 0x00, 0x61, 0x0f, 0x00, 0xbb, + 0x9d, 0x61, 0x04, 0x00, 0x61, 0x0f, 0x00, 0x46, + 0x48, 0x61, 0x0f, 0x00, 0x90, 0x62, 0x0f, 0x00, + 0x0e, 0xf2, 0xde, 0x61, 0x05, 0x00, 0x8f, 0x62, + 0x05, 0x00, 0x0e, 0x61, 0x0a, 0x00, 0x8f, 0x62, + 0x0a, 0x00, 0x0e, 0x61, 0x0a, 0x00, 0x90, 0x62, + 0x0a, 0x00, 0x0e, 0xf3, 0x2e, 0xff, 0x61, 0x02, + 0x00, 0x04, 0x12, 0x01, 0x00, 0x00, 0xad, 0xf0, + 0x56, 0x61, 0x03, 0x00, 0xf0, 0x2c, 0x60, 0x10, + 0x00, 0xba, 0xc8, 0x10, 0x61, 0x10, 0x00, 0x61, + 0x05, 0x00, 0xa5, 0xf0, 0x42, 0x61, 0x08, 0x00, + 0x61, 0x10, 0x00, 0x61, 0x03, 0x00, 0x61, 0x04, + 0x00, 0x61, 0x10, 0x00, 0x46, 0x46, 0x48, 0x61, + 0x10, 0x00, 0x90, 0x62, 0x10, 0x00, 0x0e, 0xf2, + 0xdc, 0x60, 0x11, 0x00, 0xba, 0xc8, 0x11, 0x61, + 0x11, 0x00, 0x61, 0x05, 0x00, 0xa5, 0xf0, 0x17, + 0x61, 0x08, 0x00, 0x61, 0x11, 0x00, 0x38, 0x49, + 0x00, 0x00, 0x00, 0x48, 0x61, 0x11, 0x00, 0x90, + 0x62, 0x11, 0x00, 0x0e, 0xf2, 0xe2, 0x0e, 0xf2, + 0x15, 0xc8, 0x12, 0x6b, 0x10, 0x00, 0x00, 0x00, + 0xe6, 0x61, 0x06, 0x00, 0x61, 0x05, 0x00, 0xf6, + 0x0e, 0xc7, 0x12, 0x30, 0x30, 0xba, 0xc8, 0x13, + 0x61, 0x05, 0x00, 0xc8, 0x14, 0x0b, 0x5d, 0x08, + 0x00, 0x4e, 0xc5, 0x00, 0x53, 0x71, 0x00, 0x00, + 0x00, 0x04, 0xc5, 0x01, 0x53, 0x06, 0x00, 0x00, + 0x00, 0x04, 0x28, 0xc5, 0x00, 0xcf, 0xc5, 0x01, + 0xd0, 0xc5, 0x02, 0xd1, 0xc5, 0x03, 0x28, 0xc5, + 0x00, 0xd3, 0x28, }; @@ -19154,10 +20287,10 @@ const uint8_t qjsc_builtin_iterator_zip_keyed[2582] = { #include -const uint32_t qjsc_builtin_iterator_zip_size = 2621; +const uint32_t qjsc_builtin_iterator_zip_size = 2489; -const uint8_t qjsc_builtin_iterator_zip[2621] = { - 0x19, 0x27, 0x0d, 0xb0, 0x6c, 0x2a, 0x01, 0x1c, +const uint8_t qjsc_builtin_iterator_zip[2489] = { + 0x1d, 0x5f, 0x6a, 0xc3, 0x65, 0x2a, 0x01, 0x1c, 0x49, 0x74, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x48, 0x65, 0x6c, 0x70, 0x65, 0x72, 0x01, 0x08, 0x63, 0x61, 0x6c, 0x6c, 0x01, 0x1e, 0x53, 0x79, @@ -19205,286 +20338,270 @@ const uint8_t qjsc_builtin_iterator_zip[2621] = { 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x01, 0x18, 0x62, 0x61, 0x64, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x0c, 0x00, 0x02, - 0x00, 0xa2, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, - 0x00, 0x01, 0x04, 0x01, 0xa4, 0x01, 0x00, 0x00, + 0x00, 0xa8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0x00, 0x01, 0x04, 0x01, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x02, 0x00, 0x00, 0x05, 0x03, - 0x05, 0x01, 0x08, 0x00, 0x04, 0x0c, 0x08, 0xcc, - 0x03, 0x00, 0x01, 0x40, 0x07, 0xaa, 0x03, 0x00, - 0x01, 0x40, 0x03, 0xa6, 0x03, 0x00, 0x01, 0x40, - 0x00, 0xce, 0x03, 0x00, 0x01, 0x40, 0x01, 0xd0, - 0x03, 0x00, 0x01, 0x40, 0x06, 0xd2, 0x03, 0x00, - 0x00, 0x40, 0x05, 0xd4, 0x03, 0x00, 0x01, 0x40, - 0x02, 0xd6, 0x03, 0x00, 0x02, 0x40, 0x04, 0x0c, - 0x43, 0x02, 0x00, 0xd2, 0x03, 0x02, 0x00, 0x02, - 0x03, 0x00, 0x01, 0x00, 0x17, 0x02, 0xd8, 0x03, - 0x00, 0x01, 0x00, 0xda, 0x03, 0x00, 0x01, 0x00, - 0xa6, 0x03, 0x02, 0x01, 0xd1, 0x96, 0x04, 0x4a, - 0x00, 0x00, 0x00, 0xad, 0xea, 0x07, 0xd1, 0x07, - 0xae, 0xea, 0x02, 0x29, 0xdd, 0x11, 0xd2, 0x21, - 0x01, 0x00, 0x30, 0x0c, 0x43, 0x02, 0x00, 0xd4, + 0x05, 0x01, 0x08, 0x00, 0x04, 0x0c, 0x08, 0xe2, + 0x03, 0x00, 0x01, 0x40, 0x07, 0xb6, 0x03, 0x00, + 0x01, 0x40, 0x03, 0xb2, 0x03, 0x00, 0x01, 0x40, + 0x00, 0xe4, 0x03, 0x00, 0x01, 0x40, 0x01, 0xe6, + 0x03, 0x00, 0x01, 0x40, 0x06, 0xe8, 0x03, 0x00, + 0x00, 0x40, 0x05, 0xea, 0x03, 0x00, 0x01, 0x40, + 0x02, 0xec, 0x03, 0x00, 0x02, 0x40, 0x04, 0x0c, + 0x43, 0x02, 0x00, 0xe8, 0x03, 0x02, 0x00, 0x02, + 0x03, 0x00, 0x01, 0x00, 0x17, 0x02, 0xee, 0x03, + 0x00, 0x01, 0x00, 0xf0, 0x03, 0x00, 0x01, 0x00, + 0xb2, 0x03, 0x02, 0x01, 0xd7, 0x96, 0x04, 0x4d, + 0x00, 0x00, 0x00, 0xad, 0xf0, 0x07, 0xd7, 0x07, + 0xae, 0xf0, 0x02, 0x29, 0xe3, 0x11, 0xd8, 0x21, + 0x01, 0x00, 0x30, 0x0c, 0x43, 0x02, 0x00, 0xea, 0x03, 0x01, 0x02, 0x01, 0x04, 0x00, 0x01, 0x00, - 0x2e, 0x03, 0xdc, 0x03, 0x00, 0x01, 0x00, 0xde, - 0x03, 0x02, 0x00, 0x20, 0xe0, 0x03, 0x05, 0x00, - 0x03, 0xce, 0x03, 0x03, 0x01, 0x6b, 0x23, 0x00, - 0x00, 0x00, 0x60, 0x00, 0x00, 0xd1, 0x95, 0xea, - 0x04, 0x06, 0x6e, 0x28, 0xd1, 0x40, 0x06, 0x00, - 0x00, 0x00, 0xc9, 0x61, 0x00, 0x00, 0xea, 0x08, - 0xdd, 0xd1, 0x61, 0x00, 0x00, 0xf0, 0x0e, 0x0e, - 0x29, 0xca, 0x6b, 0x07, 0x00, 0x00, 0x00, 0xc6, - 0x6e, 0x28, 0x30, 0x0c, 0x43, 0x02, 0x00, 0xd6, - 0x03, 0x02, 0x04, 0x02, 0x03, 0x00, 0x01, 0x00, - 0x55, 0x06, 0xe2, 0x03, 0x00, 0x01, 0x00, 0xe4, - 0x03, 0x00, 0x01, 0x00, 0xe6, 0x03, 0x01, 0x00, - 0x20, 0xe8, 0x03, 0x02, 0x01, 0x20, 0xdc, 0x03, - 0x03, 0x02, 0x20, 0xe0, 0x03, 0x03, 0x03, 0x20, - 0xd4, 0x03, 0x01, 0x00, 0x60, 0x00, 0x00, 0x38, - 0x46, 0x00, 0x00, 0x00, 0xc9, 0x60, 0x01, 0x00, - 0xd2, 0xca, 0x61, 0x01, 0x00, 0x8f, 0x62, 0x01, - 0x00, 0xb4, 0xa7, 0xea, 0x39, 0x60, 0x03, 0x00, - 0x60, 0x02, 0x00, 0xd1, 0x61, 0x01, 0x00, 0x46, - 0xcb, 0xd1, 0x61, 0x01, 0x00, 0x1b, 0x11, 0xaf, - 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x38, 0x46, - 0x00, 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, 0xdd, - 0x61, 0x02, 0x00, 0xef, 0xcc, 0x61, 0x00, 0x00, - 0x95, 0xea, 0xc8, 0x61, 0x03, 0x00, 0x11, 0x62, - 0x00, 0x00, 0x0e, 0xec, 0xbe, 0x61, 0x00, 0x00, - 0x28, 0x0c, 0x41, 0x02, 0x00, 0xaa, 0x02, 0x02, - 0x1a, 0x01, 0x05, 0x07, 0x08, 0x02, 0x8a, 0x06, - 0x1c, 0xea, 0x03, 0x00, 0x01, 0x00, 0xec, 0x03, - 0x00, 0x01, 0x00, 0xea, 0x03, 0x01, 0xff, 0xff, - 0xff, 0xff, 0x0f, 0x20, 0xec, 0x03, 0x01, 0x01, - 0x20, 0xee, 0x03, 0x02, 0x00, 0x60, 0x03, 0xf0, - 0x03, 0x02, 0x03, 0x20, 0xf2, 0x03, 0x02, 0x04, - 0x60, 0x04, 0xe2, 0x03, 0x02, 0x05, 0x60, 0x02, - 0xf4, 0x03, 0x02, 0x06, 0x60, 0x05, 0xe4, 0x03, - 0x02, 0x07, 0x60, 0x01, 0xf6, 0x03, 0x02, 0x08, - 0x20, 0xf8, 0x03, 0x02, 0x09, 0x20, 0xd6, 0x01, - 0x09, 0x1a, 0x20, 0xfa, 0x03, 0x0b, 0x0b, 0x20, - 0xe0, 0x03, 0x0d, 0x0f, 0x03, 0xdc, 0x03, 0x0b, - 0x0c, 0x20, 0xde, 0x03, 0x0b, 0x0e, 0x20, 0xd6, - 0x01, 0x13, 0x0b, 0x20, 0xe8, 0x03, 0x13, 0x10, - 0x20, 0xd4, 0x01, 0x13, 0x11, 0x20, 0x82, 0x01, - 0x15, 0x16, 0x20, 0xfa, 0x03, 0x16, 0x13, 0x20, - 0xe0, 0x03, 0x17, 0x13, 0x03, 0xfc, 0x03, 0x13, - 0x12, 0x20, 0xe6, 0x03, 0x1c, 0x16, 0x20, 0xe0, - 0x03, 0x1f, 0x1a, 0x03, 0xfe, 0x03, 0x02, 0x0a, - 0x60, 0x00, 0x80, 0x04, 0x02, 0x19, 0x60, 0x06, - 0xa6, 0x03, 0x02, 0x01, 0xaa, 0x03, 0x01, 0x01, - 0xce, 0x03, 0x03, 0x01, 0xd6, 0x03, 0x02, 0x00, - 0xd2, 0x03, 0x00, 0x00, 0xd0, 0x03, 0x04, 0x01, - 0xd4, 0x03, 0x01, 0x00, 0xcc, 0x03, 0x00, 0x01, - 0x0c, 0x42, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, - 0x05, 0x00, 0x0b, 0x00, 0xec, 0x04, 0x08, 0x82, - 0x04, 0x01, 0x00, 0x20, 0xd8, 0x01, 0x01, 0x01, - 0x20, 0x84, 0x04, 0x01, 0x02, 0x20, 0xe8, 0x03, - 0x03, 0x03, 0x20, 0xdc, 0x03, 0x04, 0x04, 0x20, - 0x86, 0x04, 0x04, 0x05, 0x20, 0xe0, 0x03, 0x09, - 0x06, 0x03, 0xe6, 0x03, 0x10, 0x06, 0x20, 0xfe, - 0x03, 0x18, 0x10, 0xa6, 0x03, 0x00, 0x02, 0xaa, - 0x03, 0x01, 0x02, 0xe4, 0x03, 0x07, 0x10, 0xe2, - 0x03, 0x05, 0x10, 0xee, 0x03, 0x02, 0x10, 0xf2, - 0x03, 0x04, 0x10, 0xce, 0x03, 0x02, 0x02, 0xf4, - 0x03, 0x06, 0x10, 0x80, 0x04, 0x19, 0x10, 0xd6, - 0x03, 0x03, 0x02, 0x60, 0x02, 0x00, 0x60, 0x01, - 0x00, 0x60, 0x00, 0x00, 0x64, 0x00, 0x00, 0x11, - 0xb4, 0xad, 0xeb, 0x06, 0x11, 0xb5, 0xad, 0xea, - 0x09, 0xb6, 0x11, 0x65, 0x00, 0x00, 0x0e, 0xec, - 0x33, 0x11, 0xb6, 0xad, 0xea, 0x0c, 0xde, 0x11, - 0x04, 0x04, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, - 0x30, 0x11, 0xb7, 0xad, 0xea, 0x13, 0x0b, 0x38, - 0x46, 0x00, 0x00, 0x00, 0x4b, 0x41, 0x00, 0x00, - 0x00, 0x0a, 0x4b, 0x6a, 0x00, 0x00, 0x00, 0x28, - 0xdf, 0x11, 0x04, 0x05, 0x01, 0x00, 0x00, 0x21, - 0x01, 0x00, 0x30, 0x0e, 0xb4, 0xc9, 0xb4, 0xca, - 0x26, 0x00, 0x00, 0xcb, 0x60, 0x03, 0x00, 0xb4, - 0xcc, 0x61, 0x03, 0x00, 0x64, 0x03, 0x00, 0xa5, - 0x68, 0xd1, 0x01, 0x00, 0x00, 0x60, 0x05, 0x00, - 0x60, 0x04, 0x00, 0x64, 0x04, 0x00, 0x61, 0x03, - 0x00, 0x46, 0xc2, 0x04, 0x61, 0x04, 0x00, 0x95, - 0xea, 0x34, 0x64, 0x05, 0x00, 0x04, 0x06, 0x01, - 0x00, 0x00, 0xae, 0xea, 0x0c, 0xdf, 0x11, 0x04, - 0x05, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, - 0x61, 0x02, 0x00, 0x61, 0x03, 0x00, 0x1b, 0x11, - 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x64, - 0x06, 0x00, 0x61, 0x03, 0x00, 0x46, 0x1b, 0x70, - 0x1b, 0x48, 0xed, 0x7c, 0x01, 0x06, 0xc2, 0x05, - 0x6b, 0x1a, 0x00, 0x00, 0x00, 0x5d, 0x07, 0x00, - 0x61, 0x04, 0x00, 0x64, 0x08, 0x00, 0x61, 0x03, - 0x00, 0x46, 0xf0, 0x11, 0x62, 0x05, 0x00, 0x0e, - 0x0e, 0xec, 0x35, 0xc2, 0x06, 0x6b, 0x30, 0x00, - 0x00, 0x00, 0xb4, 0x11, 0x65, 0x09, 0x00, 0x0e, - 0x64, 0x04, 0x00, 0x61, 0x03, 0x00, 0x1b, 0x11, - 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x38, - 0x46, 0x00, 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, - 0x5d, 0x0a, 0x00, 0x64, 0x04, 0x00, 0x64, 0x03, - 0x00, 0xf0, 0x0e, 0xc1, 0x06, 0x30, 0x30, 0x61, - 0x05, 0x00, 0x40, 0x6a, 0x00, 0x00, 0x00, 0x95, - 0xea, 0x4f, 0x64, 0x05, 0x00, 0x04, 0x07, 0x01, - 0x00, 0x00, 0xad, 0xea, 0x1e, 0x61, 0x00, 0x00, - 0xb4, 0xa7, 0xea, 0x17, 0x5d, 0x0a, 0x00, 0x64, - 0x04, 0x00, 0x64, 0x03, 0x00, 0xf0, 0x0e, 0xde, - 0x11, 0x04, 0x08, 0x01, 0x00, 0x00, 0x21, 0x01, + 0x2d, 0x03, 0xf2, 0x03, 0x00, 0x01, 0x00, 0xf4, + 0x03, 0x02, 0x00, 0x20, 0xf6, 0x03, 0x05, 0x00, + 0x03, 0xe4, 0x03, 0x03, 0x01, 0x6b, 0x22, 0x00, + 0x00, 0x00, 0x60, 0x00, 0x00, 0xd7, 0xf1, 0x04, + 0x06, 0x6e, 0x28, 0xd7, 0x40, 0x06, 0x00, 0x00, + 0x00, 0xcf, 0x61, 0x00, 0x00, 0xf0, 0x08, 0xe3, + 0xd7, 0x61, 0x00, 0x00, 0xf6, 0x0e, 0x0e, 0x29, + 0xd0, 0x6b, 0x07, 0x00, 0x00, 0x00, 0xcc, 0x6e, + 0x28, 0x30, 0x0c, 0x43, 0x02, 0x00, 0xec, 0x03, + 0x02, 0x04, 0x02, 0x03, 0x00, 0x01, 0x00, 0x48, + 0x06, 0xf8, 0x03, 0x00, 0x01, 0x00, 0xfa, 0x03, + 0x00, 0x01, 0x00, 0xfc, 0x03, 0x01, 0x00, 0x20, + 0xfe, 0x03, 0x02, 0x01, 0x20, 0xf2, 0x03, 0x03, + 0x02, 0x20, 0xf6, 0x03, 0x03, 0x03, 0x20, 0xea, + 0x03, 0x01, 0x00, 0x60, 0x00, 0x00, 0x38, 0x49, + 0x00, 0x00, 0x00, 0xcf, 0x60, 0x01, 0x00, 0xd8, + 0xd0, 0x61, 0x01, 0x00, 0x8f, 0x62, 0x01, 0x00, + 0xba, 0xa7, 0xf0, 0x2c, 0x60, 0x03, 0x00, 0x60, + 0x02, 0x00, 0xd7, 0x61, 0x01, 0x00, 0x46, 0xd1, + 0xd7, 0x61, 0x01, 0x00, 0x38, 0x49, 0x00, 0x00, + 0x00, 0x48, 0xe3, 0x61, 0x02, 0x00, 0xf5, 0xd2, + 0x61, 0x00, 0x00, 0xf1, 0xd5, 0x61, 0x03, 0x00, + 0x11, 0x62, 0x00, 0x00, 0x0e, 0xf2, 0xcb, 0x61, + 0x00, 0x00, 0x28, 0x0c, 0x41, 0x02, 0x00, 0xb6, + 0x02, 0x02, 0x1a, 0x01, 0x05, 0x07, 0x08, 0x02, + 0xd8, 0x05, 0x1c, 0x80, 0x04, 0x00, 0x01, 0x00, + 0x82, 0x04, 0x00, 0x01, 0x00, 0x80, 0x04, 0x01, + 0xff, 0xff, 0xff, 0xff, 0x0f, 0x20, 0x82, 0x04, + 0x01, 0x01, 0x20, 0x84, 0x04, 0x02, 0x00, 0x60, + 0x03, 0x86, 0x04, 0x02, 0x03, 0x20, 0x88, 0x04, + 0x02, 0x04, 0x60, 0x04, 0xf8, 0x03, 0x02, 0x05, + 0x60, 0x02, 0x8a, 0x04, 0x02, 0x06, 0x60, 0x05, + 0xfa, 0x03, 0x02, 0x07, 0x60, 0x01, 0x8c, 0x04, + 0x02, 0x08, 0x20, 0x8e, 0x04, 0x02, 0x09, 0x20, + 0xe2, 0x01, 0x09, 0x1a, 0x20, 0x90, 0x04, 0x0b, + 0x0b, 0x20, 0xf6, 0x03, 0x0d, 0x0f, 0x03, 0xf2, + 0x03, 0x0b, 0x0c, 0x20, 0xf4, 0x03, 0x0b, 0x0e, + 0x20, 0xe2, 0x01, 0x13, 0x0b, 0x20, 0xfe, 0x03, + 0x13, 0x10, 0x20, 0xe0, 0x01, 0x13, 0x11, 0x20, + 0x88, 0x01, 0x15, 0x16, 0x20, 0x90, 0x04, 0x16, + 0x13, 0x20, 0xf6, 0x03, 0x17, 0x13, 0x03, 0x92, + 0x04, 0x13, 0x12, 0x20, 0xfc, 0x03, 0x1c, 0x16, + 0x20, 0xf6, 0x03, 0x1f, 0x1a, 0x03, 0x94, 0x04, + 0x02, 0x0a, 0x60, 0x00, 0x96, 0x04, 0x02, 0x19, + 0x60, 0x06, 0xb2, 0x03, 0x02, 0x01, 0xb6, 0x03, + 0x01, 0x01, 0xe4, 0x03, 0x03, 0x01, 0xec, 0x03, + 0x02, 0x00, 0xe8, 0x03, 0x00, 0x00, 0xe6, 0x03, + 0x04, 0x01, 0xea, 0x03, 0x01, 0x00, 0xe2, 0x03, + 0x00, 0x01, 0x0c, 0x42, 0x03, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x05, 0x00, 0x0b, 0x00, 0xae, 0x04, + 0x08, 0x98, 0x04, 0x01, 0x00, 0x20, 0xe4, 0x01, + 0x01, 0x01, 0x20, 0x9a, 0x04, 0x01, 0x02, 0x20, + 0xfe, 0x03, 0x03, 0x03, 0x20, 0xf2, 0x03, 0x04, + 0x04, 0x20, 0x9c, 0x04, 0x04, 0x05, 0x20, 0xf6, + 0x03, 0x09, 0x06, 0x03, 0xfc, 0x03, 0x10, 0x06, + 0x20, 0x94, 0x04, 0x18, 0x10, 0xb2, 0x03, 0x00, + 0x02, 0xb6, 0x03, 0x01, 0x02, 0xfa, 0x03, 0x07, + 0x10, 0xf8, 0x03, 0x05, 0x10, 0x84, 0x04, 0x02, + 0x10, 0x88, 0x04, 0x04, 0x10, 0xe4, 0x03, 0x02, + 0x02, 0x8a, 0x04, 0x06, 0x10, 0x96, 0x04, 0x19, + 0x10, 0xec, 0x03, 0x03, 0x02, 0x60, 0x02, 0x00, + 0x60, 0x01, 0x00, 0x60, 0x00, 0x00, 0x64, 0x00, + 0x00, 0x11, 0xba, 0xad, 0xf1, 0x06, 0x11, 0xbb, + 0xad, 0xf0, 0x09, 0xbc, 0x11, 0x65, 0x00, 0x00, + 0x0e, 0xf2, 0x33, 0x11, 0xbc, 0xad, 0xf0, 0x0c, + 0xe4, 0x11, 0x04, 0x0f, 0x01, 0x00, 0x00, 0x21, + 0x01, 0x00, 0x30, 0x11, 0xbd, 0xad, 0xf0, 0x13, + 0x0b, 0x38, 0x49, 0x00, 0x00, 0x00, 0x4b, 0x44, + 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x70, 0x00, 0x00, + 0x00, 0x28, 0xe5, 0x11, 0x04, 0x10, 0x01, 0x00, + 0x00, 0x21, 0x01, 0x00, 0x30, 0x0e, 0xba, 0xcf, + 0xba, 0xd0, 0x26, 0x00, 0x00, 0xd1, 0x60, 0x03, + 0x00, 0xba, 0xd2, 0x61, 0x03, 0x00, 0x64, 0x03, + 0x00, 0xa5, 0x68, 0x93, 0x01, 0x00, 0x00, 0x60, + 0x05, 0x00, 0x60, 0x04, 0x00, 0x64, 0x04, 0x00, + 0x61, 0x03, 0x00, 0x46, 0xc8, 0x04, 0x61, 0x04, + 0x00, 0xf1, 0x28, 0x64, 0x05, 0x00, 0x04, 0x11, + 0x01, 0x00, 0x00, 0xae, 0xf0, 0x0c, 0xe5, 0x11, + 0x04, 0x10, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, + 0x30, 0x61, 0x02, 0x00, 0x61, 0x03, 0x00, 0x64, + 0x06, 0x00, 0x61, 0x03, 0x00, 0x46, 0x48, 0xf3, + 0x4b, 0x01, 0x06, 0xc8, 0x05, 0x6b, 0x1a, 0x00, + 0x00, 0x00, 0x5d, 0x07, 0x00, 0x61, 0x04, 0x00, + 0x64, 0x08, 0x00, 0x61, 0x03, 0x00, 0x46, 0xf6, + 0x11, 0x62, 0x05, 0x00, 0x0e, 0x0e, 0xf2, 0x29, + 0xc8, 0x06, 0x6b, 0x24, 0x00, 0x00, 0x00, 0xba, + 0x11, 0x65, 0x09, 0x00, 0x0e, 0x64, 0x04, 0x00, + 0x61, 0x03, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, + 0x48, 0x5d, 0x0a, 0x00, 0x64, 0x04, 0x00, 0x64, + 0x03, 0x00, 0xf6, 0x0e, 0xc7, 0x06, 0x30, 0x30, + 0x61, 0x05, 0x00, 0x40, 0x70, 0x00, 0x00, 0x00, + 0xf1, 0x43, 0x64, 0x05, 0x00, 0x04, 0x12, 0x01, + 0x00, 0x00, 0xad, 0xf0, 0x1e, 0x61, 0x00, 0x00, + 0xba, 0xa7, 0xf0, 0x17, 0x5d, 0x0a, 0x00, 0x64, + 0x04, 0x00, 0x64, 0x03, 0x00, 0xf6, 0x0e, 0xe4, + 0x11, 0x04, 0x13, 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x61, 0x02, 0x00, 0x61, 0x03, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x61, 0x05, 0x00, 0x40, 0x41, 0x00, 0x00, - 0x00, 0x1b, 0x70, 0x1b, 0x48, 0x61, 0x01, 0x00, - 0x90, 0x62, 0x01, 0x00, 0x0e, 0xed, 0xd1, 0x00, - 0x64, 0x09, 0x00, 0x8f, 0x65, 0x09, 0x00, 0x0e, - 0x61, 0x00, 0x00, 0x90, 0x62, 0x00, 0x00, 0x0e, - 0x64, 0x04, 0x00, 0x61, 0x03, 0x00, 0x1b, 0x11, - 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x38, - 0x46, 0x00, 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, + 0x61, 0x05, 0x00, 0x40, 0x44, 0x00, 0x00, 0x00, + 0x48, 0x61, 0x01, 0x00, 0x90, 0x62, 0x01, 0x00, + 0x0e, 0xf3, 0xb9, 0x00, 0x64, 0x09, 0x00, 0x8f, + 0x65, 0x09, 0x00, 0x0e, 0x61, 0x00, 0x00, 0x90, + 0x62, 0x00, 0x00, 0x0e, 0x64, 0x04, 0x00, 0x61, + 0x03, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, 0x48, 0x64, 0x05, 0x00, 0x60, 0x07, 0x00, 0x11, 0x04, - 0x09, 0x01, 0x00, 0x00, 0xad, 0xea, 0x2e, 0x5d, + 0x14, 0x01, 0x00, 0x00, 0xad, 0xf0, 0x2e, 0x5d, 0x0a, 0x00, 0x64, 0x04, 0x00, 0x64, 0x03, 0x00, - 0xf0, 0xc2, 0x07, 0x61, 0x07, 0x00, 0xea, 0x05, - 0x61, 0x07, 0x00, 0x30, 0xb7, 0x11, 0x65, 0x00, - 0x00, 0x0e, 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, - 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, - 0x00, 0x00, 0x00, 0x28, 0x11, 0x04, 0x06, 0x01, - 0x00, 0x00, 0xad, 0xea, 0x3c, 0x64, 0x09, 0x00, - 0xb5, 0xa5, 0xea, 0x19, 0xb7, 0x11, 0x65, 0x00, - 0x00, 0x0e, 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, - 0x4b, 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, + 0xf6, 0xc8, 0x07, 0x61, 0x07, 0x00, 0xf0, 0x05, + 0x61, 0x07, 0x00, 0x30, 0xbd, 0x11, 0x65, 0x00, + 0x00, 0x0e, 0x0b, 0x38, 0x49, 0x00, 0x00, 0x00, + 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x70, + 0x00, 0x00, 0x00, 0x28, 0x11, 0x04, 0x11, 0x01, + 0x00, 0x00, 0xad, 0xf0, 0x30, 0x64, 0x09, 0x00, + 0xbb, 0xa5, 0xf0, 0x19, 0xbd, 0x11, 0x65, 0x00, + 0x00, 0x0e, 0x0b, 0x38, 0x49, 0x00, 0x00, 0x00, + 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, 0x61, 0x02, 0x00, 0x61, - 0x03, 0x00, 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, - 0x70, 0x1b, 0x1b, 0x64, 0x06, 0x00, 0x61, 0x03, - 0x00, 0x46, 0x1b, 0x70, 0x1b, 0x48, 0xec, 0x27, - 0x11, 0x04, 0x07, 0x01, 0x00, 0x00, 0xad, 0xea, - 0x1e, 0x61, 0x01, 0x00, 0xb4, 0xa7, 0xea, 0x17, - 0x5d, 0x0a, 0x00, 0x64, 0x04, 0x00, 0x64, 0x03, - 0x00, 0xf0, 0x0e, 0xde, 0x11, 0x04, 0x08, 0x01, - 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x0e, 0x61, - 0x03, 0x00, 0x90, 0x62, 0x03, 0x00, 0x0e, 0xed, - 0x29, 0xfe, 0x61, 0x01, 0x00, 0xb4, 0xad, 0xea, - 0x19, 0xb7, 0x11, 0x65, 0x00, 0x00, 0x0e, 0x0b, - 0x38, 0x46, 0x00, 0x00, 0x00, 0x4b, 0x41, 0x00, - 0x00, 0x00, 0x0a, 0x4b, 0x6a, 0x00, 0x00, 0x00, - 0x28, 0xb5, 0x11, 0x65, 0x00, 0x00, 0x0e, 0x0b, - 0x61, 0x02, 0x00, 0x4b, 0x41, 0x00, 0x00, 0x00, - 0x09, 0x4b, 0x6a, 0x00, 0x00, 0x00, 0x28, 0x0c, - 0x42, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x06, - 0x00, 0x06, 0x00, 0x88, 0x01, 0x01, 0xe6, 0x03, - 0x01, 0x00, 0x20, 0xfe, 0x03, 0x18, 0x10, 0xa6, - 0x03, 0x00, 0x02, 0xaa, 0x03, 0x01, 0x02, 0xd6, - 0x03, 0x03, 0x02, 0xe2, 0x03, 0x05, 0x10, 0xe4, - 0x03, 0x07, 0x10, 0x60, 0x00, 0x00, 0x64, 0x00, - 0x00, 0x11, 0xb4, 0xad, 0xea, 0x09, 0xb7, 0x11, - 0x65, 0x00, 0x00, 0x0e, 0xec, 0x4b, 0x11, 0xb5, - 0xad, 0xea, 0x09, 0xb6, 0x11, 0x65, 0x00, 0x00, - 0x0e, 0xec, 0x3e, 0x11, 0xb6, 0xad, 0xea, 0x0c, - 0xde, 0x11, 0x04, 0x04, 0x01, 0x00, 0x00, 0x21, - 0x01, 0x00, 0x30, 0x11, 0xb7, 0xad, 0xea, 0x13, - 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x4b, 0x41, - 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, 0x00, 0x00, - 0x00, 0x28, 0xdf, 0x11, 0x04, 0x0a, 0x01, 0x00, - 0x00, 0x41, 0x5d, 0x00, 0x00, 0x00, 0x64, 0x00, - 0x00, 0x24, 0x01, 0x00, 0x21, 0x01, 0x00, 0x30, - 0x0e, 0xe0, 0x64, 0x04, 0x00, 0x64, 0x05, 0x00, - 0xf0, 0xc9, 0x61, 0x00, 0x00, 0xea, 0x05, 0x61, - 0x00, 0x00, 0x30, 0xb7, 0x11, 0x65, 0x00, 0x00, - 0x0e, 0x0b, 0x38, 0x46, 0x00, 0x00, 0x00, 0x4b, - 0x41, 0x00, 0x00, 0x00, 0x0a, 0x4b, 0x6a, 0x00, - 0x00, 0x00, 0x28, 0x60, 0x01, 0x00, 0x60, 0x00, - 0x00, 0xd1, 0xc9, 0xd2, 0x11, 0xf2, 0xea, 0x08, - 0x0e, 0x38, 0x46, 0x00, 0x00, 0x00, 0xda, 0xca, - 0x60, 0x19, 0x00, 0x60, 0x18, 0x00, 0x60, 0x09, - 0x00, 0x60, 0x08, 0x00, 0x60, 0x07, 0x00, 0x60, - 0x06, 0x00, 0x60, 0x05, 0x00, 0x60, 0x04, 0x00, - 0x60, 0x03, 0x00, 0x60, 0x02, 0x00, 0x5d, 0x04, - 0x00, 0xd1, 0x04, 0x0b, 0x01, 0x00, 0x00, 0xf0, - 0x0e, 0xd2, 0x38, 0x46, 0x00, 0x00, 0x00, 0xad, - 0xea, 0x06, 0x0c, 0x07, 0xd6, 0xec, 0x0c, 0x5d, - 0x04, 0x00, 0xd2, 0x04, 0x0c, 0x01, 0x00, 0x00, - 0xf0, 0x0e, 0xd2, 0x40, 0xf7, 0x00, 0x00, 0x00, - 0xcb, 0x61, 0x02, 0x00, 0x38, 0x46, 0x00, 0x00, - 0x00, 0xad, 0xea, 0x0b, 0x04, 0x09, 0x01, 0x00, - 0x00, 0x11, 0x62, 0x02, 0x00, 0x0e, 0x61, 0x02, - 0x00, 0x04, 0x07, 0x01, 0x00, 0x00, 0xad, 0x11, - 0xeb, 0x18, 0x0e, 0x61, 0x02, 0x00, 0x04, 0x06, - 0x01, 0x00, 0x00, 0xad, 0x11, 0xeb, 0x0b, 0x0e, - 0x61, 0x02, 0x00, 0x04, 0x09, 0x01, 0x00, 0x00, - 0xad, 0x95, 0xea, 0x0c, 0xdd, 0x11, 0x04, 0x0d, - 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x38, - 0x46, 0x00, 0x00, 0x00, 0xcc, 0x61, 0x02, 0x00, - 0x04, 0x06, 0x01, 0x00, 0x00, 0xad, 0xea, 0x24, - 0xd2, 0x40, 0xf8, 0x00, 0x00, 0x00, 0x11, 0x62, - 0x03, 0x00, 0x0e, 0x61, 0x03, 0x00, 0x38, 0x46, - 0x00, 0x00, 0x00, 0xae, 0xea, 0x0e, 0x5d, 0x04, - 0x00, 0x61, 0x03, 0x00, 0x04, 0x0e, 0x01, 0x00, - 0x00, 0xf0, 0x0e, 0x26, 0x00, 0x00, 0xc2, 0x04, - 0x26, 0x00, 0x00, 0xc2, 0x05, 0x26, 0x00, 0x00, - 0xc2, 0x06, 0xb4, 0xc2, 0x07, 0x38, 0x46, 0x00, - 0x00, 0x00, 0xc2, 0x08, 0xd1, 0x5d, 0x05, 0x00, - 0x47, 0x24, 0x00, 0x00, 0xc2, 0x09, 0x6b, 0xcc, - 0x01, 0x00, 0x00, 0x60, 0x0a, 0x00, 0x61, 0x09, - 0x00, 0x40, 0x6b, 0x00, 0x00, 0x00, 0xc2, 0x0a, - 0x60, 0x0e, 0x00, 0x60, 0x0d, 0x00, 0x60, 0x0b, - 0x00, 0x06, 0xc2, 0x0b, 0x6b, 0x14, 0x00, 0x00, - 0x00, 0xdf, 0x61, 0x09, 0x00, 0x61, 0x0a, 0x00, - 0xf0, 0x11, 0x62, 0x0b, 0x00, 0x0e, 0x0e, 0xec, - 0x16, 0xc2, 0x0c, 0x6b, 0x11, 0x00, 0x00, 0x00, - 0x38, 0x46, 0x00, 0x00, 0x00, 0x11, 0x62, 0x09, - 0x00, 0x0e, 0xc1, 0x0c, 0x30, 0x30, 0x61, 0x0b, - 0x00, 0x40, 0x6a, 0x00, 0x00, 0x00, 0xeb, 0x6f, - 0x61, 0x0b, 0x00, 0x40, 0x41, 0x00, 0x00, 0x00, - 0xc2, 0x0d, 0x5d, 0x04, 0x00, 0x61, 0x0d, 0x00, - 0x04, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0x0e, 0x61, - 0x0d, 0x00, 0x5d, 0x05, 0x00, 0x46, 0xc2, 0x0e, - 0x61, 0x0e, 0x00, 0xea, 0x0e, 0xdf, 0x61, 0x0d, - 0x00, 0x61, 0x0e, 0x00, 0xf0, 0x11, 0x62, 0x0d, - 0x00, 0x0e, 0x61, 0x05, 0x00, 0x61, 0x07, 0x00, - 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, - 0x1b, 0x61, 0x0d, 0x00, 0x1b, 0x70, 0x1b, 0x48, - 0x61, 0x06, 0x00, 0x61, 0x07, 0x00, 0x1b, 0x11, - 0xaf, 0xeb, 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x61, - 0x0d, 0x00, 0x40, 0x6b, 0x00, 0x00, 0x00, 0x1b, - 0x70, 0x1b, 0x48, 0x61, 0x07, 0x00, 0x90, 0x62, - 0x07, 0x00, 0x0e, 0xed, 0x54, 0xff, 0x38, 0x46, - 0x00, 0x00, 0x00, 0x11, 0x62, 0x09, 0x00, 0x0e, - 0x61, 0x03, 0x00, 0x68, 0xfc, 0x00, 0x00, 0x00, - 0x60, 0x15, 0x00, 0x60, 0x11, 0x00, 0x60, 0x10, - 0x00, 0x60, 0x0f, 0x00, 0x61, 0x03, 0x00, 0x5d, - 0x05, 0x00, 0x47, 0x24, 0x00, 0x00, 0x11, 0x62, - 0x08, 0x00, 0x0e, 0x61, 0x08, 0x00, 0x40, 0x6b, - 0x00, 0x00, 0x00, 0xc2, 0x0f, 0xb4, 0xc2, 0x10, - 0x09, 0xc2, 0x11, 0x61, 0x10, 0x00, 0x61, 0x07, - 0x00, 0xa5, 0xea, 0x70, 0x60, 0x12, 0x00, 0x06, - 0xc2, 0x12, 0x6b, 0x2e, 0x00, 0x00, 0x00, 0x60, - 0x13, 0x00, 0xdf, 0x61, 0x08, 0x00, 0x61, 0x0f, - 0x00, 0xf0, 0xc2, 0x13, 0x61, 0x13, 0x00, 0x40, - 0x6a, 0x00, 0x00, 0x00, 0x11, 0x62, 0x11, 0x00, - 0x0e, 0x61, 0x13, 0x00, 0x40, 0x41, 0x00, 0x00, - 0x00, 0x11, 0x62, 0x12, 0x00, 0x0e, 0x0e, 0xec, - 0x16, 0xc2, 0x14, 0x6b, 0x11, 0x00, 0x00, 0x00, - 0x38, 0x46, 0x00, 0x00, 0x00, 0x11, 0x62, 0x08, - 0x00, 0x0e, 0xc1, 0x14, 0x30, 0x30, 0x61, 0x11, - 0x00, 0xeb, 0x21, 0x61, 0x04, 0x00, 0x61, 0x10, - 0x00, 0x1b, 0x11, 0xaf, 0xeb, 0x04, 0x1b, 0x70, - 0x1b, 0x1b, 0x61, 0x12, 0x00, 0x1b, 0x70, 0x1b, - 0x48, 0x61, 0x10, 0x00, 0x90, 0x62, 0x10, 0x00, - 0x0e, 0xec, 0x89, 0x61, 0x08, 0x00, 0xc2, 0x15, - 0x38, 0x46, 0x00, 0x00, 0x00, 0x11, 0x62, 0x08, - 0x00, 0x0e, 0x61, 0x11, 0x00, 0x95, 0xea, 0x16, + 0x03, 0x00, 0x64, 0x06, 0x00, 0x61, 0x03, 0x00, + 0x46, 0x48, 0xf2, 0x27, 0x11, 0x04, 0x12, 0x01, + 0x00, 0x00, 0xad, 0xf0, 0x1e, 0x61, 0x01, 0x00, + 0xba, 0xa7, 0xf0, 0x17, 0x5d, 0x0a, 0x00, 0x64, + 0x04, 0x00, 0x64, 0x03, 0x00, 0xf6, 0x0e, 0xe4, + 0x11, 0x04, 0x13, 0x01, 0x00, 0x00, 0x21, 0x01, + 0x00, 0x30, 0x0e, 0x61, 0x03, 0x00, 0x90, 0x62, + 0x03, 0x00, 0x0e, 0xf3, 0x67, 0xfe, 0x61, 0x01, + 0x00, 0xba, 0xad, 0xf0, 0x19, 0xbd, 0x11, 0x65, + 0x00, 0x00, 0x0e, 0x0b, 0x38, 0x49, 0x00, 0x00, + 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, 0x4b, + 0x70, 0x00, 0x00, 0x00, 0x28, 0xbb, 0x11, 0x65, + 0x00, 0x00, 0x0e, 0x0b, 0x61, 0x02, 0x00, 0x4b, + 0x44, 0x00, 0x00, 0x00, 0x09, 0x4b, 0x70, 0x00, + 0x00, 0x00, 0x28, 0x0c, 0x42, 0x03, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x05, 0x00, 0x06, 0x00, 0x82, + 0x01, 0x01, 0xfc, 0x03, 0x01, 0x00, 0x20, 0x94, + 0x04, 0x18, 0x10, 0xb2, 0x03, 0x00, 0x02, 0xb6, + 0x03, 0x01, 0x02, 0xec, 0x03, 0x03, 0x02, 0xf8, + 0x03, 0x05, 0x10, 0xfa, 0x03, 0x07, 0x10, 0x60, + 0x00, 0x00, 0x64, 0x00, 0x00, 0x11, 0xba, 0xad, + 0xf0, 0x09, 0xbd, 0x11, 0x65, 0x00, 0x00, 0x0e, + 0xf2, 0x45, 0x11, 0xbb, 0xad, 0xf0, 0x09, 0xbc, + 0x11, 0x65, 0x00, 0x00, 0x0e, 0xf2, 0x38, 0x11, + 0xbc, 0xad, 0xf0, 0x0c, 0xe4, 0x11, 0x04, 0x0f, + 0x01, 0x00, 0x00, 0x21, 0x01, 0x00, 0x30, 0x11, + 0xbd, 0xad, 0xf0, 0x13, 0x0b, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x4b, 0x44, 0x00, 0x00, 0x00, 0x0a, + 0x4b, 0x70, 0x00, 0x00, 0x00, 0x28, 0xe5, 0x11, + 0x04, 0x15, 0x01, 0x00, 0x00, 0x64, 0x00, 0x00, + 0x71, 0x9c, 0x21, 0x01, 0x00, 0x30, 0x0e, 0xe6, + 0x64, 0x04, 0x00, 0x64, 0x05, 0x00, 0xf6, 0xcf, + 0x61, 0x00, 0x00, 0xf0, 0x05, 0x61, 0x00, 0x00, + 0x30, 0xbd, 0x11, 0x65, 0x00, 0x00, 0x0e, 0x0b, + 0x38, 0x49, 0x00, 0x00, 0x00, 0x4b, 0x44, 0x00, + 0x00, 0x00, 0x0a, 0x4b, 0x70, 0x00, 0x00, 0x00, + 0x28, 0x60, 0x01, 0x00, 0x60, 0x00, 0x00, 0xd7, + 0xcf, 0xd8, 0x11, 0xf8, 0xf0, 0x08, 0x0e, 0x38, + 0x49, 0x00, 0x00, 0x00, 0xe0, 0xd0, 0x60, 0x19, + 0x00, 0x60, 0x18, 0x00, 0x60, 0x09, 0x00, 0x60, + 0x08, 0x00, 0x60, 0x07, 0x00, 0x60, 0x06, 0x00, + 0x60, 0x05, 0x00, 0x60, 0x04, 0x00, 0x60, 0x03, + 0x00, 0x60, 0x02, 0x00, 0x5d, 0x04, 0x00, 0xd7, + 0x04, 0x16, 0x01, 0x00, 0x00, 0xf6, 0x0e, 0xd8, + 0x38, 0x49, 0x00, 0x00, 0x00, 0xad, 0xf0, 0x06, + 0x0c, 0x07, 0xdc, 0xf2, 0x0c, 0x5d, 0x04, 0x00, + 0xd8, 0x04, 0x17, 0x01, 0x00, 0x00, 0xf6, 0x0e, + 0xd8, 0x40, 0x02, 0x01, 0x00, 0x00, 0xd1, 0x61, + 0x02, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, 0xad, + 0xf0, 0x0b, 0x04, 0x14, 0x01, 0x00, 0x00, 0x11, + 0x62, 0x02, 0x00, 0x0e, 0x61, 0x02, 0x00, 0x04, + 0x12, 0x01, 0x00, 0x00, 0xad, 0x11, 0xf1, 0x18, + 0x0e, 0x61, 0x02, 0x00, 0x04, 0x11, 0x01, 0x00, + 0x00, 0xad, 0x11, 0xf1, 0x0b, 0x0e, 0x61, 0x02, + 0x00, 0x04, 0x14, 0x01, 0x00, 0x00, 0xad, 0xf1, + 0x0c, 0xe3, 0x11, 0x04, 0x18, 0x01, 0x00, 0x00, + 0x21, 0x01, 0x00, 0x30, 0x38, 0x49, 0x00, 0x00, + 0x00, 0xd2, 0x61, 0x02, 0x00, 0x04, 0x11, 0x01, + 0x00, 0x00, 0xad, 0xf0, 0x24, 0xd8, 0x40, 0x03, + 0x01, 0x00, 0x00, 0x11, 0x62, 0x03, 0x00, 0x0e, + 0x61, 0x03, 0x00, 0x38, 0x49, 0x00, 0x00, 0x00, + 0xae, 0xf0, 0x0e, 0x5d, 0x04, 0x00, 0x61, 0x03, + 0x00, 0x04, 0x19, 0x01, 0x00, 0x00, 0xf6, 0x0e, + 0x26, 0x00, 0x00, 0xc8, 0x04, 0x26, 0x00, 0x00, + 0xc8, 0x05, 0x26, 0x00, 0x00, 0xc8, 0x06, 0xba, + 0xc8, 0x07, 0x38, 0x49, 0x00, 0x00, 0x00, 0xc8, + 0x08, 0xd7, 0x5d, 0x05, 0x00, 0x47, 0x24, 0x00, + 0x00, 0xc8, 0x09, 0x6b, 0x9b, 0x01, 0x00, 0x00, + 0x60, 0x0a, 0x00, 0x61, 0x09, 0x00, 0x40, 0x71, + 0x00, 0x00, 0x00, 0xc8, 0x0a, 0x60, 0x0e, 0x00, + 0x60, 0x0d, 0x00, 0x60, 0x0b, 0x00, 0x06, 0xc8, + 0x0b, 0x6b, 0x14, 0x00, 0x00, 0x00, 0xe5, 0x61, + 0x09, 0x00, 0x61, 0x0a, 0x00, 0xf6, 0x11, 0x62, + 0x0b, 0x00, 0x0e, 0x0e, 0xf2, 0x16, 0xc8, 0x0c, + 0x6b, 0x11, 0x00, 0x00, 0x00, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x11, 0x62, 0x09, 0x00, 0x0e, 0xc7, + 0x0c, 0x30, 0x30, 0x61, 0x0b, 0x00, 0x40, 0x70, + 0x00, 0x00, 0x00, 0xf1, 0x57, 0x61, 0x0b, 0x00, + 0x40, 0x44, 0x00, 0x00, 0x00, 0xc8, 0x0d, 0x5d, + 0x04, 0x00, 0x61, 0x0d, 0x00, 0x04, 0x1a, 0x01, + 0x00, 0x00, 0xf6, 0x0e, 0x61, 0x0d, 0x00, 0x5d, + 0x05, 0x00, 0x46, 0xc8, 0x0e, 0x61, 0x0e, 0x00, + 0xf0, 0x0e, 0xe5, 0x61, 0x0d, 0x00, 0x61, 0x0e, + 0x00, 0xf6, 0x11, 0x62, 0x0d, 0x00, 0x0e, 0x61, + 0x05, 0x00, 0x61, 0x07, 0x00, 0x61, 0x0d, 0x00, + 0x48, 0x61, 0x06, 0x00, 0x61, 0x07, 0x00, 0x61, + 0x0d, 0x00, 0x40, 0x71, 0x00, 0x00, 0x00, 0x48, + 0x61, 0x07, 0x00, 0x90, 0x62, 0x07, 0x00, 0x0e, + 0xf3, 0x6c, 0xff, 0x38, 0x49, 0x00, 0x00, 0x00, + 0x11, 0x62, 0x09, 0x00, 0x0e, 0x61, 0x03, 0x00, + 0x68, 0xe3, 0x00, 0x00, 0x00, 0x60, 0x15, 0x00, + 0x60, 0x11, 0x00, 0x60, 0x10, 0x00, 0x60, 0x0f, + 0x00, 0x61, 0x03, 0x00, 0x5d, 0x05, 0x00, 0x47, + 0x24, 0x00, 0x00, 0x11, 0x62, 0x08, 0x00, 0x0e, + 0x61, 0x08, 0x00, 0x40, 0x71, 0x00, 0x00, 0x00, + 0xc8, 0x0f, 0xba, 0xc8, 0x10, 0x09, 0xc8, 0x11, + 0x61, 0x10, 0x00, 0x61, 0x07, 0x00, 0xa5, 0xf0, + 0x64, 0x60, 0x12, 0x00, 0x06, 0xc8, 0x12, 0x6b, + 0x2e, 0x00, 0x00, 0x00, 0x60, 0x13, 0x00, 0xe5, + 0x61, 0x08, 0x00, 0x61, 0x0f, 0x00, 0xf6, 0xc8, + 0x13, 0x61, 0x13, 0x00, 0x40, 0x70, 0x00, 0x00, + 0x00, 0x11, 0x62, 0x11, 0x00, 0x0e, 0x61, 0x13, + 0x00, 0x40, 0x44, 0x00, 0x00, 0x00, 0x11, 0x62, + 0x12, 0x00, 0x0e, 0x0e, 0xf2, 0x16, 0xc8, 0x14, + 0x6b, 0x11, 0x00, 0x00, 0x00, 0x38, 0x49, 0x00, + 0x00, 0x00, 0x11, 0x62, 0x08, 0x00, 0x0e, 0xc7, + 0x14, 0x30, 0x30, 0x61, 0x11, 0x00, 0xf1, 0x15, + 0x61, 0x04, 0x00, 0x61, 0x10, 0x00, 0x61, 0x12, + 0x00, 0x48, 0x61, 0x10, 0x00, 0x90, 0x62, 0x10, + 0x00, 0x0e, 0xf2, 0x95, 0x61, 0x08, 0x00, 0xc8, + 0x15, 0x38, 0x49, 0x00, 0x00, 0x00, 0x11, 0x62, + 0x08, 0x00, 0x0e, 0x61, 0x11, 0x00, 0xf1, 0x16, 0x60, 0x16, 0x00, 0x5d, 0x06, 0x00, 0x61, 0x15, - 0x00, 0xef, 0xc2, 0x16, 0x61, 0x16, 0x00, 0xea, + 0x00, 0xf5, 0xc8, 0x16, 0x61, 0x16, 0x00, 0xf0, 0x05, 0x61, 0x16, 0x00, 0x30, 0x61, 0x10, 0x00, - 0x61, 0x07, 0x00, 0xa5, 0xea, 0x23, 0x61, 0x04, - 0x00, 0x61, 0x10, 0x00, 0x1b, 0x11, 0xaf, 0xeb, - 0x04, 0x1b, 0x70, 0x1b, 0x1b, 0x38, 0x46, 0x00, - 0x00, 0x00, 0x1b, 0x70, 0x1b, 0x48, 0x61, 0x10, - 0x00, 0x90, 0x62, 0x10, 0x00, 0x0e, 0xec, 0xd6, - 0x0e, 0xec, 0x25, 0xc2, 0x17, 0x6b, 0x20, 0x00, - 0x00, 0x00, 0xe0, 0x61, 0x05, 0x00, 0x61, 0x07, - 0x00, 0xf0, 0x0e, 0x5d, 0x06, 0x00, 0x61, 0x09, - 0x00, 0xef, 0x0e, 0x5d, 0x06, 0x00, 0x61, 0x08, - 0x00, 0xef, 0x0e, 0xc1, 0x17, 0x30, 0x30, 0xb4, - 0xc2, 0x18, 0x61, 0x07, 0x00, 0xc2, 0x19, 0x0b, - 0x5d, 0x07, 0x00, 0x4e, 0xbf, 0x00, 0x53, 0x6b, - 0x00, 0x00, 0x00, 0x04, 0xbf, 0x01, 0x53, 0x06, - 0x00, 0x00, 0x00, 0x04, 0x28, 0xbf, 0x00, 0xc9, - 0xbf, 0x01, 0xca, 0xbf, 0x02, 0xcb, 0xbf, 0x03, - 0x28, 0xbf, 0x00, 0xcd, 0x28, + 0x61, 0x07, 0x00, 0xa5, 0xf0, 0x17, 0x61, 0x04, + 0x00, 0x61, 0x10, 0x00, 0x38, 0x49, 0x00, 0x00, + 0x00, 0x48, 0x61, 0x10, 0x00, 0x90, 0x62, 0x10, + 0x00, 0x0e, 0xf2, 0xe2, 0x0e, 0xf2, 0x25, 0xc8, + 0x17, 0x6b, 0x20, 0x00, 0x00, 0x00, 0xe6, 0x61, + 0x05, 0x00, 0x61, 0x07, 0x00, 0xf6, 0x0e, 0x5d, + 0x06, 0x00, 0x61, 0x09, 0x00, 0xf5, 0x0e, 0x5d, + 0x06, 0x00, 0x61, 0x08, 0x00, 0xf5, 0x0e, 0xc7, + 0x17, 0x30, 0x30, 0xba, 0xc8, 0x18, 0x61, 0x07, + 0x00, 0xc8, 0x19, 0x0b, 0x5d, 0x07, 0x00, 0x4e, + 0xc5, 0x00, 0x53, 0x71, 0x00, 0x00, 0x00, 0x04, + 0xc5, 0x01, 0x53, 0x06, 0x00, 0x00, 0x00, 0x04, + 0x28, 0xc5, 0x00, 0xcf, 0xc5, 0x01, 0xd0, 0xc5, + 0x02, 0xd1, 0xc5, 0x03, 0x28, 0xc5, 0x00, 0xd3, + 0x28, }; @@ -19570,6 +20687,7 @@ static JSValue js_bytecode_autoinit(JSContext *ctx, JSObject *p, JSAtom atom, "Object.defineProperty", 3, JS_CFUNC_generic_magic, 0), JS_AtomToValue(ctx, JS_ATOM_Symbol_iterator), + JS_NewCFunction(ctx, js_call_function, "call", 2), }; return js_bytecode_eval(ctx, qjsc_builtin_array_fromasync, sizeof(qjsc_builtin_array_fromasync), @@ -19822,8 +20940,10 @@ static JSValue JS_ThrowTypeErrorPrivateNotFound(JSContext *ctx, JSAtom atom) atom); } -/* Private fields can be added even on non extensible objects or - Proxies */ +/* Per the nonextensible-applies-to-private proposal, private fields cannot + be added to non-extensible objects. Note that a Proxy's JSObject stays + extensible (its trap reflects the target), so private fields can still be + stamped onto Proxies. */ static int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj, JSValue name, JSValue val) { @@ -19843,6 +20963,10 @@ static int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj, } prop = js_symbol_to_atom(ctx, name); p = JS_VALUE_GET_OBJ(obj); + if (unlikely(!p->extensible)) { + JS_ThrowTypeError(ctx, "object is not extensible"); + goto fail; + } prs = find_own_property(&pr, p, prop); if (prs) { JS_ThrowTypeErrorAtom(ctx, "private class field '%s' already exists", @@ -19946,6 +21070,11 @@ static int JS_AddBrand(JSContext *ctx, JSValueConst obj, JSValueConst home_obj) if (JS_IsObject(obj)) { p1 = JS_VALUE_GET_OBJ(obj); + if (unlikely(!p1->extensible)) { + JS_FreeAtom(ctx, brand_atom); + JS_ThrowTypeError(ctx, "object is not extensible"); + return -1; + } prs = find_own_property(&pr, p1, brand_atom); if (unlikely(prs)) { JS_FreeAtom(ctx, brand_atom); @@ -20077,7 +21206,7 @@ static int __exception JS_GetOwnPropertyNamesInternal(JSContext *ctx, exotic_count = 0; tab_exotic = NULL; sh = p->shape; - for(i = 0, prs = sh->prop; i < sh->prop_count; i++, prs++) { + for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) { atom = prs->atom; if (atom != JS_ATOM_NULL) { is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0); @@ -20164,7 +21293,7 @@ static int __exception JS_GetOwnPropertyNamesInternal(JSContext *ctx, num_sorted = true; sh = p->shape; - for(i = 0, prs = sh->prop; i < sh->prop_count; i++, prs++) { + for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) { atom = prs->atom; if (atom != JS_ATOM_NULL) { is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0); @@ -20318,20 +21447,12 @@ static int JS_GetOwnPropertyInternal2(JSContext *ctx, JSPropertyDescriptor *desc if (desc) { desc->flags = JS_PROP_WRITABLE | JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE; - if (is_typed_array(p->class_id) && typed_array_is_immutable(p)) { - desc->flags &= ~JS_PROP_WRITABLE; - desc->flags &= ~JS_PROP_CONFIGURABLE; - } desc->getter = JS_UNDEFINED; desc->setter = JS_UNDEFINED; desc->value = JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx); } else if (flags_only) { *pflags = JS_PROP_WRITABLE | JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE; - if (is_typed_array(p->class_id) && typed_array_is_immutable(p)) { - *pflags &= ~JS_PROP_WRITABLE; - *pflags &= ~JS_PROP_CONFIGURABLE; - } } return true; } @@ -20416,6 +21537,15 @@ int JS_PreventExtensions(JSContext *ctx, JSValueConst obj) p = JS_VALUE_GET_OBJ(obj); if (unlikely(p->class_id == JS_CLASS_PROXY)) return js_proxy_preventExtensions(ctx, obj); + if (is_typed_array(p->class_id)) { + JSTypedArray *ta = p->u.typed_array; + JSArrayBuffer *abuf = ta->buffer->u.array_buffer; + if (ta->track_rab || + (array_buffer_is_resizable(abuf) && !abuf->shared)) + { + return false; + } + } p->extensible = false; return true; } @@ -20565,8 +21695,8 @@ static bool js_get_fast_array_element(JSContext *ctx, JSObject *p, } } -static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj, - JSValue prop) +JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop) { JSAtom atom; JSValue ret; @@ -20718,7 +21848,7 @@ static JSProperty *add_property(JSContext *ctx, p->shape = js_dup_shape(new_sh); js_free_shape(ctx->rt, sh); return &p->prop[new_sh->prop_count - 1]; - } else if (sh->header.ref_count != 1) { + } else if (JS_REF_COUNT(sh) != 1) { /* if the shape is shared, clone it */ new_sh = js_clone_shape(ctx, sh); if (!new_sh) @@ -20730,7 +21860,7 @@ static JSProperty *add_property(JSContext *ctx, p->shape = new_sh; } } - assert(p->shape->header.ref_count == 1); + assert(JS_REF_COUNT(p->shape) == 1); if (add_shape_property(ctx, &p->shape, p, prop, prop_flags)) return NULL; return &p->prop[p->shape->prop_count - 1]; @@ -20796,9 +21926,9 @@ static int delete_property(JSContext *ctx, JSObject *p, JSAtom atom) redo: sh = p->shape; - h1 = atom & sh->prop_hash_mask; + h1 = hash_atom(atom, sh->prop_hash_mask); h = prop_hash_end(sh)[-h1 - 1]; - prop = sh->prop; + prop = get_shape_prop(sh); lpr = NULL; lpr_idx = 0; /* prevent warning */ while (h != 0) { @@ -20809,13 +21939,13 @@ static int delete_property(JSContext *ctx, JSObject *p, JSAtom atom) return false; /* realloc the shape if needed */ if (lpr) - lpr_idx = lpr - sh->prop; + lpr_idx = lpr - get_shape_prop(sh); if (js_shape_prepare_update(ctx, p, &pr)) return -1; sh = p->shape; /* remove property */ if (lpr) { - lpr = &sh->prop[lpr_idx]; + lpr = &get_shape_prop(sh)[lpr_idx]; lpr->hash_next = pr->hash_next; } else { prop_hash_end(sh)[-h1 - 1] = pr->hash_next; @@ -20904,7 +22034,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, if (ret) return -1; /* JS_ToArrayLengthFree() must be done before the read-only test */ - if (unlikely(!(p->shape->prop[0].flags & JS_PROP_WRITABLE))) + if (unlikely(!(get_shape_prop(p->shape)[0].flags & JS_PROP_WRITABLE))) return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length); if (likely(p->fast_array)) { @@ -20912,6 +22042,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, if (len < old_len) { for(i = len; i < old_len; i++) { JS_FreeValue(ctx, p->u.array.u.values[i]); + p->u.array.u.values[i] = JS_UNDEFINED; } p->u.array.count = len; } @@ -20947,7 +22078,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, passes in case one of the property is not configurable */ cur_len = len; - for(i = 0, pr = sh->prop; i < sh->prop_count; + for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) { if (pr->atom != JS_ATOM_NULL && JS_AtomIsArrayIndex(ctx, &idx, pr->atom)) { @@ -20958,7 +22089,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, } } - for(i = 0, pr = sh->prop; i < sh->prop_count; + for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) { if (pr->atom != JS_ATOM_NULL && JS_AtomIsArrayIndex(ctx, &idx, pr->atom)) { @@ -20967,7 +22098,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, delete_property(ctx, p, pr->atom); /* WARNING: the shape may have been modified */ sh = p->shape; - pr = &sh->prop[i]; + pr = &get_shape_prop(sh)[i]; } } } @@ -20987,20 +22118,23 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val, static int expand_fast_array(JSContext *ctx, JSObject *p, uint32_t new_len) { uint32_t old_size, new_size; - size_t slack; JSValue *new_array_prop; + if (unlikely(new_len > (uint32_t)INT32_MAX)) { + JS_ThrowOutOfMemory(ctx); + return -1; + } + old_size = p->u.array.u1.size; - new_size = old_size + old_size/2; // grow by 50% - if (new_size < old_size) { // integer overflow + new_size = old_size + old_size/2; + if (new_size < old_size) { JS_ThrowOutOfMemory(ctx); return -1; } - new_size = max_int(new_len, new_size); - new_array_prop = js_realloc2(ctx, p->u.array.u.values, sizeof(JSValue) * new_size, &slack); + new_size = max_uint32(new_len, new_size); + new_array_prop = js_realloc(ctx, p->u.array.u.values, sizeof(JSValue) * new_size); if (!new_array_prop) return -1; - new_size += slack / sizeof(*new_array_prop); p->u.array.u.values = new_array_prop; p->u.array.u1.size = new_size; return 0; @@ -21020,7 +22154,7 @@ static int add_fast_array_element(JSContext *ctx, JSObject *p, if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT)) { array_len = JS_VALUE_GET_INT(p->prop[0].u.value); if (new_len > array_len) { - if (unlikely(!(p->shape->prop->flags & JS_PROP_WRITABLE))) { + if (unlikely(!(get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE))) { JS_FreeValue(ctx, val); return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length); } @@ -21038,6 +22172,35 @@ static int add_fast_array_element(JSContext *ctx, JSObject *p, return true; } +/* Allocate a new fast array initialized to JS_UNDEFINED. Its maximum + size is 2^31-1 elements. For convenience, 'len' is a 64 bit + integer. */ +static JSValue js_allocate_fast_array(JSContext *ctx, int64_t len) +{ + JSValue arr; + JSObject *p; + int i; + + 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; + for(i = 0; i < len; i++) + p->u.array.u.values[i] = JS_UNDEFINED; + /* update the 'length' field */ + set_value(ctx, &p->prop[0].u.value, js_int32(len)); + } + return arr; +} + static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc) { JS_FreeValue(ctx, desc->getter); @@ -21063,9 +22226,15 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValueConst obj, JSAtom prop switch(JS_VALUE_GET_TAG(this_obj)) { case JS_TAG_NULL: + if (JS_IsObject(obj)) { + goto primitive_receiver; + } JS_ThrowTypeErrorAtom(ctx, "cannot set property '%s' of null", prop); goto fail; case JS_TAG_UNDEFINED: + if (JS_IsObject(obj)) { + goto primitive_receiver; + } JS_ThrowTypeErrorAtom(ctx, "cannot set property '%s' of undefined", prop); goto fail; case JS_TAG_OBJECT: @@ -21077,6 +22246,8 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValueConst obj, JSAtom prop default: if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) obj = JS_GetPrototypePrimitive(ctx, obj); + + primitive_receiver: p = NULL; p1 = JS_VALUE_GET_OBJ(obj); goto prototype_lookup; @@ -21133,17 +22304,28 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValueConst obj, JSAtom prop if (ret < 0) goto fail; typed_array_oob: - // per spec: evaluate value for side effects - if (p1->class_id == JS_CLASS_BIG_INT64_ARRAY || - p1->class_id == JS_CLASS_BIG_UINT64_ARRAY) { - int64_t v; - if (JS_ToBigInt64Free(ctx, &v, val)) - return -1; + /* [[Set]] (10.4.5.5): an out-of-bounds or non-canonical + integer index only coerces the value (step i, via + TypedArraySetElement) when the receiver is the typed + array itself. With any other receiver the invalid + index is dropped (step ii) without evaluating the + value. p is the receiver object (NULL for a primitive + receiver), p1 is the typed array. */ + if (p == p1) { + // evaluate value for side effects + if (p1->class_id == JS_CLASS_BIG_INT64_ARRAY || + p1->class_id == JS_CLASS_BIG_UINT64_ARRAY) { + int64_t v; + if (JS_ToBigInt64Free(ctx, &v, val)) + return -1; + } else { + val = JS_ToNumberFree(ctx, val); + JS_FreeValue(ctx, val); + if (JS_IsException(val)) + return -1; + } } else { - val = JS_ToNumberFree(ctx, val); JS_FreeValue(ctx, val); - if (JS_IsException(val)) - return -1; } return true; } @@ -21232,12 +22414,11 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValueConst obj, JSAtom prop goto done; } - if (unlikely(!p->extensible)) { - ret = JS_ThrowTypeErrorOrFalse(ctx, flags, "object is not extensible"); - goto done; - } - if (p == JS_VALUE_GET_OBJ(obj)) { + if (unlikely(!p->extensible)) { + ret = JS_ThrowTypeErrorOrFalse(ctx, flags, "object is not extensible"); + goto done; + } if (p->is_exotic) { if (p->class_id == JS_CLASS_ARRAY && p->fast_array && __JS_AtomIsTaggedInt(prop)) { @@ -21278,6 +22459,10 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValueConst obj, JSAtom prop JS_UNDEFINED, JS_UNDEFINED, JS_PROP_HAS_VALUE); } else { + if (unlikely(!p->extensible)) { + ret = JS_ThrowTypeErrorOrFalse(ctx, flags, "object is not extensible"); + goto done; + } generic_create_prop: ret = JS_CreateProperty(ctx, p, prop, val, JS_UNDEFINED, JS_UNDEFINED, flags | @@ -21308,8 +22493,8 @@ int JS_SetProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop, JSValue v } /* flags can be JS_PROP_THROW or JS_PROP_THROW_STRICT */ -static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj, - JSValue prop, JSValue val, int flags) +int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop, JSValue val, int flags) { if (likely(JS_VALUE_GET_TAG(this_obj) == JS_TAG_OBJECT && JS_VALUE_GET_TAG(prop) == JS_TAG_INT)) { @@ -21561,7 +22746,7 @@ static int JS_CreateProperty(JSContext *ctx, JSObject *p, plen = &p->prop[0]; JS_ToUint32(ctx, &len, plen->u.value); if ((idx + 1) > len) { - pslen = p->shape->prop; + pslen = get_shape_prop(p->shape); if (unlikely(!(pslen->flags & JS_PROP_WRITABLE))) return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length); /* XXX: should update the length after defining @@ -21669,9 +22854,9 @@ static int js_shape_prepare_update(JSContext *ctx, JSObject *p, sh = p->shape; if (sh->is_hashed) { - if (sh->header.ref_count != 1) { + if (JS_REF_COUNT(sh) != 1) { if (pprs) - idx = *pprs - sh->prop; + idx = *pprs - get_shape_prop(sh); /* clone the shape (the resulting one is no longer hashed) */ sh = js_clone_shape(ctx, sh); if (!sh) @@ -21679,7 +22864,7 @@ static int js_shape_prepare_update(JSContext *ctx, JSObject *p, js_free_shape(ctx->rt, p->shape); p->shape = sh; if (pprs) - *pprs = &sh->prop[idx]; + *pprs = &get_shape_prop(sh)[idx]; } else { js_shape_hash_unlink(ctx->rt, sh); sh->is_hashed = false; @@ -21869,7 +23054,7 @@ int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj, property is read-only. */ if ((flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) == JS_PROP_HAS_WRITABLE) { - prs = p->shape->prop; + prs = get_shape_prop(p->shape); if (js_update_property_flags(ctx, p, &prs, prs->flags & ~JS_PROP_WRITABLE)) return -1; @@ -22397,24 +23582,15 @@ static inline int JS_SetGlobalVar(JSContext *ctx, JSAtom prop, JSValue val, /* return -1, false or true */ static int JS_DeleteGlobalVar(JSContext *ctx, JSAtom prop) { - JSObject *p; - JSShapeProperty *prs; JSProperty *pr; - int ret; + JSObject *p; /* 9.1.1.4.7 DeleteBinding ( N ) */ p = JS_VALUE_GET_OBJ(ctx->global_var_obj); - prs = find_own_property(&pr, p, prop); - if (prs) + if (find_own_property(&pr, p, prop)) return false; /* lexical variables cannot be deleted */ - ret = JS_HasProperty(ctx, ctx->global_obj, prop); - if (ret < 0) - return -1; - if (ret) { - return JS_DeleteProperty(ctx, ctx->global_obj, prop, 0); - } else { - return true; - } + p = JS_VALUE_GET_OBJ(ctx->global_obj); + return delete_property(ctx, p, prop); } /* return -1, false or true. return false if not configurable or @@ -22562,7 +23738,7 @@ bool JS_IsUncatchableError(JSValueConst val) return p->class_id == JS_CLASS_ERROR && p->is_uncatchable_error; } -static void js_set_uncatchable_error(JSContext *ctx, JSValueConst val, bool flag) +static void js_set_uncatchable_error(JSValueConst val, bool flag) { JSObject *p; if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT) @@ -22574,17 +23750,17 @@ static void js_set_uncatchable_error(JSContext *ctx, JSValueConst val, bool flag void JS_SetUncatchableError(JSContext *ctx, JSValueConst val) { - js_set_uncatchable_error(ctx, val, true); + js_set_uncatchable_error(val, true); } void JS_ClearUncatchableError(JSContext *ctx, JSValueConst val) { - js_set_uncatchable_error(ctx, val, false); + js_set_uncatchable_error(val, false); } void JS_ResetUncatchableError(JSContext *ctx) { - js_set_uncatchable_error(ctx, ctx->rt->current_exception, false); + js_set_uncatchable_error(ctx->rt->current_exception, false); } int JS_SetOpaque(JSValueConst obj, void *opaque) @@ -23156,7 +24332,7 @@ static JSBigInt *js_bigint_new(JSContext *ctx, int len) r = js_malloc(ctx, sizeof(JSBigInt) + len * sizeof(js_limb_t)); if (!r) return NULL; - r->header.ref_count = 1; + JS_REF_COUNT(r) = 1; r->len = len; return r; } @@ -23164,7 +24340,8 @@ static JSBigInt *js_bigint_new(JSContext *ctx, int len) static JSBigInt *js_bigint_set_si(JSBigIntBuf *buf, js_slimb_t a) { JSBigInt *r = (JSBigInt *)buf->big_int_buf; - r->header.ref_count = 0; /* fail safe */ + /* r points into a stack JSBigIntBuf, not an arena block, so it has no + block-header ref_count slot; this temp is never refcounted/freed. */ r->len = 1; r->tab[0] = a; return r; @@ -23173,7 +24350,7 @@ static JSBigInt *js_bigint_set_si(JSBigIntBuf *buf, js_slimb_t a) static JSBigInt *js_bigint_set_si64(JSBigIntBuf *buf, int64_t a) { JSBigInt *r = (JSBigInt *)buf->big_int_buf; - r->header.ref_count = 0; /* fail safe */ + /* stack JSBigIntBuf: no block-header ref_count slot (see js_bigint_set_si) */ if (a >= INT32_MIN && a <= INT32_MAX) { r->len = 1; r->tab[0] = a; @@ -23274,7 +24451,7 @@ static JSBigInt *js_bigint_normalize1(JSContext *ctx, JSBigInt *a, int l) { js_limb_t v; - assert(a->header.ref_count == 1); + assert(JS_REF_COUNT(a) == 1); while (l > 1) { v = a->tab[l - 1]; if ((v != 0 && v != -1) || @@ -25276,10 +26453,10 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p) sh = p->shape; /* the shape can be NULL while freeing an object */ printf("%14p %4d ", (void *)p, - p->header.ref_count); + JS_REF_COUNT(p)); if (sh) { printf("%3d%c %14p ", - sh->header.ref_count, + JS_REF_COUNT(sh), " *"[sh->is_hashed], (void *)sh->proto); } else { @@ -25323,7 +26500,7 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p) if (sh) { printf("{ "); - for(i = 0, prs = sh->prop; i < sh->prop_count; i++, prs++) { + for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) { if (prs->atom != JS_ATOM_NULL) { pr = &p->prop[i]; if (!is_first) @@ -25370,13 +26547,13 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p) static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p) { - if (p->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) { + if (JS_GC_TYPE(p) == JS_GC_OBJ_TYPE_JS_OBJECT) { JS_DumpObject(rt, (JSObject *)p); } else { printf("%14p %4d ", (void *)p, - p->ref_count); - switch(p->gc_obj_type) { + JS_REF_COUNT(p)); + switch(JS_GC_TYPE(p)) { case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE: printf("[function bytecode]"); break; @@ -25393,7 +26570,7 @@ static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p) printf("[js_context]"); break; default: - printf("[unknown %d]", p->gc_obj_type); + printf("[unknown %d]", JS_GC_TYPE(p)); break; } printf("\n"); @@ -26270,6 +27447,11 @@ static no_inline __exception int js_binary_logic_slow(JSContext *ctx, JSBigInt *p1, *p2, *r; JSBigIntBuf buf1, buf2; slow_big_int: + /* buf2 is zero-initialized here (after the label, so it also runs on + the goto slow_big_int paths) to silence a -Wmaybe-uninitialized + false positive: GCC cannot prove buf2 is initialized through the + inlined js_bigint_get_si_sat() -> js_bigint_sign() read below. */ + memset(&buf2, 0, sizeof(buf2)); if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT) p1 = js_bigint_set_short(&buf1, op1); else @@ -27176,8 +28358,12 @@ static void js_mapped_arguments_mark(JSRuntime *rt, JSValueConst val, int i; if (var_refs) { for(i = 0; i < p->u.array.count; i++) { - if (var_refs[i] && var_refs[i]->is_detached) - mark_func(rt, &var_refs[i]->header); + /* mapped arguments hold a counted ref to each var_ref; the + ones that are GC objects (detached, or open capturing a + coroutine local) must be marked, like the other holders */ + JSVarRef *vr = var_refs[i]; + if (vr && (vr->is_detached || vr->is_coro)) + mark_func(rt, &vr->header); } } } @@ -27304,7 +28490,7 @@ static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj) JSShapeProperty *prs; /* check that there are no enumerable normal fields */ sh = p->shape; - for(i = 0, prs = sh->prop; i < sh->prop_count; i++, prs++) { + for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) { if (prs->flags & JS_PROP_ENUMERABLE) goto normal_case; } @@ -27396,7 +28582,7 @@ static __exception int js_for_in_next(JSContext *ctx, JSValue *sp) JSShapeProperty *prs; if (it->idx >= sh->prop_count) goto done; - prs = &sh->prop[it->idx]; + prs = &get_shape_prop(sh)[it->idx]; prop = prs->atom; it->idx++; if (prop == JS_ATOM_NULL || !(prs->flags & JS_PROP_ENUMERABLE)) @@ -27659,6 +28845,63 @@ static JSValue JS_IteratorGetCompleteValue(JSContext *ctx, JSValue obj, return JS_EXCEPTION; } +static JSValue js_sync_dispose_wrapper(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data); + +static __exception int js_op_using_check(JSContext *ctx, JSValueConst val, + int hint, JSValue *pmethod) +{ + JSValue method; + bool is_sync_fallback = false; + + *pmethod = JS_UNDEFINED; + if (JS_IsNull(val) || JS_IsUndefined(val)) + return 0; + if (!JS_IsObject(val)) { + JS_ThrowTypeErrorNotAnObject(ctx); + return -1; + } + if (hint == 1) { + method = JS_GetProperty(ctx, val, JS_ATOM_Symbol_asyncDispose); + if (JS_IsException(method)) + return -1; + if (JS_IsUndefined(method) || JS_IsNull(method)) { + JS_FreeValue(ctx, method); + method = JS_GetProperty(ctx, val, JS_ATOM_Symbol_dispose); + if (JS_IsException(method)) + return -1; + is_sync_fallback = true; + } + } else { + method = JS_GetProperty(ctx, val, JS_ATOM_Symbol_dispose); + if (JS_IsException(method)) + return -1; + } + if (JS_IsUndefined(method) || JS_IsNull(method)) { + JS_ThrowTypeError(ctx, "value is not disposable"); + return -1; + } + if (!JS_IsFunction(ctx, method)) { + JS_FreeValue(ctx, method); + JS_ThrowTypeError(ctx, "dispose method is not a function"); + return -1; + } + if (is_sync_fallback) { + JSValueConst data[1]; + JSValue wrapped; + data[0] = method; + wrapped = JS_NewCFunctionData(ctx, js_sync_dispose_wrapper, 0, 0, + 1, data); + JS_FreeValue(ctx, method); + if (JS_IsException(wrapped)) + return -1; + method = wrapped; + } + *pmethod = method; + return 0; +} + static __exception int js_iterator_get_value_done(JSContext *ctx, JSValue *sp) { JSValue obj, value; @@ -27674,6 +28917,16 @@ static __exception int js_iterator_get_value_done(JSContext *ctx, JSValue *sp) JS_FreeValue(ctx, obj); sp[-1] = value; sp[0] = js_bool(done); + if (done) { + /* Iterator exhausted via {done:true}: drop the iterator object (stack + layout at the for-await `next` call is iter_obj,next,catch_offset,result + so iter_obj is sp[-4]) so the trailing OP_iterator_close skips calling + return(). Mirrors js_for_of_next, which nulls the iterator on done. + Per spec AsyncIteratorClose must NOT run on normal completion. This op + is emitted only by the for-await-of loop, so the layout is fixed. */ + JS_FreeValue(ctx, sp[-4]); + sp[-4] = JS_UNDEFINED; + } return 0; } @@ -27906,8 +29159,9 @@ static JSVarRef *js_create_var_ref(JSContext *ctx, bool is_gc_object) var_ref = js_malloc(ctx, sizeof(JSVarRef)); if (!var_ref) return NULL; - var_ref->header.ref_count = 1; + JS_REF_COUNT(var_ref) = 1; var_ref->is_detached = true; + var_ref->is_coro = false; var_ref->value = JS_UNDEFINED; var_ref->pvalue = &var_ref->value; if (is_gc_object) @@ -27942,7 +29196,7 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx, var_ref = sf->var_refs[var_ref_idx]; if (var_ref) { /* reference to the already created local variable */ - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; return var_ref; } @@ -27950,7 +29204,7 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx, var_ref = js_malloc(ctx, sizeof(JSVarRef)); if (!var_ref) return NULL; - var_ref->header.ref_count = 1; + JS_REF_COUNT(var_ref) = 1; var_ref->is_detached = false; var_ref->is_lexical = false; var_ref->is_const = false; @@ -27958,6 +29212,19 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx, var_ref->stack_frame = sf; sf->var_refs[var_ref_idx] = var_ref; var_ref->pvalue = pvalue; + /* If this local belongs to a coroutine (async function, generator or + async generator), keep the coroutine reachable for as long as a + closure references the variable: make the open var_ref a GC object + holding a counted reference to the coroutine (sf->cur_gc_obj). + Otherwise (ordinary C-stack frame, cur_gc_obj == NULL) the running + function is a GC root and no extra bookkeeping is needed. Snapshot + the decision in is_coro now (cur_gc_obj may become non-NULL later); + the coroutine itself is recovered via stack_frame->cur_gc_obj. */ + var_ref->is_coro = (sf->cur_gc_obj != NULL); + if (sf->cur_gc_obj) { + JS_REF_COUNT(sf->cur_gc_obj)++; + add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF); + } return var_ref; } else { /* Variable is not captured (e.g., from eval closures on uncaptured vars). @@ -27965,8 +29232,9 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx, var_ref = js_malloc(ctx, sizeof(JSVarRef)); if (!var_ref) return NULL; - var_ref->header.ref_count = 1; + JS_REF_COUNT(var_ref) = 1; var_ref->is_detached = true; + var_ref->is_coro = false; var_ref->value = js_dup(*pvalue); var_ref->pvalue = &var_ref->value; add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF); @@ -28007,7 +29275,7 @@ static JSValue js_closure2(JSContext *ctx, JSValue func_obj, case JS_CLOSURE_REF: case JS_CLOSURE_GLOBAL_REF: var_ref = cur_var_refs[cv->var_idx]; - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; break; default: abort(); @@ -28202,11 +29470,27 @@ static int js_op_define_class(JSContext *ctx, JSValue *sp, static void close_var_ref(JSRuntime *rt, JSVarRef *var_ref) { + JSGCObjectHeader *coro; + /* Already closed. This can happen during reentrant coroutine teardown: + closing one var_ref can drop the coroutine's refcount to zero and + re-enter close_var_refs on the same frame. Once detached, the union + holds 'value' rather than stack_frame, so we must not touch it. */ + if (var_ref->is_detached) + return; + /* Read the coroutine (if any) before js_dup() overwrites the union + member that aliases stack_frame with 'value'. */ + coro = var_ref->is_coro ? var_ref->stack_frame->cur_gc_obj : NULL; var_ref->value = js_dup(*var_ref->pvalue); var_ref->pvalue = &var_ref->value; /* the reference is no longer to a local variable */ var_ref->is_detached = true; - add_gc_object(rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF); + var_ref->is_coro = false; + /* an open coroutine var_ref is already a GC object and holds a + reference to its coroutine; drop it now that it is detached. */ + if (coro) + js_release_coro(rt, coro); + else + add_gc_object(rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF); } static void close_var_refs(JSRuntime *rt, JSStackFrame *sf) @@ -28265,6 +29549,7 @@ static JSValue js_call_c_function(JSContext *ctx, JSValueConst func_obj, ctx = p->u.cfunc.realm; /* change the current realm */ sf->is_strict_mode = false; + sf->is_constructor = (flags & JS_CALL_FLAG_CONSTRUCTOR) != 0; sf->cur_func = unsafe_unconst(func_obj); sf->arg_count = argc; arg_buf = argv; @@ -28425,14 +29710,7 @@ static void print_func_name(JSFunctionBytecode *b); static bool needs_backtrace(JSValue exc) { - JSObject *p; - - if (JS_VALUE_GET_TAG(exc) != JS_TAG_OBJECT) - return false; - p = JS_VALUE_GET_OBJ(exc); - if (p->class_id != JS_CLASS_ERROR) - return false; - return !find_own_property1(p, JS_ATOM_stack); + return can_store_error_stack(exc) || can_add_backtrace(exc); } /* argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0. */ @@ -28656,6 +29934,7 @@ DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */ DEF( ret, 1, 1, 0, none) /* used to return from the finally block */ DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */ +DEF( check_object, 1, 1, 1, none) DEF( to_object, 1, 1, 1, none) //DEF( to_string, 1, 1, 1, none) DEF( to_propkey, 1, 1, 1, none) @@ -28678,7 +29957,6 @@ DEF( for_of_start, 1, 1, 3, none) DEF(for_await_of_start, 1, 1, 3, none) DEF( for_in_next, 1, 1, 3, none) DEF( for_of_next, 2, 3, 5, u8) -DEF(iterator_check_object, 1, 1, 1, none) DEF(iterator_get_value_done, 1, 1, 2, none) DEF( iterator_close, 1, 3, 0, none) DEF( iterator_next, 1, 4, 4, none) @@ -28732,6 +30010,12 @@ DEF( strict_neq, 1, 2, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none) DEF( private_in, 1, 2, 1, none) DEF(push_bigint_i32, 5, 0, 1, i32) +DEF( using_dispose_init, 1, 0, 1, none) +DEF( using_dispose, 3, 1, 1, loc) +DEF(using_dispose_async, 3, 0, 1, loc) +DEF(using_dispose_merge, 1, 2, 1, none) +DEF( using_dispose_end, 1, 1, 0, none) +DEF( using_check, 2, 1, 2, u8) /* must be the last non short and non temporary opcode */ DEF( nop, 1, 0, 0, none) @@ -28757,6 +30041,8 @@ def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ +def( dispose_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */ + def( source_loc, 9, 0, 0, u32x2) /* emitted in phase 1, removed in phase 3 */ DEF( push_minus1, 1, 0, 1, none_int) @@ -28899,6 +30185,7 @@ DEF( typeof_is_function, 1, 1, 1, none) return JS_ThrowStackOverflow(caller_ctx); sf->is_strict_mode = b->is_strict_mode; + sf->is_constructor = (flags & JS_CALL_FLAG_CONSTRUCTOR) != 0; arg_buf = (JSValue *)argv; sf->arg_count = argc; sf->cur_func = unsafe_unconst(func_obj); @@ -28926,6 +30213,8 @@ DEF( typeof_is_function, 1, 1, 1, none) sf->var_ref_count = b->var_ref_count; for(i = 0; i < b->var_ref_count; i++) sf->var_refs[i] = NULL; + /* ordinary C-stack frame: not owned by a coroutine GC object */ + sf->cur_gc_obj = NULL; sp = stack_buf; pc = b->byte_code_buf; /* sf->cur_pc must we set to pc before any recursive calls to JS_CallInternal. */ @@ -29891,7 +31180,7 @@ DEF( typeof_is_function, 1, 1, 1, none) goto exception; if (opcode == OP_make_var_ref_ref) { var_ref = var_refs[idx]; - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; } else { var_ref = get_var_ref(ctx, sf, idx, opcode == OP_make_arg_ref); if (!var_ref) @@ -30092,9 +31381,9 @@ DEF( typeof_is_function, 1, 1, 1, none) goto exception; sp += 1; BREAK; - CASE(OP_iterator_check_object): + CASE(OP_check_object): if (unlikely(!JS_IsObject(sp[-1]))) { - JS_ThrowTypeError(ctx, "iterator must return an object"); + JS_ThrowTypeErrorNotAnObject(ctx); goto exception; } BREAK; @@ -30130,6 +31419,124 @@ DEF( typeof_is_function, 1, 1, 1, none) } BREAK; + CASE(OP_using_dispose_init): + sp[0] = JS_UNINITIALIZED; + sp++; + BREAK; + + CASE(OP_using_dispose): + { + int idx; + JSValueConst val, method; + JSValue ret, error_state; + + idx = get_u16(pc); + pc += 2; + val = var_buf[idx]; + method = var_buf[idx + 1]; + error_state = sp[-1]; + + if (JS_IsNull(val) || JS_IsUndefined(val) || + JS_IsUninitialized(val)) { + /* null/undefined (spec-permitted) or uninitialized + (declaration threw before assignment). */ + BREAK; + } + sf->cur_pc = pc; + ret = JS_Call(ctx, method, val, 0, NULL); + if (JS_IsException(ret)) { + JSValue new_error = JS_GetException(ctx); + if (!JS_IsUninitialized(error_state)) { + JSValue se; + se = js_new_suppressed_error(ctx, new_error, + error_state); + JS_FreeValue(ctx, new_error); + JS_FreeValue(ctx, error_state); + if (JS_IsException(se)) { + sp[-1] = JS_GetException(ctx); + } else { + sp[-1] = se; + } + } else { + sp[-1] = new_error; + } + } else { + JS_FreeValue(ctx, ret); + } + } + BREAK; + + CASE(OP_using_dispose_async): + { + int idx; + JSValueConst val, method; + JSValue ret; + + idx = get_u16(pc); + pc += 2; + val = var_buf[idx]; + method = var_buf[idx + 1]; + + if (JS_IsNull(val) || JS_IsUndefined(val) || + JS_IsUninitialized(val)) { + sp[0] = JS_UNDEFINED; + sp++; + BREAK; + } + sf->cur_pc = pc; + ret = JS_Call(ctx, method, val, 0, NULL); + if (JS_IsException(ret)) + goto exception; + sp[0] = ret; + sp++; + } + BREAK; + + CASE(OP_using_dispose_merge): + { + JSValue new_error = sp[-1]; + JSValue error_state = sp[-2]; + sp--; + if (!JS_IsUninitialized(error_state)) { + JSValue se = js_new_suppressed_error(ctx, new_error, + error_state); + JS_FreeValue(ctx, new_error); + JS_FreeValue(ctx, error_state); + if (JS_IsException(se)) { + sp[-1] = JS_GetException(ctx); + } else { + sp[-1] = se; + } + } else { + sp[-1] = new_error; + } + } + BREAK; + + CASE(OP_using_dispose_end): + { + JSValue error_state = sp[-1]; + sp--; + if (!JS_IsUninitialized(error_state)) { + JS_Throw(ctx, error_state); + goto exception; + } + } + BREAK; + + CASE(OP_using_check): + { + int hint = pc[0]; + JSValue method; + pc += 1; + sf->cur_pc = pc; + if (js_op_using_check(ctx, sp[-1], hint, &method)) + goto exception; + sp[0] = method; + sp++; + } + BREAK; + CASE(OP_iterator_next): /* stack: iter_obj next catch_offset val */ { @@ -30509,6 +31916,26 @@ DEF( typeof_is_function, 1, 1, 1, none) { JSValue val; + /* fast path: regular/typed array element by int index */ + if (likely(JS_VALUE_GET_TAG(sp[-2]) == JS_TAG_OBJECT && + JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_INT)) { + JSObject *p = JS_VALUE_GET_OBJ(sp[-2]); + uint32_t idx = JS_VALUE_GET_INT(sp[-1]); + if (likely(p->class_id == JS_CLASS_ARRAY && + idx < p->u.array.count)) { + val = js_dup(p->u.array.u.values[idx]); + JS_FreeValue(ctx, sp[-2]); + sp[-2] = val; + sp--; + BREAK; + } + if (js_get_fast_array_element(ctx, p, idx, &val)) { + JS_FreeValue(ctx, sp[-2]); + sp[-2] = val; + sp--; + BREAK; + } + } sf->cur_pc = pc; val = JS_GetPropertyValue(ctx, sp[-2], sp[-1]); JS_FreeValue(ctx, sp[-2]); @@ -30523,6 +31950,21 @@ DEF( typeof_is_function, 1, 1, 1, none) { JSValue val; + /* fast path: regular/typed array element by int index */ + if (likely(JS_VALUE_GET_TAG(sp[-2]) == JS_TAG_OBJECT && + JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_INT)) { + JSObject *p = JS_VALUE_GET_OBJ(sp[-2]); + uint32_t idx = JS_VALUE_GET_INT(sp[-1]); + if (likely(p->class_id == JS_CLASS_ARRAY && + idx < p->u.array.count)) { + sp[-1] = js_dup(p->u.array.u.values[idx]); + BREAK; + } + if (js_get_fast_array_element(ctx, p, idx, &val)) { + sp[-1] = val; + BREAK; + } + } sf->cur_pc = pc; val = JS_GetPropertyValue(ctx, sp[-2], sp[-1]); sp[-1] = val; @@ -30734,7 +32176,19 @@ DEF( typeof_is_function, 1, 1, 1, none) JS_VALUE_GET_FLOAT64(op2)); JS_X87_FPCW_RESTORE(fpcw); sp--; + } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) || + JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { + /* mixed int/float; the BOTH_FLOAT fast path above is unchanged */ + double d1, d2; + if (!js_arith_to_float64(op1, &d1) || + !js_arith_to_float64(op2, &d2)) + goto add_slow_case; + JS_X87_FPCW_SAVE_AND_ADJUST(fpcw); + sp[-2] = js_float64(d1 + d2); + JS_X87_FPCW_RESTORE(fpcw); + sp--; } else { + add_slow_case: sf->cur_pc = pc; if (js_add_slow(ctx, sp)) goto exception; @@ -30804,6 +32258,17 @@ DEF( typeof_is_function, 1, 1, 1, none) JS_VALUE_GET_FLOAT64(op2)); JS_X87_FPCW_RESTORE(fpcw); sp--; + } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) || + JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { + /* mixed int/float; the BOTH_FLOAT fast path above is unchanged */ + double d1, d2; + if (!js_arith_to_float64(op1, &d1) || + !js_arith_to_float64(op2, &d2)) + goto binary_arith_slow; + JS_X87_FPCW_SAVE_AND_ADJUST(fpcw); + sp[-2] = js_float64(d1 - d2); + JS_X87_FPCW_RESTORE(fpcw); + sp--; } else { goto binary_arith_slow; } @@ -30839,6 +32304,17 @@ DEF( typeof_is_function, 1, 1, 1, none) mul_fp_res: sp[-2] = js_float64(d); sp--; + } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) || + JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { + /* mixed int/float; the BOTH_FLOAT fast path above is unchanged */ + double d1, d2; + if (!js_arith_to_float64(op1, &d1) || + !js_arith_to_float64(op2, &d2)) + goto binary_arith_slow; + JS_X87_FPCW_SAVE_AND_ADJUST(fpcw); + d = d1 * d2; + JS_X87_FPCW_RESTORE(fpcw); + goto mul_fp_res; } else { goto binary_arith_slow; } @@ -30855,6 +32331,23 @@ DEF( typeof_is_function, 1, 1, 1, none) v2 = JS_VALUE_GET_INT(op2); sp[-2] = js_number((double)v1 / (double)v2); sp--; + } else if (JS_VALUE_IS_BOTH_FLOAT(op1, op2)) { + JS_X87_FPCW_SAVE_AND_ADJUST(fpcw); + sp[-2] = js_number(JS_VALUE_GET_FLOAT64(op1) / + JS_VALUE_GET_FLOAT64(op2)); + JS_X87_FPCW_RESTORE(fpcw); + sp--; + } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) || + JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) { + /* mixed int/float; the BOTH_FLOAT fast path above is unchanged */ + double d1, d2; + if (!js_arith_to_float64(op1, &d1) || + !js_arith_to_float64(op2, &d2)) + goto binary_arith_slow; + JS_X87_FPCW_SAVE_AND_ADJUST(fpcw); + sp[-2] = js_number(d1 / d2); + JS_X87_FPCW_RESTORE(fpcw); + sp--; } else { goto binary_arith_slow; } @@ -31340,9 +32833,21 @@ DEF( typeof_is_function, 1, 1, 1, none) } switch (opcode) { case OP_with_get_var: - val = JS_GetProperty(ctx, obj, atom); - if (unlikely(JS_IsException(val))) + ret = JS_HasProperty(ctx, obj, atom); + if (unlikely(ret < 0)) goto exception; + + if (ret == 0) { + if (is_strict_mode(ctx)) { + JS_ThrowReferenceErrorNotDefined(ctx, atom); + goto exception; + } + val = JS_UNDEFINED; + } else { + val = JS_GetProperty(ctx, obj, atom); + if (unlikely(JS_IsException(val))) + goto exception; + } set_value(ctx, &sp[-1], val); break; case OP_with_put_var: @@ -31367,9 +32872,20 @@ DEF( typeof_is_function, 1, 1, 1, none) break; case OP_with_get_ref: /* produce a pair object/method on the stack */ - val = JS_GetProperty(ctx, obj, atom); - if (unlikely(JS_IsException(val))) + ret = JS_HasProperty(ctx, obj, atom); + if (unlikely(ret < 0)) goto exception; + if (ret == 0) { + if (is_strict_mode(ctx)) { + JS_ThrowReferenceErrorNotDefined(ctx, atom); + goto exception; + } + val = JS_UNDEFINED; + } else { + val = JS_GetProperty(ctx, obj, atom); + if (unlikely(JS_IsException(val))) + goto exception; + } *sp++ = val; break; case OP_with_get_ref_undef: @@ -31702,6 +33218,7 @@ static __exception int async_func_init(JSContext *ctx, JSAsyncFunctionState *s, p = JS_VALUE_GET_OBJ(func_obj); b = p->u.func.function_bytecode; sf->is_strict_mode = b->is_strict_mode; + sf->is_constructor = false; sf->cur_pc = b->byte_code_buf; arg_buf_len = max_int(b->arg_count, argc); local_count = arg_buf_len + b->var_count + b->stack_size; @@ -31716,6 +33233,8 @@ static __exception int async_func_init(JSContext *ctx, JSAsyncFunctionState *s, sf->arg_count = arg_buf_len; sf->var_buf = sf->arg_buf + arg_buf_len; sf->cur_sp = sf->var_buf + b->var_count; + /* set by the caller once the owning coroutine GC object exists */ + sf->cur_gc_obj = NULL; sf->var_refs = (JSVarRef **)(sf->cur_sp + b->stack_size); sf->var_ref_count = b->var_ref_count; for(i = 0; i < b->var_ref_count; i++) @@ -31953,6 +33472,10 @@ static JSValue js_call_generator_function(JSContext *ctx, JSValueConst func_obj, if (JS_IsException(obj)) goto fail; JS_SetOpaqueInternal(obj, s); + /* the body only starts running on the first next(); root captured + locals against the generator object from now on (the initial resume + above only reaches OP_initial_yield, before any user code) */ + s->func_state.frame.cur_gc_obj = &JS_VALUE_GET_OBJ(obj)->header; return obj; fail: free_generator_stack_rt(ctx->rt, s); @@ -31981,7 +33504,7 @@ static void js_async_function_free0(JSRuntime *rt, JSAsyncFunctionData *s) static void js_async_function_free(JSRuntime *rt, JSAsyncFunctionData *s) { - if (--s->header.ref_count == 0) { + if (--JS_REF_COUNT(s) == 0) { js_async_function_free0(rt, s); } } @@ -32020,10 +33543,12 @@ static int js_async_function_resolve_create(JSContext *ctx, if (JS_IsException(resolving_funcs[i])) { if (i == 1) JS_FreeValue(ctx, resolving_funcs[0]); + resolving_funcs[0] = JS_UNDEFINED; + resolving_funcs[1] = JS_UNDEFINED; return -1; } p = JS_VALUE_GET_OBJ(resolving_funcs[i]); - s->header.ref_count++; + JS_REF_COUNT(s)++; p->u.async_function_data = s; } return 0; @@ -32135,7 +33660,7 @@ static JSValue js_async_function_call(JSContext *ctx, JSValueConst func_obj, s = js_mallocz(ctx, sizeof(*s)); if (!s) return JS_EXCEPTION; - s->header.ref_count = 1; + JS_REF_COUNT(s) = 1; add_gc_object(ctx->rt, &s->header, JS_GC_OBJ_TYPE_ASYNC_FUNCTION); s->is_active = false; s->resolving_funcs[0] = JS_UNDEFINED; @@ -32152,6 +33677,9 @@ static JSValue js_async_function_call(JSContext *ctx, JSValueConst func_obj, return JS_EXCEPTION; } s->is_active = true; + /* the body runs immediately (up to the first await), so the frame must + already know its owning coroutine to root captured locals */ + s->func_state.frame.cur_gc_obj = &s->header; if (!js_async_function_resume(ctx, s)) goto fail; @@ -32259,6 +33787,8 @@ static int js_async_generator_resolve_function_create(JSContext *ctx, if (JS_IsException(func)) { if (i == 1) JS_FreeValue(ctx, resolving_funcs[0]); + resolving_funcs[0] = JS_UNDEFINED; + resolving_funcs[1] = JS_UNDEFINED; return -1; } resolving_funcs[i] = func; @@ -32598,6 +34128,9 @@ static JSValue js_async_generator_function_call(JSContext *ctx, if (JS_IsException(obj)) goto fail; s->generator = JS_VALUE_GET_OBJ(obj); + /* root captured locals against the async generator object (the initial + resume above only reaches OP_initial_yield, before any user code) */ + s->func_state.frame.cur_gc_obj = &s->generator->header; JS_SetOpaqueInternal(obj, s); return obj; fail: @@ -32690,6 +34223,7 @@ enum { TOK_EXTENDS, TOK_IMPORT, TOK_SUPER, + TOK_USING, /* FutureReservedWords when parsing strict mode code */ TOK_IMPLEMENTS, TOK_INTERFACE, @@ -32723,7 +34257,10 @@ typedef struct BlockEnv { int label_finally; /* -1 if none */ int scope_level; uint8_t has_iterator : 1; + uint8_t is_async_iterator : 1; uint8_t is_regular_stmt : 1; // i.e. not a loop statement + uint8_t has_using : 1; /* scope has using declarations needing disposal */ + int using_scope_level; /* scope level for OP_dispose_scope (-1 if none) */ } BlockEnv; typedef struct JSGlobalVar { @@ -32906,6 +34443,7 @@ typedef struct JSToken { int line_num; /* line number of token start */ int col_num; /* column number of token start */ const uint8_t *ptr; + const uint8_t *line_start; /* first character of the line of token start */ union { struct { JSValue str; @@ -32939,6 +34477,7 @@ typedef struct JSParseState { const uint8_t *buf_start; const uint8_t *buf_ptr; const uint8_t *buf_end; + const uint8_t *line_start; /* first character of the current line */ const uint8_t *eol; // most recently seen end-of-line character const uint8_t *mark; // first token character, invariant: eol < mark @@ -33156,6 +34695,7 @@ DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */ DEF( ret, 1, 1, 0, none) /* used to return from the finally block */ DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */ +DEF( check_object, 1, 1, 1, none) DEF( to_object, 1, 1, 1, none) //DEF( to_string, 1, 1, 1, none) DEF( to_propkey, 1, 1, 1, none) @@ -33178,7 +34718,6 @@ DEF( for_of_start, 1, 1, 3, none) DEF(for_await_of_start, 1, 1, 3, none) DEF( for_in_next, 1, 1, 3, none) DEF( for_of_next, 2, 3, 5, u8) -DEF(iterator_check_object, 1, 1, 1, none) DEF(iterator_get_value_done, 1, 1, 2, none) DEF( iterator_close, 1, 3, 0, none) DEF( iterator_next, 1, 4, 4, none) @@ -33232,6 +34771,12 @@ DEF( strict_neq, 1, 2, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none) DEF( private_in, 1, 2, 1, none) DEF(push_bigint_i32, 5, 0, 1, i32) +DEF( using_dispose_init, 1, 0, 1, none) +DEF( using_dispose, 3, 1, 1, loc) +DEF(using_dispose_async, 3, 0, 1, loc) +DEF(using_dispose_merge, 1, 2, 1, none) +DEF( using_dispose_end, 1, 1, 0, none) +DEF( using_check, 2, 1, 2, u8) /* must be the last non short and non temporary opcode */ DEF( nop, 1, 0, 0, none) @@ -33257,6 +34802,8 @@ def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ +def( dispose_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */ + def( source_loc, 9, 0, 0, u32x2) /* emitted in phase 1, removed in phase 3 */ DEF( push_minus1, 1, 0, 1, none_int) @@ -33465,8 +35012,13 @@ int JS_PRINTF_FORMAT_ATTR(2, 3) js_parse_error(JSParseState *s, JS_PRINTF_FORMAT backtrace_flags = 0; if (s->cur_func && s->cur_func->backtrace_barrier) backtrace_flags = JS_BACKTRACE_FLAG_SINGLE_LEVEL; + /* s->col_num is not advanced during token scanning, so derive the column + as the 1-based offset of the token from the start of its line. */ + int err_col_num = s->token.col_num; + if (s->token.ptr && s->token.ptr >= s->token.line_start) + err_col_num = (int)(s->token.ptr - s->token.line_start) + 1; build_backtrace(ctx, ctx->rt->current_exception, JS_UNDEFINED, s->filename, - s->line_num, s->col_num, backtrace_flags); + s->token.line_num, err_col_num, backtrace_flags); return -1; } @@ -33564,6 +35116,7 @@ static __exception int js_parse_template_part(JSParseState *s, } if (c == '\n') { s->line_num++; + s->line_start = p; s->eol = &p[-1]; s->mark = p; } else if (c >= 0x80) { @@ -33657,6 +35210,7 @@ static __exception int js_parse_string(JSParseState *s, int sep, p++; if (sep != '`') { s->line_num++; + s->line_start = p; s->eol = &p[-1]; s->mark = p; } @@ -33869,6 +35423,12 @@ static __exception int ident_realloc(JSContext *ctx, char **pbuf, size_t *psize, /* convert a TOK_IDENT to a keyword when needed */ static void update_token_ident(JSParseState *s) { + /* `using` is contextually reserved, not a true keyword. Leave it as + TOK_IDENT so it can be used as a regular identifier in expressions. + Using declarations are detected explicitly at statement and + for-loop head parsing via token_is_pseudo_keyword. */ + if (s->token.u.ident.atom == JS_ATOM_using) + return; if (s->token.u.ident.atom <= JS_ATOM_LAST_KEYWORD || (s->token.u.ident.atom <= JS_ATOM_LAST_STRICT_KEYWORD && s->cur_func->is_strict_mode) || @@ -33978,6 +35538,7 @@ static __exception int next_token(JSParseState *s) s->token.line_num = s->line_num; s->token.col_num = s->col_num; s->token.ptr = p; + s->token.line_start = s->line_start; c = *p; switch(c) { case 0: @@ -34005,6 +35566,7 @@ static __exception int next_token(JSParseState *s) case '\n': p++; line_terminator: + s->line_start = p; s->eol = &p[-1]; s->mark = p; s->got_lf = true; @@ -34033,10 +35595,12 @@ static __exception int next_token(JSParseState *s) s->line_num++; s->got_lf = true; /* considered as LF for ASI */ s->eol = p++; + s->line_start = p; s->mark = p; } else if (*p == '\r') { s->got_lf = true; /* considered as LF for ASI */ p++; + s->line_start = p; } else if (*p >= 0x80) { c = utf8_decode(p, &p); /* ignore invalid UTF-8 in comments */ @@ -34187,6 +35751,7 @@ static __exception int next_token(JSParseState *s) if (JS_VALUE_IS_NAN(ret) || lre_js_is_ident_next(utf8_decode(p, &p1))) { JS_FreeValue(s->ctx, ret); + s->col_num = max_int(1, s->mark - s->eol); js_parse_error(s, "invalid number literal"); goto fail; } @@ -34623,6 +36188,7 @@ static __exception int json_next_token(JSParseState *s) s->token.line_num = s->line_num; s->token.col_num = s->col_num; s->token.ptr = p; + s->token.line_start = s->line_start; c = *p; switch(c) { case 0: @@ -34648,6 +36214,7 @@ static __exception int json_next_token(JSParseState *s) case '\n': s->line_num++; s->eol = p++; + s->line_start = p; s->mark = p; goto redo; case '\f': @@ -34812,6 +36379,9 @@ static int simple_next_token(const uint8_t **pp, bool no_line_terminator) } else if (c == 'a' && p[0] == 'w' && p[1] == 'a' && p[2] == 'i' && p[3] == 't' && !lre_js_is_ident_next(p[4])) { return TOK_AWAIT; + } else if (c == 'u' && p[0] == 's' && p[1] == 'i' && + p[2] == 'n' && p[3] == 'g' && !lre_js_is_ident_next(p[4])) { + return TOK_USING; } return TOK_IDENT; } @@ -34891,14 +36461,19 @@ static void emit_u32(JSParseState *s, uint32_t val) dbuf_put_u32(&s->cur_func->byte_code, val); } -static void emit_source_loc(JSParseState *s) +static void emit_source_loc_at(JSParseState *s, int line_num, int col_num) { JSFunctionDef *fd = s->cur_func; DynBuf *bc = &fd->byte_code; dbuf_putc(bc, OP_source_loc); - dbuf_put_u32(bc, s->token.line_num); - dbuf_put_u32(bc, s->token.col_num); + dbuf_put_u32(bc, line_num); + dbuf_put_u32(bc, col_num); +} + +static void emit_source_loc(JSParseState *s) +{ + emit_source_loc_at(s, s->token.line_num, s->token.col_num); } static void emit_op(JSParseState *s, uint8_t val) @@ -35119,7 +36694,7 @@ static int find_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name) if (i == -1) goto not_found; vd = &fd->vars[i]; - if (fd->vars[i].scope_level == 0) + if (vd->scope_level == 0) return i; } for(i = fd->var_count; i-- > 0;) { @@ -35224,27 +36799,29 @@ static int push_scope(JSParseState *s) { /* XXX: should check for scope overflow */ if ((fd->scope_count + 1) > fd->scope_size) { int new_size; - size_t slack; JSVarScope *new_buf; /* XXX: potential arithmetic overflow */ new_size = max_int(fd->scope_count + 1, fd->scope_size * 3 / 2); if (fd->scopes == fd->def_scope_array) { - new_buf = js_realloc2(s->ctx, NULL, new_size * sizeof(*fd->scopes), &slack); + new_buf = js_realloc(s->ctx, NULL, new_size * sizeof(*fd->scopes)); if (!new_buf) return -1; memcpy(new_buf, fd->scopes, fd->scope_count * sizeof(*fd->scopes)); } else { - new_buf = js_realloc2(s->ctx, fd->scopes, new_size * sizeof(*fd->scopes), &slack); + new_buf = js_realloc(s->ctx, fd->scopes, new_size * sizeof(*fd->scopes)); if (!new_buf) return -1; } - new_size += slack / sizeof(*new_buf); fd->scopes = new_buf; fd->scope_size = new_size; } fd->scope_count++; fd->scopes[scope].parent = fd->scope_level; fd->scopes[scope].first = fd->scope_first; + fd->scopes[scope].has_using = 0; + fd->scopes[scope].is_await_using = 0; + fd->scopes[scope].using_label_catch = -1; + fd->scopes[scope].using_label_end = -1; emit_op(s, OP_enter_scope); emit_u16(s, scope); return fd->scope_level = scope; @@ -35418,6 +36995,7 @@ typedef enum { JS_VAR_DEF_NEW_FUNCTION_DECL, /* async/generator function declaration */ JS_VAR_DEF_CATCH, JS_VAR_DEF_VAR, + JS_VAR_DEF_USING, } JSVarDefEnum; static int define_var(JSParseState *s, JSFunctionDef *fd, JSAtom name, @@ -35434,6 +37012,7 @@ static int define_var(JSParseState *s, JSFunctionDef *fd, JSAtom name, case JS_VAR_DEF_LET: case JS_VAR_DEF_CONST: + case JS_VAR_DEF_USING: case JS_VAR_DEF_FUNCTION_DECL: case JS_VAR_DEF_NEW_FUNCTION_DECL: idx = find_lexical_decl(ctx, fd, name, fd->scope_first, true); @@ -35480,9 +37059,10 @@ static int define_var(JSParseState *s, JSFunctionDef *fd, JSAtom name, } if (fd->is_eval && - (fd->eval_type == JS_EVAL_TYPE_GLOBAL || - fd->eval_type == JS_EVAL_TYPE_MODULE) && - fd->scope_level == fd->body_scope) { + (fd->eval_type == JS_EVAL_TYPE_GLOBAL || + fd->eval_type == JS_EVAL_TYPE_MODULE) && + fd->scope_level == fd->body_scope && + var_def_type != JS_VAR_DEF_USING) { JSGlobalVar *hf; hf = add_global_var(s->ctx, fd, name); if (!hf) @@ -35496,13 +37076,16 @@ static int define_var(JSParseState *s, JSFunctionDef *fd, JSAtom name, var_kind = JS_VAR_FUNCTION_DECL; else if (var_def_type == JS_VAR_DEF_NEW_FUNCTION_DECL) var_kind = JS_VAR_NEW_FUNCTION_DECL; + else if (var_def_type == JS_VAR_DEF_USING) + var_kind = JS_VAR_USING; else var_kind = JS_VAR_NORMAL; idx = add_scope_var(ctx, fd, name, var_kind); if (idx >= 0) { vd = &fd->vars[idx]; vd->is_lexical = 1; - vd->is_const = (var_def_type == JS_VAR_DEF_CONST); + vd->is_const = (var_def_type == JS_VAR_DEF_CONST || + var_def_type == JS_VAR_DEF_USING); } } break; @@ -35679,8 +37262,8 @@ static __exception int js_parse_template(JSParseState *s, int call, int *argc) if (depth == 0) { if (s->token.u.str.sep == '`') goto done1; - emit_op(s, OP_get_field2); - emit_atom(s, JS_ATOM_concat); + } else { + emit_op(s, OP_add); } depth++; } else { @@ -35693,11 +37276,17 @@ static __exception int js_parse_template(JSParseState *s, int call, int *argc) return -1; if (js_parse_expr(s)) return -1; + if (!call) { + /* ToString right away, before the next substitution is evaluated + (ECMA-262 13.2.8.6). OP_to_propkey keeps a symbol, which + OP_add then rejects, as ToString does. */ + emit_op(s, OP_to_propkey); + emit_op(s, OP_add); + } depth++; if (s->token.val != '}') { return js_parse_error(s, "expected '}' after template expression"); } - /* XXX: should convert to string at this stage? */ free_token(s, &s->token); /* Resume TOK_TEMPLATE parsing (s->token.line_num and * s->token.ptr are OK) */ @@ -35715,9 +37304,6 @@ static __exception int js_parse_template(JSParseState *s, int call, int *argc) seal_template_obj(ctx, raw_array); seal_template_obj(ctx, template_object); *argc = depth + 1; - } else { - emit_op(s, OP_call_method); - emit_u16(s, depth - 1); } done1: return next_token(s); @@ -35864,6 +37450,7 @@ typedef struct JSParsePos { int col_num; bool got_lf; const uint8_t *ptr; + const uint8_t *line_start; const uint8_t *eol; const uint8_t *mark; } JSParsePos; @@ -35875,6 +37462,7 @@ static int js_parse_get_pos(JSParseState *s, JSParsePos *sp) sp->line_num = s->token.line_num; sp->col_num = s->token.col_num; sp->ptr = s->token.ptr; + sp->line_start = s->line_start; sp->eol = s->eol; sp->mark = s->mark; sp->got_lf = s->got_lf; @@ -35888,6 +37476,7 @@ static __exception int js_parse_seek_token(JSParseState *s, const JSParsePos *sp s->line_num = sp->line_num; s->col_num = sp->col_num; s->buf_ptr = sp->ptr; + s->line_start = sp->line_start; s->eol = sp->eol; s->mark = sp->mark; s->got_lf = sp->got_lf; @@ -36215,6 +37804,7 @@ static __exception int js_parse_object_literal(JSParseState *s) #define PF_POW_ALLOWED (1 << 2) /* forbid the exponentiation operator in js_parse_unary() */ #define PF_POW_FORBIDDEN (1 << 3) +#define PF_AWAIT_USING (1 << 4) static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags); @@ -37166,11 +38756,7 @@ static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope, opcode = OP_get_ref_value; } break; - case OP_get_array_el: - emit_op(s, OP_to_propkey2); - break; - case OP_get_super_value: - emit_op(s, OP_to_propkey); + default: break; } } @@ -37367,6 +38953,9 @@ static __exception int js_define_var(JSParseState *s, JSAtom name, int tok) case TOK_VAR: var_def_type = JS_VAR_DEF_VAR; break; + case TOK_USING: + var_def_type = JS_VAR_DEF_USING; + break; case TOK_CATCH: var_def_type = JS_VAR_DEF_CATCH; break; @@ -37743,8 +39332,11 @@ static int js_parse_destructuring_element(JSParseState *s, int tok, } else if (s->token.val == '[') { bool has_spread; int enum_depth; + int source_line_num, source_col_num; BlockEnv block_env; + source_line_num = s->token.line_num; + source_col_num = s->token.col_num; if (next_token(s)) return -1; /* the block environment is only needed in generators in case @@ -37792,6 +39384,11 @@ static int js_parse_destructuring_element(JSParseState *s, int tok, goto var_error; if (js_define_var(s, var_name, tok)) goto var_error; + if (export_flag) { + if (!add_export_entry(s, s->cur_func->module, var_name, var_name, + JS_EXPORT_TYPE_LOCAL)) + goto var_error; + } opcode = OP_scope_get_var; scope = s->cur_func->scope_level; } else { @@ -37840,6 +39437,7 @@ static int js_parse_destructuring_element(JSParseState *s, int tok, } /* close iterator object: if completed, enum_obj has been replaced by undefined */ + emit_source_loc_at(s, source_line_num, source_col_num); emit_op(s, OP_iterator_close); pop_break_entry(s->cur_func); if (next_token(s)) @@ -37911,6 +39509,7 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) { FuncCallType call_type; int optional_chaining_label; + int import_line_num, import_col_num; bool accept_lparen = (parse_flags & PF_POSTFIX_CALL) != 0; call_type = FUNC_CALL_NORMAL; @@ -38034,6 +39633,9 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) case TOK_IDENT: { JSAtom name; + int identifier_line_num = s->token.line_num; + int identifier_col_num = + (int)(s->token.ptr - s->line_start) + 1; if (s->token.u.ident.is_reserved) { return js_parse_error_reserved_identifier(s); } @@ -38071,6 +39673,7 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) return -1; } do_get_var: + emit_source_loc_at(s, identifier_line_num, identifier_col_num); emit_op(s, OP_scope_get_var); emit_u32(s, name); emit_u16(s, s->cur_func->scope_level); @@ -38147,6 +39750,8 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) } break; case TOK_IMPORT: + import_line_num = s->token.line_num; + import_col_num = s->token.col_num; if (next_token(s)) return -1; if (s->token.val == '.') { @@ -38186,6 +39791,7 @@ static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags) } if (js_parse_expect(s, ')')) return -1; + emit_source_loc_at(s, import_line_num, import_col_num); emit_op(s, OP_import); } break; @@ -38652,6 +40258,9 @@ static __exception int js_parse_delete(JSParseState *s) return 0; } +static __exception int js_parse_var(JSParseState *s, int parse_flags, int tok, + bool export_flag); + /* allowed parse_flags: PF_POW_ALLOWED, PF_POW_FORBIDDEN */ static __exception int js_parse_unary(JSParseState *s, int parse_flags) { @@ -38738,8 +40347,8 @@ static __exception int js_parse_unary(JSParseState *s, int parse_flags) return -1; if (js_parse_unary(s, PF_POW_FORBIDDEN)) return -1; - s->cur_func->has_await = true; emit_op(s, OP_await); + s->cur_func->has_await = true; parse_flags = 0; break; default: @@ -39095,7 +40704,7 @@ static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags) emit_op(s, OP_iterator_next); if (is_async) emit_op(s, OP_await); - emit_op(s, OP_iterator_check_object); + emit_op(s, OP_check_object); emit_op(s, OP_get_field2); emit_atom(s, JS_ATOM_done); label_next = emit_goto(s, OP_if_true, -1); /* end of loop */ @@ -39128,7 +40737,7 @@ static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags) label_return1 = emit_goto(s, OP_if_true, -1); if (is_async) emit_op(s, OP_await); - emit_op(s, OP_iterator_check_object); + emit_op(s, OP_check_object); emit_op(s, OP_get_field2); emit_atom(s, JS_ATOM_done); emit_goto(s, OP_if_false, label_yield); @@ -39149,7 +40758,7 @@ static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags) label_throw1 = emit_goto(s, OP_if_true, -1); if (is_async) emit_op(s, OP_await); - emit_op(s, OP_iterator_check_object); + emit_op(s, OP_check_object); emit_op(s, OP_get_field2); emit_atom(s, JS_ATOM_done); emit_goto(s, OP_if_false, label_yield); @@ -39243,40 +40852,11 @@ static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags) if (get_lvalue(s, &opcode, &scope, &name, &label, NULL, (op != '='), op) < 0) return -1; - // comply with rather obtuse evaluation order of computed properties: - // obj[key]=val evaluates val->obj->key when obj is null/undefined - // but key->obj->val when an object - // FIXME(bnoordhuis) less stack shuffling; don't to_propkey twice in - // happy path; replace `dup is_undefined_or_null if_true` with new - // opcode if_undefined_or_null? replace `swap dup` with over? - if (op == '=' && opcode == OP_get_array_el) { - int label_next = -1; - JSFunctionDef *fd = s->cur_func; - assert(OP_to_propkey2 == fd->byte_code.buf[fd->last_opcode_pos]); - fd->byte_code.size = fd->last_opcode_pos; - fd->last_opcode_pos = -1; - emit_op(s, OP_swap); // obj key -> key obj - emit_op(s, OP_dup); - emit_op(s, OP_is_undefined_or_null); - label_next = emit_goto(s, OP_if_true, -1); - emit_op(s, OP_swap); - emit_op(s, OP_to_propkey); - emit_op(s, OP_swap); - emit_label(s, label_next); - emit_op(s, OP_swap); - } - if (js_parse_assign_expr2(s, parse_flags)) { JS_FreeAtom(s->ctx, name); return -1; } - if (op == '=' && opcode == OP_get_array_el) { - emit_op(s, OP_swap); // obj key val -> obj val key - emit_op(s, OP_to_propkey); - emit_op(s, OP_swap); - } - if (op == '=') { if ((opcode == OP_get_ref_value || opcode == OP_scope_get_var) && name == name0) { set_object_name(s, name); @@ -39395,7 +40975,10 @@ static void push_break_entry(JSFunctionDef *fd, BlockEnv *be, be->label_finally = -1; be->scope_level = fd->scope_level; be->has_iterator = false; + be->is_async_iterator = false; be->is_regular_stmt = false; + be->has_using = false; + be->using_scope_level = -1; } static void pop_break_entry(JSFunctionDef *fd) @@ -39405,6 +40988,45 @@ static void pop_break_entry(JSFunctionDef *fd) fd->top_break = be->prev; } +static void emit_iterator_close(JSParseState *s, bool is_async, + bool preserve_completion) +{ + int label_no_return, label_done; + + if (!is_async) { + emit_op(s, OP_iterator_close); + return; + } + + if (preserve_completion) { + /* iter_obj next completion -> completion iter_obj */ + emit_op(s, OP_nip); + emit_op(s, OP_swap); + } else { + /* iter_obj next catch_offset -> iter_obj */ + emit_op(s, OP_drop); + emit_op(s, OP_drop); + } + emit_op(s, OP_dup); + emit_op(s, OP_is_undefined); + label_done = emit_goto(s, OP_if_true, -1); + emit_op(s, OP_get_field2); + emit_atom(s, JS_ATOM_return); + /* completion? iter_obj return_func */ + emit_op(s, OP_dup); + emit_op(s, OP_is_undefined_or_null); + label_no_return = emit_goto(s, OP_if_true, -1); + emit_op(s, OP_call_method); + emit_u16(s, 0); + emit_op(s, OP_await); + emit_op(s, OP_check_object); + emit_goto(s, OP_goto, label_done); + emit_label(s, label_no_return); + emit_op(s, OP_drop); + emit_label(s, label_done); + emit_op(s, OP_drop); +} + static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont) { BlockEnv *top; @@ -39431,11 +41053,17 @@ static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont) } i = 0; if (top->has_iterator) { - emit_op(s, OP_iterator_close); + emit_iterator_close(s, top->is_async_iterator, false); i += 3; } for(; i < top->drop_count; i++) emit_op(s, OP_drop); + if (top->has_using) { + emit_op(s, OP_using_dispose_init); + emit_op(s, OP_dispose_scope); + emit_u16(s, top->using_scope_level); + emit_op(s, OP_using_dispose_end); + } if (top->label_finally != -1) { /* must push dummy value to keep same stack depth */ emit_op(s, OP_undefined); @@ -39473,7 +41101,8 @@ static void emit_return(JSParseState *s, bool hasval) top = s->cur_func->top_break; while (top != NULL) { - if (top->has_iterator || top->label_finally != -1) { + if (top->has_iterator || top->label_finally != -1 || + top->has_using) { if (!hasval) { emit_op(s, OP_undefined); hasval = true; @@ -39485,30 +41114,21 @@ static void emit_return(JSParseState *s, bool hasval) emit_op(s, OP_nip_catch); /* stack: iter_obj next ret_val */ if (top->has_iterator) { - if (s->cur_func->func_kind == JS_FUNC_ASYNC_GENERATOR) { - int label_next, label_next2; - emit_op(s, OP_nip); /* next */ - emit_op(s, OP_swap); - emit_op(s, OP_get_field2); - emit_atom(s, JS_ATOM_return); - /* stack: iter_obj return_func */ - emit_op(s, OP_dup); - emit_op(s, OP_is_undefined_or_null); - label_next = emit_goto(s, OP_if_true, -1); - emit_op(s, OP_call_method); - emit_u16(s, 0); - emit_op(s, OP_iterator_check_object); - emit_op(s, OP_await); - label_next2 = emit_goto(s, OP_goto, -1); - emit_label(s, label_next); - emit_op(s, OP_drop); - emit_label(s, label_next2); - emit_op(s, OP_drop); + if (top->is_async_iterator) { + emit_iterator_close(s, true, true); } else { emit_op(s, OP_rot3r); emit_op(s, OP_undefined); /* dummy catch offset */ emit_op(s, OP_iterator_close); } + } else if (top->has_using) { + /* Dispose using variables. The return value is on TOS. + stack: ret_val */ + emit_op(s, OP_using_dispose_init); /* initial error_state */ + emit_op(s, OP_dispose_scope); + emit_u16(s, top->using_scope_level); + emit_op(s, OP_using_dispose_end); + /* stack: ret_val */ } else { /* execute the "finally" block */ emit_goto(s, OP_gosub, top->label_finally); @@ -39560,16 +41180,53 @@ static __exception int js_parse_statement(JSParseState *s) static __exception int js_parse_block(JSParseState *s) { + JSFunctionDef *fd = s->cur_func; + if (js_parse_expect(s, '{')) return -1; if (s->token.val != '}') { + BlockEnv using_be; + int has_using_be = 0; + int scope_level; + push_scope(s); + scope_level = fd->scope_level; for(;;) { if (js_parse_statement_or_decl(s, DECL_MASK_ALL)) return -1; + if (!has_using_be && fd->scopes[scope_level].has_using) { + has_using_be = 1; + push_break_entry(fd, &using_be, JS_ATOM_NULL, -1, -1, 1); + using_be.has_using = true; + using_be.using_scope_level = scope_level; + } if (s->token.val == '}') break; } + if (has_using_be) { + int label_catch = fd->scopes[scope_level].using_label_catch; + int label_end = fd->scopes[scope_level].using_label_end; + + pop_break_entry(fd); + + if (js_is_live_code(s)) { + emit_op(s, OP_drop); /* drop catch_offset */ + emit_op(s, OP_using_dispose_init); /* initial error_state: no error */ + emit_op(s, OP_dispose_scope); + emit_u16(s, scope_level); + emit_op(s, OP_using_dispose_end); + emit_goto(s, OP_goto, label_end); + } + + emit_label(s, label_catch); + /* Stack: exception_value (= initial error_state) */ + emit_op(s, OP_dispose_scope); + emit_u16(s, scope_level); + /* Stack: final_error_state (original or SuppressedError) */ + emit_op(s, OP_throw); + + emit_label(s, label_end); + } pop_scope(s); } if (next_token(s)) @@ -39591,14 +41248,29 @@ static __exception int js_parse_var(JSParseState *s, int parse_flags, int tok, return js_parse_error_reserved_identifier(s); } name = JS_DupAtom(ctx, s->token.u.ident.atom); - if (name == JS_ATOM_let && (tok == TOK_LET || tok == TOK_CONST)) { + if (name == JS_ATOM_let && + (tok == TOK_LET || tok == TOK_CONST || tok == TOK_USING)) { js_parse_error(s, "'let' is not a valid lexical identifier"); goto var_error; } + int using_method_idx = -1; if (next_token(s)) goto var_error; if (js_define_var(s, name, tok)) goto var_error; + if (tok == TOK_USING) { + /* Allocate a paired hidden local for the cached dispose + method. Must be allocated immediately after the value + var so OP_using_dispose can locate it at value_idx + 1. + */ + using_method_idx = add_scope_var(ctx, fd, + JS_ATOM__using_dispose_, + JS_VAR_USING_METHOD); + if (using_method_idx < 0) + goto var_error; + fd->vars[using_method_idx].is_lexical = 1; + fd->vars[using_method_idx].is_const = 1; + } if (export_flag) { if (!add_export_entry(s, s->cur_func->module, name, name, JS_EXPORT_TYPE_LOCAL)) @@ -39626,17 +41298,54 @@ static __exception int js_parse_var(JSParseState *s, int parse_flags, int tok, put_lvalue(s, opcode, scope, name1, label, PUT_LVALUE_NOKEEP, false); } else { + bool init; + + if (tok == TOK_USING) { + bool is_await = (parse_flags & PF_AWAIT_USING) != 0; + + if (!fd->scopes[fd->scope_level].has_using) { + /* First 'using' in this scope: set up labels + for the catch handler and end of disposal */ + fd->scopes[fd->scope_level].has_using = 1; + fd->scopes[fd->scope_level].using_label_catch = + new_label(s); + fd->scopes[fd->scope_level].using_label_end = + new_label(s); + + /* Emit OP_catch: push catch_offset on the value + stack. If an exception occurs, control jumps + to catch_label with the exception value on the + stack instead of catch_offset. */ + emit_goto(s, OP_catch, + fd->scopes[fd->scope_level].using_label_catch); + } + if (is_await) + fd->scopes[fd->scope_level].is_await_using = 1; + } + if (js_parse_assign_expr2(s, parse_flags)) goto var_error; set_object_name(s, name); - emit_op(s, (tok == TOK_CONST || tok == TOK_LET) ? - OP_scope_put_var_init : OP_scope_put_var); + + if (tok == TOK_USING) { + emit_op(s, OP_using_check); + emit_u8(s, (parse_flags & PF_AWAIT_USING) != 0); + /* Stack: value, method. Store the method first. + Emit OP_put_loc directly (bypasses atom lookup) + so multiple using decls with the shared hidden + atom name don't collide. */ + emit_op(s, OP_put_loc); + emit_u16(s, using_method_idx); + } + + init = (tok == TOK_CONST || tok == TOK_LET || tok == TOK_USING); + emit_op(s, init ? OP_scope_put_var_init : OP_scope_put_var); emit_atom(s, name); emit_u16(s, fd->scope_level); } } else { - if (tok == TOK_CONST) { - js_parse_error(s, "missing initializer for const variable"); + if (tok == TOK_CONST || tok == TOK_USING) { + js_parse_error(s, "missing initializer for variable"); goto var_error; } if (tok == TOK_LET) { @@ -39652,6 +41361,10 @@ static __exception int js_parse_var(JSParseState *s, int parse_flags, int tok, int skip_bits; if ((s->token.val == '[' || s->token.val == '{') && js_parse_skip_parens_token(s, &skip_bits, false) == '=') { + /* using declarations do not allow binding patterns */ + if (tok == TOK_USING) { + return js_parse_error(s, "binding patterns are not allowed in using declarations"); + } emit_op(s, OP_undefined); if (js_parse_destructuring_element(s, tok, false, true, skip_bits & SKIP_HAS_ELLIPSIS, true, export_flag) < 0) return -1; @@ -39720,10 +41433,38 @@ static int is_let(JSParseState *s, int decl_mask) return res; } +/* Return 1 if the current token is `using` introducing a UsingDeclaration, + 0 if it is a plain identifier usage, or -1 on error. + If `is_for_of` is true, `using of` is specifically treated as identifier + per the for-of lookahead restriction. */ +static int is_using(JSParseState *s, bool is_for_of) +{ + int res = false; + if (token_is_pseudo_keyword(s, JS_ATOM_using)) { + JSParsePos pos; + js_parse_get_pos(s, &pos); + if (next_token(s)) + return -1; + /* No line terminator allowed between `using` and the binding */ + if (s->last_line_num == s->token.line_num) { + if (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved) { + if (!(is_for_of && s->token.u.ident.atom == JS_ATOM_of)) { + res = true; + } + } + } + if (js_parse_seek_token(s, &pos)) + res = -1; + } + return res; +} + /* XXX: handle IteratorClose when exiting the loop before the enumeration is done */ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, - bool is_async) + bool is_async, + int source_line_num, + int source_col_num) { JSContext *ctx = s->ctx; JSFunctionDef *fd = s->cur_func; @@ -39769,7 +41510,27 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, default: return -1; } - if (tok == TOK_VAR || tok == TOK_LET || tok == TOK_CONST) { + bool is_await_using = false; + if (tok == TOK_AWAIT) { + int u; + if (next_token(s)) + return -1; + u = is_using(s, false); + if (u < 0) + return -1; + if (!u) + return js_parse_error(s, "'using' expected"); + tok = TOK_USING; + is_await_using = true; + s->cur_func->has_await = true; + } else if (token_is_pseudo_keyword(s, JS_ATOM_using)) { + int u = is_using(s, true); + if (u < 0) + return -1; + if (u) + tok = TOK_USING; + } + if (tok == TOK_VAR || tok == TOK_LET || tok == TOK_CONST || tok == TOK_USING) { if (next_token(s)) return -1; @@ -39783,7 +41544,13 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, } var_name = JS_ATOM_NULL; } else { + bool init; var_name = JS_DupAtom(ctx, s->token.u.ident.atom); + if (var_name == JS_ATOM_let && + (tok == TOK_LET || tok == TOK_CONST || tok == TOK_USING)) { + JS_FreeAtom(s->ctx, var_name); + return js_parse_error(s, "'let' is not a valid lexical identifier"); + } if (next_token(s)) { JS_FreeAtom(s->ctx, var_name); return -1; @@ -39792,10 +41559,33 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, JS_FreeAtom(s->ctx, var_name); return -1; } - emit_op(s, (tok == TOK_CONST || tok == TOK_LET) ? - OP_scope_put_var_init : OP_scope_put_var); + if (tok == TOK_USING) { + int mi = add_scope_var(ctx, fd, JS_ATOM__using_dispose_, + JS_VAR_USING_METHOD); + if (mi < 0) { + JS_FreeAtom(s->ctx, var_name); + return -1; + } + fd->vars[mi].is_lexical = 1; + fd->vars[mi].is_const = 1; + emit_op(s, OP_using_check); + emit_u8(s, is_await_using); + emit_op(s, OP_put_loc); + emit_u16(s, mi); + } + init = (tok == TOK_CONST || tok == TOK_LET || tok == TOK_USING); + emit_op(s, init ? OP_scope_put_var_init : OP_scope_put_var); emit_atom(s, var_name); emit_u16(s, fd->scope_level); + if (tok == TOK_USING) { + if (!fd->scopes[fd->scope_level].has_using) { + fd->scopes[fd->scope_level].has_using = 1; + fd->scopes[fd->scope_level].using_label_catch = new_label(s); + fd->scopes[fd->scope_level].using_label_end = new_label(s); + } + if (is_await_using) + fd->scopes[fd->scope_level].is_await_using = 1; + } } } else if (!is_async && token_is_pseudo_keyword(s, JS_ATOM_async) && peek_token(s, false) == TOK_OF) { return js_parse_error(s, "'for of' expression cannot start with 'async'"); @@ -39846,6 +41636,8 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, } else if (s->token.val == TOK_IN) { if (is_async) return js_parse_error(s, "'for await' loop should be used with 'of'"); + if (tok == TOK_USING) + return js_parse_error(s, "using declaration not allowed in for-in"); if (has_initializer && (tok != TOK_VAR || fd->is_strict_mode || has_destructuring)) { initializer_error: @@ -39872,6 +41664,7 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, that a yield in the expression does not try to close a not-yet-created iterator */ break_entry.has_iterator = true; + break_entry.is_async_iterator = is_async; break_entry.drop_count += 2; if (is_async) emit_op(s, OP_for_await_of_start); @@ -39908,8 +41701,46 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, } emit_label(s, label_body); - if (js_parse_statement(s)) - return -1; + { + bool scope_has_using = fd->scopes[fd->scope_level].has_using; + int using_scope_level = fd->scope_level; + BlockEnv using_be; + int had_using_be = 0; + + if (scope_has_using) { + emit_goto(s, OP_catch, fd->scopes[using_scope_level].using_label_catch); + push_break_entry(fd, &using_be, JS_ATOM_NULL, -1, -1, 1); + using_be.has_using = true; + using_be.using_scope_level = using_scope_level; + had_using_be = 1; + } + + if (js_parse_statement(s)) + return -1; + + if (had_using_be) { + pop_break_entry(fd); + } + + if (scope_has_using && js_is_live_code(s)) { + emit_op(s, OP_drop); + emit_op(s, OP_using_dispose_init); + emit_op(s, OP_dispose_scope); + emit_u16(s, using_scope_level); + emit_op(s, OP_using_dispose_end); + emit_goto(s, OP_goto, + fd->scopes[using_scope_level].using_label_end); + } + + if (scope_has_using) { + emit_label(s, fd->scopes[using_scope_level].using_label_catch); + emit_op(s, OP_dispose_scope); + emit_u16(s, using_scope_level); + emit_op(s, OP_throw); + + emit_label(s, fd->scopes[using_scope_level].using_label_end); + } + } close_scopes(s, s->cur_func->scope_level, block_scope_level); @@ -39941,7 +41772,8 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name, emit_label(s, label_break); if (is_for_of) { /* close and drop enum_rec */ - emit_op(s, OP_iterator_close); + emit_source_loc_at(s, source_line_num, source_col_num); + emit_iterator_close(s, is_async, false); } else { emit_op(s, OP_drop); } @@ -40050,6 +41882,36 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (js_parse_expect_semi(s)) goto fail; break; + case TOK_AWAIT: + /* Check for 'await using' declaration */ + if (s->cur_func->func_kind & JS_FUNC_ASYNC) { + JSParsePos pos; + int u; + js_parse_get_pos(s, &pos); + if (next_token(s)) /* skip 'await' */ + goto fail; + u = is_using(s, false); + if (u < 0) + goto fail; + if (u) { + if (!(decl_mask & DECL_MASK_OTHER)) { + js_parse_error(s, "lexical declarations can't appear in single-statement context"); + goto fail; + } + s->cur_func->has_await = true; + if (next_token(s)) /* skip 'using' */ + goto fail; + if (js_parse_var(s, PF_IN_ACCEPTED | PF_AWAIT_USING, TOK_USING, /*export_flag*/false)) + goto fail; + if (js_parse_expect_semi(s)) + goto fail; + break; + } + /* Not 'await using': restore to parse as expression */ + if (js_parse_seek_token(s, &pos)) + goto fail; + } + goto hasexpr; case TOK_LET: case TOK_CONST: haslet: @@ -40173,10 +42035,14 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, { int label_cont, label_break, label_body, label_test; int pos_cont, pos_body, block_scope_level; + int for_scope_level; BlockEnv break_entry; int tok, bits; + int source_line_num, source_col_num; bool is_async; + source_line_num = s->token.line_num; + source_col_num = s->token.col_num; if (next_token(s)) goto fail; @@ -40200,7 +42066,8 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, if (!(bits & SKIP_HAS_SEMI)) { /* parse for/in or for/of */ - if (js_parse_for_in_of(s, label_name, is_async)) + if (js_parse_for_in_of(s, label_name, is_async, + source_line_num, source_col_num)) goto fail; break; } @@ -40209,6 +42076,7 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, /* create scope for the lexical variables declared in the initial, test and increment expressions */ push_scope(s); + for_scope_level = s->cur_func->scope_level; /* initial expression */ tok = s->token.val; if (tok != ';') { @@ -40221,10 +42089,48 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, default: goto fail; } - if (tok == TOK_VAR || tok == TOK_LET || tok == TOK_CONST) { + if (tok != TOK_VAR && tok != TOK_LET && tok != TOK_CONST && + token_is_pseudo_keyword(s, JS_ATOM_using)) { + int u = is_using(s, false); + if (u < 0) + goto fail; + if (u) + tok = TOK_USING; + } + if (tok == TOK_AWAIT && + (s->cur_func->func_kind & JS_FUNC_ASYNC)) { + /* Check for `await using` declaration head */ + JSParsePos pos; + int u; + js_parse_get_pos(s, &pos); + if (next_token(s)) /* skip 'await' */ + goto fail; + u = is_using(s, false); + if (u < 0) + goto fail; + if (u) { + s->cur_func->has_await = true; + tok = TOK_USING; + if (next_token(s)) /* skip 'using' */ + goto fail; + if (js_parse_var(s, PF_IN_ACCEPTED | PF_AWAIT_USING, + TOK_USING, false)) + goto fail; + goto for_init_done; + } + if (js_parse_seek_token(s, &pos)) + goto fail; + } + if (tok == TOK_VAR + || tok == TOK_LET + || tok == TOK_CONST + || tok == TOK_USING) { + int pf = 0; + if (tok == TOK_USING && is_async) + pf |= PF_AWAIT_USING; if (next_token(s)) goto fail; - if (js_parse_var(s, 0, tok, /*export_flag*/false)) + if (js_parse_var(s, pf, tok, /*export_flag*/false)) goto fail; } else { if (js_parse_expr2(s, false)) @@ -40232,6 +42138,7 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, emit_op(s, OP_drop); } + for_init_done: /* close the closures before the first iteration */ close_scopes(s, s->cur_func->scope_level, block_scope_level); } @@ -40245,6 +42152,11 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, push_break_entry(s->cur_func, &break_entry, label_name, label_break, label_cont, 0); + if (s->cur_func->scopes[for_scope_level].has_using) { + break_entry.has_using = true; + break_entry.using_scope_level = for_scope_level; + break_entry.drop_count = 1; /* catch_offset from OP_catch */ + } /* test expression */ if (s->token.val == ';') { @@ -40312,6 +42224,27 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, emit_label(s, label_break); pop_break_entry(s->cur_func); + + if (s->cur_func->scopes[for_scope_level].has_using) { + int label_catch = s->cur_func->scopes[for_scope_level].using_label_catch; + int label_end = s->cur_func->scopes[for_scope_level].using_label_end; + + /* Normal exit: drop catch_offset, dispose */ + emit_op(s, OP_drop); + emit_op(s, OP_using_dispose_init); + emit_op(s, OP_dispose_scope); + emit_u16(s, for_scope_level); + emit_op(s, OP_using_dispose_end); + emit_goto(s, OP_goto, label_end); + + /* Catch handler: exception on stack */ + emit_label(s, label_catch); + emit_op(s, OP_dispose_scope); + emit_u16(s, for_scope_level); + emit_op(s, OP_throw); + + emit_label(s, label_end); + } pop_scope(s); } break; @@ -40414,6 +42347,35 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, js_parse_error(s, "invalid switch statement"); goto fail; } + /* `using` / `await using` declarations are not allowed + directly within a CaseClause or DefaultClause + (early error per spec). */ + if (token_is_pseudo_keyword(s, JS_ATOM_using)) { + int u = is_using(s, false); + if (u < 0) + goto fail; + if (u) { + js_parse_error(s, "using declaration is not allowed in this context"); + goto fail; + } + } + if (s->token.val == TOK_AWAIT && + (s->cur_func->func_kind & JS_FUNC_ASYNC)) { + JSParsePos pos; + int u; + js_parse_get_pos(s, &pos); + if (next_token(s)) + goto fail; + u = is_using(s, false); + if (u < 0) + goto fail; + if (js_parse_seek_token(s, &pos)) + goto fail; + if (u) { + js_parse_error(s, "await using declaration is not allowed in this context"); + goto fail; + } + } if (js_parse_statement_or_decl(s, DECL_MASK_ALL)) goto fail; } @@ -40647,6 +42609,33 @@ static __exception int js_parse_statement_or_decl(JSParseState *s, default: goto fail; } + if (token_is_pseudo_keyword(s, JS_ATOM_using)) { + int u = is_using(s, false); + if (u < 0) + goto fail; + if (u) { + JSFunctionDef *fd = s->cur_func; + if (!(decl_mask & DECL_MASK_OTHER)) { + js_parse_error(s, "lexical declarations can't appear in single-statement context"); + goto fail; + } + if (fd->is_eval && + (fd->eval_type == JS_EVAL_TYPE_GLOBAL || + fd->eval_type == JS_EVAL_TYPE_DIRECT || + fd->eval_type == JS_EVAL_TYPE_INDIRECT) && + fd->scope_level == fd->body_scope) { + js_parse_error(s, "using declaration is not allowed at the top level of a script"); + goto fail; + } + if (next_token(s)) + goto fail; + if (js_parse_var(s, PF_IN_ACCEPTED, TOK_USING, /*export_flag*/false)) + goto fail; + if (js_parse_expect_semi(s)) + goto fail; + break; + } + } if (token_is_pseudo_keyword(s, JS_ATOM_async) && peek_token(s, true) == TOK_FUNCTION) { if (!(decl_mask & DECL_MASK_OTHER)) { @@ -40724,7 +42713,7 @@ static JSModuleDef *js_new_module_def(JSContext *ctx, JSAtom name) JS_FreeAtom(ctx, name); return NULL; } - m->header.ref_count = 1; + JS_REF_COUNT(m) = 1; m->module_name = name; m->module_ns = JS_UNDEFINED; m->func_obj = JS_UNDEFINED; @@ -40734,6 +42723,7 @@ static JSModuleDef *js_new_module_def(JSContext *ctx, JSAtom name) m->resolving_funcs[0] = JS_UNDEFINED; m->resolving_funcs[1] = JS_UNDEFINED; m->private_value = JS_UNDEFINED; + m->attributes = JS_UNDEFINED; list_add_tail(&m->link, &ctx->loaded_modules); return m; } @@ -40764,6 +42754,7 @@ static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m, JS_MarkValue(rt, m->resolving_funcs[0], mark_func); JS_MarkValue(rt, m->resolving_funcs[1], mark_func); JS_MarkValue(rt, m->private_value, mark_func); + JS_MarkValue(rt, m->attributes, mark_func); } static void js_free_module_def(JSContext *ctx, JSModuleDef *m) @@ -40805,6 +42796,7 @@ static void js_free_module_def(JSContext *ctx, JSModuleDef *m) JS_FreeValue(ctx, m->resolving_funcs[0]); JS_FreeValue(ctx, m->resolving_funcs[1]); JS_FreeValue(ctx, m->private_value); + JS_FreeValue(ctx, m->attributes); list_del(&m->link); js_free(ctx, m); } @@ -40812,16 +42804,22 @@ static void js_free_module_def(JSContext *ctx, JSModuleDef *m) #ifndef QJS_DISABLE_PARSER static int add_req_module_entry(JSContext *ctx, JSModuleDef *m, - JSAtom module_name) + JSAtom module_name, JSValueConst attributes) { JSReqModuleEntry *rme; - int i; + int i, eq; - /* no need to add the module request if it is already present */ + /* no need to add the module request if an equivalent one (same + specifier and same import attributes) is already present */ for(i = 0; i < m->req_module_entries_count; i++) { rme = &m->req_module_entries[i]; - if (rme->module_name == module_name) - return i; + if (rme->module_name == module_name) { + eq = js_module_attributes_equal(ctx, rme->attributes, attributes); + if (eq < 0) + return -1; + if (eq) + return i; + } } if (js_resize_array(ctx, (void **)&m->req_module_entries, @@ -40832,7 +42830,7 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m, rme = &m->req_module_entries[m->req_module_entries_count++]; rme->module_name = JS_DupAtom(ctx, module_name); rme->module = NULL; - rme->attributes = JS_UNDEFINED; + rme->attributes = js_dup(attributes); return i; } @@ -40989,6 +42987,13 @@ void JS_SetModuleLoaderFunc2(JSRuntime *rt, rt->module_loader_opaque = opaque; } +void JS_SetDynamicImportFunc(JSRuntime *rt, JSDynamicImportFunc *func, + void *opaque) +{ + rt->dynamic_import = func; + rt->dynamic_import_opaque = opaque; +} + void JS_SetModuleNormalizeFunc2(JSRuntime *rt, JSModuleNormalizeFunc2 *module_normalize) { @@ -41067,12 +43072,101 @@ static char *js_default_module_normalize_name(JSContext *ctx, return filename; } -static JSModuleDef *js_find_loaded_module(JSContext *ctx, JSAtom name) +/* Two sets of import attributes are equal if they have the same + key/value pairs; absent attributes are equivalent to an empty set + (ModuleRequestsEqual). Returns 1, 0 or -1 in case of exception. */ +static int js_module_attributes_equal(JSContext *ctx, JSValueConst attr1, + JSValueConst attr2) +{ + JSPropertyEnum *tab = NULL; + uint32_t len = 0, len2 = 0, i; + JSValue v1, v2; + JSAtom a1, a2; + int res; + + if (JS_IsObject(attr1)) { + if (JS_GetOwnPropertyNames(ctx, &tab, &len, attr1, + JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0) + return -1; + } + if (JS_IsObject(attr2)) { + JSPropertyEnum *tab2 = NULL; + if (JS_GetOwnPropertyNames(ctx, &tab2, &len2, attr2, + JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0) { + res = -1; + goto done; + } + js_free_prop_enum(ctx, tab2, len2); + } + res = 0; + if (len != len2) + goto done; + for (i = 0; i < len; i++) { + v1 = JS_GetProperty(ctx, attr1, tab[i].atom); + if (JS_IsException(v1)) { + res = -1; + goto done; + } + v2 = JS_GetProperty(ctx, attr2, tab[i].atom); + if (JS_IsException(v2)) { + JS_FreeValue(ctx, v1); + res = -1; + goto done; + } + if (JS_IsUndefined(v2)) { + /* attribute values are strings, undefined means the key is + not present */ + JS_FreeValue(ctx, v1); + goto done; + } + a1 = JS_ValueToAtom(ctx, v1); + a2 = JS_ValueToAtom(ctx, v2); + JS_FreeValue(ctx, v1); + JS_FreeValue(ctx, v2); + if (a1 == JS_ATOM_NULL || a2 == JS_ATOM_NULL) { + JS_FreeAtom(ctx, a1); + JS_FreeAtom(ctx, a2); + res = -1; + goto done; + } + res = (a1 == a2); + JS_FreeAtom(ctx, a1); + JS_FreeAtom(ctx, a2); + if (!res) + goto done; + } + res = 1; +done: + js_free_prop_enum(ctx, tab, len); + return res; +} + +static JSModuleDef *js_find_loaded_module(JSContext *ctx, JSAtom name, + JSValueConst attributes) { struct list_head *el; JSModuleDef *m; + int eq; /* first look at the loaded modules */ + list_for_each(el, &ctx->loaded_modules) { + m = list_entry(el, JSModuleDef, link); + if (m->module_name == name) { + eq = js_module_attributes_equal(ctx, m->attributes, attributes); + if (eq < 0) + return NULL; /* exception */ + if (eq) + return m; + } + } + return NULL; +} + +static JSModuleDef *js_find_loaded_module_by_name(JSContext *ctx, JSAtom name) +{ + struct list_head *el; + JSModuleDef *m; + list_for_each(el, &ctx->loaded_modules) { m = list_entry(el, JSModuleDef, link); if (m->module_name == name) @@ -41113,12 +43207,17 @@ static JSModuleDef *js_host_resolve_imported_module(JSContext *ctx, } /* first look at the loaded modules */ - m = js_find_loaded_module(ctx, module_name); + m = js_find_loaded_module(ctx, module_name, attributes); if (m) { js_free(ctx, cname); JS_FreeAtom(ctx, module_name); return m; } + if (JS_HasException(ctx)) { + js_free(ctx, cname); + JS_FreeAtom(ctx, module_name); + return NULL; + } JS_FreeAtom(ctx, module_name); @@ -41137,6 +43236,11 @@ static JSModuleDef *js_host_resolve_imported_module(JSContext *ctx, } else { m = rt->u.module_loader_func(ctx, cname, rt->module_loader_opaque); } + /* remember the attributes the module was requested with so that + js_find_loaded_module() can tell apart e.g. a text module and a + JS module with the same name */ + if (m && JS_IsObject(attributes) && JS_IsUndefined(m->attributes)) + m->attributes = js_dup(attributes); js_free(ctx, cname); return m; } @@ -41309,6 +43413,62 @@ static JSResolveResultEnum js_resolve_export(JSContext *ctx, return ret; } +static JSVarRef *js_get_local_export_var_ref1(JSContext *ctx, JSModuleDef *m, + JSExportEntry *me, + JSResolveState *s) +{ + JSVarRef *var_ref = me->u.local.var_ref; + int i; + + if (!var_ref) { + JSObject *p = JS_VALUE_GET_OBJ(m->func_obj); + var_ref = p->u.func.var_refs[me->u.local.var_idx]; + } + if (var_ref) + return var_ref; + /* the slot is a re-exported import whose source is not linked yet: follow + the import alias, guarding against re-export cycles like js_resolve_export1 */ + for(i = 0; i < m->import_entries_count; i++) { + JSImportEntry *mi = &m->import_entries[i]; + JSModuleDef *m1, *res_m; + JSExportEntry *res_me; + + if (mi->var_idx != me->u.local.var_idx) + continue; + if (find_resolve_entry(s, m, mi->import_name) >= 0) + return NULL; + if (add_resolve_entry(ctx, s, m, mi->import_name) < 0) + return NULL; + m1 = m->req_module_entries[mi->req_module_idx].module; + if (js_resolve_export1(ctx, &res_m, &res_me, m1, mi->import_name, s) == + JS_RESOLVE_RES_FOUND && + res_me->local_name != JS_ATOM__star_) + return js_get_local_export_var_ref1(ctx, res_m, res_me, s); + break; + } + return NULL; +} + +/* var_ref of a local export. For a re-exported import the slot is NULL until + the source is linked, so follow the import alias; returns NULL if + unresolvable (e.g. a re-export cycle with no concrete binding). */ +static JSVarRef *js_get_local_export_var_ref(JSContext *ctx, JSModuleDef *m, + JSExportEntry *me) +{ + JSResolveState ss, *s = &ss; + JSVarRef *var_ref; + int i; + + s->array = NULL; + s->size = 0; + s->count = 0; + var_ref = js_get_local_export_var_ref1(ctx, m, me, s); + for(i = 0; i < s->count; i++) + JS_FreeAtom(ctx, s->array[i].name); + js_free(ctx, s->array); + return var_ref; +} + static void js_resolve_export_throw_error(JSContext *ctx, JSResolveResultEnum res, JSModuleDef *m, JSAtom export_name) @@ -41499,7 +43659,17 @@ static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m) } if (res != JS_RESOLVE_RES_FOUND) { if (res != JS_RESOLVE_RES_AMBIGUOUS) { - js_resolve_export_throw_error(ctx, res, m, en->export_name); + /* for a re-export the binding is missing from the target + module, not from m; report the target and the looked-up name */ + JSExportEntry *me = find_export_entry(ctx, m, en->export_name); + if (me && me->export_type == JS_EXPORT_TYPE_INDIRECT && + me->local_name != JS_ATOM__star_) { + JSModuleDef *m1 = + m->req_module_entries[me->u.req_module_idx].module; + js_resolve_export_throw_error(ctx, res, m1, me->local_name); + } else { + js_resolve_export_throw_error(ctx, res, m, en->export_name); + } goto fail; } en->export_type = EXPORTED_NAME_AMBIGUOUS; @@ -41509,13 +43679,9 @@ static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m) en->u.module = res_m->req_module_entries[res_me->u.req_module_idx].module; } else { en->export_type = EXPORTED_NAME_NORMAL; - if (res_me->u.local.var_ref) { - en->u.var_ref = res_me->u.local.var_ref; - } else { - JSObject *p1 = JS_VALUE_GET_OBJ(res_m->func_obj); - p1 = JS_VALUE_GET_OBJ(res_m->func_obj); - en->u.var_ref = p1->u.func.var_refs[res_me->u.local.var_idx]; - } + /* follow the import alias when the slot is NULL (cyclic + re-export of an imported binding) */ + en->u.var_ref = js_get_local_export_var_ref(ctx, res_m, res_me); } } } @@ -41540,7 +43706,7 @@ static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m) JS_PROP_VARREF); if (!pr) goto fail; - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; pr->u.var_ref = var_ref; } break; @@ -41631,13 +43797,14 @@ static JSVarRef *js_create_module_var(JSContext *ctx, bool is_lexical) var_ref = js_malloc(ctx, sizeof(JSVarRef)); if (!var_ref) return NULL; - var_ref->header.ref_count = 1; + JS_REF_COUNT(var_ref) = 1; if (is_lexical) var_ref->value = JS_UNINITIALIZED; else var_ref->value = JS_UNDEFINED; var_ref->pvalue = &var_ref->value; var_ref->is_detached = true; + var_ref->is_coro = false; add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF); return var_ref; } @@ -41661,7 +43828,7 @@ static int js_create_module_bytecode_function(JSContext *ctx, JSModuleDef *m) p = JS_VALUE_GET_OBJ(func_obj); p->u.func.function_bytecode = b; - b->header.ref_count++; + JS_REF_COUNT(b)++; p->u.func.home_object = NULL; p->u.func.var_refs = NULL; if (b->closure_var_count) { @@ -41808,7 +43975,10 @@ static int js_inner_module_linking(JSContext *ctx, JSModuleDef *m, m1 = m->req_module_entries[me->u.req_module_idx].module; ret = js_resolve_export(ctx, &res_m, &res_me, m1, me->local_name); if (ret != JS_RESOLVE_RES_FOUND) { - js_resolve_export_throw_error(ctx, ret, m, me->export_name); + /* the binding is missing from the re-exported module m1, not + from the re-exporting module m; report m1 and the name being + looked up there, matching the direct-import diagnostic */ + js_resolve_export_throw_error(ctx, ret, m1, me->local_name); goto fail; } } @@ -41856,7 +44026,6 @@ static int js_inner_module_linking(JSContext *ctx, JSModuleDef *m, JSResolveResultEnum ret; JSExportEntry *res_me; JSModuleDef *res_m; - JSObject *p1; ret = js_resolve_export(ctx, &res_m, &res_me, m1, mi->import_name); @@ -41883,12 +44052,19 @@ static int js_inner_module_linking(JSContext *ctx, JSModuleDef *m, module_trace(ctx, "namespace from\n"); } else { - var_ref = res_me->u.local.var_ref; + /* follow the import alias when the slot is NULL (cyclic + re-export of an imported binding) */ + var_ref = js_get_local_export_var_ref(ctx, res_m, res_me); if (!var_ref) { - p1 = JS_VALUE_GET_OBJ(res_m->func_obj); - var_ref = p1->u.func.var_refs[res_me->u.local.var_idx]; + char buf2[ATOM_GET_STR_BUF_SIZE]; + char buf3[ATOM_GET_STR_BUF_SIZE]; + JS_ThrowSyntaxError(ctx, + "circular import: binding '%s' is not resolvable in module '%s'", + JS_AtomGetStr(ctx, buf2, sizeof(buf2), mi->import_name), + JS_AtomGetStr(ctx, buf3, sizeof(buf3), res_m->module_name)); + goto fail; } - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; var_refs[mi->var_idx] = var_ref; module_trace(ctx, "local export (var_ref=%p)\n", (void *)var_ref); @@ -41903,7 +44079,7 @@ static int js_inner_module_linking(JSContext *ctx, JSModuleDef *m, JSExportEntry *me = &m->export_entries[i]; if (me->export_type == JS_EXPORT_TYPE_LOCAL) { var_ref = var_refs[me->u.local.var_idx]; - var_ref->header.ref_count++; + JS_REF_COUNT(var_ref)++; me->u.local.var_ref = var_ref; } } @@ -42027,7 +44203,7 @@ static JSValue js_import_meta(JSContext *ctx) /* XXX: inefficient, need to add a module or script pointer in JSFunctionBytecode */ - m = js_find_loaded_module(ctx, filename); + m = js_find_loaded_module_by_name(ctx, filename); JS_FreeAtom(ctx, filename); if (!m) { fail: @@ -42126,17 +44302,17 @@ static void JS_LoadModuleInternal(JSContext *ctx, const char *basename, JS_FreeValue(ctx, evaluate_promise); } -/* Return a promise or an exception in case of memory error. Used by - os.Worker() */ +/* Return a promise or an exception in case of memory error. */ JSValue JS_LoadModule(JSContext *ctx, const char *basename, - const char *filename) + const char *filename, JSValueConst attributes) { JSValue promise, resolving_funcs[2]; promise = JS_NewPromiseCapability(ctx, resolving_funcs); if (JS_IsException(promise)) return JS_EXCEPTION; - JS_LoadModuleInternal(ctx, basename, filename, vc(resolving_funcs), JS_UNDEFINED); + JS_LoadModuleInternal(ctx, basename, filename, vc(resolving_funcs), + attributes); JS_FreeValue(ctx, resolving_funcs[0]); JS_FreeValue(ctx, resolving_funcs[1]); return promise; @@ -42145,12 +44321,13 @@ JSValue JS_LoadModule(JSContext *ctx, const char *basename, static JSValue js_dynamic_import_job(JSContext *ctx, int argc, JSValueConst *argv) { + JSRuntime *rt = ctx->rt; JSValueConst *resolving_funcs = argv; JSValueConst basename_val = argv[2]; JSValueConst specifier = argv[3]; JSValueConst attributes = argv[4]; - const char *basename = NULL, *filename; - JSValue ret, err; + JSValue ret, err, token = JS_UNDEFINED; + const char *basename = NULL, *filename = NULL; if (!JS_IsString(basename_val)) { JS_ThrowTypeError(ctx, "no function filename for import()"); @@ -42159,12 +44336,28 @@ static JSValue js_dynamic_import_job(JSContext *ctx, basename = JS_ToCString(ctx, basename_val); if (!basename) goto exception; - filename = JS_ToCString(ctx, specifier); if (!filename) goto exception; - - JS_LoadModuleInternal(ctx, basename, filename, resolving_funcs, attributes); + if (rt->dynamic_import) { + token = JS_NewObjectProto(ctx, JS_NULL); + if (JS_IsException(token)) + goto exception; + if (JS_SetProperty(ctx, token, JS_ATOM_resolve, + js_dup(resolving_funcs[0])) < 0) { + goto exception; + } + if (JS_SetProperty(ctx, token, JS_ATOM_reject, + js_dup(resolving_funcs[1])) < 0) { + goto exception; + } + rt->dynamic_import(ctx, basename, filename, token, attributes, + rt->dynamic_import_opaque); + } else { + // fallback for legacy code + JS_LoadModuleInternal(ctx, basename, filename, resolving_funcs, + attributes); + } JS_FreeCString(ctx, filename); JS_FreeCString(ctx, basename); return JS_UNDEFINED; @@ -42173,7 +44366,9 @@ static JSValue js_dynamic_import_job(JSContext *ctx, ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, vc(&err)); JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */ JS_FreeValue(ctx, err); + JS_FreeValue(ctx, token); JS_FreeCString(ctx, basename); + JS_FreeCString(ctx, filename); return JS_UNDEFINED; } @@ -42281,6 +44476,43 @@ static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier, goto done; } +static JSValue js_finish_dynamic_import_job(JSContext *ctx, + int argc, JSValueConst *argv) +{ + JSValueConst token = argv[0]; + JSValueConst promise = argv[1]; + JSValueConst exception = argv[2]; + JSValue ret, resolve, reject; + + resolve = JS_GetProperty(ctx, token, JS_ATOM_resolve); + reject = JS_GetProperty(ctx, token, JS_ATOM_reject); + if (JS_IsException(promise)) { + ret = JS_Call(ctx, reject, JS_UNDEFINED, 1, &exception); + } else { + ret = JS_PromiseThen(ctx, promise, resolve, reject); + } + JS_FreeValue(ctx, resolve); + JS_FreeValue(ctx, reject); + return ret; +} + +int JS_FinishDynamicImport(JSContext *ctx, JSValue token, JSValue promise, + JSValue exception) +{ + JSValue args[3]; + int ret; + + args[0] = token; + args[1] = promise; + args[2] = exception; + ret = JS_EnqueueJob(ctx, js_finish_dynamic_import_job, countof(args), + vc(args)); + JS_FreeValue(ctx, token); + JS_FreeValue(ctx, promise); + JS_FreeValue(ctx, exception); + return ret; +} + static void js_set_module_evaluated(JSContext *ctx, JSModuleDef *m) { m->status = JS_MODULE_STATUS_EVALUATED; @@ -42354,6 +44586,7 @@ static int exec_module_list_cmp(const void *p1, const void *p2, void *opaque) static int js_execute_async_module(JSContext *ctx, JSModuleDef *m); static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m, JSValue *pvalue); +static void js_promise_set_handled(JSContext *ctx, JSValueConst promise); static JSValue js_async_module_execution_rejected(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, @@ -42491,6 +44724,9 @@ static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m, JS_FreeValue(ctx, promise); } else if (state == JS_PROMISE_REJECTED) { *pvalue = JS_PromiseResult(ctx, promise); + // rejection is propagated to module's evaluation promise; mark this internal + // promise as handled so it's not also surfaced as an unhandled rejection + js_promise_set_handled(ctx, promise); JS_FreeValue(ctx, promise); return -1; } else { @@ -42677,24 +44913,18 @@ static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m) #ifndef QJS_DISABLE_PARSER /* Parse 'with { key: "value", ... }' clause for import attributes. - rme->attributes is set to JS_UNDEFINED if no 'with' clause or an object - containing the attributes as key/value pairs. If rme->attributes is already - set (from a previous import of the same module), we still parse the tokens - but skip adding to the object since they should be the same. */ -static __exception int js_parse_with_clause(JSParseState *s, JSReqModuleEntry *rme) + *pattributes is set to JS_UNDEFINED if no 'with' clause or to an object + containing the attributes as key/value pairs. */ +static __exception int js_parse_with_clause(JSParseState *s, JSValue *pattributes) { JSContext *ctx = s->ctx; JSAtom key; int ret; - bool already_set; + *pattributes = JS_UNDEFINED; if (s->token.val != TOK_WITH) return 0; /* no 'with' clause */ - /* If attributes already set from previous import of same module, - just parse to consume tokens but don't modify the object. */ - already_set = !JS_IsUndefined(rme->attributes); - if (next_token(s)) return -1; if (js_parse_expect(s, '{')) @@ -42724,34 +44954,32 @@ static __exception int js_parse_with_clause(JSParseState *s, JSReqModuleEntry *r JS_FreeAtom(ctx, key); return -1; } - if (!already_set) { - if (JS_IsUndefined(rme->attributes)) { - JSValue attributes = JS_NewObjectProto(ctx, JS_NULL); - if (JS_IsException(attributes)) { - JS_FreeAtom(ctx, key); - return -1; - } - rme->attributes = attributes; - } - /* check for duplicate keys */ - ret = JS_HasProperty(ctx, rme->attributes, key); - if (ret != 0) { - if (ret < 0) { - JS_FreeAtom(ctx, key); - return -1; - } else { - js_parse_error(s, "duplicate with key"); - JS_FreeAtom(ctx, key); - return -1; - } + if (JS_IsUndefined(*pattributes)) { + JSValue attributes = JS_NewObjectProto(ctx, JS_NULL); + if (JS_IsException(attributes)) { + JS_FreeAtom(ctx, key); + return -1; } - ret = JS_DefinePropertyValue(ctx, rme->attributes, key, - js_dup(s->token.u.str.str), JS_PROP_C_W_E); + *pattributes = attributes; + } + /* check for duplicate keys */ + ret = JS_HasProperty(ctx, *pattributes, key); + if (ret != 0) { if (ret < 0) { JS_FreeAtom(ctx, key); return -1; + } else { + js_parse_error(s, "duplicate with key"); + JS_FreeAtom(ctx, key); + return -1; } } + ret = JS_DefinePropertyValue(ctx, *pattributes, key, + js_dup(s->token.u.str.str), JS_PROP_C_W_E); + if (ret < 0) { + JS_FreeAtom(ctx, key); + return -1; + } JS_FreeAtom(ctx, key); if (next_token(s)) return -1; @@ -42761,9 +44989,9 @@ static __exception int js_parse_with_clause(JSParseState *s, JSReqModuleEntry *r } } /* check attributes validity if checker function provided */ - if (!already_set && !JS_IsUndefined(rme->attributes) && + if (!JS_IsUndefined(*pattributes) && ctx->rt->module_check_attrs && - ctx->rt->module_check_attrs(ctx, ctx->rt->module_loader_opaque, rme->attributes) < 0) { + ctx->rt->module_check_attrs(ctx, ctx->rt->module_loader_opaque, *pattributes) < 0) { return -1; } return js_parse_expect(s, '}'); @@ -42773,6 +45001,7 @@ static __exception int js_parse_with_clause(JSParseState *s, JSReqModuleEntry *r static __exception int js_parse_from_clause(JSParseState *s, JSModuleDef *m) { JSAtom module_name; + JSValue attributes; int idx; if (!token_is_pseudo_keyword(s, JS_ATOM_from)) { @@ -42793,14 +45022,14 @@ static __exception int js_parse_from_clause(JSParseState *s, JSModuleDef *m) return -1; } - idx = add_req_module_entry(s->ctx, m, module_name); - JS_FreeAtom(s->ctx, module_name); - if (idx < 0) + if (js_parse_with_clause(s, &attributes)) { + JS_FreeValue(s->ctx, attributes); + JS_FreeAtom(s->ctx, module_name); return -1; - if (s->token.val == TOK_WITH) { - if (js_parse_with_clause(s, &m->req_module_entries[idx])) - return -1; } + idx = add_req_module_entry(s->ctx, m, module_name, attributes); + JS_FreeValue(s->ctx, attributes); + JS_FreeAtom(s->ctx, module_name); return idx; } @@ -42992,6 +45221,7 @@ static __exception int js_parse_export(JSParseState *s) case TOK_VAR: case TOK_LET: case TOK_CONST: + case TOK_USING: return js_parse_var(s, PF_IN_ACCEPTED, tok, /*export_flag*/true); default: return js_parse_error(s, "invalid export syntax"); @@ -43055,6 +45285,8 @@ static __exception int js_parse_import(JSParseState *s) first_import = m->import_entries_count; if (s->token.val == TOK_STRING) { + JSValue attributes; + module_name = JS_ValueToAtom(ctx, s->token.u.str.str); if (module_name == JS_ATOM_NULL) return -1; @@ -43062,14 +45294,16 @@ static __exception int js_parse_import(JSParseState *s) JS_FreeAtom(ctx, module_name); return -1; } - idx = add_req_module_entry(ctx, m, module_name); + if (js_parse_with_clause(s, &attributes)) { + JS_FreeValue(ctx, attributes); + JS_FreeAtom(ctx, module_name); + return -1; + } + idx = add_req_module_entry(ctx, m, module_name, attributes); + JS_FreeValue(ctx, attributes); JS_FreeAtom(ctx, module_name); if (idx < 0) return -1; - if (s->token.val == TOK_WITH) { - if (js_parse_with_clause(s, &m->req_module_entries[idx])) - return -1; - } } else { if (s->token.val == TOK_IDENT) { if (s->token.u.ident.is_reserved) { @@ -43245,6 +45479,10 @@ static JSFunctionDef *js_new_function_def(JSContext *ctx, fd->scope_count = 1; fd->scopes[0].first = -1; fd->scopes[0].parent = -1; + fd->scopes[0].has_using = 0; + fd->scopes[0].is_await_using = 0; + fd->scopes[0].using_label_catch = -1; + fd->scopes[0].using_label_end = -1; fd->scope_level = 0; /* 0: var/arg scope */ fd->scope_first = -1; fd->body_scope = -1; @@ -45508,7 +47746,16 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s) break; } goto no_change; - + case OP_lnot: + /* Transformation: lnot if_false|if_true -> if_true|if_false */ + if (code_match(&cc, pos_next, M2(OP_if_false, OP_if_true), -1)) { + dbuf_putc(&bc_out, cc.op ^ OP_if_false ^ OP_if_true); + dbuf_put_u32(&bc_out, cc.label); + pos_next = cc.pos; + s->jump_size++; + break; + } + goto no_change; case OP_goto: s->jump_size++; /* fall thru */ @@ -45606,6 +47853,62 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s) } break; + case OP_dispose_scope: + { + int scope_idx, scope = get_u16(bc_buf + pos + 1); + bool is_async = s->scopes[scope].is_await_using; + + for(scope_idx = s->scopes[scope].first; scope_idx >= 0;) { + JSVarDef *vd = &s->vars[scope_idx]; + if (vd->scope_level == scope) { + if (vd->var_kind == JS_VAR_USING) { + if (is_async) { + int label_catch = new_label_fd(s); + int label_end = new_label_fd(s); + if (label_catch < 0 || label_end < 0) { + dbuf_set_error(&bc_out); + break; + } + + dbuf_putc(&bc_out, OP_catch); + dbuf_put_u32(&bc_out, label_catch); + update_label(s, label_catch, 1); + s->jump_size++; + + dbuf_putc(&bc_out, OP_using_dispose_async); + dbuf_put_u16(&bc_out, scope_idx); + + dbuf_putc(&bc_out, OP_await); + dbuf_putc(&bc_out, OP_drop); + dbuf_putc(&bc_out, OP_drop); + + dbuf_putc(&bc_out, OP_goto); + dbuf_put_u32(&bc_out, label_end); + update_label(s, label_end, 1); + s->jump_size++; + + dbuf_putc(&bc_out, OP_label); + dbuf_put_u32(&bc_out, label_catch); + s->label_slots[label_catch].pos2 = bc_out.size; + + dbuf_putc(&bc_out, OP_using_dispose_merge); + + dbuf_putc(&bc_out, OP_label); + dbuf_put_u32(&bc_out, label_end); + s->label_slots[label_end].pos2 = bc_out.size; + } else { + dbuf_putc(&bc_out, OP_using_dispose); + dbuf_put_u16(&bc_out, scope_idx); + } + } + scope_idx = vd->scope_next; + } else { + break; + } + } + } + break; + case OP_set_name: { /* remove dummy set_name opcodes */ @@ -47231,8 +49534,8 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd) goto fail; function_size = sizeof(*b); - cpool_offset = function_size; - function_size += fd->cpool_count * sizeof(*fd->cpool); + cpool_offset = (function_size + 7) & ~7; + function_size = cpool_offset + fd->cpool_count * sizeof(*fd->cpool); vardefs_offset = function_size; function_size += (fd->arg_count + fd->var_count) * sizeof(*b->vardefs); closure_var_offset = function_size; @@ -47243,7 +49546,7 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd) b = js_mallocz(ctx, function_size); if (!b) goto fail; - b->header.ref_count = 1; + JS_REF_COUNT(b) = 1; b->byte_code_buf = (void *)((uint8_t*)b + byte_code_offset); b->byte_code_len = fd->byte_code.size; @@ -47364,7 +49667,7 @@ static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b) js_free_rt(rt, b->source); remove_gc_object(&b->header); - if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && b->header.ref_count != 0) { + if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && JS_REF_COUNT(b) != 0) { list_add_tail(&b->header.link, &rt->gc_zero_ref_count_list); } else { js_free_rt(rt, b); @@ -47416,6 +49719,7 @@ static __exception int js_parse_directives(JSParseState *s) case TOK_IF: case TOK_RETURN: case TOK_VAR: + case TOK_USING: case TOK_THIS: case TOK_DELETE: case TOK_TYPEOF: @@ -47980,25 +50284,59 @@ static __exception int js_parse_function_decl2(JSParseState *s, if (js_parse_function_check_names(s, fd, func_name)) goto fail; - while (s->token.val != '}') { - if (js_parse_source_element(s)) + { + BlockEnv using_be; + int has_using_be = 0; + + while (s->token.val != '}') { + if (js_parse_source_element(s)) + goto fail; + /* Check if a 'using' was encountered in the body scope */ + if (!has_using_be && fd->scopes[fd->body_scope].has_using) { + has_using_be = 1; + push_break_entry(fd, &using_be, JS_ATOM_NULL, -1, -1, 1); + using_be.has_using = true; + using_be.using_scope_level = fd->body_scope; + } + } + + /* save the function source code */ + fd->source_len = s->buf_ptr - ptr; + fd->source = js_strndup(ctx, (const char *)ptr, fd->source_len); + if (!fd->source) goto fail; - } - /* save the function source code */ - fd->source_len = s->buf_ptr - ptr; - fd->source = js_strndup(ctx, (const char *)ptr, fd->source_len); - if (!fd->source) - goto fail; + if (next_token(s)) { + /* consume the '}' */ + goto fail; + } - if (next_token(s)) { - /* consume the '}' */ - goto fail; - } + if (has_using_be) { + int label_catch = fd->scopes[fd->body_scope].using_label_catch; + int label_end = fd->scopes[fd->body_scope].using_label_end; - /* in case there is no return, add one */ - if (js_is_live_code(s)) { - emit_return(s, false); + pop_break_entry(fd); + + if (js_is_live_code(s)) { + emit_op(s, OP_drop); /* drop catch_offset */ + emit_op(s, OP_using_dispose_init); /* initial error_state */ + emit_op(s, OP_dispose_scope); + emit_u16(s, fd->body_scope); + emit_op(s, OP_using_dispose_end); + emit_return(s, false); + } + + emit_label(s, label_catch); + emit_op(s, OP_dispose_scope); + emit_u16(s, fd->body_scope); + emit_op(s, OP_throw); + + emit_label(s, label_end); + } else { + if (js_is_live_code(s)) { + emit_return(s, false); + } + } } done: s->cur_func = fd->parent; @@ -48136,6 +50474,8 @@ static __exception int js_parse_program(JSParseState *s) { JSFunctionDef *fd = s->cur_func; int idx; + BlockEnv using_be; + int has_using_be = 0; if (next_token(s)) return -1; @@ -48157,28 +50497,71 @@ static __exception int js_parse_program(JSParseState *s) while (s->token.val != TOK_EOF) { if (js_parse_source_element(s)) return -1; + /* Check if a 'using' was encountered at the body scope level */ + if (!has_using_be && fd->scopes[fd->body_scope].has_using) { + has_using_be = 1; + push_break_entry(fd, &using_be, JS_ATOM_NULL, -1, -1, 1); + using_be.has_using = true; + using_be.using_scope_level = fd->body_scope; + } } - if (!s->is_module) { - /* return the value of the hidden variable eval_ret_idx */ - if (fd->func_kind == JS_FUNC_ASYNC) { - /* wrap the return value in an object so that promises can - be safely returned */ - emit_op(s, OP_object); - emit_op(s, OP_dup); + if (has_using_be) { + int label_catch = fd->scopes[fd->body_scope].using_label_catch; + int label_end = fd->scopes[fd->body_scope].using_label_end; - emit_op(s, OP_get_loc); - emit_u16(s, fd->eval_ret_idx); + pop_break_entry(fd); - emit_op(s, OP_put_field); - emit_atom(s, JS_ATOM_value); + if (js_is_live_code(s)) { + /* Normal path: drop catch_offset, dispose, then return */ + emit_op(s, OP_drop); /* drop catch_offset */ + emit_op(s, OP_using_dispose_init); /* initial error_state */ + emit_op(s, OP_dispose_scope); + emit_u16(s, fd->body_scope); + emit_op(s, OP_using_dispose_end); + } + + if (!s->is_module) { + if (fd->func_kind == JS_FUNC_ASYNC) { + emit_op(s, OP_object); + emit_op(s, OP_dup); + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + emit_op(s, OP_put_field); + emit_atom(s, JS_ATOM_value); + } else { + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + } + emit_return(s, true); } else { - emit_op(s, OP_get_loc); - emit_u16(s, fd->eval_ret_idx); + emit_return(s, false); } - emit_return(s, true); + + /* Catch handler */ + emit_label(s, label_catch); + emit_op(s, OP_dispose_scope); + emit_u16(s, fd->body_scope); + emit_op(s, OP_throw); + + emit_label(s, label_end); } else { - emit_return(s, false); + if (!s->is_module) { + if (fd->func_kind == JS_FUNC_ASYNC) { + emit_op(s, OP_object); + emit_op(s, OP_dup); + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + emit_op(s, OP_put_field); + emit_atom(s, JS_ATOM_value); + } else { + emit_op(s, OP_get_loc); + emit_u16(s, fd->eval_ret_idx); + } + emit_return(s, true); + } else { + emit_return(s, false); + } } return 0; @@ -48197,6 +50580,7 @@ static void js_parse_init(JSContext *ctx, JSParseState *s, s->col_num = 1; s->buf_start = s->buf_ptr = (const uint8_t *)input; s->buf_end = s->buf_ptr + input_len; + s->line_start = s->buf_ptr; s->mark = s->buf_ptr + min_int(1, input_len); s->eol = s->buf_ptr; s->token.val = ' '; @@ -48597,7 +50981,7 @@ typedef enum BCTagEnum { BC_TAG_SYMBOL, } BCTagEnum; -#define BC_VERSION 25 +#define BC_VERSION 29 typedef struct BCWriterState { JSContext *ctx; @@ -49079,7 +51463,7 @@ static int JS_WriteObjectTag(BCWriterState *s, JSValueConst obj) for(pass = 0; pass < 2; pass++) { if (pass == 1) bc_put_leb128(s, prop_count); - for(i = 0, pr = sh->prop; i < sh->prop_count; i++, pr++) { + for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) { atom = pr->atom; if (atom != JS_ATOM_NULL && (pr->flags & JS_PROP_ENUMERABLE)) { if (pr->flags & JS_PROP_TMASK) { @@ -49341,10 +51725,12 @@ static int JS_WriteObjectAtoms(BCWriterState *s) for(i = 0; i < s->idx_to_atom_count; i++) { JSAtom atom = s->idx_to_atom[i]; if (__JS_AtomIsConst(atom)) { - bc_put_u8(s, 0 /* the type */); - /* TODO(saghul): encoding for tagged integers and keyword-ish atoms could be - more efficient. */ - bc_put_u32(s, atom); + if (atom < 64) { + bc_put_u8(s, 0x80 | atom); + } else { + bc_put_u8(s, 0xC0 | (atom & 0x3F)); + bc_put_leb128(s, atom >> 6); + } } else { JSAtomStruct *p = rt->atom_array[atom]; uint8_t type = p->atom_type; @@ -49735,9 +52121,8 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b, static JSValue JS_ReadBigInt(BCReaderState *s) { - JSValue obj = JS_UNDEFINED; uint32_t len, i, n; - JSBigInt *p; + JSBigInt *p = NULL; js_limb_t v; uint8_t v8; @@ -49776,7 +52161,7 @@ static JSValue JS_ReadBigInt(BCReaderState *s) bc_read_trace(s, "}\n"); return JS_CompactBigInt(s->ctx, p); fail: - JS_FreeValue(s->ctx, obj); + js_free(s->ctx, p); return JS_EXCEPTION; } @@ -49811,7 +52196,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s) int closure_var_offset, vardefs_offset; memset(&bc, 0, sizeof(bc)); - bc.header.ref_count = 1; //bc.gc_header.mark = 0; if (bc_get_u16(s, &v16)) @@ -49853,8 +52237,8 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s) goto fail; function_size = sizeof(*b); - cpool_offset = function_size; - function_size += bc.cpool_count * sizeof(*bc.cpool); + cpool_offset = (function_size + 7) & ~7; + function_size = cpool_offset + bc.cpool_count * sizeof(*bc.cpool); vardefs_offset = function_size; function_size += local_count * sizeof(*bc.vardefs); closure_var_offset = function_size; @@ -49868,7 +52252,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s) memcpy(b, &bc, sizeof(*b)); bc.func_name = JS_ATOM_NULL; - b->header.ref_count = 1; + JS_REF_COUNT(b) = 1; if (local_count != 0) { b->vardefs = (void *)((uint8_t*)b + vardefs_offset); } @@ -50071,6 +52455,7 @@ static JSValue JS_ReadModule(BCReaderState *s) for(i = 0; i < m->req_module_entries_count; i++) { JSReqModuleEntry *rme = &m->req_module_entries[i]; JSModuleDef **pm = &rme->module; + rme->attributes = JS_UNDEFINED; if (bc_get_atom(s, &rme->module_name)) goto fail; // Resolves a module either from the cache or by requesting @@ -50320,7 +52705,7 @@ static JSValue JS_ReadArrayBuffer(BCReaderState *s) byte_length, pmax_byte_length, JS_CLASS_ARRAY_BUFFER, (uint8_t*)s->ptr, - js_array_buffer_free, NULL, + js_array_buffer_realloc, NULL, /*alloc_flag*/true); if (JS_IsException(obj)) goto fail; @@ -50392,7 +52777,8 @@ static JSValue JS_ReadRegExp(BCReaderState *s) return JS_EXCEPTION; } - if (bc->is_wide_char) { + if (bc->is_wide_char || + lre_check_bytecode(str8(bc), bc->len) != 0) { js_free_string(ctx->rt, pattern); js_free_string(ctx->rt, bc); return JS_ThrowInternalError(ctx, "bad regexp bytecode"); @@ -50633,9 +53019,11 @@ static int JS_ReadObjectAtoms(BCReaderState *s) if (bc_get_u8(s, &type)) { return -1; } - if (type == 0) { - if (bc_get_u32(s, &atom)) + if (type & 0x80) { + atom = 0; + if ((type & 0x40) && bc_get_leb128(s, &atom)) return -1; + atom = (atom << 6) | (type & 0x3F); if (!__JS_AtomIsConst(atom)) { JS_ThrowInternalError(s->ctx, "out of range atom"); return -1; @@ -51001,6 +53389,20 @@ static JSValue JS_NewGlobalCConstructor(JSContext *ctx, const char *name, return func_obj; } +static JSValue JS_NewGlobalCConstructorMagic(JSContext *ctx, const char *name, + JSCFunctionMagic *func, int length, + JSValueConst proto, int magic) +{ + /* Used to squelch a -Wcast-function-type warning. */ + JSCFunctionType ft = { .constructor_magic = func }; + JSValue func_obj; + + func_obj = JS_NewCFunction2(ctx, ft.constructor, name, length, + JS_CFUNC_constructor_or_func_magic, magic); + JS_NewGlobalCConstructor2(ctx, func_obj, name, proto); + return func_obj; +} + static JSValue JS_NewObjectProtoList(JSContext *ctx, JSValueConst proto, const JSCFunctionListEntry *fields, int n_fields) { @@ -51031,7 +53433,7 @@ static JSValue JS_NewCConstructor(JSContext *ctx, int class_id, const char *name const JSCFunctionListEntry *proto_fields, int n_proto_fields, int flags) { - JSValue ctor = JS_UNDEFINED, proto, parent_proto; + JSValue ctor = JS_UNDEFINED, proto, parent_proto, *class_proto; int proto_class_id, proto_flags, ctor_flags; proto_flags = 0; @@ -51062,8 +53464,12 @@ static JSValue JS_NewCConstructor(JSContext *ctx, int class_id, const char *name n_proto_fields + 1); if (JS_IsException(proto)) goto fail; - if (class_id >= 0) - ctx->class_proto[class_id] = js_dup(proto); + if (class_id >= 0) { + class_proto = &ctx->class_proto[class_id]; + if (!JS_IsNull(*class_proto)) + JS_FreeValue(ctx, *class_proto); + *class_proto = js_dup(proto); + } } if (JS_SetPropertyFunctionList(ctx, proto, proto_fields, n_proto_fields)) goto fail; @@ -51172,7 +53578,7 @@ JSValue JS_ToObject(JSContext *ctx, JSValueConst val) if (!JS_IsException(obj)) { JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, JS_NewInt32(ctx, JS_VALUE_GET_STRING(str)->len), 0); - JS_SetObjectData(ctx, obj, JS_DupValue(ctx, str)); + JS_SetObjectData(ctx, obj, js_dup(str)); } JS_FreeValue(ctx, str); return obj; @@ -51262,8 +53668,9 @@ static int js_obj_to_desc(JSContext *ctx, JSPropertyDescriptor *d, if (present) { flags |= JS_PROP_HAS_GET; getter = JS_GetProperty(ctx, desc, JS_ATOM_get); - if (JS_IsException(getter) || - !(JS_IsUndefined(getter) || JS_IsFunction(ctx, getter))) { + if (JS_IsException(getter)) + goto fail; + if (!(JS_IsUndefined(getter) || JS_IsFunction(ctx, getter))) { JS_ThrowTypeError(ctx, "Getter must be a function"); goto fail; } @@ -51274,8 +53681,9 @@ static int js_obj_to_desc(JSContext *ctx, JSPropertyDescriptor *d, if (present) { flags |= JS_PROP_HAS_SET; setter = JS_GetProperty(ctx, desc, JS_ATOM_set); - if (JS_IsException(setter) || - !(JS_IsUndefined(setter) || JS_IsFunction(ctx, setter))) { + if (JS_IsException(setter)) + goto fail; + if (!(JS_IsUndefined(setter) || JS_IsFunction(ctx, setter))) { JS_ThrowTypeError(ctx, "Setter must be a function"); goto fail; } @@ -51857,7 +54265,7 @@ static JSValue js_object_preventExtensions(JSContext *ctx, JSValueConst this_val return js_bool(ret); } else { if (!ret) - return JS_ThrowTypeError(ctx, "proxy preventExtensions handler returned false"); + return JS_ThrowTypeError(ctx, "Cannot prevent extensions"); return js_dup(obj); } } @@ -52634,8 +55042,16 @@ static JSValue js_function_bind(JSContext *ctx, JSValueConst this_val, if (check_function(ctx, this_val)) return JS_EXCEPTION; - func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto, + JSValue prototype = JS_GetPrototype(ctx, this_val); + + if (JS_IsException(prototype)) + return JS_EXCEPTION; + + func_obj = JS_NewObjectProtoClass(ctx, prototype, JS_CLASS_BOUND_FUNCTION); + + JS_FreeValue(ctx, prototype); + if (JS_IsException(func_obj)) return JS_EXCEPTION; p = JS_VALUE_GET_OBJ(func_obj); @@ -52846,10 +55262,19 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target, JS_FreeValue(ctx, proto); if (JS_IsException(obj)) return obj; - if (magic == JS_AGGREGATE_ERROR) { + switch (magic) { + case JS_AGGREGATE_ERROR: message = argv[1]; opts = 2; - } else { + break; + case JS_SUPPRESSED_ERROR: + message = argv[2]; + opts = 3; + break; + case JS_INTERNAL_ERROR: + js_set_uncatchable_error(obj, true); + // fallthru + default: message = argv[0]; opts = 1; } @@ -52883,6 +55308,15 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); } + if (magic == JS_SUPPRESSED_ERROR) { + JS_DefinePropertyValue(ctx, obj, JS_ATOM_error, + js_dup(argv[0]), + JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + JS_DefinePropertyValue(ctx, obj, JS_ATOM_suppressed, + js_dup(argv[1]), + JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + } + /* skip the Error() function in the backtrace */ build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0, JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL); return obj; @@ -52920,10 +55354,50 @@ static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val, return JS_ConcatString(ctx, name, msg); } +static JSValue js_error_get_stack(JSContext *ctx, JSValueConst this_val) +{ + JSObject *p; + + if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT) + return JS_ThrowTypeErrorNotAnObject(ctx); + p = JS_VALUE_GET_OBJ(this_val); + if (p->class_id != JS_CLASS_ERROR) + return JS_UNDEFINED; + return js_dup(p->u.object_data); +} + +static JSValue js_error_set_stack(JSContext *ctx, JSValueConst this_val, + JSValueConst value) +{ + int ret, flags; + + if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT) + return JS_ThrowTypeErrorNotAnObject(ctx); + if (!JS_IsString(value)) + return JS_ThrowTypeError(ctx, "Error.prototype.stack setter expects a string"); + if (js_same_value(ctx, this_val, ctx->class_proto[JS_CLASS_ERROR])) + return JS_ThrowTypeError(ctx, "Error.prototype.stack setter called on the home object"); + ret = JS_GetOwnPropertyFlagsInternal(ctx, &flags, JS_VALUE_GET_OBJ(this_val), + JS_ATOM_stack); + if (ret < 0) + return JS_EXCEPTION; + if (ret == 0) { + if (JS_DefinePropertyValue(ctx, this_val, JS_ATOM_stack, js_dup(value), + JS_PROP_C_W_E | JS_PROP_THROW) < 0) + return JS_EXCEPTION; + } else { + if (JS_SetPropertyInternal2(ctx, this_val, JS_ATOM_stack, js_dup(value), + this_val, JS_PROP_THROW) < 0) + return JS_EXCEPTION; + } + return JS_UNDEFINED; +} + static const JSCFunctionListEntry js_error_proto_funcs[] = { JS_CFUNC_DEF("toString", 0, js_error_toString ), JS_PROP_STRING_DEF("name", "Error", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ), JS_PROP_STRING_DEF("message", "", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ), + JS_CGETSET_DEF("stack", js_error_get_stack, js_error_set_stack ), }; static JSValue js_error_isError(JSContext *ctx, JSValueConst this_val, @@ -53371,11 +55845,6 @@ static JSValue js_array_with(JSContext *ctx, JSValueConst this_val, if (js_get_length64(ctx, &len, obj)) goto exception; - if (len > UINT32_MAX) { - JS_ThrowRangeError(ctx, "invalid array length"); - goto exception; - } - if (JS_ToInt64Sat(ctx, &idx, argv[0])) goto exception; @@ -53387,15 +55856,11 @@ static JSValue js_array_with(JSContext *ctx, JSValueConst this_val, goto exception; } - arr = JS_NewArray(ctx); + arr = js_allocate_fast_array(ctx, len); if (JS_IsException(arr)) goto exception; p = JS_VALUE_GET_OBJ(arr); - if (expand_fast_array(ctx, p, len) < 0) - goto exception; - p->u.array.count = len; - i = 0; pval = p->u.array.u.values; if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) { @@ -53407,21 +55872,14 @@ static JSValue js_array_with(JSContext *ctx, JSValueConst this_val, } else { for (; i < idx; i++, pval++) if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) - goto fill_and_fail; + goto exception; *pval = js_dup(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; + if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) goto exception; - } } } - if (JS_SetProperty(ctx, arr, JS_ATOM_length, js_int64(len)) < 0) - goto exception; - ret = arr; arr = JS_UNDEFINED; @@ -53541,7 +55999,8 @@ static int js_typed_array_get_length_unsafe(JSContext *ctx, JSValueConst obj) static JSValue js_typed_array___speciesCreate(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv); + int argc, JSValueConst *argv, + bool require_mutable); static JSValue js_array_every(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int special) @@ -53595,7 +56054,7 @@ static JSValue js_array_every(JSContext *ctx, JSValueConst this_val, case special_map | special_TA: args[0] = obj; args[1] = js_int32(len); - ret = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args); + ret = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args, true); if (JS_IsException(ret)) goto exception; break; @@ -53608,6 +56067,8 @@ static JSValue js_array_every(JSContext *ctx, JSValueConst this_val, n = 0; for(k = 0; k < len; k++) { + if (js_poll_interrupts(ctx)) + goto exception; if (special & special_TA) { val = JS_GetPropertyInt64(ctx, obj, k); if (JS_IsException(val)) @@ -53672,7 +56133,7 @@ static JSValue js_array_every(JSContext *ctx, JSValueConst this_val, JSValue arr; args[0] = obj; args[1] = js_int32(n); - arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args); + arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args, true); if (JS_IsException(arr)) goto exception; args[0] = ret; @@ -53729,6 +56190,8 @@ static JSValue js_array_reduce(JSContext *ctx, JSValueConst this_val, acc = js_dup(argv[1]); } else { for(;;) { + if (js_poll_interrupts(ctx)) + goto exception; if (k >= len) { JS_ThrowTypeError(ctx, "empty array"); goto exception; @@ -53750,6 +56213,8 @@ static JSValue js_array_reduce(JSContext *ctx, JSValueConst this_val, } } for (; k < len; k++) { + if (js_poll_interrupts(ctx)) + goto exception; k1 = (special & special_reduceRight) ? len - k - 1 : k; if (special & special_TA) { val = JS_GetPropertyInt64(ctx, obj, k1); @@ -53851,6 +56316,8 @@ static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val, } } for (; n < len; n++) { + if (js_poll_interrupts(ctx)) + goto exception; val = JS_GetPropertyInt64(ctx, obj, n); if (JS_IsException(val)) goto exception; @@ -53897,6 +56364,8 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValueConst this_val, } } for (; n < len; n++) { + if (js_poll_interrupts(ctx)) + goto exception; int present = JS_TryGetPropertyInt64(ctx, obj, n, &val); if (present < 0) goto exception; @@ -53926,6 +56395,7 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val, int64_t len, n; JSValue *arrp; uint32_t count; + int present; obj = JS_ToObject(ctx, this_val); if (js_get_length64(ctx, &len, obj)) @@ -53946,7 +56416,9 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val, } } for (; n >= 0; n--) { - int present = JS_TryGetPropertyInt64(ctx, obj, n, &val); + if (js_poll_interrupts(ctx)) + goto exception; + present = JS_TryGetPropertyInt64(ctx, obj, n, &val); if (present < 0) goto exception; if (present) { @@ -54009,6 +56481,8 @@ static JSValue js_array_find(JSContext *ctx, JSValueConst this_val, // TODO(bnoordhuis) add fast path for fast arrays for(; k != end; k += dir) { + if (js_poll_interrupts(ctx)) + goto exception; index_val = js_int64(k); val = JS_GetPropertyValue(ctx, obj, index_val); if (JS_IsException(val)) @@ -54196,10 +56670,11 @@ static JSValue js_array_push(JSContext *ctx, JSValueConst this_val, ctx->std_array_prototype)) { uint32_t array_len, new_len; if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT && - (p->shape->prop->flags & JS_PROP_WRITABLE))) { + (get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE))) { array_len = JS_VALUE_GET_INT(p->prop[0].u.value); new_len = array_len + argc; - if (likely(new_len >= array_len)) { /* no overflow */ + if (likely(array_len == p->u.array.count && + new_len >= array_len && new_len <= (uint32_t)INT32_MAX)) { /* no overflow and within fast-array bounds */ if (unlikely(new_len > p->u.array.u1.size)) { if (expand_fast_array(ctx, p, new_len)) return JS_EXCEPTION; @@ -54208,7 +56683,7 @@ static JSValue js_array_push(JSContext *ctx, JSValueConst this_val, p->u.array.u.values[array_len + i] = js_dup(argv[i]); } p->u.array.count = new_len; - p->prop[0].u.value = js_int32(new_len); + p->prop[0].u.value = js_uint32(new_len); return js_int32(new_len); } } @@ -54331,20 +56806,12 @@ static JSValue js_array_toReversed(JSContext *ctx, JSValueConst this_val, if (js_get_length64(ctx, &len, obj)) goto exception; - if (len > UINT32_MAX) { - JS_ThrowRangeError(ctx, "invalid array length"); - goto exception; - } - - arr = JS_NewArray(ctx); + arr = js_allocate_fast_array(ctx, len); if (JS_IsException(arr)) goto exception; if (len > 0) { p = JS_VALUE_GET_OBJ(arr); - if (expand_fast_array(ctx, p, len) < 0) - goto exception; - p->u.array.count = len; i = len - 1; pval = p->u.array.u.values; @@ -54354,17 +56821,10 @@ static JSValue js_array_toReversed(JSContext *ctx, JSValueConst this_val, } 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; + if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) goto exception; - } } } - - if (JS_SetProperty(ctx, arr, JS_ATOM_length, js_int64(len)) < 0) - goto exception; } ret = arr; @@ -54490,8 +56950,6 @@ static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val, int64_t i, j, len, newlen, start, add, del; uint32_t count32; - pval = NULL; - last = NULL; ret = JS_EXCEPTION; arr = JS_UNDEFINED; @@ -54516,17 +56974,12 @@ static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val, add = argc - 2; newlen = len + add - del; - if (newlen > UINT32_MAX) { - // Per spec: TypeError if newlen >= 2**53, RangeError below - if (newlen > MAX_SAFE_INTEGER) { - JS_ThrowTypeError(ctx, "invalid array length"); - } else { - JS_ThrowRangeError(ctx, "invalid array length"); - } + if (newlen > MAX_SAFE_INTEGER) { + JS_ThrowTypeError(ctx, "invalid array length"); goto exception; } - arr = JS_NewArray(ctx); + arr = js_allocate_fast_array(ctx, newlen); if (JS_IsException(arr)) goto exception; @@ -54534,10 +56987,6 @@ static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val, goto done; p = JS_VALUE_GET_OBJ(arr); - if (expand_fast_array(ctx, p, newlen) < 0) - goto exception; - - p->u.array.count = newlen; pval = &p->u.array.u.values[0]; last = &p->u.array.u.values[newlen]; @@ -54561,17 +57010,11 @@ static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val, assert(pval == last); - if (JS_SetProperty(ctx, arr, JS_ATOM_length, js_int64(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; @@ -54748,11 +57191,6 @@ static int js_array_cmp_generic(const void *a, const void *b, void *opaque) { return 0; if (psc->has_method) { - /* custom sort function is specified as returning 0 for identical - * objects: avoid method call overhead. - */ - if (!memcmp(&ap->val, &bp->val, sizeof(ap->val))) - goto cmp_same; argv[0] = ap->val; argv[1] = bp->val; res = JS_Call(ctx, psc->method, JS_UNDEFINED, 2, argv); @@ -54787,7 +57225,6 @@ static int js_array_cmp_generic(const void *a, const void *b, void *opaque) { } if (cmp != 0) return cmp; -cmp_same: /* make sort stable: compare array offsets */ return (ap->pos > bp->pos) - (ap->pos < bp->pos); @@ -54818,13 +57255,12 @@ static JSValue js_array_sort(JSContext *ctx, JSValueConst this_val, /* XXX: should special case fast arrays */ for (i = 0; i < len; i++) { if (pos >= array_size) { - size_t new_size, slack; + size_t new_size; ValueSlot *new_array; new_size = (array_size + (array_size >> 1) + 31) & ~15; - new_array = js_realloc2(ctx, array, new_size * sizeof(*array), &slack); + new_array = js_realloc(ctx, array, new_size * sizeof(*array)); if (new_array == NULL) goto exception; - new_size += slack / sizeof(*new_array); array = new_array; array_size = new_size; } @@ -54904,21 +57340,12 @@ static JSValue js_array_toSorted(JSContext *ctx, JSValueConst this_val, if (js_get_length64(ctx, &len, obj)) goto exception; - if (len > UINT32_MAX) { - JS_ThrowRangeError(ctx, "invalid array length"); - goto exception; - } - - arr = JS_NewArray(ctx); + arr = js_allocate_fast_array(ctx, len); if (JS_IsException(arr)) goto exception; if (len > 0) { p = JS_VALUE_GET_OBJ(arr); - if (expand_fast_array(ctx, p, len) < 0) - goto exception; - p->u.array.count = len; - i = 0; pval = p->u.array.u.values; if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) { @@ -54926,16 +57353,10 @@ static JSValue js_array_toSorted(JSContext *ctx, JSValueConst this_val, *pval = js_dup(arrp[i]); } else { for (; i < len; i++, pval++) { - if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) { - for (; i < len; i++, pval++) - *pval = JS_UNDEFINED; + if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval)) goto exception; - } } } - - if (JS_SetProperty(ctx, arr, JS_ATOM_length, js_int64(len)) < 0) - goto exception; } ret = js_array_sort(ctx, arr, argc, argv); @@ -55154,6 +57575,8 @@ static const JSCFunctionListEntry js_iterator_wrap_proto_funcs[] = { JS_ITERATOR_NEXT_DEF("return", 0, js_iterator_wrap_next, GEN_MAGIC_RETURN ), }; +static int check_iterator(JSContext *ctx, JSValueConst obj); + static JSValue js_iterator_constructor_getset(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, @@ -55162,14 +57585,27 @@ static JSValue js_iterator_constructor_getset(JSContext *ctx, { int ret; - if (argc > 0) { // if setter - if (!JS_IsObject(argv[0])) - return JS_ThrowTypeErrorNotAnObject(ctx); - ret = JS_DefinePropertyValue(ctx, this_val, JS_ATOM_constructor, - js_dup(argv[0]), - JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + if (magic) { // setter (the getter is registered with magic == 0) + // SetterThatIgnoresPrototypeProperties(%Iterator.prototype%, "constructor", v) + // argv[0] is the assigned value; it defaults to undefined because the + // setter is registered with length 1 (see JS_AddIntrinsicBaseObjects). + if (check_iterator(ctx, this_val) < 0) + return JS_EXCEPTION; + if (js_same_value(ctx, this_val, ctx->class_proto[JS_CLASS_ITERATOR])) + return JS_ThrowTypeError(ctx, "Cannot assign to read only property"); + ret = JS_GetOwnProperty(ctx, NULL, this_val, JS_ATOM_constructor); if (ret < 0) return JS_EXCEPTION; + if (ret) { + if (JS_SetProperty(ctx, this_val, JS_ATOM_constructor, js_dup(argv[0])) < 0) + return JS_EXCEPTION; + } else { + if (JS_DefinePropertyValue(ctx, this_val, JS_ATOM_constructor, + js_dup(argv[0]), + JS_PROP_C_W_E | JS_PROP_THROW) < 0) + return JS_EXCEPTION; + } + return JS_UNDEFINED; } return js_dup(func_data[0]); } @@ -55190,7 +57626,7 @@ static JSValue js_iterator_constructor(JSContext *ctx, JSValueConst new_target, typedef struct JSIteratorConcatData { uint32_t index : 31; // elements (not pairs!) in values[] array - uint32_t unused : 1; + uint32_t done : 1; uint32_t count : 31; uint32_t running : 1; JSValue iter, next, values[]; // array of (object, method) pairs @@ -55235,6 +57671,10 @@ static JSValue js_iterator_concat_next(JSContext *ctx, JSValueConst this_val, return JS_EXCEPTION; if (it->running) return JS_ThrowTypeError(ctx, "already running"); + if (it->done) { + *pdone = true; + return JS_UNDEFINED; + } it->running = true; next: if (it->index >= it->count) { @@ -55288,35 +57728,47 @@ static JSValue js_iterator_concat_next(JSContext *ctx, JSValueConst this_val, } val = JS_GetProperty(ctx, item, JS_ATOM_value); JS_FreeValue(ctx, item); + if (JS_IsException(val)) + goto fail; *pdone = false; done: it->running = false; return val; fail: + /* an abrupt completion finishes the iterator */ + it->done = true; val = JS_EXCEPTION; goto done; } static JSValue js_iterator_concat_return(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv) + int argc, JSValueConst *argv, + int *pdone, int magic) { JSIteratorConcatData *it; - JSValue ret, *pval; + JSValue *pval; + int res; it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_CONCAT); if (!it) return JS_EXCEPTION; if (it->running) return JS_ThrowTypeError(ctx, "already running"); - ret = JS_UNDEFINED; + if (it->done) { + /* a completed generator does not run IteratorClose */ + *pdone = true; + return JS_UNDEFINED; + } + res = 0; if (!JS_IsUndefined(it->iter)) { - ret = JS_GetProperty(ctx, it->iter, JS_ATOM_return); - if (JS_IsException(ret)) - return JS_EXCEPTION; + /* IteratorClose: leaves the iterator alone when it has no .return + method and rejects a result that is not an object */ it->running = true; - ret = JS_CallFree(ctx, ret, it->iter, 0, NULL); + res = JS_IteratorClose(ctx, it->iter, /*is_exception_pending*/false); it->running = false; } + /* the iterator is closed either way, the remaining iterables are never + opened */ while (it->index < it->count) { pval = &it->values[it->index++]; JS_FreeValue(ctx, *pval); @@ -55326,15 +57778,13 @@ static JSValue js_iterator_concat_return(JSContext *ctx, JSValueConst this_val, JS_FreeValue(ctx, it->next); it->iter = JS_UNDEFINED; it->next = JS_UNDEFINED; - return ret; + it->done = true; + *pdone = true; + if (res < 0) + return JS_EXCEPTION; + return JS_UNDEFINED; } -static const JSCFunctionListEntry js_iterator_concat_proto_funcs[] = { - JS_ITERATOR_NEXT_DEF("next", 1, js_iterator_concat_next, 0 ), - JS_CFUNC_DEF("return", 1, js_iterator_concat_return ), - JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Iterator Concat", JS_PROP_CONFIGURABLE ), -}; - static JSValue js_iterator_concat(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -55345,6 +57795,7 @@ static JSValue js_iterator_concat(JSContext *ctx, JSValueConst this_val, if (!it) return JS_EXCEPTION; it->running = false; + it->done = false; it->index = 0; it->count = 0; it->iter = JS_UNDEFINED; @@ -55381,50 +57832,58 @@ static JSValue js_iterator_concat(JSContext *ctx, JSValueConst this_val, static JSValue js_iterator_from(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { - JSValue method, iter; + JSValue method, iter, next, wrapper; JSIteratorWrapData *it; int ret; JSValueConst obj = argv[0]; - if (JS_IsString(obj)) { - method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator); - if (JS_IsException(method)) - return JS_EXCEPTION; - return JS_CallFree(ctx, method, obj, 0, NULL); - } - if (!JS_IsObject(obj)) + if (!JS_IsObject(obj) && !JS_IsString(obj)) return JS_ThrowTypeError(ctx, "Iterator.from called on non-object"); - ret = JS_OrdinaryIsInstanceOf(ctx, obj, ctx->iterator_ctor); - if (ret < 0) - return JS_EXCEPTION; - if (ret) - return js_dup(obj); method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator); if (JS_IsException(method)) return JS_EXCEPTION; if (JS_IsNull(method) || JS_IsUndefined(method)) { - method = JS_GetProperty(ctx, obj, JS_ATOM_next); - if (JS_IsException(method)) - return JS_EXCEPTION; - iter = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_WRAP); - if (JS_IsException(iter)) - goto fail; - it = js_malloc(ctx, sizeof(*it)); - if (!it) - goto fail; - it->wrapped_iter = js_dup(obj); - it->wrapped_next = method; - JS_SetOpaqueInternal(iter, it); + iter = js_dup(obj); } else { iter = JS_GetIterator2(ctx, obj, method); - JS_FreeValue(ctx, method); - if (JS_IsException(iter)) - return JS_EXCEPTION; } - return iter; -fail: JS_FreeValue(ctx, method); + if (JS_IsException(iter)) + return JS_EXCEPTION; + if (!JS_IsObject(iter)) { + JS_FreeValue(ctx, iter); + return JS_ThrowTypeErrorNotAnObject(ctx); + } + + /* GetIteratorDirect */ + next = JS_GetProperty(ctx, iter, JS_ATOM_next); + if (JS_IsException(next)) { + JS_FreeValue(ctx, iter); + return JS_EXCEPTION; + } + ret = JS_OrdinaryIsInstanceOf(ctx, iter, ctx->iterator_ctor); + if (ret < 0) + goto fail; + if (ret) { + JS_FreeValue(ctx, next); + return iter; + } + + wrapper = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_WRAP); + if (JS_IsException(wrapper)) + goto fail; + it = js_malloc(ctx, sizeof(*it)); + if (!it) { + JS_FreeValue(ctx, wrapper); + goto fail; + } + it->wrapped_iter = iter; + it->wrapped_next = next; + JS_SetOpaqueInternal(wrapper, it); + return wrapper; +fail: JS_FreeValue(ctx, iter); + JS_FreeValue(ctx, next); return JS_EXCEPTION; } @@ -55442,10 +57901,15 @@ typedef struct JSIteratorHelperData { JSValue next; JSValue func; // predicate (filter) or mapper (flatMap, map) JSValue inner; // innerValue (flatMap) - int64_t count; // limit (drop, take) or counter (filter, map, flatMap) + JSValue *buf; // buffered values (chunks, windows) + int64_t count; // limit (drop, take), counter (filter, map, flatMap) or + // chunk/window size (chunks, windows) + int buf_count; // number of values in |buf| + int buf_size; // allocated size of |buf| JSIteratorHelperKindEnum kind : 8; uint8_t executing : 1; uint8_t done : 1; + uint8_t allow_partial : 1; // yield a partial final window (windows) } JSIteratorHelperData; static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, @@ -55454,14 +57918,56 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, JSValueConst func; JSValue obj, method; int64_t count; + bool allow_partial; JSIteratorHelperData *it; if (check_iterator(ctx, this_val) < 0) return JS_EXCEPTION; func = JS_UNDEFINED; count = 0; + allow_partial = false; switch(magic) { + case JS_ITERATOR_HELPER_KIND_CHUNKS: + case JS_ITERATOR_HELPER_KIND_WINDOWS: + { + const char *name; + double d; + bool ok; + + name = magic == JS_ITERATOR_HELPER_KIND_CHUNKS ? "chunkSize" + : "windowSize"; + // note: no coercion, the argument must be an integral number + d = NAN; + if (JS_IsNumber(argv[0]) && JS_ToFloat64(ctx, &d, argv[0])) + goto fail; + if (!isfinite(d) || trunc(d) != d) { + JS_ThrowTypeError(ctx, "%s is not an integral number", name); + goto fail; + } + if (d < 1 || d > UINT32_MAX) { + JS_ThrowRangeError(ctx, "%s out of range", name); + goto fail; + } + count = (int64_t)d; + if (magic == JS_ITERATOR_HELPER_KIND_WINDOWS && argc > 1 && + !JS_IsUndefined(argv[1])) { + ok = false; + if (JS_IsString(argv[1])) { + const char *str = JS_ToCString(ctx, argv[1]); + if (!str) + goto fail; + allow_partial = !strcmp(str, "allow-partial"); + ok = allow_partial || !strcmp(str, "only-full"); + JS_FreeCString(ctx, str); + } + if (!ok) { + JS_ThrowTypeError(ctx, "bad undersized option"); + goto fail; + } + } + } + break; case JS_ITERATOR_HELPER_KIND_DROP: case JS_ITERATOR_HELPER_KIND_TAKE: { @@ -55470,30 +57976,16 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, 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); - goto fail; - } - if (isnan(dlimit)) { - JS_FreeValue(ctx, v); + JS_ToFloat64Free(ctx, &dlimit, v); + if (isnan(dlimit)) goto range_error; - } - if (!isfinite(dlimit)) { - JS_FreeValue(ctx, v); - if (dlimit < 0) - 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) + if (isfinite(dlimit) && dlimit > MAX_SAFE_INTEGER) goto range_error; + dlimit = trunc(dlimit); + if (dlimit < 0) + goto range_error; + // +Infinity means "no limit"; use MAX_SAFE_INTEGER + count = isfinite(dlimit) ? (int64_t)dlimit : MAX_SAFE_INTEGER; } break; case JS_ITERATOR_HELPER_KIND_FILTER: @@ -55510,9 +58002,11 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, break; } + /* GetIteratorDirect: an exception here propagates as is, the iterator + object is only closed for a bad argument */ method = JS_GetProperty(ctx, this_val, JS_ATOM_next); if (JS_IsException(method)) - goto fail; + return JS_EXCEPTION; obj = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_HELPER); if (JS_IsException(obj)) { JS_FreeValue(ctx, method); @@ -55529,9 +58023,13 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val, it->func = js_dup(func); it->next = method; it->inner = JS_UNDEFINED; + it->buf = NULL; it->count = count; + it->buf_count = 0; + it->buf_size = 0; it->executing = 0; it->done = 0; + it->allow_partial = allow_partial; JS_SetOpaqueInternal(obj, it); return obj; range_error: @@ -55556,9 +58054,10 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, if (check_function(ctx, argv[0])) goto fail; func = js_dup(argv[0]); + /* GetIteratorDirect: propagates as is, no IteratorClose */ method = JS_GetProperty(ctx, this_val, JS_ATOM_next); if (JS_IsException(method)) - goto fail; + goto step_fail; r = JS_UNDEFINED; @@ -55569,7 +58068,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, for (idx = 0; /*empty*/; idx++) { item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(item)) - goto fail; + goto step_fail; if (done) break; index_val = js_int64(idx); @@ -55598,7 +58097,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, for (idx = 0; /*empty*/; idx++) { item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(item)) - goto fail; + goto step_fail; if (done) break; index_val = js_int64(idx); @@ -55619,6 +58118,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, } break; } + JS_FreeValue(ctx, item); index_val = JS_UNDEFINED; ret = JS_UNDEFINED; item = JS_UNDEFINED; @@ -55630,7 +58130,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, for (idx = 0; /*empty*/; idx++) { item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(item)) - goto fail; + goto step_fail; if (done) break; index_val = js_int64(idx); @@ -55654,7 +58154,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, for (idx = 0; /*empty*/; idx++) { item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(item)) - goto fail; + goto step_fail; if (done) break; index_val = js_int64(idx); @@ -55687,12 +58187,81 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, JS_FreeValue(ctx, method); return r; fail: + /* an abrupt completion from the callback closes the iterator + (IfAbruptCloseIterator) */ JS_IteratorClose(ctx, this_val, true); +step_fail: + /* GetIteratorDirect and IteratorStepValue leave the iterator alone */ JS_FreeValue(ctx, func); JS_FreeValue(ctx, method); return JS_EXCEPTION; } +static JSValue js_iterator_proto_includes(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue item, method; + int64_t to_skip, skipped; + double d; + int done; + + if (check_iterator(ctx, this_val) < 0) + return JS_EXCEPTION; + + // skippedElements is deliberately not coerced: anything that isn't an + // integral Number (or +/-Infinity) is a TypeError, and the underlying + // iterator is closed before the error propagates. + to_skip = 0; + if (argc > 1 && !JS_IsUndefined(argv[1])) { + if (!JS_IsNumber(argv[1])) { + JS_ThrowTypeError(ctx, "not a number"); + goto fail; + } + JS_ToFloat64(ctx, &d, argv[1]); // cannot fail + if (isnan(d) || (isfinite(d) && trunc(d) != d)) { + JS_ThrowTypeError(ctx, "not an integral number"); + goto fail; + } + if (d < 0 || (isfinite(d) && d > MAX_SAFE_INTEGER)) { + JS_ThrowRangeError(ctx, "must be positive"); + goto fail; + } + // +Infinity means "skip everything"; use MAX_SAFE_INTEGER + to_skip = isfinite(d) ? (int64_t)d : MAX_SAFE_INTEGER; + } + + // Note: a failure here propagates without closing the iterator. + method = JS_GetProperty(ctx, this_val, JS_ATOM_next); + if (JS_IsException(method)) + return JS_EXCEPTION; + + for (skipped = 0; /*empty*/; /*empty*/) { + item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); + if (JS_IsException(item)) { + // IteratorStepValue marks the iterator done, it does not close it. + JS_FreeValue(ctx, method); + return JS_EXCEPTION; + } + if (done) { + JS_FreeValue(ctx, method); + return JS_FALSE; + } + if (skipped < to_skip) { + skipped++; + } else if (js_same_value_zero(ctx, item, argv[0])) { + JS_FreeValue(ctx, item); + JS_FreeValue(ctx, method); + if (JS_IteratorClose(ctx, this_val, false) < 0) + return JS_EXCEPTION; + return JS_TRUE; + } + JS_FreeValue(ctx, item); + } +fail: + JS_IteratorClose(ctx, this_val, true); + return JS_EXCEPTION; +} + static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -55709,26 +58278,28 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val, if (check_function(ctx, argv[0])) goto exception; func = js_dup(argv[0]); + /* GetIteratorDirect: propagates as is, no IteratorClose */ method = JS_GetProperty(ctx, this_val, JS_ATOM_next); if (JS_IsException(method)) - goto exception; + goto step_exception; if (argc > 1) { acc = js_dup(argv[1]); idx = 0; } else { acc = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(acc)) - goto exception; + goto step_exception; if (done) { + /* the iterator is already done, don't close it */ JS_ThrowTypeError(ctx, "empty iterator"); - goto exception; + goto step_exception; } idx = 1; } for (/* empty */; /*empty*/; idx++) { item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); if (JS_IsException(item)) - goto exception; + goto step_exception; if (done) break; index_val = js_int64(idx); @@ -55750,7 +58321,11 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val, JS_FreeValue(ctx, method); return acc; exception: + /* an abrupt completion from the reducer closes the iterator + (IfAbruptCloseIterator) */ JS_IteratorClose(ctx, this_val, true); +step_exception: + /* GetIteratorDirect and IteratorStepValue leave the iterator alone */ JS_FreeValue(ctx, acc); JS_FreeValue(ctx, func); JS_FreeValue(ctx, method); @@ -55793,6 +58368,129 @@ static JSValue js_iterator_proto_toArray(JSContext *ctx, JSValueConst this_val, return JS_EXCEPTION; } +static JSValue js_iterator_proto_join(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue item, method, sep = JS_UNDEFINED; + StringBuffer b_s, *b = &b_s; + JSString *p = NULL; + bool first; + int c, done; + + if (check_iterator(ctx, this_val) < 0) + return JS_EXCEPTION; + c = ','; /* default separator */ + if (!JS_IsUndefined(argv[0])) { + sep = JS_ToString(ctx, argv[0]); + if (JS_IsException(sep)) { + JS_IteratorClose(ctx, this_val, /*is_exception_pending*/true); + return JS_EXCEPTION; + } + p = JS_VALUE_GET_STRING(sep); + if (p->len == 1 && !p->is_wide_char) + c = str8(p)[0]; + else + c = -1; + } + method = JS_GetProperty(ctx, this_val, JS_ATOM_next); + if (JS_IsException(method)) { + JS_FreeValue(ctx, sep); + return JS_EXCEPTION; + } + string_buffer_init(ctx, b, 0); + for (first = true; /*empty*/; first = false) { + item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done); + if (JS_IsException(item)) + goto fail; + if (done) + break; + if (!first) { + if (c >= 0) + string_buffer_putc8(b, c); + else + string_buffer_concat(b, p, 0, p->len); + } + /* nullish values contribute the empty string */ + if (JS_IsNull(item) || JS_IsUndefined(item)) { + JS_FreeValue(ctx, item); + continue; + } + if (string_buffer_concat_value_free(b, item)) + goto close; + } + JS_FreeValue(ctx, method); + JS_FreeValue(ctx, sep); + return string_buffer_end(b); + +close: + JS_IteratorClose(ctx, this_val, /*is_exception_pending*/true); +fail: + string_buffer_free(b); + JS_FreeValue(ctx, method); + JS_FreeValue(ctx, sep); + return JS_EXCEPTION; +} + +static JSValue js_async_dispose_to_undef(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data); + +static JSValue js_iterator_proto_dispose(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue method; + + method = JS_GetProperty(ctx, this_val, JS_ATOM_return); + if (JS_IsException(method)) + return JS_EXCEPTION; + if (JS_IsUndefined(method)) + return JS_UNDEFINED; + return JS_CallFree(ctx, method, this_val, 0, NULL); +} + +static JSValue js_async_iterator_proto_dispose(JSContext *ctx, + JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue method, ret, promise, undef_fn, then_args[1], result; + JSValue undef = JS_UNDEFINED; + + method = JS_GetProperty(ctx, this_val, JS_ATOM_return); + if (JS_IsException(method)) { + JSValue exc = JS_GetException(ctx); + JSValue p = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&exc), 1); + JS_FreeValue(ctx, exc); + return p; + } + if (JS_IsUndefined(method)) { + return js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&undef), 0); + } + ret = JS_Call(ctx, method, this_val, 0, NULL); + JS_FreeValue(ctx, method); + if (JS_IsException(ret)) { + JSValue exc = JS_GetException(ctx); + JSValue p = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&exc), 1); + JS_FreeValue(ctx, exc); + return p; + } + /* Wrap in Promise.resolve(ret).then(() => undefined) */ + promise = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&ret), 0); + JS_FreeValue(ctx, ret); + if (JS_IsException(promise)) + return promise; + undef_fn = JS_NewCFunctionData(ctx, js_async_dispose_to_undef, 0, 0, 0, + NULL); + if (JS_IsException(undef_fn)) { + JS_FreeValue(ctx, promise); + return JS_EXCEPTION; + } + then_args[0] = undef_fn; + result = JS_Invoke(ctx, promise, JS_ATOM_then, 1, vc(then_args)); + JS_FreeValue(ctx, undef_fn); + JS_FreeValue(ctx, promise); + return result; +} + static JSValue js_iterator_proto_iterator(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -55819,7 +58517,7 @@ static JSValue js_iterator_proto_set_toStringTag(JSContext *ctx, JSValueConst th if (JS_SetProperty(ctx, this_val, JS_ATOM_Symbol_toStringTag, js_dup(val)) < 0) return JS_EXCEPTION; } else { - if (JS_DefinePropertyValueConst(ctx, this_val, JS_ATOM_Symbol_toStringTag, val, JS_PROP_C_W_E) < 0) + if (JS_DefinePropertyValueConst(ctx, this_val, JS_ATOM_Symbol_toStringTag, val, JS_PROP_C_W_E | JS_PROP_THROW) < 0) return JS_EXCEPTION; } return JS_UNDEFINED; @@ -55834,6 +58532,9 @@ static void js_iterator_helper_finalizer(JSRuntime *rt, JSValueConst val) JS_FreeValueRT(rt, it->func); JS_FreeValueRT(rt, it->next); JS_FreeValueRT(rt, it->inner); + while (it->buf_count > 0) + JS_FreeValueRT(rt, it->buf[--it->buf_count]); + js_free_rt(rt, it->buf); js_free_rt(rt, it); } } @@ -55848,6 +58549,8 @@ static void js_iterator_helper_mark(JSRuntime *rt, JSValueConst val, JS_MarkValue(rt, it->func, mark_func); JS_MarkValue(rt, it->next, mark_func); JS_MarkValue(rt, it->inner, mark_func); + for (int i = 0; i < it->buf_count; i++) + JS_MarkValue(rt, it->buf[i], mark_func); } } @@ -55856,9 +58559,21 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, int *pdone, int magic) { JSIteratorHelperData *it; + bool finished; JSValue ret; *pdone = false; + finished = false; + + /* Iterator.concat() also hands out Iterator Helper objects, sharing this + prototype but keeping their own state */ + if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT && + JS_VALUE_GET_OBJ(this_val)->class_id == JS_CLASS_ITERATOR_CONCAT) { + if (magic == GEN_MAGIC_RETURN) + return js_iterator_concat_return(ctx, this_val, argc, argv, pdone, + magic); + return js_iterator_concat_next(ctx, this_val, argc, argv, pdone, magic); + } it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_HELPER); if (!it) @@ -55873,60 +58588,102 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, it->executing = 1; switch (it->kind) { - case JS_ITERATOR_HELPER_KIND_DROP: + case JS_ITERATOR_HELPER_KIND_CHUNKS: + case JS_ITERATOR_HELPER_KIND_WINDOWS: { - JSValue item, method; - if (magic == GEN_MAGIC_NEXT) { - method = js_dup(it->next); - } else { - method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); - if (JS_IsException(method)) - goto fail; + bool windows = it->kind == JS_ITERATOR_HELPER_KIND_WINDOWS; + JSValue item; + + if (magic == GEN_MAGIC_RETURN) + goto close; + for (;;) { + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); + if (JS_IsException(item)) + goto step_fail; + if (*pdone) { + JS_FreeValue(ctx, item); + /* the underlying iterator is done: yield what's left in + the buffer, if anything, then stop */ + finished = true; + ret = JS_UNDEFINED; + if (it->buf_count < 1) + goto done; + if (windows) { + /* a full window was already yielded */ + if (!it->allow_partial || it->buf_count >= it->count) + goto done; + ret = js_create_array(ctx, it->buf_count, vc(it->buf)); + } else { + ret = JS_NewArrayFrom(ctx, it->buf_count, it->buf); + it->buf_count = 0; // ownership transferred + } + if (JS_IsException(ret)) + goto step_fail; + *pdone = false; + goto done; + } + if (windows && it->buf_count == it->count) { + /* slide the window */ + JS_FreeValue(ctx, it->buf[0]); + it->buf_count--; + memmove(it->buf, &it->buf[1], + it->buf_count * sizeof(*it->buf)); + } + if (js_resize_array(ctx, (void **)&it->buf, sizeof(*it->buf), + &it->buf_size, it->buf_count + 1)) { + JS_FreeValue(ctx, item); + goto step_fail; + } + it->buf[it->buf_count++] = item; + if (it->buf_count == it->count) { + if (windows) { + ret = js_create_array(ctx, it->buf_count, vc(it->buf)); + } else { + ret = JS_NewArrayFrom(ctx, it->buf_count, it->buf); + it->buf_count = 0; // ownership transferred + } + if (JS_IsException(ret)) + goto step_fail; + goto done; + } } + } + break; + case JS_ITERATOR_HELPER_KIND_DROP: + { + JSValue item; + if (magic == GEN_MAGIC_RETURN) + goto close; while (it->count > 0) { it->count--; - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - if (JS_IsException(item)) { - JS_FreeValue(ctx, method); - goto fail; - } + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); + if (JS_IsException(item)) + goto step_fail; JS_FreeValue(ctx, item); - if (magic == GEN_MAGIC_RETURN) - *pdone = true; if (*pdone) { - JS_FreeValue(ctx, method); ret = JS_UNDEFINED; goto done; } } - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); if (JS_IsException(item)) - goto fail; + goto step_fail; ret = item; goto done; } break; case JS_ITERATOR_HELPER_KIND_FILTER: { - JSValue item, method, selected, index_val; + JSValue item, selected, index_val; JSValueConst args[2]; - if (magic == GEN_MAGIC_NEXT) { - method = js_dup(it->next); - } else { - method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); - if (JS_IsException(method)) - goto fail; - } + if (magic == GEN_MAGIC_RETURN) + goto close; filter_again: - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - if (JS_IsException(item)) { - JS_FreeValue(ctx, method); - goto fail; - } - if (*pdone || magic == GEN_MAGIC_RETURN) { - JS_FreeValue(ctx, method); + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); + if (JS_IsException(item)) + goto step_fail; + if (*pdone) { ret = item; goto done; } @@ -55936,14 +58693,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, selected = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args); JS_FreeValue(ctx, index_val); if (JS_IsException(selected)) { - JS_FreeValue(ctx, method); + JS_FreeValue(ctx, item); goto fail; } if (JS_ToBoolFree(ctx, selected)) { - JS_FreeValue(ctx, method); ret = item; goto done; } + JS_FreeValue(ctx, item); goto filter_again; } break; @@ -55951,20 +58708,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, { JSValue item, method, index_val, iter; JSValueConst args[2]; + if (magic == GEN_MAGIC_RETURN) + goto close; flat_map_again: if (JS_IsUndefined(it->inner)) { - if (magic == GEN_MAGIC_NEXT) { - method = js_dup(it->next); - } else { - method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); - if (JS_IsException(method)) - goto fail; - } - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); if (JS_IsException(item)) - goto fail; - if (*pdone || magic == GEN_MAGIC_RETURN) { + goto step_fail; + if (*pdone) { ret = item; goto done; } @@ -56000,27 +58751,20 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, it->inner = iter; } - if (magic == GEN_MAGIC_NEXT) - method = JS_GetProperty(ctx, it->inner, JS_ATOM_next); - else - method = JS_GetProperty(ctx, it->inner, JS_ATOM_return); + method = JS_GetProperty(ctx, it->inner, JS_ATOM_next); if (JS_IsException(method)) { inner_fail: - JS_IteratorClose(ctx, it->inner, false); + /* the inner iterator is only marked done, the outer one is + closed by the |fail| label */ JS_FreeValue(ctx, it->inner); it->inner = JS_UNDEFINED; goto fail; } - if (magic == GEN_MAGIC_RETURN && (JS_IsUndefined(method) || JS_IsNull(method))) { - goto inner_end; - } else { - item = JS_IteratorNext(ctx, it->inner, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); - if (JS_IsException(item)) - goto inner_fail; - } + item = JS_IteratorNext(ctx, it->inner, method, 0, NULL, pdone); + JS_FreeValue(ctx, method); + if (JS_IsException(item)) + goto inner_fail; if (*pdone) { - inner_end: *pdone = false; // The outer iterator must continue. JS_IteratorClose(ctx, it->inner, false); JS_FreeValue(ctx, it->inner); @@ -56033,20 +58777,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, break; case JS_ITERATOR_HELPER_KIND_MAP: { - JSValue item, method, index_val; + JSValue item, index_val; JSValueConst args[2]; - if (magic == GEN_MAGIC_NEXT) { - method = js_dup(it->next); - } else { - method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); - if (JS_IsException(method)) - goto fail; - } - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); + if (magic == GEN_MAGIC_RETURN) + goto close; + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); if (JS_IsException(item)) - goto fail; - if (*pdone || magic == GEN_MAGIC_RETURN) { + goto step_fail; + if (*pdone) { ret = item; goto done; } @@ -56063,29 +58801,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, break; case JS_ITERATOR_HELPER_KIND_TAKE: { - JSValue item, method; - if (it->count > 0) { - if (magic == GEN_MAGIC_NEXT) { - method = js_dup(it->next); - } else { - method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); - if (JS_IsException(method)) - goto fail; - } - it->count--; - item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); - JS_FreeValue(ctx, method); - if (JS_IsException(item)) - goto fail; - ret = item; - goto done; - } - - *pdone = true; - if (JS_IteratorClose(ctx, it->obj, false)) - ret = JS_EXCEPTION; - else - ret = JS_UNDEFINED; + JSValue item; + if (magic == GEN_MAGIC_RETURN || it->count == 0) + goto close; + it->count--; + item = JS_IteratorNext(ctx, it->obj, it->next, 0, NULL, pdone); + if (JS_IsException(item)) + goto step_fail; + ret = item; goto done; } break; @@ -56093,12 +58816,40 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val, abort(); } +close: + *pdone = true; + ret = JS_UNDEFINED; + if (!JS_IsUndefined(it->inner)) { + if (JS_IteratorClose(ctx, it->inner, false)) + ret = JS_EXCEPTION; + JS_FreeValue(ctx, it->inner); + it->inner = JS_UNDEFINED; + } + if (JS_IteratorClose(ctx, it->obj, JS_IsException(ret))) + ret = JS_EXCEPTION; + done: - it->done = magic == GEN_MAGIC_NEXT ? *pdone : 1; + it->done = finished || (magic == GEN_MAGIC_NEXT ? *pdone : 1); it->executing = 0; + if (it->done && it->buf) { + while (it->buf_count > 0) + JS_FreeValue(ctx, it->buf[--it->buf_count]); + js_free(ctx, it->buf); + it->buf = NULL; + it->buf_size = 0; + } return ret; +step_fail: + /* an abrupt completion from stepping the iterator object, or from the body + of the closure, does not close it: IteratorStepValue only marks the + iterator record done */ + finished = true; + ret = JS_EXCEPTION; + goto done; fail: - /* close the iterator object, preserving pending exception */ + /* an abrupt completion from the callback closes the iterator object + (IfAbruptCloseIterator), preserving the pending exception */ + finished = true; JS_IteratorClose(ctx, it->obj, true); ret = JS_EXCEPTION; goto done; @@ -56110,17 +58861,22 @@ static const JSCFunctionListEntry js_iterator_funcs[] = { }; static const JSCFunctionListEntry js_iterator_proto_funcs[] = { + JS_CFUNC_MAGIC_DEF("chunks", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_CHUNKS ), JS_CFUNC_MAGIC_DEF("drop", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_DROP ), JS_CFUNC_MAGIC_DEF("filter", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_FILTER ), JS_CFUNC_MAGIC_DEF("flatMap", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_FLAT_MAP ), JS_CFUNC_MAGIC_DEF("map", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_MAP ), JS_CFUNC_MAGIC_DEF("take", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_TAKE ), + JS_CFUNC_MAGIC_DEF("windows", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_WINDOWS ), JS_CFUNC_MAGIC_DEF("every", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_EVERY ), JS_CFUNC_MAGIC_DEF("find", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FIND), JS_CFUNC_MAGIC_DEF("forEach", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FOR_EACH ), JS_CFUNC_MAGIC_DEF("some", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_SOME ), + JS_CFUNC_DEF("includes", 1, js_iterator_proto_includes ), + JS_CFUNC_DEF("join", 1, js_iterator_proto_join ), JS_CFUNC_DEF("reduce", 1, js_iterator_proto_reduce ), JS_CFUNC_DEF("toArray", 0, js_iterator_proto_toArray ), + JS_CFUNC_DEF("[Symbol.dispose]", 0, js_iterator_proto_dispose ), JS_CFUNC_DEF("[Symbol.iterator]", 0, js_iterator_proto_iterator ), JS_CGETSET_DEF("[Symbol.toStringTag]", js_iterator_proto_get_toStringTag, js_iterator_proto_set_toStringTag), }; @@ -56454,7 +59210,8 @@ static JSValue js_parseInt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *str, *p; - int radix, flags; + int flags; + int32_t radix; JSValue ret; str = JS_ToCString(ctx, argv[0]); @@ -57800,27 +60557,35 @@ static JSValue js_string_repeat(JSContext *ctx, JSValueConst this_val, JSValue str; StringBuffer b_s, *b = &b_s; JSString *p; - int64_t val; + double val; int n, len; str = JS_ToStringCheckObject(ctx, this_val); if (JS_IsException(str)) goto fail; - if (JS_ToInt64Sat(ctx, &val, argv[0])) + if (JS_ToFloat64(ctx, &val, argv[0])) goto fail; - if (val < 0 || val > 2147483647) { + // ToIntegerOrInfinity: NaN maps to zero, everything else truncates + if (isnan(val)) + val = 0; + else + val = trunc(val); + // only a negative or infinite count is out of range; a large finite + // count is not, it merely risks exceeding the maximum string length + if (val < 0 || val == INFINITY) { JS_ThrowRangeError(ctx, "invalid repeat count"); goto fail; } - n = val; p = JS_VALUE_GET_STRING(str); len = p->len; - if (len == 0 || n == 1) + // any number of copies of the empty string is the empty string + if (len == 0 || val == 1) return str; if (val * len > JS_STRING_LEN_MAX) { - JS_ThrowRangeError(ctx, "invalid string length"); + 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) { @@ -58748,14 +61513,6 @@ static uint64_t xorshift64star(uint64_t *pstate) return x * 0x2545F4914F6CDD1D; } -static void js_random_init(JSContext *ctx) -{ - ctx->random_state = js__gettimeofday_us(); - /* the state must be non zero */ - if (ctx->random_state == 0) - ctx->random_state = 1; -} - static JSValue js_math_random(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -58979,7 +61736,10 @@ static JSValue js_compile_regexp(JSContext *ctx, JSValueConst pattern, if (re_flags & LRE_FLAG_UNICODE_SETS) return JS_ThrowSyntaxError(ctx, "invalid regular expression flags"); - str = JS_ToCStringLen2(ctx, &len, pattern, !(re_flags & LRE_FLAG_UNICODE)); + /* The v flag implies full Unicode, like u, so the pattern must be + UTF-8 (not CESU-8) for both. */ + str = JS_ToCStringLen2(ctx, &len, pattern, + !(re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS))); if (!str) return JS_EXCEPTION; re_bytecode_buf = lre_compile(&re_bytecode_len, error_msg, @@ -59371,7 +62131,7 @@ int lre_check_timeout(void *opaque) JSContext *ctx = opaque; JSRuntime *rt = ctx->rt; return (rt->interrupt_handler && - rt->interrupt_handler(rt, rt->interrupt_opaque)); + rt->interrupt_handler(ctx, rt->interrupt_opaque)); } void *lre_realloc(void *opaque, void *ptr, size_t size) @@ -59385,13 +62145,21 @@ static JSValue js_regexp_escape(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { StringBuffer b_s, *b = &b_s; + JSValue str, ret; JSString *p; uint32_t c, i; char s[16]; if (!JS_IsString(argv[0])) return JS_ThrowTypeError(ctx, "not a string"); - p = JS_VALUE_GET_STRING(argv[0]); + if (JS_VALUE_GET_TAG(argv[0]) == JS_TAG_STRING_ROPE) { + str = js_linearize_string_rope(ctx, argv[0]); + if (JS_IsException(str)) + return JS_EXCEPTION; + } else { + str = js_dup(argv[0]); + } + p = JS_VALUE_GET_STRING(str); string_buffer_init2(ctx, b, 0, p->is_wide_char); for (i = 0; i < p->len; i++) { c = p->is_wide_char ? (uint32_t)str16(p)[i] : (uint32_t)str8(p)[i]; @@ -59425,13 +62193,15 @@ static JSValue js_regexp_escape(JSContext *ctx, JSValueConst this_val, string_buffer_putc16(b, c); } } - return string_buffer_end(b); + ret = string_buffer_end(b); + JS_FreeValue(ctx, str); + return ret; } static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { - int rc, capture_count, shift, index, i, re_flags, prop_flags; + int rc, capture_count, alloc_count, shift, index, i, re_flags, prop_flags; JSRegExp *re = js_get_regexp(ctx, this_val, true); JSString *str; JSValue t, ret, str_val, obj, val, groups; @@ -59440,6 +62210,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, uint8_t **capture, *str_buf; int64_t last_index; const char *group_name_ptr; + JSAtom group_name; JSProperty props[4]; // length, index, input, groups, in that order if (!re) @@ -59454,6 +62225,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, groups = JS_UNDEFINED; indices = JS_UNDEFINED; indices_groups = JS_UNDEFINED; + group_name = JS_ATOM_NULL; capture = NULL; val = JS_GetProperty(ctx, this_val, JS_ATOM_lastIndex); @@ -59467,8 +62239,14 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, } str = JS_VALUE_GET_STRING(str_val); capture_count = lre_get_capture_count(re_bytecode); - if (capture_count > 0) { - capture = js_malloc(ctx, sizeof(capture[0]) * capture_count * 2); + /* The register-based executor writes capture positions AND temporary + registers into the capture buffer; it must be sized by alloc_count + (= capture_count*2 + register_count), not capture_count*2. Sizing it + by capture_count*2 silently overflows the heap on any regexp that + uses registers (i.e. most non-trivial patterns). */ + alloc_count = lre_get_alloc_count(re_bytecode); + if (alloc_count > 0) { + capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count); if (!capture) goto fail; } @@ -59494,7 +62272,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, JS_ThrowInterrupted(ctx); break; case LRE_RET_MEMORY_ERROR: - JS_ThrowInternalError(ctx, "out of memory in regexp execution"); + JS_ThrowOutOfMemory(ctx); break; case LRE_RET_BYTECODE_ERROR: JS_ThrowInternalError(ctx, "corrupted bytecode in regexp execution"); @@ -59538,14 +62316,19 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, goto fail; prop_flags = JS_PROP_C_W_E | JS_PROP_THROW; for(i = 0; i < capture_count; i++) { - const char *name = NULL; uint8_t **match = &capture[2 * i]; int start = -1; int end = -1; if (group_name_ptr && i > 0) { - if (*group_name_ptr) name = group_name_ptr; - group_name_ptr += strlen(group_name_ptr) + 1; + if (*group_name_ptr) { + /* XXX: slow, should create a shape when the regexp is + compiled */ + group_name = JS_NewAtom(ctx, group_name_ptr); + if (group_name == JS_ATOM_NULL) + goto fail; + } + group_name_ptr += strlen(group_name_ptr) + LRE_GROUP_NAME_TRAILER_LEN; } if (match[0] && match[1]) { @@ -59572,12 +62355,19 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, goto fail; } } - if (name && !JS_IsUndefined(indices_groups)) { - val = js_dup(val); - if (JS_DefinePropertyValueStr(ctx, indices_groups, - name, val, prop_flags) < 0) { - JS_FreeValue(ctx, val); - goto fail; + if (group_name != JS_ATOM_NULL && + !JS_IsUndefined(indices_groups)) { + /* For duplicate named groups, only the alternative that + actually matched (non-undefined) wins; a later + undefined alternative must not clobber it. */ + if (!JS_IsUndefined(val) || + !JS_HasProperty(ctx, indices_groups, group_name)) { + if (JS_DefinePropertyValue(ctx, indices_groups, + group_name, js_dup(val), + prop_flags) < 0) { + JS_FreeValue(ctx, val); + goto fail; + } } } if (JS_DefinePropertyValueUint32(ctx, indices, i, val, @@ -59593,13 +62383,18 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, goto fail; } - if (name) { - if (JS_DefinePropertyValueStr(ctx, groups, name, - js_dup(val), - prop_flags) < 0) { - JS_FreeValue(ctx, val); - goto fail; + if (group_name != JS_ATOM_NULL) { + /* duplicate named groups: matched alternative wins (see above) */ + if (!JS_IsUndefined(val) || + !JS_HasProperty(ctx, groups, group_name)) { + if (JS_DefinePropertyValue(ctx, groups, group_name, + js_dup(val), prop_flags) < 0) { + JS_FreeValue(ctx, val); + goto fail; + } } + JS_FreeAtom(ctx, group_name); + group_name = JS_ATOM_NULL; } if (JS_DefinePropertyValueUint32(ctx, obj, i, val, prop_flags) < 0) @@ -59622,6 +62417,7 @@ static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val, ret = obj; obj = JS_UNDEFINED; fail: + JS_FreeAtom(ctx, group_name); JS_FreeValue(ctx, indices_groups); JS_FreeValue(ctx, indices); JS_FreeValue(ctx, str_val); @@ -59640,7 +62436,7 @@ static JSValue JS_RegExpDelete(JSContext *ctx, JSValueConst this_val, JSValue ar uint8_t *re_bytecode; int ret; uint8_t **capture, *str_buf; - int capture_count, shift, re_flags; + int alloc_count, shift, re_flags; int next_src_pos, start, end; int64_t last_index; StringBuffer b_s, *b = &b_s; @@ -59664,9 +62460,11 @@ static JSValue JS_RegExpDelete(JSContext *ctx, JSValueConst this_val, JSValue ar if (JS_IsException(val) || JS_ToLengthFree(ctx, &last_index, val)) goto fail; } - capture_count = lre_get_capture_count(re_bytecode); - if (capture_count > 0) { - capture = js_malloc(ctx, sizeof(capture[0]) * capture_count * 2); + /* size by alloc_count: the register executor uses capture[] beyond + the capture positions for its registers (see js_regexp_exec). */ + alloc_count = lre_get_alloc_count(re_bytecode); + if (alloc_count > 0) { + capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count); if (!capture) goto fail; } @@ -59719,7 +62517,7 @@ static JSValue JS_RegExpDelete(JSContext *ctx, JSValueConst this_val, JSValue ar break; } if (end == start) { - if (!(re_flags & LRE_FLAG_UNICODE) || (unsigned)end >= str->len || !str->is_wide_char) { + if (!(re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) || (unsigned)end >= str->len || !str->is_wide_char) { end++; } else { string_getc(str, &end); @@ -59802,14 +62600,13 @@ static JSValue js_regexp_Symbol_match(JSContext *ctx, JSValueConst this_val, goto exception; p = JS_VALUE_GET_STRING(flags); - // TODO(bnoordhuis) query 'u' flag the same way? global = (-1 != string_indexof_char(p, 'g', 0)); if (!global) { A = JS_RegExpExec(ctx, rx, S); } else { - fullUnicode = JS_ToBoolFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_unicode)); - if (fullUnicode < 0) - goto exception; + // 'v' flag implies full Unicode, like 'u' + fullUnicode = (string_indexof_char(p, 'u', 0) >= 0 || + string_indexof_char(p, 'v', 0) >= 0); if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, js_int32(0)) < 0) goto exception; @@ -59992,7 +62789,8 @@ static JSValue js_regexp_Symbol_matchAll(JSContext *ctx, JSValueConst this_val, it->iterated_string = S; strp = JS_VALUE_GET_STRING(flags); it->global = string_indexof_char(strp, 'g', 0) >= 0; - it->unicode = string_indexof_char(strp, 'u', 0) >= 0; + it->unicode = string_indexof_char(strp, 'u', 0) >= 0 || + string_indexof_char(strp, 'v', 0) >= 0; it->done = false; JS_SetOpaqueInternal(iter, it); @@ -60044,15 +62842,14 @@ static int value_buffer_append(ValueBuffer *b, JSValue val) if (b->len >= b->size) { int new_size = (b->len + (b->len >> 1) + 31) & ~16; - size_t slack; JSValue *new_arr; if (b->arr == b->def) { - new_arr = js_realloc2(b->ctx, NULL, sizeof(*b->arr) * new_size, &slack); + new_arr = js_realloc(b->ctx, NULL, sizeof(*b->arr) * new_size); if (new_arr) memcpy(new_arr, b->def, sizeof b->def); } else { - new_arr = js_realloc2(b->ctx, b->arr, sizeof(*b->arr) * new_size, &slack); + new_arr = js_realloc(b->ctx, b->arr, sizeof(*b->arr) * new_size); } if (!new_arr) { value_buffer_free(b); @@ -60060,7 +62857,6 @@ static int value_buffer_append(ValueBuffer *b, JSValue val) b->error_status = -1; return -1; } - new_size += slack / sizeof(*new_arr); b->arr = new_arr; b->size = new_size; } @@ -60139,13 +62935,12 @@ static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val, goto exception; p = JS_VALUE_GET_STRING(flags); - // TODO(bnoordhuis) query 'u' flag the same way? fullUnicode = 0; is_global = (-1 != string_indexof_char(p, 'g', 0)); if (is_global) { - fullUnicode = JS_ToBoolFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_unicode)); - if (fullUnicode < 0) - goto exception; + // 'v' flag implies full Unicode, like 'u' + fullUnicode = (string_indexof_char(p, 'u', 0) >= 0 || + string_indexof_char(p, 'v', 0) >= 0); if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, js_int32(0)) < 0) goto exception; } @@ -60371,7 +63166,8 @@ static JSValue js_regexp_Symbol_split(JSContext *ctx, JSValueConst this_val, if (JS_IsException(flags)) goto exception; strp = JS_VALUE_GET_STRING(flags); - unicodeMatching = string_indexof_char(strp, 'u', 0) >= 0; + unicodeMatching = string_indexof_char(strp, 'u', 0) >= 0 || + string_indexof_char(strp, 'v', 0) >= 0; if (string_indexof_char(strp, 'y', 0) < 0) { flags = JS_ConcatString3(ctx, "", flags, "y"); if (JS_IsException(flags)) @@ -61308,8 +64104,10 @@ static int js_json_to_str(JSContext *ctx, JSONStringifyContext *jsc, goto exception; /* XXX: could do this string conversion only when needed */ prop = JS_ToStringFree(ctx, js_int64(i)); - if (JS_IsException(prop)) + if (JS_IsException(prop)) { + JS_FreeValue(ctx, v); goto exception; + } v = js_json_check(ctx, jsc, val, v, prop); JS_FreeValue(ctx, prop); prop = JS_UNDEFINED; @@ -61510,7 +64308,11 @@ JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj, goto exception; jsc->gap = JS_NewStringLen(ctx, " ", n); } else if (JS_IsString(space)) { - JSString *p = JS_VALUE_GET_STRING(space); + JSString *p; + space = JS_ToStringFree(ctx, space); + if (JS_IsException(space)) + goto exception; + p = JS_VALUE_GET_STRING(space); jsc->gap = js_sub_string(ctx, p, 0, min_int(p->len, 10)); } else { jsc->gap = js_dup(jsc->empty); @@ -61953,7 +64755,7 @@ static int js_proxy_has(JSContext *ctx, JSValueConst obj, JSAtom atom) int res; JSObject *p; JSValueConst args[2]; - bool ret, res2; + bool ret; s = get_proxy_method(ctx, &method, obj, JS_ATOM_has); if (!s) @@ -61979,8 +64781,15 @@ static int js_proxy_has(JSContext *ctx, JSValueConst obj, JSAtom atom) if (res < 0) return -1; if (res) { - res2 = !(desc_flags & JS_PROP_CONFIGURABLE); - if (res2 || !p->extensible) { + if (!(desc_flags & JS_PROP_CONFIGURABLE)) + goto inconsistent; + /* must go through IsExtensible(): the target can be a proxy + itself, whose isExtensible trap is observable */ + res = JS_IsExtensible(ctx, s->target); + if (res < 0) + return -1; + if (!res) { + inconsistent: JS_ThrowTypeError(ctx, "proxy: inconsistent has"); return -1; } @@ -62176,7 +64985,14 @@ static int js_proxy_get_own_property(JSContext *ctx, JSPropertyDescriptor *pdesc js_free_desc(ctx, &target_desc); if (JS_IsUndefined(trap_result_obj)) { if (target_desc_ret) { - if (!(target_desc.flags & JS_PROP_CONFIGURABLE) || !p->extensible) + if (!(target_desc.flags & JS_PROP_CONFIGURABLE)) + goto fail; + /* must go through IsExtensible(): the target can be a proxy + itself, whose isExtensible trap is observable */ + res = JS_IsExtensible(ctx, s->target); + if (res < 0) + return -1; + if (!res) goto fail; } ret = false; @@ -62239,7 +65055,7 @@ static int js_proxy_define_own_property(JSContext *ctx, JSValueConst obj, { JSProxyData *s; JSValue method, ret1, prop_val, desc_val; - int res; + int res, extensible_target; JSObject *p; JSValueConst args[3]; JSPropertyDescriptor desc; @@ -62283,11 +65099,19 @@ static int js_proxy_define_own_property(JSContext *ctx, JSValueConst obj, res = JS_GetOwnPropertyInternal(ctx, &desc, p, prop); if (res < 0) return -1; + /* must go through IsExtensible(): the target can be a proxy itself, in + which case its isExtensible trap is observable and may throw */ + extensible_target = JS_IsExtensible(ctx, s->target); + if (extensible_target < 0) { + if (res) + js_free_desc(ctx, &desc); + return -1; + } setting_not_configurable = ((flags & (JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE)) == JS_PROP_HAS_CONFIGURABLE); if (!res) { - if (!p->extensible || setting_not_configurable) + if (!extensible_target || setting_not_configurable) goto fail; } else { if (!check_define_prop_flags(desc.flags, flags) || @@ -62400,7 +65224,7 @@ static int js_proxy_get_own_property_names(JSContext *ctx, { JSProxyData *s; JSValue method, prop_array, val; - uint32_t len, i, len2; + uint32_t len, target_len, tab_size, i, len2; JSPropertyEnum *tab, *tab2; JSAtom atom; int res, is_extensible, idx; @@ -62416,18 +65240,20 @@ static int js_proxy_get_own_property_names(JSContext *ctx, prop_array = JS_CallFree(ctx, method, s->handler, 1, vc(&s->target)); if (JS_IsException(prop_array)) return -1; + /* CreateListFromArrayLike() requires an object */ + if (JS_VALUE_GET_TAG(prop_array) != JS_TAG_OBJECT) { + JS_FreeValue(ctx, prop_array); + JS_ThrowTypeError(ctx, "proxy: ownKeys must return an object"); + return -1; + } tab = NULL; len = 0; + tab_size = 0; tab2 = NULL; len2 = 0; - if (js_get_length32(ctx, &len, prop_array)) + if (js_get_length32(ctx, &target_len, prop_array)) goto fail; - if (len > 0) { - tab = js_mallocz(ctx, sizeof(tab[0]) * len); - if (!tab) - goto fail; - } - for(i = 0; i < len; i++) { + for(i = 0; i < target_len; i++) { val = JS_GetPropertyUint32(ctx, prop_array, i); if (JS_IsException(val)) goto fail; @@ -62440,8 +65266,26 @@ static int js_proxy_get_own_property_names(JSContext *ctx, JS_FreeValue(ctx, val); if (atom == JS_ATOM_NULL) goto fail; - tab[i].atom = atom; - tab[i].is_enumerable = false; /* XXX: redundant? */ + if (len >= tab_size) { + int64_t new_size = max_int64(8, tab_size + (int64_t)tab_size / 2); + JSPropertyEnum *new_tab; + new_size = min_int64(new_size, target_len); + if (new_size > (int64_t)(SIZE_MAX / sizeof(tab[0]))) { + JS_FreeAtom(ctx, atom); + JS_ThrowOutOfMemory(ctx); + goto fail; + } + new_tab = js_realloc(ctx, tab, sizeof(tab[0]) * (size_t)new_size); + if (!new_tab) { + JS_FreeAtom(ctx, atom); + goto fail; + } + tab = new_tab; + tab_size = new_size; + } + tab[len].atom = atom; + tab[len].is_enumerable = false; /* XXX: redundant? */ + len++; } /* check duplicate properties (XXX: inefficient, could store the @@ -62882,6 +65726,8 @@ static const JSCFunctionListEntry js_symbol_funcs[] = { JS_PROP_SYMBOL_DEF("species", JS_ATOM_Symbol_species, 0), JS_PROP_SYMBOL_DEF("unscopables", JS_ATOM_Symbol_unscopables, 0), JS_PROP_SYMBOL_DEF("asyncIterator", JS_ATOM_Symbol_asyncIterator, 0), + JS_PROP_SYMBOL_DEF("dispose", JS_ATOM_Symbol_dispose, 0), + JS_PROP_SYMBOL_DEF("asyncDispose", JS_ATOM_Symbol_asyncDispose, 0), }; /* Set/Map/WeakSet/WeakMap */ @@ -63054,13 +65900,13 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key) hash_float64: u.d = d; h = (u.u32[0] ^ u.u32[1]) * 3163; - return h ^= JS_TAG_FLOAT64; + tag = JS_TAG_FLOAT64; + break; default: h = 0; break; } - h ^= tag; - return h; + return h ^ ctx->hash_seed ^ hash32(tag); } static JSMapRecord *map_find_record(JSContext *ctx, JSMapState *s, @@ -63505,12 +66351,20 @@ static void js_map_finalizer(JSRuntime *rt, JSValueConst val) if (s) { /* if the object is deleted we are sure that no iterator is using it */ + if (s->is_weak) { + /* first pass to remove the weak references, so that freeing + a value (e.g. a symbol that is also a key of this map) + cannot reset and free records we have yet to visit */ + list_for_each(el, &s->records) { + mr = list_entry(el, JSMapRecord, link); + if (!mr->empty) + delete_map_weak_ref(rt, mr); + } + } list_for_each_safe(el, el1, &s->records) { mr = list_entry(el, JSMapRecord, link); if (!mr->empty) { - if (s->is_weak) - delete_map_weak_ref(rt, mr); - else + if (!s->is_weak) JS_FreeValueRT(rt, mr->key); JS_FreeValueRT(rt, mr->value); } @@ -64033,6 +66887,7 @@ static JSValue js_set_intersection(JSContext *ctx, JSValueConst this_val, } else if (map_find_record(ctx, t, item)) { JS_FreeValue(ctx, item); // no duplicates } else if ((mr = map_add_record(ctx, t, item))) { + JS_FreeValue(ctx, item); mr->value = JS_UNDEFINED; } else { JS_FreeValue(ctx, item); @@ -64060,94 +66915,96 @@ static JSValue js_set_intersection(JSContext *ctx, JSValueConst this_val, if (map_find_record(ctx, t, item)) { JS_FreeValue(ctx, item); // no duplicates } else if ((mr = map_add_record(ctx, t, item))) { - mr->value = JS_UNDEFINED; - } else { JS_FreeValue(ctx, item); - goto exception; - } - } else { - JS_FreeValue(ctx, item); - if (ok < 0) - goto exception; - } - } - } - goto fini; -exception: - JS_FreeValue(ctx, newset); - newset = JS_EXCEPTION; -fini: - JS_FreeValue(ctx, has); - JS_FreeValue(ctx, keys); - JS_FreeValue(ctx, iter); - JS_FreeValue(ctx, next); - return newset; -} - -static JSValue js_set_difference(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv) -{ - JSValue has, item, iter, keys, newset, next, rv; - JSValueConst setlike; - JSMapState *s, *t; - JSMapRecord *mr; - uint64_t size; - int done; - int ok; - - s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET); - if (!s) - return JS_EXCEPTION; - setlike = argv[0]; - if (js_setlike_get_props(ctx, setlike, &size, &has, &keys) < 0) - return JS_EXCEPTION; - iter = JS_UNDEFINED; - next = JS_UNDEFINED; - newset = JS_UNDEFINED; - if (s->record_count > size) { - iter = JS_Call(ctx, keys, setlike, 0, NULL); - if (JS_IsException(iter)) - goto exception; - next = JS_GetProperty(ctx, iter, JS_ATOM_next); - if (JS_IsException(next)) - goto exception; - newset = js_map_constructor(ctx, JS_UNDEFINED, 1, &this_val, MAGIC_SET); - if (JS_IsException(newset)) - goto exception; - t = JS_GetOpaque(newset, JS_CLASS_SET); - for (;;) { - item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done); - if (JS_IsException(item)) - goto exception; - if (done) // item is JS_UNDEFINED - break; - item = map_normalize_key(ctx, item); - mr = map_find_record(ctx, t, item); - if (mr) - map_delete_record(ctx->rt, t, mr); - JS_FreeValue(ctx, item); - } - } else { - iter = js_create_map_iterator(ctx, this_val, 0, NULL, MAGIC_SET); - if (JS_IsException(iter)) - goto exception; - newset = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET); - if (JS_IsException(newset)) - goto exception; - t = JS_GetOpaque(newset, JS_CLASS_SET); - for (;;) { - item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET); - if (JS_IsException(item)) - goto exception; - if (done) // item is JS_UNDEFINED - break; - rv = JS_Call(ctx, has, setlike, 1, vc(&item)); - ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION - if (ok == 0) { - item = map_normalize_key(ctx, item); - if (map_find_record(ctx, t, item)) { - JS_FreeValue(ctx, item); // no duplicates - } else if ((mr = map_add_record(ctx, t, item))) { + mr->value = JS_UNDEFINED; + } else { + JS_FreeValue(ctx, item); + goto exception; + } + } else { + JS_FreeValue(ctx, item); + if (ok < 0) + goto exception; + } + } + } + goto fini; +exception: + JS_FreeValue(ctx, newset); + newset = JS_EXCEPTION; +fini: + JS_FreeValue(ctx, has); + JS_FreeValue(ctx, keys); + JS_FreeValue(ctx, iter); + JS_FreeValue(ctx, next); + return newset; +} + +static JSValue js_set_difference(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue has, item, iter, keys, newset, next, rv; + JSValueConst setlike; + JSMapState *s, *t; + JSMapRecord *mr; + uint64_t size; + int done; + int ok; + + s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET); + if (!s) + return JS_EXCEPTION; + setlike = argv[0]; + if (js_setlike_get_props(ctx, setlike, &size, &has, &keys) < 0) + return JS_EXCEPTION; + iter = JS_UNDEFINED; + next = JS_UNDEFINED; + newset = JS_UNDEFINED; + if (s->record_count > size) { + iter = JS_Call(ctx, keys, setlike, 0, NULL); + if (JS_IsException(iter)) + goto exception; + next = JS_GetProperty(ctx, iter, JS_ATOM_next); + if (JS_IsException(next)) + goto exception; + newset = js_map_constructor(ctx, JS_UNDEFINED, 1, &this_val, MAGIC_SET); + if (JS_IsException(newset)) + goto exception; + t = JS_GetOpaque(newset, JS_CLASS_SET); + for (;;) { + item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done); + if (JS_IsException(item)) + goto exception; + if (done) // item is JS_UNDEFINED + break; + item = map_normalize_key(ctx, item); + mr = map_find_record(ctx, t, item); + if (mr) + map_delete_record(ctx->rt, t, mr); + JS_FreeValue(ctx, item); + } + } else { + iter = js_create_map_iterator(ctx, this_val, 0, NULL, MAGIC_SET); + if (JS_IsException(iter)) + goto exception; + newset = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET); + if (JS_IsException(newset)) + goto exception; + t = JS_GetOpaque(newset, JS_CLASS_SET); + for (;;) { + item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET); + if (JS_IsException(item)) + goto exception; + if (done) // item is JS_UNDEFINED + break; + rv = JS_Call(ctx, has, setlike, 1, vc(&item)); + ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION + if (ok == 0) { + item = map_normalize_key(ctx, item); + if (map_find_record(ctx, t, item)) { + JS_FreeValue(ctx, item); // no duplicates + } else if ((mr = map_add_record(ctx, t, item))) { + JS_FreeValue(ctx, item); mr->value = JS_UNDEFINED; } else { JS_FreeValue(ctx, item); @@ -64203,7 +67060,7 @@ static JSValue js_set_symmetricDifference(JSContext *ctx, JSValueConst this_val, mr = list_entry(el, JSMapRecord, link); if (mr->empty) continue; - mr = map_add_record(ctx, t, js_dup(mr->key)); + mr = map_add_record(ctx, t, mr->key); if (!mr) goto exception; mr->value = JS_UNDEFINED; @@ -64239,10 +67096,9 @@ static JSValue js_set_symmetricDifference(JSContext *ctx, JSValueConst this_val, JS_FreeValue(ctx, item); } else { mr = map_add_record(ctx, t, item); - if (!mr) { - JS_FreeValue(ctx, item); + JS_FreeValue(ctx, item); + if (!mr) goto exception; - } mr->value = JS_UNDEFINED; } } @@ -64286,7 +67142,7 @@ static JSValue js_set_union(JSContext *ctx, JSValueConst this_val, mr = list_entry(el, JSMapRecord, link); if (mr->empty) continue; - mr = map_add_record(ctx, t, js_dup(mr->key)); + mr = map_add_record(ctx, t, mr->key); if (!mr) goto exception; mr->value = JS_UNDEFINED; @@ -64474,13 +67330,668 @@ static const JSCFunctionListEntry js_generator_proto_funcs[] = { JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Generator", JS_PROP_CONFIGURABLE), }; +/* Explicit resource management */ + +enum { + JS_DISPOSE_HINT_SYNC, /* use: Call(method, value) */ + JS_DISPOSE_HINT_ADOPT, /* adopt: Call(method, undefined, [value]) */ + JS_DISPOSE_HINT_DEFER, /* defer: Call(method, undefined) */ +}; + +typedef struct JSDisposableResource { + JSValue value; + JSValue method; /* dispose method */ + uint8_t hint; +} JSDisposableResource; + +typedef struct JSDisposableStack { + bool disposed; + int resource_count; + int resource_capacity; + JSDisposableResource *resources; +} JSDisposableStack; + +static JSValue js_new_suppressed_error(JSContext *ctx, JSValueConst error, + JSValueConst suppressed) +{ + JSValue obj; + + /* Construct via the intrinsic prototype rather than going through + SuppressedError.prototype.constructor, which is user-writable. */ + obj = JS_NewObjectProtoClass(ctx, + ctx->native_error_proto[JS_SUPPRESSED_ERROR], + JS_CLASS_ERROR); + if (JS_IsException(obj)) + return JS_EXCEPTION; + JS_DefinePropertyValue(ctx, obj, JS_ATOM_error, js_dup(error), + JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + JS_DefinePropertyValue(ctx, obj, JS_ATOM_suppressed, js_dup(suppressed), + JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); + build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0, + JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL); + return obj; +} + +/* Perform DisposeResources. Returns 0 on success, -1 on exception. + completion_error is the pending error (JS_UNDEFINED if none). + It is consumed (freed) by this function. */ +static int js_dispose_resources(JSContext *ctx, JSDisposableStack *ds, + JSValue completion_error) +{ + JSValue error = completion_error; + bool has_error = !JS_IsUndefined(error); + int i; + + ds->disposed = true; + /* dispose in LIFO order */ + for (i = ds->resource_count - 1; i >= 0; i--) { + JSDisposableResource *res = &ds->resources[i]; + JSValue ret; + if (JS_IsUndefined(res->method)) { + /* null/undefined resource, skip */ + JS_FreeValue(ctx, res->value); + res->value = JS_UNDEFINED; + continue; + } + switch (res->hint) { + case JS_DISPOSE_HINT_ADOPT: + ret = JS_Call(ctx, res->method, JS_UNDEFINED, 1, vc(&res->value)); + break; + case JS_DISPOSE_HINT_DEFER: + ret = JS_Call(ctx, res->method, JS_UNDEFINED, 0, NULL); + break; + default: /* JS_DISPOSE_HINT_SYNC */ + ret = JS_Call(ctx, res->method, res->value, 0, NULL); + break; + } + JS_FreeValue(ctx, res->value); + JS_FreeValue(ctx, res->method); + res->value = JS_UNDEFINED; + res->method = JS_UNDEFINED; + if (JS_IsException(ret)) { + JSValue new_error = JS_GetException(ctx); + if (has_error) { + JSValue suppressed = js_new_suppressed_error(ctx, new_error, error); + JS_FreeValue(ctx, new_error); + JS_FreeValue(ctx, error); + if (JS_IsException(suppressed)) { + error = JS_GetException(ctx); + } else { + error = suppressed; + } + } else { + error = new_error; + has_error = true; + } + } else { + JS_FreeValue(ctx, ret); + } + } + ds->resource_count = 0; + if (has_error) { + JS_Throw(ctx, error); + return -1; + } + return 0; +} + +static void js_disposable_stack_clear(JSRuntime *rt, JSDisposableStack *ds) +{ + int i; + for (i = 0; i < ds->resource_count; i++) { + JS_FreeValueRT(rt, ds->resources[i].value); + JS_FreeValueRT(rt, ds->resources[i].method); + } + js_free_rt(rt, ds->resources); +} + +static int js_disposable_stack_add(JSContext *ctx, JSDisposableStack *ds, + JSValueConst value, JSValueConst method, + int hint) +{ + if (ds->resource_count >= ds->resource_capacity) { + int new_cap = max_int(ds->resource_capacity * 2, 4); + JSDisposableResource *new_res; + new_res = js_realloc(ctx, ds->resources, + new_cap * sizeof(JSDisposableResource)); + if (!new_res) + return -1; + ds->resources = new_res; + ds->resource_capacity = new_cap; + } + ds->resources[ds->resource_count].value = js_dup(value); + ds->resources[ds->resource_count].method = js_dup(method); + ds->resources[ds->resource_count].hint = hint; + ds->resource_count++; + return 0; +} + +static JSValue js_sync_dispose_wrapper(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data) +{ + JSValueConst method = func_data[0]; + JSValue ret = JS_Call(ctx, method, this_val, 0, NULL); + if (JS_IsException(ret)) + return JS_EXCEPTION; + JS_FreeValue(ctx, ret); + return JS_UNDEFINED; +} + +static JSValue js_get_dispose_method(JSContext *ctx, JSValueConst value, int hint) +{ + JSValue method; + + if (hint == 1) { + /* async: try Symbol.asyncDispose first */ + method = JS_GetProperty(ctx, value, JS_ATOM_Symbol_asyncDispose); + if (JS_IsException(method)) + return JS_EXCEPTION; + if (!JS_IsUndefined(method) && !JS_IsNull(method)) { + if (!JS_IsFunction(ctx, method)) { + JS_FreeValue(ctx, method); + return JS_ThrowTypeError(ctx, "property is not a function"); + } + return method; + } + JS_FreeValue(ctx, method); + /* Fall back to Symbol.dispose, but wrap it so its return value is + NOT awaited (per spec GetDisposeMethod). */ + method = JS_GetProperty(ctx, value, JS_ATOM_Symbol_dispose); + if (JS_IsException(method)) + return JS_EXCEPTION; + if (JS_IsUndefined(method) || JS_IsNull(method)) + return JS_ThrowTypeError(ctx, "property is not a function"); + if (!JS_IsFunction(ctx, method)) { + JS_FreeValue(ctx, method); + return JS_ThrowTypeError(ctx, "property is not a function"); + } + + { + JSValue data[1], wrapped; + data[0] = method; + wrapped = JS_NewCFunctionData(ctx, js_sync_dispose_wrapper, 0, 0, + 1, vc(data)); + JS_FreeValue(ctx, method); + return wrapped; + } + } + + /* sync dispose */ + method = JS_GetProperty(ctx, value, JS_ATOM_Symbol_dispose); + if (JS_IsException(method)) + return JS_EXCEPTION; + if (JS_IsUndefined(method) || JS_IsNull(method)) + return JS_ThrowTypeError(ctx, "property is not a function"); + if (!JS_IsFunction(ctx, method)) { + JS_FreeValue(ctx, method); + return JS_ThrowTypeError(ctx, "property is not a function"); + } + return method; +} + +static JSValue js_disposable_stack_constructor(JSContext *ctx, + JSValueConst new_target, + int argc, JSValueConst *argv, + int class_id) +{ + JSDisposableStack *s; + JSValue obj; + + if (JS_IsUndefined(new_target)) + return JS_ThrowTypeError(ctx, "Constructor requires 'new'"); + obj = js_create_from_ctor(ctx, new_target, class_id); + if (JS_IsException(obj)) + return JS_EXCEPTION; + s = js_mallocz(ctx, sizeof(*s)); + if (!s) { + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; + } + JS_SetOpaqueInternal(obj, s); + return obj; +} + +static void js_disposable_stack_finalizer(JSRuntime *rt, JSValueConst val) +{ + JSObject *p; + JSDisposableStack *s; + + p = JS_VALUE_GET_OBJ(val); + s = p->u.opaque; + if (s) { + js_disposable_stack_clear(rt, s); + js_free_rt(rt, s); + } +} + +static void js_disposable_stack_mark(JSRuntime *rt, JSValueConst val, + JS_MarkFunc *mark_func) +{ + JSObject *p; + JSDisposableStack *s; + int i; + + p = JS_VALUE_GET_OBJ(val); + s = p->u.opaque; + if (s) { + for (i = 0; i < s->resource_count; i++) { + JS_MarkValue(rt, s->resources[i].value, mark_func); + JS_MarkValue(rt, s->resources[i].method, mark_func); + } + } +} + +static JSDisposableStack *js_disposable_stack_get(JSContext *ctx, + JSValueConst this_val, + int class_id) +{ + JSDisposableStack *s; + s = JS_GetOpaque2(ctx, this_val, class_id); + if (!s) + return NULL; + if (s->disposed) { + JS_ThrowReferenceError(ctx, "DisposableStack has been disposed"); + return NULL; + } + return s; +} + +static JSValue js_disposable_stack_use(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, int class_id) +{ + JSDisposableStack *s; + JSValueConst value; + JSValue method; + int hint = (class_id == JS_CLASS_ASYNC_DISPOSABLE_STACK) ? 1 : 0; + + s = js_disposable_stack_get(ctx, this_val, class_id); + if (!s) + return JS_EXCEPTION; + value = argv[0]; + if (JS_IsNull(value) || JS_IsUndefined(value)) { + /* For async stacks, a null/undefined resource still needs a record + so disposeAsync performs the required Await(undefined). */ + if (class_id == JS_CLASS_ASYNC_DISPOSABLE_STACK) { + if (js_disposable_stack_add(ctx, s, JS_UNDEFINED, JS_UNDEFINED, + JS_DISPOSE_HINT_SYNC) < 0) + return JS_EXCEPTION; + } + return js_dup(value); + } + if (!JS_IsObject(value)) + return JS_ThrowTypeError(ctx, "not an object"); + method = js_get_dispose_method(ctx, value, hint); + if (JS_IsException(method)) + return JS_EXCEPTION; + if (js_disposable_stack_add(ctx, s, value, method, JS_DISPOSE_HINT_SYNC) < 0) { + JS_FreeValue(ctx, method); + return JS_EXCEPTION; + } + JS_FreeValue(ctx, method); + return js_dup(value); +} + +static JSValue js_disposable_stack_adopt(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, int class_id) +{ + JSDisposableStack *s; + JSValueConst value, on_dispose; + + s = js_disposable_stack_get(ctx, this_val, class_id); + if (!s) + return JS_EXCEPTION; + value = argv[0]; + on_dispose = argv[1]; + if (!JS_IsFunction(ctx, on_dispose)) + return JS_ThrowTypeError(ctx, "not a function"); + if (js_disposable_stack_add(ctx, s, value, on_dispose, JS_DISPOSE_HINT_ADOPT) < 0) + return JS_EXCEPTION; + return js_dup(value); +} + +static JSValue js_disposable_stack_defer(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, int class_id) +{ + JSDisposableStack *s; + JSValueConst on_dispose; + + s = js_disposable_stack_get(ctx, this_val, class_id); + if (!s) + return JS_EXCEPTION; + on_dispose = argv[0]; + if (!JS_IsFunction(ctx, on_dispose)) + return JS_ThrowTypeError(ctx, "not a function"); + if (js_disposable_stack_add(ctx, s, JS_UNDEFINED, on_dispose, JS_DISPOSE_HINT_DEFER) < 0) + return JS_EXCEPTION; + return JS_UNDEFINED; +} + +/* Simple .then handler that discards its argument and returns undefined; + used to normalize the chain's resolved value after all disposals. */ +static JSValue js_async_dispose_to_undef(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data) +{ + return JS_UNDEFINED; +} + +static JSValue js_async_dispose_rethrow(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data) +{ + JSValue prev_err = js_dup(func_data[0]); + if (magic == 0) { + return JS_Throw(ctx, prev_err); + } else { + JSValue se = js_new_suppressed_error(ctx, argv[0], prev_err); + JS_FreeValue(ctx, prev_err); + if (JS_IsException(se)) + return JS_EXCEPTION; + return JS_Throw(ctx, se); + } +} + +static JSValue js_async_dispose_step(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, + int magic, JSValueConst *func_data) +{ + JSValueConst value = func_data[0]; + JSValueConst method = func_data[1]; + int hint = JS_VALUE_GET_INT(func_data[2]); + bool has_prev_err = (magic == 1); + JSValue ret; + + if (JS_IsUndefined(method)) { + /* null/undefined resource on async stack: Await(undefined) */ + if (has_prev_err) + return JS_Throw(ctx, js_dup(argv[0])); + return JS_UNDEFINED; + } + + switch (hint) { + case JS_DISPOSE_HINT_ADOPT: + ret = JS_Call(ctx, method, JS_UNDEFINED, 1, &value); + break; + case JS_DISPOSE_HINT_DEFER: + ret = JS_Call(ctx, method, JS_UNDEFINED, 0, NULL); + break; + default: + ret = JS_Call(ctx, method, value, 0, NULL); + break; + } + + if (JS_IsException(ret)) { + JSValue new_err = JS_GetException(ctx); + if (has_prev_err) { + JSValue se = js_new_suppressed_error(ctx, new_err, argv[0]); + JS_FreeValue(ctx, new_err); + if (JS_IsException(se)) + return JS_EXCEPTION; + return JS_Throw(ctx, se); + } + return JS_Throw(ctx, new_err); + } + + if (!has_prev_err) { + /* Propagate method result; next .then awaits it */ + return ret; + } + + /* Await ret, then rethrow the stored error (possibly wrapped) */ + { + JSValueConst prev_err = argv[0]; + JSValue ret_promise, resolve_fn, reject_fn, then_args[2], result; + ret_promise = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&ret), 0); + JS_FreeValue(ctx, ret); + if (JS_IsException(ret_promise)) + return JS_EXCEPTION; + resolve_fn = JS_NewCFunctionData(ctx, js_async_dispose_rethrow, 0, 0, + 1, &prev_err); + reject_fn = JS_NewCFunctionData(ctx, js_async_dispose_rethrow, 1, 1, + 1, &prev_err); + then_args[0] = resolve_fn; + then_args[1] = reject_fn; + result = JS_Invoke(ctx, ret_promise, JS_ATOM_then, 2, vc(then_args)); + JS_FreeValue(ctx, resolve_fn); + JS_FreeValue(ctx, reject_fn); + JS_FreeValue(ctx, ret_promise); + return result; + } +} + +static JSValue js_disposable_stack_dispose(JSContext *ctx, + JSValueConst this_val, + int argc, + JSValueConst *argv, + int class_id) +{ + JSDisposableStack *s; + + s = JS_GetOpaque2(ctx, this_val, class_id); + if (!s) { + if (class_id == JS_CLASS_ASYNC_DISPOSABLE_STACK) { + JSValue exc = JS_GetException(ctx); + JSValue p = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&exc), + /*is_reject*/1); + JS_FreeValue(ctx, exc); + return p; + } + return JS_EXCEPTION; + } + if (s->disposed) { + if (class_id == JS_CLASS_ASYNC_DISPOSABLE_STACK) { + JSValue undef = JS_UNDEFINED; + return js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&undef), + /*is_reject*/0); + } + return JS_UNDEFINED; + } + if (class_id == JS_CLASS_ASYNC_DISPOSABLE_STACK) { + /* Per spec DisposeResources: iterate resources in LIFO order and + for each do Call + Await. The first Call happens synchronously + inside disposeAsync(); subsequent Calls fire in microtasks via a + Promise.then() chain so that each call sees the previous + dispose's promise already settled. */ + int i, count = s->resource_count; + JSValue chain, undef, ret; + + s->disposed = true; + + /* First (top-of-stack) resource: synchronous Call. */ + undef = JS_UNDEFINED; + i = count - 1; + if (i < 0) { + chain = js_promise_resolve(ctx, ctx->promise_ctor, 1, vc(&undef), + /*is_reject*/0); + if (JS_IsException(chain)) + goto async_dispose_fail; + } else { + JSDisposableResource *res = &s->resources[i]; + if (JS_IsUndefined(res->method)) { + /* null/undefined resource: Await(undefined) */ + chain = js_promise_resolve(ctx, ctx->promise_ctor, 1, + vc(&undef), /*is_reject*/0); + } else { + switch (res->hint) { + case JS_DISPOSE_HINT_ADOPT: + ret = JS_Call(ctx, res->method, JS_UNDEFINED, 1, + vc(&res->value)); + break; + case JS_DISPOSE_HINT_DEFER: + ret = JS_Call(ctx, res->method, JS_UNDEFINED, 0, NULL); + break; + default: + ret = JS_Call(ctx, res->method, res->value, 0, NULL); + break; + } + if (JS_IsException(ret)) { + JSValue err = JS_GetException(ctx); + chain = js_promise_resolve(ctx, ctx->promise_ctor, 1, + vc(&err), /*is_reject*/1); + JS_FreeValue(ctx, err); + } else { + chain = js_promise_resolve(ctx, ctx->promise_ctor, 1, + vc(&ret), /*is_reject*/0); + JS_FreeValue(ctx, ret); + } + } + JS_FreeValue(ctx, res->value); + JS_FreeValue(ctx, res->method); + res->value = JS_UNDEFINED; + res->method = JS_UNDEFINED; + if (JS_IsException(chain)) { + i--; + goto async_dispose_fail; + } + i--; + } + + /* Remaining resources: chain lazy steps. */ + for (; i >= 0; i--) { + JSDisposableResource *res = &s->resources[i]; + JSValueConst data[3]; + JSValue hint_val, resolve_fn, reject_fn, then_args[2], new_chain; + + hint_val = JS_NewInt32(ctx, res->hint); + data[0] = res->value; + data[1] = res->method; + data[2] = hint_val; + resolve_fn = JS_NewCFunctionData(ctx, js_async_dispose_step, 0, 0, + 3, data); + reject_fn = JS_NewCFunctionData(ctx, js_async_dispose_step, 1, 1, + 3, data); + JS_FreeValue(ctx, hint_val); + JS_FreeValue(ctx, res->value); + JS_FreeValue(ctx, res->method); + res->value = JS_UNDEFINED; + res->method = JS_UNDEFINED; + if (JS_IsException(resolve_fn) || JS_IsException(reject_fn)) { + JS_FreeValue(ctx, resolve_fn); + JS_FreeValue(ctx, reject_fn); + JS_FreeValue(ctx, chain); + chain = JS_EXCEPTION; + goto async_dispose_fail; + } + then_args[0] = resolve_fn; + then_args[1] = reject_fn; + new_chain = JS_Invoke(ctx, chain, JS_ATOM_then, 2, vc(then_args)); + JS_FreeValue(ctx, resolve_fn); + JS_FreeValue(ctx, reject_fn); + JS_FreeValue(ctx, chain); + if (JS_IsException(new_chain)) { + chain = JS_EXCEPTION; + goto async_dispose_fail; + } + chain = new_chain; + } + s->resource_count = 0; + + if (count > 0) { + JSValue undef_fn, then_args[1], new_chain; + undef_fn = JS_NewCFunctionData(ctx, js_async_dispose_to_undef, + 0, 0, 0, NULL); + if (JS_IsException(undef_fn)) { + JS_FreeValue(ctx, chain); + return JS_EXCEPTION; + } + then_args[0] = undef_fn; + new_chain = JS_Invoke(ctx, chain, JS_ATOM_then, 1, vc(then_args)); + JS_FreeValue(ctx, undef_fn); + JS_FreeValue(ctx, chain); + return new_chain; + } + return chain; + + async_dispose_fail: + for (; i >= 0; i--) { + JSDisposableResource *res = &s->resources[i]; + JS_FreeValue(ctx, res->value); + JS_FreeValue(ctx, res->method); + res->value = JS_UNDEFINED; + res->method = JS_UNDEFINED; + } + s->resource_count = 0; + return JS_EXCEPTION; + } + if (js_dispose_resources(ctx, s, JS_UNDEFINED) < 0) + return JS_EXCEPTION; + return JS_UNDEFINED; +} + +static JSValue js_disposable_stack_move(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv, int class_id) +{ + JSDisposableStack *s, *ns; + JSValue new_obj; + + s = js_disposable_stack_get(ctx, this_val, class_id); + if (!s) + return JS_EXCEPTION; + /* Use the intrinsic prototype directly so tampering with the global + binding or subclassing cannot redirect move(). */ + new_obj = JS_NewObjectProtoClass(ctx, ctx->class_proto[class_id], + class_id); + if (JS_IsException(new_obj)) + return JS_EXCEPTION; + ns = js_mallocz(ctx, sizeof(*ns)); + if (!ns) { + JS_FreeValue(ctx, new_obj); + return JS_EXCEPTION; + } + JS_SetOpaqueInternal(new_obj, ns); + /* Transfer resources to new stack */ + ns->resources = s->resources; + ns->resource_count = s->resource_count; + ns->resource_capacity = s->resource_capacity; + /* Reset original stack */ + s->resources = NULL; + s->resource_count = 0; + s->resource_capacity = 0; + s->disposed = true; + return new_obj; +} + +static JSValue js_disposable_stack_get_disposed(JSContext *ctx, + JSValueConst this_val, int class_id) +{ + JSDisposableStack *s; + + s = JS_GetOpaque2(ctx, this_val, class_id); + if (!s) + return JS_EXCEPTION; + return js_bool(s->disposed); +} + +static const JSCFunctionListEntry js_disposable_stack_proto_funcs[] = { + JS_CFUNC_MAGIC_DEF("adopt", 2, js_disposable_stack_adopt, JS_CLASS_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("defer", 1, js_disposable_stack_defer, JS_CLASS_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("dispose", 0, js_disposable_stack_dispose, JS_CLASS_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("move", 0, js_disposable_stack_move, JS_CLASS_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("use", 1, js_disposable_stack_use, JS_CLASS_DISPOSABLE_STACK ), + JS_CGETSET_MAGIC_DEF("disposed", js_disposable_stack_get_disposed, NULL, JS_CLASS_DISPOSABLE_STACK ), + JS_ALIAS_DEF("[Symbol.dispose]", "dispose" ), + JS_PROP_STRING_DEF("[Symbol.toStringTag]", "DisposableStack", JS_PROP_CONFIGURABLE ), +}; + +static const JSCFunctionListEntry js_async_disposable_stack_proto_funcs[] = { + JS_CFUNC_MAGIC_DEF("adopt", 2, js_disposable_stack_adopt, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("defer", 1, js_disposable_stack_defer, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("disposeAsync", 0, js_disposable_stack_dispose, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("move", 0, js_disposable_stack_move, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_CFUNC_MAGIC_DEF("use", 1, js_disposable_stack_use, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_CGETSET_MAGIC_DEF("disposed", js_disposable_stack_get_disposed, NULL, JS_CLASS_ASYNC_DISPOSABLE_STACK ), + JS_ALIAS_DEF("[Symbol.asyncDispose]", "disposeAsync" ), + JS_PROP_STRING_DEF("[Symbol.toStringTag]", "AsyncDisposableStack", JS_PROP_CONFIGURABLE), +}; + /* Promise */ typedef struct JSPromiseData { JSPromiseStateEnum promise_state; /* 0=fulfill, 1=reject, list of JSPromiseReactionData.link */ struct list_head promise_reactions[2]; - bool is_handled; /* Note: only useful to debug */ + bool is_handled; JSValue promise_result; } JSPromiseData; @@ -64516,6 +68027,29 @@ JSValue JS_PromiseResult(JSContext *ctx, JSValueConst promise) return js_dup(s->promise_result); } +// Notify the host tracker of an unreported rejection's state +static void call_promise_rejection_tracker(JSContext *ctx, JSValueConst promise, + bool is_handled) +{ + JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE); + JSRuntime *rt = ctx->rt; + if (s && s->promise_state == JS_PROMISE_REJECTED && !s->is_handled && + rt->host_promise_rejection_tracker) { + rt->host_promise_rejection_tracker(ctx, promise, s->promise_result, + is_handled, rt->host_promise_rejection_tracker_opaque); + } +} + +// Mark rejected promise as handled by the engine itself and let the host know that the promise is handled +static void js_promise_set_handled(JSContext *ctx, JSValueConst promise) +{ + JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE); + if (!s) + return; + call_promise_rejection_tracker(ctx, promise, true); + s->is_handled = true; +} + bool JS_IsPromise(JSValueConst val) { if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT) @@ -64523,6 +68057,11 @@ bool JS_IsPromise(JSValueConst val) return JS_VALUE_GET_OBJ(val)->class_id == JS_CLASS_PROMISE; } +void JS_PromiseMarkAsHandled(JSContext *ctx, JSValueConst promise) +{ + js_promise_set_handled(ctx, promise); +} + JSValue JS_NewSettledPromise(JSContext *ctx, bool is_reject, JSValueConst value) { return js_promise_resolve(ctx, ctx->promise_ctor, 1, &value, is_reject); @@ -64631,12 +68170,7 @@ static void fulfill_or_reject_promise(JSContext *ctx, JSValueConst promise, } } - if (s->promise_state == JS_PROMISE_REJECTED && !s->is_handled) { - JSRuntime *rt = ctx->rt; - if (rt->host_promise_rejection_tracker) - rt->host_promise_rejection_tracker(ctx, promise, value, false, - rt->host_promise_rejection_tracker_opaque); - } + call_promise_rejection_tracker(ctx, promise, false); list_for_each_safe(el, el1, &s->promise_reactions[is_reject]) { rd = list_entry(el, JSPromiseReactionData, link); @@ -64710,6 +68244,8 @@ static int js_create_resolving_functions(JSContext *ctx, JSPromiseFunctionDataResolved *sr; int i, ret; + resolving_funcs[0] = JS_UNDEFINED; + resolving_funcs[1] = JS_UNDEFINED; sr = js_malloc(ctx, sizeof(*sr)); if (!sr) return -1; @@ -64725,9 +68261,8 @@ static int js_create_resolving_functions(JSContext *ctx, if (!s) { JS_FreeValue(ctx, obj); fail: - - if (i != 0) - JS_FreeValue(ctx, resolving_funcs[0]); + JS_FreeValue(ctx, resolving_funcs[0]); + resolving_funcs[0] = JS_UNDEFINED; ret = -1; break; } @@ -64991,6 +68526,43 @@ JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs) return js_new_promise_capability(ctx, resolving_funcs, JS_UNDEFINED); } +JSValue JS_PromiseThen(JSContext *ctx, JSValueConst promise, + JSValueConst on_fulfilled, + JSValueConst on_rejected) +{ + JSValue result_promise, resolving_funcs[2]; + JSValueConst handlers[2] = { on_fulfilled, on_rejected }; + JSRuntime *rt = ctx->rt; + JSValueLink link; + bool have_promise_hook; + int i, ret; + + if (!JS_GetOpaque2(ctx, promise, JS_CLASS_PROMISE)) + return JS_EXCEPTION; + + /* Match the embedding API: create an intrinsic promise and do not consult + Promise.prototype.then or Symbol.species. */ + have_promise_hook = (rt->promise_hook != NULL); + if (have_promise_hook) { + link = (JSValueLink){rt->parent_promise, promise}; + rt->parent_promise = &link; + } + result_promise = JS_NewPromiseCapability(ctx, resolving_funcs); + if (have_promise_hook) + rt->parent_promise = link.next; + if (JS_IsException(result_promise)) + return result_promise; + + ret = perform_promise_then(ctx, promise, handlers, vc(resolving_funcs)); + for (i = 0; i < 2; i++) + JS_FreeValue(ctx, resolving_funcs[i]); + if (ret) { + JS_FreeValue(ctx, result_promise); + return JS_EXCEPTION; + } + return result_promise; +} + static JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) { @@ -65027,34 +68599,29 @@ static JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val, static JSValue js_promise_withResolvers(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { - JSValue result_promise, resolving_funcs[2], obj; + static const JSAtom atoms[] = {JS_ATOM_promise, JS_ATOM_resolve, JS_ATOM_reject}; + JSValue values[3], obj, *pval; + int i, ret; + if (!JS_IsObject(this_val)) return JS_ThrowTypeErrorNotAnObject(ctx); - result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val); - if (JS_IsException(result_promise)) + values[0] = js_new_promise_capability(ctx, &values[1], this_val); + if (JS_IsException(values[0])) return JS_EXCEPTION; obj = JS_NewObject(ctx); if (JS_IsException(obj)) goto exception; - if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_promise, result_promise, - JS_PROP_C_W_E) < 0) { - goto exception; - } - result_promise = JS_UNDEFINED; - if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_resolve, resolving_funcs[0], - JS_PROP_C_W_E) < 0) { - goto exception; - } - resolving_funcs[0] = JS_UNDEFINED; - if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_reject, resolving_funcs[1], - JS_PROP_C_W_E) < 0) { - goto exception; + for (i = 0; i < (int)countof(values); i++) { + pval = &values[i]; + ret = JS_DefinePropertyValue(ctx, obj, atoms[i], *pval, JS_PROP_C_W_E); + *pval = JS_UNDEFINED; // consumed by JS_DefinePropertyValue + if (ret < 0) + goto exception; } return obj; exception: - JS_FreeValue(ctx, resolving_funcs[0]); - JS_FreeValue(ctx, resolving_funcs[1]); - JS_FreeValue(ctx, result_promise); + for (i = 0; i < (int)countof(values); i++) + JS_FreeValue(ctx, values[i]); JS_FreeValue(ctx, obj); return JS_EXCEPTION; } @@ -65092,7 +68659,7 @@ static __exception int remainingElementsCount_add(JSContext *ctx, int addend) { JSValue val; - int remainingElementsCount; + int32_t remainingElementsCount; val = JS_GetPropertyUint32(ctx, resolve_element_env, 0); if (JS_IsException(val)) @@ -65123,7 +68690,8 @@ static JSValue js_promise_all_resolve_element(JSContext *ctx, JSValueConst resolve = func_data[3]; JSValueConst resolve_element_env = func_data[4]; JSValue ret, obj; - int is_zero, index; + int is_zero; + int32_t index; if (JS_ToInt32(ctx, &index, func_data[1])) return JS_EXCEPTION; @@ -65425,12 +68993,7 @@ static __exception int perform_promise_then(JSContext *ctx, list_add_tail(&rd_array[i]->link, &s->promise_reactions[i]); } else { JSValueConst args[5]; - if (s->promise_state == JS_PROMISE_REJECTED && !s->is_handled) { - JSRuntime *rt = ctx->rt; - if (rt->host_promise_rejection_tracker) - rt->host_promise_rejection_tracker(ctx, promise, s->promise_result, - true, rt->host_promise_rejection_tracker_opaque); - } + call_promise_rejection_tracker(ctx, promise, true); i = s->promise_state - JS_PROMISE_FULFILLED; rd = rd_array[i]; args[0] = rd->resolving_funcs[0]; @@ -65622,6 +69185,7 @@ static JSValue js_async_from_sync_iterator_unwrap_func_create(JSContext *ctx, static const JSCFunctionListEntry js_async_iterator_proto_funcs[] = { JS_CFUNC_DEF("[Symbol.asyncIterator]", 0, js_iterator_proto_iterator ), + JS_CFUNC_DEF("[Symbol.asyncDispose]", 0, js_async_iterator_proto_dispose ), }; /* AsyncFromSyncIteratorPrototype */ @@ -65820,6 +69384,7 @@ static JSClassShortDef const js_async_class_def[] = { { JS_ATOM_empty_string, js_async_from_sync_iterator_finalizer, js_async_from_sync_iterator_mark }, /* JS_CLASS_ASYNC_FROM_SYNC_ITERATOR */ { JS_ATOM_AsyncGeneratorFunction, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_ASYNC_GENERATOR_FUNCTION */ { JS_ATOM_AsyncGenerator, js_async_generator_finalizer, js_async_generator_mark }, /* JS_CLASS_ASYNC_GENERATOR */ + { JS_ATOM_AsyncDisposableStack, js_disposable_stack_finalizer, js_disposable_stack_mark }, /* JS_CLASS_ASYNC_DISPOSABLE_STACK */ }; int JS_AddIntrinsicPromise(JSContext *ctx) @@ -65899,9 +69464,21 @@ int JS_AddIntrinsicPromise(JSContext *ctx) return -1; JS_FreeValue(ctx, obj1); - return JS_SetConstructor2(ctx, ctx->class_proto[JS_CLASS_ASYNC_GENERATOR_FUNCTION], - ctx->class_proto[JS_CLASS_ASYNC_GENERATOR], - JS_PROP_CONFIGURABLE, JS_PROP_CONFIGURABLE); + if (JS_SetConstructor2(ctx, ctx->class_proto[JS_CLASS_ASYNC_GENERATOR_FUNCTION], + ctx->class_proto[JS_CLASS_ASYNC_GENERATOR], + JS_PROP_CONFIGURABLE, JS_PROP_CONFIGURABLE)) + return -1; + + /* AsyncDisposableStack */ + ctx->class_proto[JS_CLASS_ASYNC_DISPOSABLE_STACK] = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_ASYNC_DISPOSABLE_STACK], + js_async_disposable_stack_proto_funcs, + countof(js_async_disposable_stack_proto_funcs)); + JS_NewGlobalCConstructorMagic(ctx, "AsyncDisposableStack", + js_disposable_stack_constructor, 0, + ctx->class_proto[JS_CLASS_ASYNC_DISPOSABLE_STACK], + JS_CLASS_ASYNC_DISPOSABLE_STACK); + return 0; } /* URI handling */ @@ -67111,7 +70688,11 @@ static JSValue js_Date_parse(JSContext *ctx, JSValueConst this_val, /* convert the string as a byte array */ for (i = 0; i < sp->len && i < (int)countof(buf) - 1; i++) { c = string_get(sp, i); - if (c > 255) + /* match V8 behaviour: treat Unicode space characters as regular spaces + in legacy dates, but not U+2028 and U+2029 (line terminators). */ + if (c != 0x2028 && c != 0x2029 && lre_is_space(c)) + c = ' '; + else if (c > 255) c = (c == 0x2212) ? '-' : 'x'; buf[i] = c; } @@ -67498,28 +71079,62 @@ 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 */ - if (bits >= JS_SHORT_BIG_INT_BITS) { + js_slimb_t sv = JS_VALUE_GET_SHORT_BIG_INT(a); + if (bits >= JS_SHORT_BIG_INT_BITS && (asIntN || sv >= 0)) { res = a; - } else { + } else if (bits < JS_SHORT_BIG_INT_BITS) { uint64_t v; int shift; shift = 64 - bits; - v = JS_VALUE_GET_SHORT_BIG_INT(a); + v = sv; v = v << shift; if (asIntN) v = (int64_t)v >> shift; else v = v >> shift; res = __JS_NewShortBigInt(ctx, v); + } else { + JSBigInt *r; + uint64_t len64 = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS; + int len, shift, i; + + if (len64 > JS_BIGINT_MAX_SIZE) { + JS_FreeValue(ctx, a); + return JS_ThrowRangeError(ctx, "BigInt is too large to allocate"); + } + len = len64; + r = js_bigint_new(ctx, len); + if (!r) { + JS_FreeValue(ctx, a); + return JS_EXCEPTION; + } + r->len = len; + r->tab[0] = sv; + for(i = 1; i < len; i++) + r->tab[i] = -1; + shift = (-bits) & (JS_LIMB_BITS - 1); + r->tab[len - 1] = (r->tab[len - 1] << shift) >> shift; + r = js_bigint_extend(ctx, r, 0); + JS_FreeValue(ctx, a); + if (!r) + return JS_EXCEPTION; + res = JS_CompactBigInt(ctx, r); } } else { JSBigInt *r, *p = JS_VALUE_GET_PTR(a); - if (bits >= p->len * JS_LIMB_BITS) { + int is_neg = js_bigint_sign(p); + if (bits >= (uint64_t)p->len * JS_LIMB_BITS && + (asIntN || !is_neg)) { res = a; } else { + uint64_t len64 = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS; int len, shift, i; js_limb_t v; - len = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS; + if (len64 > JS_BIGINT_MAX_SIZE) { + JS_FreeValue(ctx, a); + return JS_ThrowRangeError(ctx, "BigInt is too large to allocate"); + } + len = len64; r = js_bigint_new(ctx, len); if (!r) { JS_FreeValue(ctx, a); @@ -67527,15 +71142,21 @@ static JSValue js_bigint_asUintN(JSContext *ctx, } r->len = len; for(i = 0; i < len - 1; i++) - r->tab[i] = p->tab[i]; + r->tab[i] = i < p->len ? p->tab[i] : -is_neg; shift = (-bits) & (JS_LIMB_BITS - 1); /* 0 <= shift <= JS_LIMB_BITS - 1 */ - v = p->tab[len - 1] << shift; + v = (len - 1 < p->len ? p->tab[len - 1] : -is_neg) << shift; if (asIntN) v = (js_slimb_t)v >> shift; else v = v >> shift; r->tab[len - 1] = v; + if (!asIntN) + r = js_bigint_extend(ctx, r, 0); + if (!r) { + JS_FreeValue(ctx, a); + return JS_EXCEPTION; + } r = js_bigint_normalize(ctx, r); JS_FreeValue(ctx, a); res = JS_CompactBigInt(ctx, r); @@ -67575,6 +71196,7 @@ static const char * const native_error_name[JS_NATIVE_ERROR_COUNT] = { "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError", "InternalError", "AggregateError", + "SuppressedError", }; /* Minimum amount of objects to be able to compile code and display @@ -67723,7 +71345,16 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) for(int i = 0; i < JS_NATIVE_ERROR_COUNT; i++) { JSValue func_obj; int n_args; - n_args = 1 + (i == JS_AGGREGATE_ERROR); + switch (i) { + case JS_AGGREGATE_ERROR: + n_args = 2; + break; + case JS_SUPPRESSED_ERROR: + n_args = 3; + break; + default: + n_args = 1; + } func_obj = JS_NewCFunction3(ctx, ft.generic, native_error_name[i], n_args, JS_CFUNC_constructor_or_func_magic, i, @@ -67747,26 +71378,41 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) 0); if (JS_IsException(obj2)) return -1; - // quirk: Iterator.prototype.constructor is an accessor property + // quirk: Iterator.prototype.constructor is an accessor property. The getter + // (magic == 0) returns %Iterator%; the setter (magic == 1, length 1) + // implements SetterThatIgnoresPrototypeProperties. They must be separate + // function objects so that magic, not argc, distinguishes get from set. // TODO(bnoordhuis) mildly inefficient because JS_NewCConstructor // first creates a .constructor value property that we then replace with // an accessor - obj1 = JS_NewCFunctionData(ctx, js_iterator_constructor_getset, - 0, 0, 1, vc(&obj2)); - if (JS_IsException(obj1)) { - JS_FreeValue(ctx, obj2); - return -1; - } - ctx->iterator_ctor_getset = js_dup(obj1); - if (JS_DefineProperty(ctx, ctx->class_proto[JS_CLASS_ITERATOR], - JS_ATOM_constructor, JS_UNDEFINED, - obj1, obj1, - JS_PROP_HAS_GET | JS_PROP_HAS_SET | JS_PROP_CONFIGURABLE) < 0) { - JS_FreeValue(ctx, obj2); - JS_FreeValue(ctx, obj1); - return -1; + { + JSValue getter, setter; + getter = JS_NewCFunctionData(ctx, js_iterator_constructor_getset, + 0, 0, 1, vc(&obj2)); + if (JS_IsException(getter)) { + JS_FreeValue(ctx, obj2); + return -1; + } + setter = JS_NewCFunctionData(ctx, js_iterator_constructor_getset, + 1, 1, 1, vc(&obj2)); + if (JS_IsException(setter)) { + JS_FreeValue(ctx, getter); + JS_FreeValue(ctx, obj2); + return -1; + } + ctx->iterator_ctor_getset = js_dup(getter); + if (JS_DefineProperty(ctx, ctx->class_proto[JS_CLASS_ITERATOR], + JS_ATOM_constructor, JS_UNDEFINED, + getter, setter, + JS_PROP_HAS_GET | JS_PROP_HAS_SET | JS_PROP_CONFIGURABLE) < 0) { + JS_FreeValue(ctx, getter); + JS_FreeValue(ctx, setter); + JS_FreeValue(ctx, obj2); + return -1; + } + JS_FreeValue(ctx, getter); + JS_FreeValue(ctx, setter); } - JS_FreeValue(ctx, obj1); ctx->iterator_ctor = obj2; JS_DefineAutoInitProperty(ctx, obj2, JS_ATOM_zip, JS_AUTOINIT_ID_BYTECODE, (void *)(uintptr_t)JS_BUILTIN_ITERATOR_ZIP, @@ -67775,13 +71421,6 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) (void *)(uintptr_t)JS_BUILTIN_ITERATOR_ZIP_KEYED, JS_PROP_WRITABLE|JS_PROP_CONFIGURABLE); - ctx->class_proto[JS_CLASS_ITERATOR_CONCAT] = - JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR], - js_iterator_concat_proto_funcs, - countof(js_iterator_concat_proto_funcs)); - if (JS_IsException(ctx->class_proto[JS_CLASS_ITERATOR_CONCAT])) - return -1; - ctx->class_proto[JS_CLASS_ITERATOR_HELPER] = JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR], js_iterator_helper_proto_funcs, @@ -67789,6 +71428,10 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) if (JS_IsException(ctx->class_proto[JS_CLASS_ITERATOR_HELPER])) return -1; + /* Iterator.concat() returns an Iterator Helper */ + ctx->class_proto[JS_CLASS_ITERATOR_CONCAT] = + js_dup(ctx->class_proto[JS_CLASS_ITERATOR_HELPER]); + ctx->class_proto[JS_CLASS_ITERATOR_WRAP] = JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR], js_iterator_wrap_proto_funcs, @@ -67878,7 +71521,6 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) return -1; /* Math: create as autoinit object */ - js_random_init(ctx); if (JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_math_obj, countof(js_math_obj))) return -1; @@ -67921,6 +71563,16 @@ int JS_AddIntrinsicBaseObjects(JSContext *ctx) JS_PROP_CONFIGURABLE, JS_PROP_CONFIGURABLE)) return -1; + /* explicit resource management */ + ctx->class_proto[JS_CLASS_DISPOSABLE_STACK] = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_DISPOSABLE_STACK], + js_disposable_stack_proto_funcs, + countof(js_disposable_stack_proto_funcs)); + JS_NewGlobalCConstructorMagic(ctx, "DisposableStack", + js_disposable_stack_constructor, 0, + ctx->class_proto[JS_CLASS_DISPOSABLE_STACK], + JS_CLASS_DISPOSABLE_STACK); + /* global properties */ ctx->eval_obj = JS_GetProperty(ctx, ctx->global_obj, JS_ATOM_eval); if (JS_IsException(ctx->eval_obj)) @@ -67950,7 +71602,7 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, uint64_t len, uint64_t *max_len, JSClassID class_id, uint8_t *buf, - JSFreeArrayBufferDataFunc *free_func, + JSReallocArrayBufferDataFunc *realloc_func, void *opaque, bool alloc_flag) { JSRuntime *rt = ctx->rt; @@ -67958,12 +71610,15 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, JSArrayBuffer *abuf = NULL; uint64_t sab_alloc_len; - if (!alloc_flag && buf && max_len && free_func != js_array_buffer_free) { - // not observable from JS land, only through C API misuse; - // JS code cannot create externally managed buffers directly + // SABs commit their maximum size upfront and are therefore never + // reallocated; everything else needs a way to resize the backing memory. + // Not observable from JS land, only through C API misuse; JS code cannot + // create externally managed buffers directly. + if (!alloc_flag && buf && max_len && !realloc_func && + class_id != JS_CLASS_SHARED_ARRAY_BUFFER) { return JS_ThrowInternalError(ctx, - "resizable ArrayBuffers not supported " - "for externally managed buffers"); + "resizable ArrayBuffers require a " + "realloc function"); } obj = js_create_from_ctor(ctx, new_target, class_id); if (JS_IsException(obj)) @@ -67977,6 +71632,13 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, JS_ThrowRangeError(ctx, "invalid max array buffer length"); goto fail; } + if (alloc_flag && class_id == JS_CLASS_SHARED_ARRAY_BUFFER && max_len && + *max_len > len && !rt->sab_funcs.sab_alloc) { + JS_ThrowTypeError(ctx, + "growable SharedArrayBuffer requires " + "SAB allocator hooks"); + goto fail; + } abuf = js_malloc(ctx, sizeof(*abuf)); if (!abuf) goto fail; @@ -68010,7 +71672,7 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, abuf->data = js_mallocz(ctx, 1); if (!abuf->data) goto fail; - free_func = js_array_buffer_free; + realloc_func = js_array_buffer_realloc; } } init_list_head(&abuf->array_list); @@ -68018,7 +71680,7 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, abuf->immutable = false; abuf->shared = (class_id == JS_CLASS_SHARED_ARRAY_BUFFER); abuf->opaque = opaque; - abuf->free_func = free_func; + abuf->realloc_func = realloc_func; if (alloc_flag && buf) memcpy(abuf->data, buf, len); JS_SetOpaqueInternal(obj, abuf); @@ -68029,9 +71691,10 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx, return JS_EXCEPTION; } -static void js_array_buffer_free(JSRuntime *rt, void *opaque, void *ptr) +static void *js_array_buffer_realloc(JSRuntime *rt, void *opaque, void *ptr, + size_t size) { - js_free_rt(rt, ptr); + return js_realloc_rt(rt, ptr, size); } static JSValue js_array_buffer_constructor2(JSContext *ctx, @@ -68040,8 +71703,8 @@ static JSValue js_array_buffer_constructor2(JSContext *ctx, JSClassID class_id) { return js_array_buffer_constructor3(ctx, new_target, len, max_len, - class_id, NULL, js_array_buffer_free, - NULL, true); + class_id, NULL, + js_array_buffer_realloc, NULL, true); } static JSValue js_array_buffer_constructor1(JSContext *ctx, @@ -68053,13 +71716,23 @@ static JSValue js_array_buffer_constructor1(JSContext *ctx, } JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared) + size_t max_len, + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared) { JSClassID class_id = is_shared ? JS_CLASS_SHARED_ARRAY_BUFFER : JS_CLASS_ARRAY_BUFFER; - return js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL, class_id, - buf, free_func, opaque, false); + uint64_t max_len64 = max_len, *pmax_len = NULL; + + /* a zero 'max_len' means the ArrayBuffer is not resizable */ + if (max_len > 0) { + if (max_len < len) + return JS_ThrowRangeError(ctx, "invalid max array buffer length"); + pmax_len = &max_len64; + } + return js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, pmax_len, + class_id, buf, realloc_func, opaque, + false); } bool JS_IsArrayBuffer(JSValueConst obj) { @@ -68072,7 +71745,7 @@ JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len) return js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL, JS_CLASS_ARRAY_BUFFER, (uint8_t *)buf, - js_array_buffer_free, NULL, + js_array_buffer_realloc, NULL, true); } @@ -68154,8 +71827,8 @@ static void js_array_buffer_finalizer(JSRuntime *rt, JSValueConst val) if (abuf->shared && rt->sab_funcs.sab_free) { rt->sab_funcs.sab_free(rt->sab_funcs.sab_opaque, abuf->data); } else { - if (abuf->free_func) - abuf->free_func(rt, abuf->opaque, abuf->data); + if (abuf->realloc_func) + abuf->realloc_func(rt, abuf->opaque, abuf->data, 0); } js_free_rt(rt, abuf); } @@ -68256,8 +71929,13 @@ void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj) if (!abuf || abuf->detached) return; - if (abuf->free_func) - abuf->free_func(ctx->rt, abuf->opaque, abuf->data); + if (abuf->realloc_func) { + abuf->realloc_func(ctx->rt, abuf->opaque, abuf->data, 0); + /* The backing data has been released. Do not release it again when + the detached ArrayBuffer object is later finalized. */ + abuf->realloc_func = NULL; + abuf->opaque = NULL; + } abuf->data = NULL; abuf->byte_length = 0; abuf->detached = true; @@ -68382,6 +72060,8 @@ static JSValue js_array_buffer_transfer(JSContext *ctx, JSValueConst this_val, JSArrayBuffer *abuf; uint64_t new_len, old_len, max_len, *pmax_len; uint8_t *bs, *new_bs; + JSReallocArrayBufferDataFunc *realloc_func; + void *opaque; JSValue ret; abuf = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_BUFFER); @@ -68389,13 +72069,14 @@ static JSValue js_array_buffer_transfer(JSContext *ctx, JSValueConst this_val, return JS_EXCEPTION; if (abuf->shared) return JS_ThrowTypeError(ctx, "cannot transfer a SharedArrayBuffer"); - if (magic == JS_ARRAY_BUFFER_TRANSFER_TO_IMMUTABLE && abuf->immutable) - return JS_ThrowTypeErrorImmutableArrayBuffer(ctx); + // Spec (ArrayBufferCopyAndDetach): the newLength argument must be + // coerced (its valueOf observed) before the buffer's detachability / + // immutability is checked, so side effects of coercion are visible. if (argc < 1 || JS_IsUndefined(argv[0])) new_len = abuf->byte_length; else if (JS_ToIndex(ctx, &new_len, argv[0])) return JS_EXCEPTION; - if (magic != JS_ARRAY_BUFFER_TRANSFER_TO_IMMUTABLE && abuf->immutable) + if (abuf->immutable) return JS_ThrowTypeErrorImmutableArrayBuffer(ctx); if (abuf->detached) return JS_ThrowTypeErrorDetachedArrayBuffer(ctx); @@ -68405,9 +72086,7 @@ static JSValue js_array_buffer_transfer(JSContext *ctx, JSValueConst this_val, max_len = abuf->max_byte_length; if (new_len > max_len) return JS_ThrowTypeError(ctx, "invalid array buffer length"); - // TODO(bnoordhuis) support externally managed RABs - if (abuf->free_func == js_array_buffer_free) - pmax_len = &max_len; + pmax_len = &max_len; } } /* create an empty AB */ @@ -68419,23 +72098,33 @@ static JSValue js_array_buffer_transfer(JSContext *ctx, JSValueConst this_val, } bs = abuf->data; old_len = abuf->byte_length; + realloc_func = abuf->realloc_func; + opaque = abuf->opaque; /* if length mismatch, realloc. Otherwise, use the same backing buffer. */ if (new_len != old_len) { - new_bs = js_realloc(ctx, bs, new_len); + if (!realloc_func) + return JS_ThrowTypeError(ctx, "array buffer is not resizable"); + /* XXX: we are currently limited to 2 GB; check before reallocating, + new_len does not necessarily fit in a size_t */ + if (new_len > INT32_MAX) + return JS_ThrowRangeError(ctx, "invalid array buffer length"); + new_bs = realloc_func(ctx->rt, opaque, bs, new_len); if (!new_bs) - return JS_EXCEPTION; + return JS_ThrowOutOfMemory(ctx); bs = new_bs; if (new_len > old_len) memset(bs + old_len, 0, new_len - old_len); } - /* neuter the backing buffer */ + /* neuter the source */ abuf->data = NULL; abuf->byte_length = 0; abuf->detached = true; + abuf->realloc_func = NULL; + abuf->opaque = NULL; js_array_buffer_update_typed_arrays(abuf); ret = js_array_buffer_constructor3(ctx, JS_UNDEFINED, new_len, pmax_len, JS_CLASS_ARRAY_BUFFER, bs, - abuf->free_func, NULL, + realloc_func, opaque, /*alloc_flag*/false); fini: if (magic == JS_ARRAY_BUFFER_TRANSFER_TO_IMMUTABLE) { @@ -68467,9 +72156,6 @@ static JSValue js_array_buffer_resize(JSContext *ctx, JSValueConst this_val, return JS_ThrowTypeErrorDetachedArrayBuffer(ctx); if (!array_buffer_is_resizable(abuf)) return JS_ThrowTypeError(ctx, "array buffer is not resizable"); - // TODO(bnoordhuis) support externally managed RABs - if (abuf->free_func != js_array_buffer_free) - return JS_ThrowTypeError(ctx, "external array buffer is not resizable"); if (len < 0 || len > abuf->max_byte_length) { bad_length: return JS_ThrowRangeError(ctx, "invalid array buffer length"); @@ -68489,9 +72175,13 @@ static JSValue js_array_buffer_resize(JSContext *ctx, JSValueConst this_val, // 2 bytes big in A, and 1 byte big in B abuf->byte_length = len; } else { - data = js_realloc(ctx, abuf->data, max_int(len, 1)); + // the ArrayBuffer cannot be resizable without a way to resize it, + // js_array_buffer_constructor3 rejects that combination + assert(abuf->realloc_func); + data = abuf->realloc_func(ctx->rt, abuf->opaque, abuf->data, + max_int(len, 1)); if (!data) - return JS_EXCEPTION; + return JS_ThrowOutOfMemory(ctx); if (len > abuf->byte_length) memset(&data[abuf->byte_length], 0, len - abuf->byte_length); abuf->byte_length = len; @@ -68572,10 +72262,17 @@ static JSValue js_array_buffer_slice(JSContext *ctx, goto fail; } /* must test again because of side effects */ - if (abuf->detached || abuf->byte_length < start + new_len) { + if (abuf->detached) { JS_ThrowTypeErrorDetachedArrayBuffer(ctx); goto fail; } + if (abuf->byte_length < start + new_len) { + if (immutable) + JS_ThrowRangeError(ctx, "invalid array buffer length"); + else + JS_ThrowTypeErrorDetachedArrayBuffer(ctx); + goto fail; + } memcpy(new_abuf->data, abuf->data + start, new_len); new_abuf->immutable = immutable; return new_obj; @@ -68734,9 +72431,21 @@ static JSValue js_typed_array_get_byteOffset(JSContext *ctx, JSValueConst this_v JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv, JSTypedArrayEnum type) { + JSValueConst temp[3]; + if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64) return JS_ThrowRangeError(ctx, "invalid typed array type"); - + // js_typed_array_constructor makes assumptions about the length + // of the argv vector without checking argc, so let's ensure our + // side upholds said assumptions + if (argc < countof(temp)) { + temp[0] = temp[1] = temp[2] = JS_UNDEFINED; + switch (argc) { + case 2: temp[1] = argv[1]; // fallthru + case 1: temp[0] = argv[0]; // fallthru + } + argv = temp; + } return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv, JS_CLASS_UINT8C_ARRAY + type); } @@ -68917,7 +72626,7 @@ static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val, if (idx < 0) idx = len + idx; - if (idx < 0 || idx >= p->u.array.count) + if (idx < 0 || idx >= len || idx >= p->u.array.count) return JS_UNDEFINED; switch (p->class_id) { @@ -68972,10 +72681,13 @@ static JSValue js_typed_array_with(JSContext *ctx, JSValueConst this_val, if (idx < 0) idx = len + idx; - val = JS_ToPrimitive(ctx, argv[1], HINT_NUMBER); + if (p->class_id == JS_CLASS_BIG_INT64_ARRAY || p->class_id == JS_CLASS_BIG_UINT64_ARRAY) { + val = JS_ToBigInt(ctx, argv[1]); + } else { + val = JS_ToNumber(ctx, argv[1]); + } if (JS_IsException(val)) return JS_EXCEPTION; - /* re-validate after user code (spec step 9: IsValidIntegerIndex) */ if (typed_array_is_oob(p)) { JS_FreeValue(ctx, val); @@ -69052,7 +72764,8 @@ static JSValue js_create_typed_array_iterator(JSContext *ctx, JSValueConst this_ } static JSValue js_typed_array_create(JSContext *ctx, JSValueConst ctor, - int argc, JSValueConst *argv) + int argc, JSValueConst *argv, + bool require_mutable) { JSValue ret; int new_len; @@ -69065,6 +72778,10 @@ static JSValue js_typed_array_create(JSContext *ctx, JSValueConst ctor, new_len = js_typed_array_get_length_unsafe(ctx, ret); if (new_len < 0) goto fail; + if (require_mutable && typed_array_is_immutable(JS_VALUE_GET_OBJ(ret))) { + JS_ThrowTypeErrorImmutableArrayBuffer(ctx); + goto fail; + } if (argc == 1) { /* ensure that it is large enough */ if (JS_ToLengthFree(ctx, &len, js_dup(argv[0]))) @@ -69079,15 +72796,17 @@ static JSValue js_typed_array_create(JSContext *ctx, JSValueConst ctor, return ret; } +// expects typed array object in argv[0] so argc *must* be > 0 static JSValue js_typed_array___speciesCreate(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv) + int argc, JSValueConst *argv, + bool require_mutable) { JSValueConst obj; JSObject *p; JSValue ctor, ret; - int argc1; + assert(argc > 0); obj = argv[0]; p = get_typed_array(ctx, obj); if (!p) @@ -69095,12 +72814,13 @@ static JSValue js_typed_array___speciesCreate(JSContext *ctx, ctor = JS_SpeciesConstructor(ctx, obj, JS_UNDEFINED); if (JS_IsException(ctor)) return ctor; - argc1 = max_int(argc - 1, 0); + argc--; + argv++; if (JS_IsUndefined(ctor)) { - ret = js_typed_array_constructor(ctx, JS_UNDEFINED, argc1, argv + 1, + ret = js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv, p->class_id); } else { - ret = js_typed_array_create(ctx, ctor, argc1, argv + 1); + ret = js_typed_array_create(ctx, ctor, argc, argv, require_mutable); JS_FreeValue(ctx, ctor); } return ret; @@ -69138,14 +72858,24 @@ static JSValue js_typed_array_from(JSContext *ctx, JSValueConst this_val, iter = JS_GetProperty(ctx, items, JS_ATOM_Symbol_iterator); if (JS_IsException(iter)) goto exception; - if (!JS_IsUndefined(iter)) { - JS_FreeValue(ctx, iter); + if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) { + if (!JS_IsFunction(ctx, iter)) { + JS_FreeValue(ctx, iter); + JS_ThrowTypeError(ctx, "value is not iterable"); + goto exception; + } arr = JS_NewArray(ctx); - if (JS_IsException(arr)) + if (JS_IsException(arr)) { + JS_FreeValue(ctx, iter); goto exception; - stack[0] = js_dup(items); - if (js_for_of_start(ctx, &stack[1], false)) + } + stack[0] = JS_GetIterator2(ctx, items, iter); + JS_FreeValue(ctx, iter); + if (JS_IsException(stack[0])) goto exception; + stack[1] = JS_GetProperty(ctx, stack[0], JS_ATOM_next); + if (JS_IsException(stack[1])) + goto exception_close; for (k = 0;; k++) { v = JS_IteratorNext(ctx, stack[0], stack[1], 0, NULL, &done); if (JS_IsException(v)) @@ -69163,7 +72893,7 @@ static JSValue js_typed_array_from(JSContext *ctx, JSValueConst this_val, if (js_get_length64(ctx, &len, arr) < 0) goto exception; v = js_int64(len); - r = js_typed_array_create(ctx, this_val, 1, vc(&v)); + r = js_typed_array_create(ctx, this_val, 1, vc(&v), true); JS_FreeValue(ctx, v); if (JS_IsException(r)) goto exception; @@ -69205,7 +72935,7 @@ static JSValue js_typed_array_of(JSContext *ctx, JSValueConst this_val, int i; v = js_int32(argc); - obj = js_typed_array_create(ctx, this_val, 1, vc(&v)); + obj = js_typed_array_create(ctx, this_val, 1, vc(&v), true); if (JS_IsException(obj)) return obj; @@ -69474,18 +73204,27 @@ static JSValue js_typed_array_indexOf(JSContext *ctx, JSValueConst this_val, exception is raised) */ if (typed_array_is_oob(p) || len > p->u.array.count) { /* "includes" scans all the properties, so "undefined" can match */ - if (special == special_includes) + if (special == special_includes) { if (JS_IsUndefined(argv[0])) if (k < typed_array_length(p)) res = 0; - goto done; + goto done; + } + /* lastIndexOf scans downward, so when the buffer merely shrank + during argument coercion the scan continues in the still + valid range; the vanished indices simply cannot match */ + if (special != special_lastIndexOf || typed_array_is_oob(p)) + goto done; } // RAB may have been resized by evil .valueOf method len = min_int(len, p->u.array.count); if (len == 0) goto done; - k = min_int(k, len); + if (special == special_lastIndexOf) + k = min_int(k, len - 1); + else + k = min_int(k, len); stop = min_int(stop, len); is_bigint = 0; @@ -69804,8 +73543,10 @@ static JSValue js_typed_array_reverse(JSContext *ctx, JSValueConst this_val, len = js_typed_array_get_length_unsafe(ctx, this_val); if (len < 0) return JS_EXCEPTION; + p = JS_VALUE_GET_OBJ(this_val); + if (typed_array_is_immutable(p)) + return JS_ThrowTypeErrorImmutableArrayBuffer(ctx); if (len > 0) { - p = JS_VALUE_GET_OBJ(this_val); switch (typed_array_size_log2(p->class_id)) { case 0: { @@ -69903,7 +73644,7 @@ static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val, args[0] = this_val; args[1] = js_int32(count); - arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args); + arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args, true); if (JS_IsException(arr)) goto exception; @@ -69919,10 +73660,22 @@ static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val, if (p1 != NULL && p->class_id == p1->class_id && typed_array_length(p1) >= count && typed_array_length(p) >= start + count) { + uint8_t *src, *dst; + int nbytes; shift = typed_array_size_log2(p->class_id); - memmove(p1->u.array.u.uint8_ptr, - p->u.array.u.uint8_ptr + (start << shift), - count << shift); + src = p->u.array.u.uint8_ptr + (start << shift); + dst = p1->u.array.u.uint8_ptr; + nbytes = count << shift; + if (dst > src && dst < src + nbytes) { + /* per spec (step 14.g) the copy is performed byte by byte + in increasing order, observable when the species + constructor returns a view over the same buffer that + overlaps the source ahead of it */ + for (n = 0; n < nbytes; n++) + dst[n] = src[n]; + } else { + memmove(dst, src, nbytes); + } } else { slow_path: space = max_int(0, p->u.array.count - start); @@ -69947,12 +73700,11 @@ static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val, static JSValue js_typed_array_subarray(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { - JSArrayBuffer *abuf; JSTypedArray *ta; JSValueConst args[4]; - JSValue arr, byteOffset, ta_buffer; + JSValue arr, ta_buffer; JSObject *p; - int len, start, final, count, shift, offset; + int len, start, final, count, shift, offset, nargs; p = get_typed_array(ctx, this_val); if (!p) @@ -69966,21 +73718,13 @@ static JSValue js_typed_array_subarray(JSContext *ctx, JSValueConst this_val, goto exception; } count = max_int(final - start, 0); - byteOffset = js_typed_array_get_byteOffset(ctx, this_val); - if (JS_IsException(byteOffset)) - goto exception; ta = p->u.typed_array; - abuf = ta->buffer->u.array_buffer; - if (ta->offset > abuf->byte_length) - goto range_error; - if (ta->offset == abuf->byte_length && count > 0) { - range_error: - JS_ThrowRangeError(ctx, "invalid offset"); - goto exception; - } shift = typed_array_size_log2(p->class_id); - offset = JS_VALUE_GET_INT(byteOffset) + (start << shift); - JS_FreeValue(ctx, byteOffset); + /* per spec, read the [[ByteOffset]] slot directly: it keeps its value + even when the buffer was detached or resized by the argument + coercions above; validation is left to the constructor called by + TypedArraySpeciesCreate */ + offset = ta->offset + (start << shift); ta_buffer = js_typed_array_get_buffer(ctx, this_val); if (JS_IsException(ta_buffer)) goto exception; @@ -69988,10 +73732,13 @@ static JSValue js_typed_array_subarray(JSContext *ctx, JSValueConst this_val, args[1] = safe_const(ta_buffer); args[2] = safe_const(js_int32(offset)); args[3] = safe_const(js_int32(count)); - // result is length-tracking if source TA is and no explicit count is given + // result is length-tracking if source TA is and no explicit end is given; + // the constructor is then called without a length argument if (ta->track_rab && JS_IsUndefined(argv[1])) - args[3] = JS_UNDEFINED; - arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 4, args); + nargs = 3; + else + nargs = 4; + arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, nargs, args, false); JS_FreeValue(ctx, ta_buffer); return arr; exception: @@ -70251,7 +73998,7 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValueConst this_val, size_t i, j; /* XXX: a stable sort would use less memory */ - array_idx = js_malloc(ctx, len * sizeof(array_idx[0])); + array_idx = js_calloc(ctx, len, sizeof(array_idx[0])); if (!array_idx) return JS_EXCEPTION; for(i = 0; i < len; i++) @@ -70540,6 +74287,14 @@ static JSValue js_typed_array_constructor_ta(JSContext *ctx, JS_ThrowTypeErrorArrayBufferOOB(ctx); goto fail; } + bool src_is_bigint = p->class_id == JS_CLASS_BIG_INT64_ARRAY || + p->class_id == JS_CLASS_BIG_UINT64_ARRAY; + bool dst_is_bigint = classid == JS_CLASS_BIG_INT64_ARRAY || + classid == JS_CLASS_BIG_UINT64_ARRAY; + if (src_is_bigint != dst_is_bigint) { + JS_ThrowTypeError(ctx, "Cannot mix BigInt and other types, use explicit conversions"); + goto fail; + } if (len > p->u.array.count) { JS_ThrowRangeError(ctx, "length out of bounds"); goto fail; @@ -71109,13 +74864,13 @@ static JSValue js_new_uint8array(JSContext *ctx, JSValue buffer) } JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared) + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared) { JSClassID class_id = is_shared ? JS_CLASS_SHARED_ARRAY_BUFFER : JS_CLASS_ARRAY_BUFFER; JSValue buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL, - class_id, buf, free_func, + class_id, buf, realloc_func, opaque, false); return js_new_uint8array(ctx, buffer); } @@ -71125,7 +74880,7 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len) JSValue buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL, JS_CLASS_ARRAY_BUFFER, (uint8_t *)buf, - js_array_buffer_free, NULL, + js_array_buffer_realloc, NULL, true); return js_new_uint8array(ctx, buffer); } @@ -71375,6 +75130,7 @@ static JSValue js_atomics_store(JSContext *ctx, } /* check if an evil .valueOf has resized or detached the array */ 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); @@ -71391,6 +75147,7 @@ static JSValue js_atomics_store(JSContext *ctx, } /* check if an evil .valueOf has resized or detached the array */ 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); @@ -71545,7 +75302,8 @@ static JSValue js_atomics_notify(JSContext *ctx, { struct list_head *el, *el1, waiter_list; int size_log2; - int32_t count, n; + int count; + int32_t n; void *ptr; uint64_t idx; JSObject *p; @@ -71630,6 +75388,8 @@ static int JS_AddIntrinsicAtomics(JSContext *ctx) #endif /* CONFIG_ATOMICS */ +static int js_uint8array_funcs_init(JSContext *ctx); + int JS_AddIntrinsicTypedArrays(JSContext *ctx) { JSValue typed_array_base_func, typed_array_base_proto, obj; @@ -71704,6 +75464,10 @@ int JS_AddIntrinsicTypedArrays(JSContext *ctx) } JS_FreeValue(ctx, typed_array_base_func); + /* Uint8Array base64/hex methods */ + if (js_uint8array_funcs_init(ctx)) + return -1; + /* DataView */ obj = JS_NewCConstructor(ctx, JS_CLASS_DATAVIEW, "DataView", js_dataview_constructor, 1, JS_CFUNC_constructor, 0, @@ -71727,7 +75491,8 @@ int JS_AddIntrinsicTypedArrays(JSContext *ctx) static double js__now_ms(void) { - return js__hrtime_ns() / 1e6; + uint64_t ns = js__hrtime_ns(); + return (double)(ns / 1000000) + (double)(ns % 1000000) / 1e6; } static JSValue js_perf_now(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) @@ -72146,12 +75911,12 @@ static void reset_weak_ref(JSRuntime *rt, JSWeakRefRecord **first_weak_ref) bool enqueue = !rt->in_free; if (enqueue && JS_IsObject(fre->held_val)) { JSObject *p = JS_VALUE_GET_OBJ(fre->held_val); - if (p->free_mark || p->header.mark) + if (p->free_mark || JS_GC_MARK(p)) enqueue = false; } if (enqueue && JS_IsObject(fre->cb)) { JSObject *p = JS_VALUE_GET_OBJ(fre->cb); - if (p->free_mark || p->header.mark) + if (p->free_mark || JS_GC_MARK(p)) enqueue = false; } if (enqueue) { @@ -72235,7 +76000,12 @@ static JSValue js_new_callsite(JSContext *ctx, JSCallSiteData *csd) { return JS_EXCEPTION; } + /* The new object takes ownership of the values in |csd|; clear them so + the caller doesn't free them a second time. */ memcpy(csd1, csd, sizeof(*csd)); + csd->filename = JS_UNDEFINED; + csd->func = JS_UNDEFINED; + csd->func_name = JS_UNDEFINED; JS_SetOpaqueInternal(obj, csd1); @@ -72247,6 +76017,7 @@ static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFra const char *func_name_str; JSObject *p; + csd->constructor = sf->is_constructor; csd->func = js_dup(sf->cur_func); /* func_name_str is UTF-8 encoded if needed */ func_name_str = get_func_name(ctx, sf->cur_func); @@ -72286,6 +76057,7 @@ static void js_new_callsite_data2(JSContext *ctx, JSCallSiteData *csd, const cha csd->func = JS_NULL; csd->func_name = JS_NULL; csd->native = false; + csd->constructor = false; csd->line_num = line_num; csd->col_num = col_num; /* filename is UTF-8 encoded if needed (original argument to __JS_EvalInternal()) */ @@ -72313,17 +76085,29 @@ static JSValue js_callsite_isnative(JSContext *ctx, JSValueConst this_val, int a return js_bool(csd->native); } +static JSValue js_callsite_isconstructor(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) +{ + JSCallSiteData *csd = JS_GetOpaque2(ctx, this_val, JS_CLASS_CALL_SITE); + if (!csd) + return JS_EXCEPTION; + return js_bool(csd->constructor); +} + static JSValue js_callsite_getnumber(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) { JSCallSiteData *csd = JS_GetOpaque2(ctx, this_val, JS_CLASS_CALL_SITE); if (!csd) return JS_EXCEPTION; int *field = (void *)((char *)csd + magic); + /* V8 returns null when the frame has no source position (native frames) */ + if (*field < 0) + return JS_NULL; return js_int32(*field); } static const JSCFunctionListEntry js_callsite_proto_funcs[] = { JS_CFUNC_DEF("isNative", 0, js_callsite_isnative), + JS_CFUNC_DEF("isConstructor", 0, js_callsite_isconstructor), JS_CFUNC_MAGIC_DEF("getFileName", 0, js_callsite_getfield, offsetof(JSCallSiteData, filename)), JS_CFUNC_MAGIC_DEF("getFunction", 0, js_callsite_getfield, offsetof(JSCallSiteData, func)), JS_CFUNC_MAGIC_DEF("getFunctionName", 0, js_callsite_getfield, offsetof(JSCallSiteData, func_name)), @@ -72421,13 +76205,13 @@ static JSValue js_domexception_constructor0(JSContext *ctx, JSValueConst new_tar obj = js_create_from_ctor(ctx, new_target, JS_CLASS_DOM_EXCEPTION); if (JS_IsException(obj)) return JS_EXCEPTION; - if (!JS_IsUndefined(argv[0])) + if (argc > 0 && !JS_IsUndefined(argv[0])) message = JS_ToString(ctx, argv[0]); else message = js_empty_string(ctx->rt); if (JS_IsException(message)) goto fail1; - if (!JS_IsUndefined(argv[1])) + if (argc > 1 && !JS_IsUndefined(argv[1])) name = JS_ToString(ctx, argv[1]); else name = JS_AtomToString(ctx, JS_ATOM_Error); @@ -72501,11 +76285,12 @@ static JSValue js_domexception_get_code(JSContext *ctx, JSValueConst this_val) } static const JSCFunctionListEntry js_domexception_proto_funcs[] = { - JS_CGETSET_MAGIC_DEF("name", js_domexception_getfield, NULL, - offsetof(JSDOMExceptionData, name) ), - JS_CGETSET_MAGIC_DEF("message", js_domexception_getfield, NULL, - offsetof(JSDOMExceptionData, message) ), - JS_CGETSET_DEF("code", js_domexception_get_code, NULL ), + JS_CGETSET_MAGIC_DEF2("name", js_domexception_getfield, NULL, + offsetof(JSDOMExceptionData, name), JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE ), + JS_CGETSET_MAGIC_DEF2("message", js_domexception_getfield, NULL, + offsetof(JSDOMExceptionData, message), JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE ), + JS_CGETSET_DEF2("code", js_domexception_get_code, NULL, + JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE ), JS_PROP_STRING_DEF("[Symbol.toStringTag]", "DOMException", JS_PROP_CONFIGURABLE ), }; @@ -72558,7 +76343,7 @@ int JS_AddIntrinsicDOMException(JSContext *ctx) JS_SetPropertyFunctionList(ctx, proto, js_domexception_proto_funcs, countof(js_domexception_proto_funcs)); - ctor = JS_NewCFunction2(ctx, js_domexception_constructor, "DOMException", 2, + ctor = JS_NewCFunction2(ctx, js_domexception_constructor, "DOMException", 0, JS_CFUNC_constructor_or_func, 0); JS_SetConstructor(ctx, ctor, proto); for (i = 0; i < countof(js_dom_exception_names_table); i++) { @@ -72571,7 +76356,1000 @@ int JS_AddIntrinsicDOMException(JSContext *ctx) } JS_DefinePropertyValue(ctx, ctx->global_obj, JS_ATOM_DOMException, ctor, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); - ctx->class_proto[JS_CLASS_DOM_EXCEPTION] = proto; + set_value(ctx, &ctx->class_proto[JS_CLASS_DOM_EXCEPTION], proto); + return 0; +} +/* base64 */ + +static const unsigned char b64_enc[64] = { + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', + 'Q','R','S','T','U','V','W','X','Y','Z', + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p', + 'q','r','s','t','u','v','w','x','y','z', + '0','1','2','3','4','5','6','7','8','9', + '+','/' +}; + +static const unsigned char b64url_enc[64] = { + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', + 'Q','R','S','T','U','V','W','X','Y','Z', + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p', + 'q','r','s','t','u','v','w','x','y','z', + '0','1','2','3','4','5','6','7','8','9', + '-','_' +}; + +enum { K_VAL = 1u, K_WS = 2u }; + +static const uint8_t b64_val[256] = { + ['A']=0, ['B']=1, ['C']=2, ['D']=3, ['E']=4, ['F']=5, ['G']=6, ['H']=7, + ['I']=8, ['J']=9, ['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15, + ['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25, + ['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33, + ['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41, + ['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51, + ['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61, + ['+']=62, ['/']=63, + ['-']=62, ['_']=63, +}; + +static const char b64_flags[256] = { + [' ']=K_WS, ['\t']=K_WS, ['\n']=K_WS, ['\f']=K_WS, ['\r']=K_WS, + ['A']=K_VAL,['B']=K_VAL,['C']=K_VAL,['D']=K_VAL,['E']=K_VAL,['F']=K_VAL,['G']=K_VAL,['H']=K_VAL, + ['I']=K_VAL,['J']=K_VAL,['K']=K_VAL,['L']=K_VAL,['M']=K_VAL,['N']=K_VAL,['O']=K_VAL,['P']=K_VAL, + ['Q']=K_VAL,['R']=K_VAL,['S']=K_VAL,['T']=K_VAL,['U']=K_VAL,['V']=K_VAL,['W']=K_VAL,['X']=K_VAL, + ['Y']=K_VAL,['Z']=K_VAL, + ['a']=K_VAL,['b']=K_VAL,['c']=K_VAL,['d']=K_VAL,['e']=K_VAL,['f']=K_VAL,['g']=K_VAL,['h']=K_VAL, + ['i']=K_VAL,['j']=K_VAL,['k']=K_VAL,['l']=K_VAL,['m']=K_VAL,['n']=K_VAL,['o']=K_VAL,['p']=K_VAL, + ['q']=K_VAL,['r']=K_VAL,['s']=K_VAL,['t']=K_VAL,['u']=K_VAL,['v']=K_VAL,['w']=K_VAL,['x']=K_VAL, + ['y']=K_VAL,['z']=K_VAL, + ['0']=K_VAL,['1']=K_VAL,['2']=K_VAL,['3']=K_VAL,['4']=K_VAL,['5']=K_VAL,['6']=K_VAL,['7']=K_VAL, + ['8']=K_VAL,['9']=K_VAL, + ['+']=K_VAL,['/']=K_VAL, +}; + +static const char b64_flags_url[256] = { + [' ']=K_WS, ['\t']=K_WS, ['\n']=K_WS, ['\f']=K_WS, ['\r']=K_WS, + ['A']=K_VAL,['B']=K_VAL,['C']=K_VAL,['D']=K_VAL,['E']=K_VAL,['F']=K_VAL,['G']=K_VAL,['H']=K_VAL, + ['I']=K_VAL,['J']=K_VAL,['K']=K_VAL,['L']=K_VAL,['M']=K_VAL,['N']=K_VAL,['O']=K_VAL,['P']=K_VAL, + ['Q']=K_VAL,['R']=K_VAL,['S']=K_VAL,['T']=K_VAL,['U']=K_VAL,['V']=K_VAL,['W']=K_VAL,['X']=K_VAL, + ['Y']=K_VAL,['Z']=K_VAL, + ['a']=K_VAL,['b']=K_VAL,['c']=K_VAL,['d']=K_VAL,['e']=K_VAL,['f']=K_VAL,['g']=K_VAL,['h']=K_VAL, + ['i']=K_VAL,['j']=K_VAL,['k']=K_VAL,['l']=K_VAL,['m']=K_VAL,['n']=K_VAL,['o']=K_VAL,['p']=K_VAL, + ['q']=K_VAL,['r']=K_VAL,['s']=K_VAL,['t']=K_VAL,['u']=K_VAL,['v']=K_VAL,['w']=K_VAL,['x']=K_VAL, + ['y']=K_VAL,['z']=K_VAL, + ['0']=K_VAL,['1']=K_VAL,['2']=K_VAL,['3']=K_VAL,['4']=K_VAL,['5']=K_VAL,['6']=K_VAL,['7']=K_VAL, + ['8']=K_VAL,['9']=K_VAL, + ['-']=K_VAL,['_']=K_VAL, +}; + +static size_t b64_encode(const uint8_t *src, size_t len, char *dst, + const unsigned char *alpha) +{ + size_t i = 0, j = 0; + size_t main_len = (len / 3) * 3; + + for (; i < main_len; i += 3, j += 4) { + uint32_t v = 65536*src[i] + 256*src[i + 1] + src[i + 2]; + dst[j + 0] = alpha[(v >> 18) & 63]; + dst[j + 1] = alpha[(v >> 12) & 63]; + dst[j + 2] = alpha[(v >> 6) & 63]; + dst[j + 3] = alpha[v & 63]; + } + + size_t rem = len - i; + if (rem == 1) { + uint32_t v = 65536*src[i]; + dst[j++] = alpha[(v >> 18) & 63]; + dst[j++] = alpha[(v >> 12) & 63]; + dst[j++] = '='; + dst[j++] = '='; + } else if (rem == 2) { + uint32_t v = 65536*src[i] + 256*src[i + 1]; + dst[j++] = alpha[(v >> 18) & 63]; + dst[j++] = alpha[(v >> 12) & 63]; + dst[j++] = alpha[(v >> 6) & 63]; + dst[j++] = '='; + } + return j; +} + +/* Implements https://infra.spec.whatwg.org/#forgiving-base64-decode */ +static size_t +b64_decode(const char *src, size_t len, uint8_t *dst, int *err) +{ + size_t i, j; + uint32_t acc; + int seen, pad; + unsigned ch; + + acc = 0; + seen = 0; + for (i = 0, j = 0; i < len; i++) { + ch = (unsigned char)src[i]; + if ((b64_flags[ch] & K_WS)) + continue; + if (!(b64_flags[ch] & K_VAL)) + break; + acc = (acc << 6) | b64_val[ch]; + seen++; + if (seen == 4) { + dst[j++] = (acc >> 16) & 0xFF; + dst[j++] = (acc >> 8) & 0xFF; + dst[j++] = acc & 0xFF; + seen = 0; + acc = 0; + } + } + + if (seen != 0) { + if (seen == 3) { + dst[j++] = (acc >> 10) & 0xFF; + dst[j++] = (acc >> 2) & 0xFF; + } else if (seen == 2) { + dst[j++] = (acc >> 4) & 0xFF; + } else { + *err = 1; + return 0; + } + for (pad = 0; i < len; i++) { + ch = (unsigned char)src[i]; + if (pad < 2 && ch == '=') + pad++; + else if (!(b64_flags[ch] & K_WS)) + break; + } + if (pad != 0 && seen + pad != 4) { + *err = 1; + return 0; + } + } + + *err = i < len; + return j; +} + +static JSValue js_btoa(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + const uint8_t *in8; + uint8_t *tmp = NULL; + uint8_t *outp; + JSValue val, ret = JS_EXCEPTION; + JSString *s, *ostr; + size_t len, out_len, written; + + val = JS_ToString(ctx, argv[0]); + if (unlikely(JS_IsException(val))) + return JS_EXCEPTION; + + s = JS_VALUE_GET_STRING(val); + len = (size_t)s->len; + + if (likely(!s->is_wide_char)) { + in8 = (const uint8_t *)str8(s); + } else { + const uint16_t *src = str16(s); + tmp = js_malloc(ctx, likely(len) ? len : 1); + if (unlikely(!tmp)) + goto fail; + for (size_t i = 0; i < len; i++) { + uint32_t c = src[i]; + if (unlikely(c > 0xFF)) { + JS_ThrowDOMException(ctx, "InvalidCharacterError", + "String contains an invalid character"); + goto fail; + } + tmp[i] = (uint8_t)c; + } + in8 = tmp; + } + + if (unlikely(len > (SIZE_MAX - 2) / 3)) { + JS_ThrowRangeError(ctx, "input too large"); + goto fail; + } + out_len = 4 * ((len + 2) / 3); + if (unlikely(out_len > JS_STRING_LEN_MAX)) { + JS_ThrowRangeError(ctx, "output too large"); + goto fail; + } + + ostr = js_alloc_string(ctx, out_len, 0); + if (unlikely(!ostr)) + goto fail; + + outp = str8(ostr); + written = b64_encode(in8, len, (char *)outp, b64_enc); + outp[written] = '\0'; + ostr->len = out_len; + ret = JS_MKPTR(JS_TAG_STRING, ostr); +fail: + if (tmp) + js_free(ctx, tmp); + JS_FreeValue(ctx, val); + return ret; +} + +static JSValue js_atob(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + const uint8_t *in; + uint8_t *tmp = NULL, *outp; + JSValue val, ret = JS_EXCEPTION; + JSString *s, *ostr; + size_t slen, out_cap, out_len; + int err; + + val = JS_ToString(ctx, argv[0]); + if (unlikely(JS_IsException(val))) + return JS_EXCEPTION; + + s = JS_VALUE_GET_STRING(val); + slen = (size_t)s->len; + + if (likely(!s->is_wide_char)) { + const uint8_t *p = (const uint8_t *)str8(s); + for (size_t i = 0; i < slen; i++) { + if (unlikely(p[i] & 0x80)) { + JS_ThrowDOMException(ctx, "InvalidCharacterError", + "The string to be decoded is not correctly encoded"); + goto fail; + } + } + in = p; + } else { + const uint16_t *src = str16(s); + tmp = js_malloc(ctx, likely(slen) ? slen : 1); + if (unlikely(!tmp)) + goto fail; + for (size_t i = 0; i < slen; i++) { + if (unlikely(src[i] > 0x7F)) { + JS_ThrowDOMException(ctx, "InvalidCharacterError", + "The string to be decoded is not correctly encoded"); + goto fail; + } + tmp[i] = (uint8_t)src[i]; + } + in = tmp; + } + + if (unlikely(slen > (SIZE_MAX / 3) * 4)) { + JS_ThrowRangeError(ctx, "input too large"); + goto fail; + } + out_cap = (slen / 4) * 3 + 3; + if (unlikely(out_cap > JS_STRING_LEN_MAX)) { + JS_ThrowRangeError(ctx, "output too large"); + goto fail; + } + + ostr = js_alloc_string(ctx, out_cap, 0); + if (unlikely(!ostr)) + goto fail; + + outp = str8(ostr); + err = 0; + out_len = b64_decode((const char *)in, slen, outp, &err); + + if (unlikely(err)) { + js_free_string(ctx->rt, ostr); + JS_ThrowDOMException(ctx, "InvalidCharacterError", + "The string to be decoded is not correctly encoded"); + goto fail; + } + outp[out_len] = '\0'; + ostr->len = out_len; + ret = JS_MKPTR(JS_TAG_STRING, ostr); +fail: + if (tmp) + js_free(ctx, tmp); + JS_FreeValue(ctx, val); + return ret; +} + +static const JSCFunctionListEntry js_base64_funcs[] = { + JS_CFUNC_DEF("btoa", 1, js_btoa), + JS_CFUNC_DEF("atob", 1, js_atob), +}; + + +/* Uint8Array base64/hex (tc39 proposal-arraybuffer-base64) */ + +enum { + B64_ALPHABET_BASE64 = 0, + B64_ALPHABET_BASE64URL = 1, +}; + +enum { + B64_LAST_LOOSE = 0, + B64_LAST_STRICT = 1, + B64_LAST_STOP_BEFORE_PARTIAL = 2, +}; + + +static size_t b64_skip_ws(const char *src, size_t len, size_t index, + const char *flags) +{ + while (index < len && (flags[(unsigned char)src[index]] & K_WS)) + index++; + return index; +} + +/* Implements the FromBase64 abstract operation. + src/src_len: the input string (must be ASCII/latin1) + dst/max_len: output buffer + flags: b64_flags or b64_flags_url (selects valid characters) + last_chunk: B64_LAST_LOOSE, B64_LAST_STRICT, or B64_LAST_STOP_BEFORE_PARTIAL + *p_read: set to number of input characters consumed + *p_err: set to 1 on error, 0 on success + Returns: number of bytes written to dst */ +static size_t from_base64(const char *src, size_t src_len, + uint8_t *dst, size_t max_len, + const char *flags, int last_chunk, + size_t *p_read, int *p_err) +{ + size_t read = 0, written = 0; + uint32_t acc = 0; + int seen = 0; + size_t index = 0; + + *p_err = 0; + + if (max_len == 0) { + *p_read = 0; + return 0; + } + + /* Fast path: decode complete groups of 4 valid characters. + Breaks out on whitespace, padding, invalid chars, or capacity. */ + while (index + 4 <= src_len && written + 3 <= max_len) { + uint8_t f = flags[(unsigned char)src[index]] + & flags[(unsigned char)src[index + 1]] + & flags[(unsigned char)src[index + 2]] + & flags[(unsigned char)src[index + 3]]; + if (!(f & K_VAL)) + break; + uint32_t v = ((uint32_t)b64_val[(unsigned char)src[index]] << 18) + | ((uint32_t)b64_val[(unsigned char)src[index + 1]] << 12) + | ((uint32_t)b64_val[(unsigned char)src[index + 2]] << 6) + | (uint32_t)b64_val[(unsigned char)src[index + 3]]; + dst[written] = (uint8_t)(v >> 16); + dst[written + 1] = (uint8_t)(v >> 8); + dst[written + 2] = (uint8_t)(v); + written += 3; + index += 4; + } + read = index; + + if (written >= max_len) { + *p_read = read; + return written; + } + + /* Slow path: handle whitespace, padding, partial groups, capacity. */ + for (;;) { + index = b64_skip_ws(src, src_len, index, flags); + + if (index == src_len) { + if (seen > 0) { + if (last_chunk == B64_LAST_STOP_BEFORE_PARTIAL) { + *p_read = read; + return written; + } + if (last_chunk == B64_LAST_STRICT) { + *p_err = 1; + return 0; + } + /* loose */ + if (seen == 1) { + *p_err = 1; + return 0; + } + goto decode_partial; + } + *p_read = src_len; + return written; + } + + unsigned char ch = src[index++]; + + if (ch == '=') { + if (seen < 2) { + *p_err = 1; + return 0; + } + index = b64_skip_ws(src, src_len, index, flags); + if (seen == 2) { + if (index == src_len) { + if (last_chunk == B64_LAST_STOP_BEFORE_PARTIAL) { + *p_read = read; + return written; + } + *p_err = 1; + return 0; + } + if (src[index] == '=') { + index++; + index = b64_skip_ws(src, src_len, index, flags); + } else { + *p_err = 1; + return 0; + } + } + /* After padding, only whitespace is allowed */ + if (index != src_len) { + *p_err = 1; + return 0; + } + if (last_chunk == B64_LAST_STRICT) { + uint32_t mask = (seen == 2) ? 0xF : 0x3; + if (acc & mask) { + *p_err = 1; + return 0; + } + } + goto decode_partial; + } + + if (!(flags[ch] & K_VAL)) { + *p_err = 1; + return 0; + } + + /* Check remaining capacity before committing to this group */ + { + size_t remaining = max_len - written; + if ((remaining == 1 && seen == 2) || + (remaining == 2 && seen == 3)) { + *p_read = read; + return written; + } + } + + acc = (acc << 6) | b64_val[ch]; + seen++; + + if (seen == 4) { + dst[written] = (uint8_t)(acc >> 16); + dst[written + 1] = (uint8_t)(acc >> 8); + dst[written + 2] = (uint8_t)(acc); + written += 3; + acc = 0; + seen = 0; + read = index; + if (written >= max_len) { + *p_read = read; + return written; + } + } + } + +decode_partial: + if (seen == 2) { + dst[written++] = (uint8_t)(acc >> 4); + } else if (seen == 3) { + dst[written] = (uint8_t)(acc >> 10); + dst[written + 1] = (uint8_t)(acc >> 2); + written += 2; + } + *p_read = src_len; + return written; +} + +/* Hex helpers */ +static const char u8a_hex_digits[] = "0123456789abcdef"; + +static int u8a_hex_nibble(unsigned char ch) +{ + if (ch >= '0' && ch <= '9') return ch - '0'; + if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; + if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; + return -1; +} + +static size_t u8a_hex_encode(const uint8_t *src, size_t len, char *dst) +{ + for (size_t i = 0; i < len; i++) { + dst[i * 2] = u8a_hex_digits[src[i] >> 4]; + dst[i * 2 + 1] = u8a_hex_digits[src[i] & 0xF]; + } + return len * 2; +} + +/* Decode hex string to bytes. + Returns bytes written. Sets *p_read to chars consumed, *p_err on error. */ +static size_t u8a_hex_decode(const char *src, size_t src_len, + uint8_t *dst, size_t max_len, + size_t *p_read, int *p_err) +{ + size_t written = 0, i = 0; + *p_err = 0; + + if (src_len & 1) { + *p_err = 1; + return 0; + } + + while (i < src_len && written < max_len) { + int hi = u8a_hex_nibble(src[i]); + int lo = u8a_hex_nibble(src[i + 1]); + if (hi < 0 || lo < 0) { + *p_err = 1; + return 0; + } + dst[written++] = (uint8_t)((hi << 4) | lo); + i += 2; + } + + *p_read = i; + return written; +} + +/* Validate that this_val is a Uint8Array (type check only, no detach check). + Returns the JSObject pointer or NULL on error (throws). */ +static JSObject *check_uint8array(JSContext *ctx, JSValueConst this_val) +{ + JSObject *p; + + if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT) + goto fail; + p = JS_VALUE_GET_OBJ(this_val); + if (p->class_id != JS_CLASS_UINT8_ARRAY) + goto fail; + return p; +fail: + JS_ThrowTypeError(ctx, "not a Uint8Array"); + return NULL; +} + +/* Get the data pointer and length of a Uint8Array, checking for detached + buffers. Must be called after options are read (per spec ordering). + Returns 0 on success, -1 on error (throws). */ +static int get_uint8array_bytes(JSContext *ctx, JSObject *p, + uint8_t **pdata, size_t *plen) +{ + if (typed_array_is_oob(p)) { + JS_ThrowTypeErrorArrayBufferOOB(ctx); + return -1; + } + *pdata = p->u.array.u.uint8_ptr; + *plen = p->u.array.count; + return 0; +} + +/* Validate options is undefined or an object (GetOptionsObject). + Returns 0 on success, -1 on error (throws). */ +static int check_options_object(JSContext *ctx, JSValueConst options) +{ + if (JS_IsUndefined(options)) + return 0; + if (!JS_IsObject(options)) { + JS_ThrowTypeError(ctx, "options must be an object"); + return -1; + } + return 0; +} + +/* Parse the 'alphabet' option from an options object. + Returns B64_ALPHABET_BASE64 or B64_ALPHABET_BASE64URL, or -1 on error. */ +static int parse_alphabet_option(JSContext *ctx, JSValueConst options) +{ + JSValue val; + const char *str; + int ret; + + if (JS_IsUndefined(options)) + return B64_ALPHABET_BASE64; + + val = JS_GetPropertyStr(ctx, options, "alphabet"); + if (JS_IsException(val)) + return -1; + if (JS_IsUndefined(val)) + return B64_ALPHABET_BASE64; + if (!JS_IsString(val)) { + JS_FreeValue(ctx, val); + JS_ThrowTypeError(ctx, "expected string for alphabet"); + return -1; + } + + str = JS_ToCString(ctx, val); + JS_FreeValue(ctx, val); + if (!str) + return -1; + + if (!strcmp(str, "base64")) + ret = B64_ALPHABET_BASE64; + else if (!strcmp(str, "base64url")) + ret = B64_ALPHABET_BASE64URL; + else { + JS_ThrowTypeError(ctx, "invalid alphabet"); + ret = -1; + } + JS_FreeCString(ctx, str); + return ret; +} + +/* Parse the 'lastChunkHandling' option. Returns mode or -1 on error. */ +static int parse_last_chunk_option(JSContext *ctx, JSValueConst options) +{ + JSValue val; + const char *str; + int ret; + + if (JS_IsUndefined(options)) + return B64_LAST_LOOSE; + + val = JS_GetPropertyStr(ctx, options, "lastChunkHandling"); + if (JS_IsException(val)) + return -1; + if (JS_IsUndefined(val)) + return B64_LAST_LOOSE; + if (!JS_IsString(val)) { + JS_FreeValue(ctx, val); + JS_ThrowTypeError(ctx, "expected string for lastChunkHandling"); + return -1; + } + + str = JS_ToCString(ctx, val); + JS_FreeValue(ctx, val); + if (!str) + return -1; + + if (!strcmp(str, "loose")) + ret = B64_LAST_LOOSE; + else if (!strcmp(str, "strict")) + ret = B64_LAST_STRICT; + else if (!strcmp(str, "stop-before-partial")) + ret = B64_LAST_STOP_BEFORE_PARTIAL; + else { + JS_ThrowTypeError(ctx, "invalid lastChunkHandling option"); + ret = -1; + } + JS_FreeCString(ctx, str); + return ret; +} + +/* Uint8Array.prototype.toBase64([options]) */ +static JSValue js_uint8array_to_base64(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + uint8_t *data; + size_t len; + JSValue options; + JSObject *p; + int alphabet, omit_padding; + size_t out_len, written; + JSString *ostr; + char *dst; + + p = check_uint8array(ctx, this_val); + if (!p) + return JS_EXCEPTION; + + options = argc > 0 ? unsafe_unconst(argv[0]) : JS_UNDEFINED; + if (check_options_object(ctx, options)) + return JS_EXCEPTION; + alphabet = parse_alphabet_option(ctx, options); + if (alphabet < 0) + return JS_EXCEPTION; + + omit_padding = 0; + if (!JS_IsUndefined(options)) { + JSValue op_val = JS_GetPropertyStr(ctx, options, "omitPadding"); + if (JS_IsException(op_val)) + return JS_EXCEPTION; + omit_padding = JS_ToBool(ctx, op_val); + JS_FreeValue(ctx, op_val); + } + + if (get_uint8array_bytes(ctx, p, &data, &len)) + return JS_EXCEPTION; + + out_len = 4 * ((len + 2) / 3); + + if (unlikely(out_len > JS_STRING_LEN_MAX)) + return JS_ThrowRangeError(ctx, "output too large"); + + ostr = js_alloc_string(ctx, out_len, 0); + if (!ostr) + return JS_EXCEPTION; + + dst = (char *)str8(ostr); + written = b64_encode(data, len, dst, + alphabet == B64_ALPHABET_BASE64URL ? b64url_enc : b64_enc); + if (omit_padding) { + while (written > 0 && dst[written - 1] == '=') + written--; + } + dst[written] = '\0'; + + ostr->len = written; + return JS_MKPTR(JS_TAG_STRING, ostr); +} + +/* Uint8Array.prototype.toHex() */ +static JSValue js_uint8array_to_hex(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + uint8_t *data; + size_t len, out_len; + JSObject *p; + JSString *ostr; + + p = check_uint8array(ctx, this_val); + if (!p) + return JS_EXCEPTION; + if (get_uint8array_bytes(ctx, p, &data, &len)) + return JS_EXCEPTION; + + out_len = len * 2; + if (unlikely(out_len > JS_STRING_LEN_MAX)) + return JS_ThrowRangeError(ctx, "output too large"); + + ostr = js_alloc_string(ctx, out_len, 0); + if (!ostr) + return JS_EXCEPTION; + + u8a_hex_encode(data, len, (char *)str8(ostr)); + str8(ostr)[out_len] = '\0'; + return JS_MKPTR(JS_TAG_STRING, ostr); +} + +/* Uint8Array.fromBase64(string[, options]) */ +static JSValue js_uint8array_from_base64(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + const char *str; + size_t str_len, read_pos, decoded_len, out_cap; + int alphabet, last_chunk, err; + uint8_t *buf; + JSValue result, options; + + if (!JS_IsString(argv[0])) + return JS_ThrowTypeError(ctx, "expected string"); + + str = JS_ToCStringLen(ctx, &str_len, argv[0]); + if (!str) + return JS_EXCEPTION; + + options = argc > 1 ? unsafe_unconst(argv[1]) : JS_UNDEFINED; + if (check_options_object(ctx, options)) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + alphabet = parse_alphabet_option(ctx, options); + if (alphabet < 0) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + last_chunk = parse_last_chunk_option(ctx, options); + if (last_chunk < 0) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + out_cap = (str_len / 4) * 3 + 3; + buf = js_malloc(ctx, out_cap ? out_cap : 1); + if (!buf) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + decoded_len = from_base64(str, str_len, buf, out_cap, + alphabet == B64_ALPHABET_BASE64URL + ? b64_flags_url : b64_flags, + last_chunk, &read_pos, &err); + JS_FreeCString(ctx, str); + + if (err) { + js_free(ctx, buf); + return JS_ThrowSyntaxError(ctx, "invalid base64 string"); + } + + result = JS_NewUint8ArrayCopy(ctx, buf, decoded_len); + js_free(ctx, buf); + return result; +} + +/* Uint8Array.fromHex(string) */ +static JSValue js_uint8array_from_hex(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + const char *str; + size_t str_len, read_pos, decoded_len, out_cap; + int err; + uint8_t *buf; + JSValue result; + + if (!JS_IsString(argv[0])) + return JS_ThrowTypeError(ctx, "expected string"); + + str = JS_ToCStringLen(ctx, &str_len, argv[0]); + if (!str) + return JS_EXCEPTION; + + out_cap = str_len / 2 + 1; + buf = js_malloc(ctx, out_cap ? out_cap : 1); + if (!buf) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + decoded_len = u8a_hex_decode(str, str_len, buf, out_cap, &read_pos, &err); + JS_FreeCString(ctx, str); + + if (err) { + js_free(ctx, buf); + return JS_ThrowSyntaxError(ctx, "invalid hex string"); + } + + result = JS_NewUint8ArrayCopy(ctx, buf, decoded_len); + js_free(ctx, buf); + return result; +} + +/* Return a { read, written } result object */ +static JSValue js_make_read_written(JSContext *ctx, size_t read, size_t written) +{ + JSValue obj = JS_NewObject(ctx); + if (JS_IsException(obj)) + return JS_EXCEPTION; + if (JS_DefinePropertyValueStr(ctx, obj, "read", + js_uint32(read), JS_PROP_C_W_E) < 0) + goto fail; + if (JS_DefinePropertyValueStr(ctx, obj, "written", + js_uint32(written), JS_PROP_C_W_E) < 0) + goto fail; + return obj; +fail: + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; +} + +/* Uint8Array.prototype.setFromBase64(string[, options]) */ +static JSValue js_uint8array_set_from_base64(JSContext *ctx, + JSValueConst this_val, + int argc, JSValueConst *argv) +{ + uint8_t *data; + size_t len; + const char *str; + size_t str_len, read_pos, decoded_len; + JSObject *p; + int alphabet, last_chunk, err; + JSValue options; + + p = check_uint8array(ctx, this_val); + if (!p) + return JS_EXCEPTION; + + if (typed_array_is_immutable(p)) + return JS_ThrowTypeErrorImmutableArrayBuffer(ctx); + + if (!JS_IsString(argv[0])) + return JS_ThrowTypeError(ctx, "expected string"); + + str = JS_ToCStringLen(ctx, &str_len, argv[0]); + if (!str) + return JS_EXCEPTION; + + options = argc > 1 ? unsafe_unconst(argv[1]) : JS_UNDEFINED; + if (check_options_object(ctx, options)) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + alphabet = parse_alphabet_option(ctx, options); + if (alphabet < 0) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + last_chunk = parse_last_chunk_option(ctx, options); + if (last_chunk < 0) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + if (get_uint8array_bytes(ctx, p, &data, &len)) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + decoded_len = from_base64(str, str_len, data, len, + alphabet == B64_ALPHABET_BASE64URL + ? b64_flags_url : b64_flags, + last_chunk, &read_pos, &err); + JS_FreeCString(ctx, str); + + if (err) + return JS_ThrowSyntaxError(ctx, "invalid base64 string"); + + return js_make_read_written(ctx, read_pos, decoded_len); +} + +/* Uint8Array.prototype.setFromHex(string) */ +static JSValue js_uint8array_set_from_hex(JSContext *ctx, + JSValueConst this_val, + int argc, JSValueConst *argv) +{ + uint8_t *data; + size_t len; + const char *str; + size_t str_len, read_pos, decoded_len; + JSObject *p; + int err; + + p = check_uint8array(ctx, this_val); + if (!p) + return JS_EXCEPTION; + + if (typed_array_is_immutable(p)) + return JS_ThrowTypeErrorImmutableArrayBuffer(ctx); + + if (!JS_IsString(argv[0])) + return JS_ThrowTypeError(ctx, "expected string"); + + str = JS_ToCStringLen(ctx, &str_len, argv[0]); + if (!str) + return JS_EXCEPTION; + + if (get_uint8array_bytes(ctx, p, &data, &len)) { + JS_FreeCString(ctx, str); + return JS_EXCEPTION; + } + + decoded_len = u8a_hex_decode(str, str_len, data, len, &read_pos, &err); + JS_FreeCString(ctx, str); + + if (err) + return JS_ThrowSyntaxError(ctx, "invalid hex string"); + + return js_make_read_written(ctx, read_pos, decoded_len); +} + +static const JSCFunctionListEntry js_uint8array_proto_funcs[] = { + JS_CFUNC_DEF("toBase64", 0, js_uint8array_to_base64), + JS_CFUNC_DEF("toHex", 0, js_uint8array_to_hex), + JS_CFUNC_DEF("setFromBase64", 1, js_uint8array_set_from_base64), + JS_CFUNC_DEF("setFromHex", 1, js_uint8array_set_from_hex), +}; + +static const JSCFunctionListEntry js_uint8array_funcs[] = { + JS_CFUNC_DEF("fromBase64", 1, js_uint8array_from_base64), + JS_CFUNC_DEF("fromHex", 1, js_uint8array_from_hex), +}; + +static int js_uint8array_funcs_init(JSContext *ctx) +{ + JSValue ctor, proto; + + ctor = JS_GetProperty(ctx, ctx->global_obj, JS_ATOM_Uint8Array); + if (JS_IsException(ctor)) + return -1; + proto = JS_GetProperty(ctx, ctor, JS_ATOM_prototype); + if (JS_IsException(proto)) { + JS_FreeValue(ctx, ctor); + return -1; + } + JS_SetPropertyFunctionList(ctx, proto, + js_uint8array_proto_funcs, + countof(js_uint8array_proto_funcs)); + JS_FreeValue(ctx, proto); + JS_SetPropertyFunctionList(ctx, ctor, + js_uint8array_funcs, + countof(js_uint8array_funcs)); + JS_FreeValue(ctx, ctor); + return 0; +} + +int JS_AddIntrinsicAToB(JSContext *ctx) +{ + if (!JS_IsRegisteredClass(ctx->rt, JS_CLASS_DOM_EXCEPTION)) { + if (JS_AddIntrinsicDOMException(ctx)) + return -1; + } + JS_SetPropertyFunctionList(ctx, ctx->global_obj, + js_base64_funcs, countof(js_base64_funcs)); return 0; } @@ -72642,6 +77420,18 @@ uintptr_t js_std_cmd(int cmd, ...) { if (JS_IsString(*pv)) rv = JS_VALUE_GET_STRING(*pv)->kind; break; + case 4: // GetShapeHashCount + rt = va_arg(ap, JSRuntime *); + rv = rt->shape_hash_count; + break; + case 5: // GetInterruptHandler + rt = va_arg(ap, JSRuntime *); + rv = (uintptr_t)rt->interrupt_handler; + break; + case 6: // GetInterruptOpaque + rt = va_arg(ap, JSRuntime *); + rv = (uintptr_t)rt->interrupt_opaque; + break; default: rv = -1; } @@ -74305,13 +79095,24 @@ double js_atod(const char *str, const char **pnext, int radix, int flags, -#if defined(__sun) -#include -#endif + +/* ASCII identifier tables, used by lre_js_is_ident_first/next in libregexp.h + and by quickjs.c. */ +uint32_t const lre_id_start_table_ascii[4] = { + /* $ A-Z _ a-z */ + 0x00000000, 0x00000010, 0x87FFFFFE, 0x07FFFFFE +}; + +uint32_t const lre_id_continue_table_ascii[4] = { + /* $ 0-9 A-Z _ a-z */ + 0x00000000, 0x03FF0010, 0x87FFFFFE, 0x07FFFFFE +}; /* TODO: - + - remove REOP_char_i and REOP_range_i by precomputing the case folding. + - add specific opcodes for simple unicode property tests so that the + generated bytecode is smaller. - Add a lock step execution mode (=linear time execution guaranteed) when the regular expression is "simple" i.e. no backreference nor complicated lookahead. The opcodes are designed for this execution @@ -74321,6 +79122,8 @@ double js_atod(const char *str, const char **pnext, int radix, int flags, #if defined(TEST) #define DUMP_REOP #endif +//#define DUMP_REOP +//#define DUMP_EXEC typedef enum { #define DEF(id, size) REOP_ ## id, @@ -74351,35 +79154,50 @@ typedef enum { #ifdef DEF DEF(invalid, 1) /* never used */ -DEF(char8, 2) /* 7 bits in fact */ -DEF(char16, 3) +DEF(char, 3) +DEF(char_i, 3) DEF(char32, 5) +DEF(char32_i, 5) DEF(dot, 1) DEF(any, 1) /* same as dot but match any character including line terminator */ +DEF(space, 1) +DEF(not_space, 1) /* must come after */ DEF(line_start, 1) +DEF(line_start_m, 1) DEF(line_end, 1) +DEF(line_end_m, 1) DEF(goto, 5) DEF(split_goto_first, 5) DEF(split_next_first, 5) DEF(match, 1) +DEF(lookahead_match, 1) +DEF(negative_lookahead_match, 1) /* must come after */ DEF(save_start, 2) /* save start position */ DEF(save_end, 2) /* save end position, must come after saved_start */ DEF(save_reset, 3) /* reset save positions */ -DEF(loop, 5) /* decrement the top the stack and goto if != 0 */ -DEF(push_i32, 5) /* push integer on the stack */ -DEF(drop, 1) +DEF(loop, 6) /* decrement the top the stack and goto if != 0 */ +DEF(loop_split_goto_first, 10) /* loop and then split */ +DEF(loop_split_next_first, 10) +DEF(loop_check_adv_split_goto_first, 10) /* loop and then check advance and split */ +DEF(loop_check_adv_split_next_first, 10) +DEF(set_i32, 6) /* store the immediate value to a register */ DEF(word_boundary, 1) +DEF(word_boundary_i, 1) DEF(not_word_boundary, 1) -DEF(back_reference, 2) -DEF(backward_back_reference, 2) /* must come after back_reference */ +DEF(not_word_boundary_i, 1) +DEF(back_reference, 2) /* variable length */ +DEF(back_reference_i, 2) /* must come after */ +DEF(backward_back_reference, 2) /* must come after */ +DEF(backward_back_reference_i, 2) /* must come after */ DEF(range, 3) /* variable length */ +DEF(range_i, 3) /* variable length */ DEF(range32, 3) /* variable length */ +DEF(range32_i, 3) /* variable length */ DEF(lookahead, 5) -DEF(negative_lookahead, 5) -DEF(push_char_pos, 1) /* push the character position on the stack */ -DEF(check_advance, 1) /* pop one stack element and check that it is different from the character position */ +DEF(negative_lookahead, 5) /* must come after */ +DEF(set_char_pos, 2) /* store the character position to a register */ +DEF(check_advance, 2) /* check that the register is different from the character position */ DEF(prev, 1) /* go to the previous char */ -DEF(simple_greedy_quant, 17) #endif /* DEF */ @@ -74388,7 +79206,8 @@ DEF(simple_greedy_quant, 17) } REOPCodeEnum; #define CAPTURE_COUNT_MAX 255 -#define STACK_SIZE_MAX 255 +#define REGISTER_COUNT_MAX 255 +#define GROUP_NAME_SCOPE_MAX 255 /* must be large enough to have a negligible runtime cost and small enough to call the interrupt callback often. */ #define INTERRUPT_COUNTER_INIT 10000 @@ -74399,7 +79218,6 @@ DEF(simple_greedy_quant, 17) #define TMP_BUF_SIZE 128 -// invariant: is_unicode ^ unicode_sets (or neither, but not both) typedef struct { DynBuf byte_code; const uint8_t *buf_ptr; @@ -74407,9 +79225,11 @@ typedef struct { const uint8_t *buf_start; int re_flags; bool is_unicode; - bool unicode_sets; + bool unicode_sets; /* if set, is_unicode is also set */ bool ignore_case; + bool multi_line; bool dotall; + uint8_t group_name_scope; int capture_count; int total_capture_count; /* -1 = not computed yet */ int has_named_captures; /* -1 = don't know, 0 = no, 1 = yes */ @@ -74461,45 +79281,60 @@ static const REOpCode reopcode_info[REOP_COUNT] = { #ifdef DEF DEF(invalid, 1) /* never used */ -DEF(char8, 2) /* 7 bits in fact */ -DEF(char16, 3) +DEF(char, 3) +DEF(char_i, 3) DEF(char32, 5) +DEF(char32_i, 5) DEF(dot, 1) DEF(any, 1) /* same as dot but match any character including line terminator */ +DEF(space, 1) +DEF(not_space, 1) /* must come after */ DEF(line_start, 1) +DEF(line_start_m, 1) DEF(line_end, 1) +DEF(line_end_m, 1) DEF(goto, 5) DEF(split_goto_first, 5) DEF(split_next_first, 5) DEF(match, 1) +DEF(lookahead_match, 1) +DEF(negative_lookahead_match, 1) /* must come after */ DEF(save_start, 2) /* save start position */ DEF(save_end, 2) /* save end position, must come after saved_start */ DEF(save_reset, 3) /* reset save positions */ -DEF(loop, 5) /* decrement the top the stack and goto if != 0 */ -DEF(push_i32, 5) /* push integer on the stack */ -DEF(drop, 1) +DEF(loop, 6) /* decrement the top the stack and goto if != 0 */ +DEF(loop_split_goto_first, 10) /* loop and then split */ +DEF(loop_split_next_first, 10) +DEF(loop_check_adv_split_goto_first, 10) /* loop and then check advance and split */ +DEF(loop_check_adv_split_next_first, 10) +DEF(set_i32, 6) /* store the immediate value to a register */ DEF(word_boundary, 1) +DEF(word_boundary_i, 1) DEF(not_word_boundary, 1) -DEF(back_reference, 2) -DEF(backward_back_reference, 2) /* must come after back_reference */ +DEF(not_word_boundary_i, 1) +DEF(back_reference, 2) /* variable length */ +DEF(back_reference_i, 2) /* must come after */ +DEF(backward_back_reference, 2) /* must come after */ +DEF(backward_back_reference_i, 2) /* must come after */ DEF(range, 3) /* variable length */ +DEF(range_i, 3) /* variable length */ DEF(range32, 3) /* variable length */ +DEF(range32_i, 3) /* variable length */ DEF(lookahead, 5) -DEF(negative_lookahead, 5) -DEF(push_char_pos, 1) /* push the character position on the stack */ -DEF(check_advance, 1) /* pop one stack element and check that it is different from the character position */ +DEF(negative_lookahead, 5) /* must come after */ +DEF(set_char_pos, 2) /* store the character position to a register */ +DEF(check_advance, 2) /* check that the register is different from the character position */ DEF(prev, 1) /* go to the previous char */ -DEF(simple_greedy_quant, 17) #endif /* DEF */ #undef DEF }; -#define RE_HEADER_FLAGS 0 -#define RE_HEADER_CAPTURE_COUNT 2 -#define RE_HEADER_STACK_SIZE 3 -#define RE_HEADER_BYTECODE_LEN 4 +#define RE_HEADER_FLAGS 0 +#define RE_HEADER_CAPTURE_COUNT 2 +#define RE_HEADER_REGISTER_COUNT 3 +#define RE_HEADER_BYTECODE_LEN 4 #define RE_HEADER_LEN 8 @@ -74517,6 +79352,270 @@ static int dbuf_insert(DynBuf *s, int pos, int len) return 0; } +typedef struct REString { + struct REString *next; + uint32_t hash; + uint32_t len; + uint32_t buf[]; +} REString; + +typedef struct { + /* the string list is the union of 'char_range' and of the strings + in hash_table[]. The strings in hash_table[] have a length != + 1. */ + CharRange cr; + uint32_t n_strings; + uint32_t hash_size; + int hash_bits; + REString **hash_table; +} REStringList; + +static uint32_t re_string_hash(int len, const uint32_t *buf) +{ + int i; + uint32_t h; + h = 1; + for(i = 0; i < len; i++) + h = h * 263 + buf[i]; + return hash32(h); +} + +static void re_string_list_init(REParseState *s1, REStringList *s) +{ + cr_init(&s->cr, s1->opaque, lre_realloc); + s->n_strings = 0; + s->hash_size = 0; + s->hash_bits = 0; + s->hash_table = NULL; +} + +static void re_string_list_free(REStringList *s) +{ + REString *p, *p_next; + int i; + for(i = 0; i < s->hash_size; i++) { + for(p = s->hash_table[i]; p != NULL; p = p_next) { + p_next = p->next; + lre_realloc(s->cr.mem_opaque, p, 0); + } + } + lre_realloc(s->cr.mem_opaque, s->hash_table, 0); + + cr_free(&s->cr); +} + +#ifdef DUMP_REOP +static void lre_print_char(int c, bool is_range) +{ + if (c == '\'' || c == '\\' || + (is_range && (c == '-' || c == ']'))) { + printf("\\%c", c); + } else if (c >= ' ' && c <= 126) { + printf("%c", c); + } else { + printf("\\u{%04x}", c); + } +} + +static __maybe_unused void re_string_list_dump(const char *str, const REStringList *s) +{ + REString *p; + const CharRange *cr; + int i, j, k; + + printf("%s:\n", str); + printf(" ranges: ["); + cr = &s->cr; + for(i = 0; i < cr->len; i += 2) { + lre_print_char(cr->points[i], true); + if (cr->points[i] != cr->points[i + 1] - 1) { + printf("-"); + lre_print_char(cr->points[i + 1] - 1, true); + } + } + printf("]\n"); + + j = 0; + for(i = 0; i < s->hash_size; i++) { + for(p = s->hash_table[i]; p != NULL; p = p->next) { + printf(" %d/%d: '", j, s->n_strings); + for(k = 0; k < p->len; k++) { + lre_print_char(p->buf[k], false); + } + printf("'\n"); + j++; + } + } +} +#endif /* DUMP_REOP */ + +/* 'buf' is NULL if 'len' is zero: the empty string is a valid member of a + string set, e.g. /[\q{}]/v */ +static int re_string_find2(REStringList *s, int len, const uint32_t *buf, + uint32_t h0, bool add_flag) +{ + uint32_t h = 0; /* avoid warning */ + REString *p; + if (s->n_strings != 0) { + h = h0 >> (32 - s->hash_bits); + for(p = s->hash_table[h]; p != NULL; p = p->next) { + if (p->hash == h0 && p->len == len && + (len == 0 || + !memcmp(p->buf, buf, len * sizeof(buf[0])))) { + return 1; + } + } + } + /* not found */ + if (!add_flag) + return 0; + /* increase the size of the hash table if needed */ + if (unlikely((s->n_strings + 1) > s->hash_size)) { + REString **new_hash_table, *p_next; + int new_hash_bits, i; + uint32_t new_hash_size; + new_hash_bits = max_int(s->hash_bits + 1, 4); + new_hash_size = 1 << new_hash_bits; + new_hash_table = lre_realloc(s->cr.mem_opaque, NULL, + sizeof(new_hash_table[0]) * new_hash_size); + if (!new_hash_table) + return -1; + memset(new_hash_table, 0, sizeof(new_hash_table[0]) * new_hash_size); + for(i = 0; i < s->hash_size; i++) { + for(p = s->hash_table[i]; p != NULL; p = p_next) { + p_next = p->next; + h = p->hash >> (32 - new_hash_bits); + p->next = new_hash_table[h]; + new_hash_table[h] = p; + } + } + lre_realloc(s->cr.mem_opaque, s->hash_table, 0); + s->hash_bits = new_hash_bits; + s->hash_size = new_hash_size; + s->hash_table = new_hash_table; + h = h0 >> (32 - s->hash_bits); + } + + p = lre_realloc(s->cr.mem_opaque, NULL, sizeof(REString) + len * sizeof(buf[0])); + if (!p) + return -1; + p->next = s->hash_table[h]; + s->hash_table[h] = p; + s->n_strings++; + p->hash = h0; + p->len = len; + if (len != 0) + memcpy(p->buf, buf, sizeof(buf[0]) * len); + return 1; +} + +static int re_string_find(REStringList *s, int len, const uint32_t *buf, + bool add_flag) +{ + uint32_t h0; + h0 = re_string_hash(len, buf); + return re_string_find2(s, len, buf, h0, add_flag); +} + +/* return -1 if memory error, 0 if OK */ +static int re_string_add(REStringList *s, int len, const uint32_t *buf) +{ + if (len == 1) { + return cr_union_interval(&s->cr, buf[0], buf[0]); + } + if (re_string_find(s, len, buf, true) < 0) + return -1; + return 0; +} + +/* a = a op b */ +static int re_string_list_op(REStringList *a, REStringList *b, int op) +{ + int i, ret; + REString *p, **pp; + + if (cr_op1(&a->cr, b->cr.points, b->cr.len, op)) + return -1; + + switch(op) { + case CR_OP_UNION: + if (b->n_strings != 0) { + for(i = 0; i < b->hash_size; i++) { + for(p = b->hash_table[i]; p != NULL; p = p->next) { + if (re_string_find2(a, p->len, p->buf, p->hash, true) < 0) + return -1; + } + } + } + break; + case CR_OP_INTER: + case CR_OP_SUB: + for(i = 0; i < a->hash_size; i++) { + pp = &a->hash_table[i]; + for(;;) { + p = *pp; + if (p == NULL) + break; + ret = re_string_find2(b, p->len, p->buf, p->hash, false); + if (op == CR_OP_SUB) + ret = !ret; + if (!ret) { + /* remove it */ + *pp = p->next; + a->n_strings--; + lre_realloc(a->cr.mem_opaque, p, 0); + } else { + /* keep it */ + pp = &p->next; + } + } + } + break; + default: + abort(); + } + return 0; +} + +static int re_string_list_canonicalize(REParseState *s1, + REStringList *s, bool is_unicode) +{ + if (cr_regexp_canonicalize(&s->cr, is_unicode)) + return -1; + if (s->n_strings != 0) { + REStringList a_s, *a = &a_s; + int i, j; + REString *p; + + /* XXX: simplify */ + re_string_list_init(s1, a); + + a->n_strings = s->n_strings; + a->hash_size = s->hash_size; + a->hash_bits = s->hash_bits; + a->hash_table = s->hash_table; + + s->n_strings = 0; + s->hash_size = 0; + s->hash_bits = 0; + s->hash_table = NULL; + + for(i = 0; i < a->hash_size; i++) { + for(p = a->hash_table[i]; p != NULL; p = p->next) { + for(j = 0; j < p->len; j++) { + p->buf[j] = lre_canonicalize(p->buf[j], is_unicode); + } + if (re_string_add(s, p->len, p->buf)) { + re_string_list_free(a); + return -1; + } + } + } + re_string_list_free(a); + } + return 0; +} + static const uint16_t char_range_d[] = { 1, 0x0030, 0x0039 + 1, @@ -74540,32 +79639,6 @@ static const uint16_t char_range_s[] = { 0xFEFF, 0xFEFF + 1, }; -bool lre_is_space(int c) -{ - int i, n, low, high; - n = (countof(char_range_s) - 1) / 2; - for(i = 0; i < n; i++) { - low = char_range_s[2 * i + 1]; - if (c < low) - return false; - high = char_range_s[2 * i + 2]; - if (c < high) - return true; - } - return false; -} - -uint32_t const lre_id_start_table_ascii[4] = { - /* $ A-Z _ a-z */ - 0x00000000, 0x00000010, 0x87FFFFFE, 0x07FFFFFE -}; - -uint32_t const lre_id_continue_table_ascii[4] = { - /* $ 0-9 A-Z _ a-z */ - 0x00000000, 0x03FF0010, 0x87FFFFFE, 0x07FFFFFE -}; - - static const uint16_t char_range_w[] = { 4, 0x0030, 0x0039 + 1, @@ -74585,13 +79658,13 @@ typedef enum { CHAR_RANGE_W, } CharRangeEnum; -static const uint16_t *char_range_table[] = { +static const uint16_t * const char_range_table[] = { char_range_d, char_range_s, char_range_w, }; -static int cr_init_char_range(REParseState *s, CharRange *cr, uint32_t c) +static int cr_init_char_range(REParseState *s, REStringList *cr, uint32_t c) { bool invert; const uint16_t *c_pt; @@ -74600,18 +79673,18 @@ static int cr_init_char_range(REParseState *s, CharRange *cr, uint32_t c) invert = c & 1; c_pt = char_range_table[c >> 1]; len = *c_pt++; - cr_init(cr, s->opaque, lre_realloc); + re_string_list_init(s, cr); for(i = 0; i < len * 2; i++) { - if (cr_add_point(cr, c_pt[i])) + if (cr_add_point(&cr->cr, c_pt[i])) goto fail; } if (invert) { - if (cr_invert(cr)) + if (cr_invert(&cr->cr)) goto fail; } return 0; fail: - cr_free(cr); + re_string_list_free(cr); return -1; } @@ -74620,15 +79693,15 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf, int buf_len) { int pos, len, opcode, bc_len, re_flags, i; - uint32_t val; + uint32_t val, val2; assert(buf_len >= RE_HEADER_LEN); re_flags = lre_get_flags(buf); bc_len = get_u32(buf + RE_HEADER_BYTECODE_LEN); assert(bc_len + RE_HEADER_LEN <= buf_len); - printf("flags: 0x%x capture_count=%d stack_size=%d\n", - re_flags, buf[RE_HEADER_CAPTURE_COUNT], buf[RE_HEADER_STACK_SIZE]); + printf("flags: 0x%x capture_count=%d reg_count=%d\n", + re_flags, buf[RE_HEADER_CAPTURE_COUNT], buf[RE_HEADER_REGISTER_COUNT]); if (re_flags & LRE_FLAG_NAMED_GROUPS) { const char *p; p = (char *)buf + RE_HEADER_LEN + bc_len; @@ -74637,7 +79710,7 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf, if (i != 1) printf(","); printf("<%s>", p); - p += strlen(p) + 1; + p += strlen(p) + LRE_GROUP_NAME_TRAILER_LEN; } printf("\n"); assert(p == (char *)(buf + buf_len)); @@ -74660,15 +79733,17 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf, } printf("%s", reopcode_info[opcode].name); switch(opcode) { - case REOP_char8: - val = get_u8(buf + pos + 1); - goto printchar; - case REOP_char16: + case REOP_char: + case REOP_char_i: val = get_u16(buf + pos + 1); - goto printchar; + if (val >= ' ' && val <= 126) + printf(" '%c'", val); + else + printf(" 0x%04x", val); + break; case REOP_char32: + case REOP_char32_i: val = get_u32(buf + pos + 1); - printchar: if (val >= ' ' && val <= 126) printf(" '%c'", val); else @@ -74677,34 +79752,65 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf, case REOP_goto: case REOP_split_goto_first: case REOP_split_next_first: - case REOP_loop: case REOP_lookahead: case REOP_negative_lookahead: val = get_u32(buf + pos + 1); val += (pos + 5); printf(" %u", val); break; - case REOP_simple_greedy_quant: - printf(" %u %u %u %u", - get_u32(buf + pos + 1) + (pos + 17), - get_u32(buf + pos + 1 + 4), - get_u32(buf + pos + 1 + 8), - get_u32(buf + pos + 1 + 12)); + case REOP_loop: + val2 = buf[pos + 1]; + val = get_u32(buf + pos + 2); + val += (pos + 6); + printf(" r%u, %u", val2, val); + break; + case REOP_loop_split_goto_first: + case REOP_loop_split_next_first: + case REOP_loop_check_adv_split_goto_first: + case REOP_loop_check_adv_split_next_first: + { + uint32_t limit; + val2 = buf[pos + 1]; + limit = get_u32(buf + pos + 2); + val = get_u32(buf + pos + 6); + val += (pos + 10); + printf(" r%u, %u, %u", val2, limit, val); + } break; case REOP_save_start: case REOP_save_end: + printf(" %u", buf[pos + 1]); + break; case REOP_back_reference: + case REOP_back_reference_i: case REOP_backward_back_reference: - printf(" %u", buf[pos + 1]); + case REOP_backward_back_reference_i: + { + int n, i; + n = buf[pos + 1]; + len += n; + for(i = 0; i < n; i++) { + if (i != 0) + printf(","); + printf(" %u", buf[pos + 2 + i]); + } + } break; case REOP_save_reset: printf(" %u %u", buf[pos + 1], buf[pos + 2]); break; - case REOP_push_i32: - val = get_u32(buf + pos + 1); - printf(" %d", val); + case REOP_set_i32: + val = buf[pos + 1]; + val2 = get_u32(buf + pos + 2); + printf(" r%u, %d", val, val2); + break; + case REOP_set_char_pos: + case REOP_check_advance: + val = buf[pos + 1]; + printf(" r%u", val); break; case REOP_range: + case REOP_range_i: { int n, i; n = get_u16(buf + pos + 1); @@ -74716,6 +79822,7 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf, } break; case REOP_range32: + case REOP_range32_i: { int n, i; n = get_u16(buf + pos + 1); @@ -74759,6 +79866,27 @@ static int re_emit_goto(REParseState *s, int op, uint32_t val) return pos; } +static int re_emit_goto_u8(REParseState *s, int op, uint32_t arg, uint32_t val) +{ + int pos; + dbuf_putc(&s->byte_code, op); + dbuf_putc(&s->byte_code, arg); + pos = s->byte_code.size; + dbuf_put_u32(&s->byte_code, val - (pos + 4)); + return pos; +} + +static int re_emit_goto_u8_u32(REParseState *s, int op, uint32_t arg0, uint32_t arg1, uint32_t val) +{ + int pos; + dbuf_putc(&s->byte_code, op); + dbuf_putc(&s->byte_code, arg0); + dbuf_put_u32(&s->byte_code, arg1); + pos = s->byte_code.size; + dbuf_put_u32(&s->byte_code, val - (pos + 4)); + return pos; +} + static void re_emit_op_u8(REParseState *s, int op, uint32_t val) { dbuf_putc(&s->byte_code, op); @@ -74771,7 +79899,7 @@ static void re_emit_op_u16(REParseState *s, int op, uint32_t val) dbuf_put_u16(&s->byte_code, val); } -static int JS_PRINTF_FORMAT_ATTR(2, 3) re_parse_error(REParseState *s, const char *fmt, ...) +static int JS_PRINTF_FORMAT_ATTR(2, 3) re_parse_error(REParseState *s, JS_PRINTF_FORMAT const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -74785,13 +79913,6 @@ static int re_parse_out_of_memory(REParseState *s) return re_parse_error(s, "out of memory"); } -static int lre_check_size(REParseState *s) -{ - if (s->byte_code.size < 64*1024*1024) - return 0; - return re_parse_out_of_memory(s); -} - /* If allow_overflow is false, return -1 in case of overflow. Otherwise return INT32_MAX. */ static int parse_digits(const uint8_t **pp, bool allow_overflow) @@ -74867,9 +79988,21 @@ int lre_parse_escape(const uint8_t **pp, int allow_utf16) c = '\v'; break; case 'x': + { + int h0, h1; + + h0 = from_hex(*p++); + if (h0 < 0) + return -1; + h1 = from_hex(*p++); + if (h1 < 0) + return -1; + c = (h0 << 4) | h1; + } + break; case 'u': { - int h, n, i; + int h, i; uint32_t c1; if (*p == '{' && allow_utf16) { @@ -74887,14 +80020,8 @@ int lre_parse_escape(const uint8_t **pp, int allow_utf16) } p++; } else { - if (c == 'x') { - n = 2; - } else { - n = 4; - } - c = 0; - for(i = 0; i < n; i++) { + for(i = 0; i < 4; i++) { h = from_hex(*p++); if (h < 0) { return -1; @@ -74960,8 +80087,16 @@ static bool is_unicode_char(int c) (c == '_')); } -static int parse_unicode_property(REParseState *s, CharRange *cr, - const uint8_t **pp, bool is_inv) +/* XXX: memory error test */ +static void seq_prop_cb(void *opaque, const uint32_t *seq, int seq_len) +{ + REStringList *sl = opaque; + re_string_add(sl, seq_len, seq); +} + +static int parse_unicode_property(REParseState *s, REStringList *cr, + const uint8_t **pp, bool is_inv, + bool allow_sequence_prop) { const uint8_t *p; char name[64], value[64]; @@ -75001,51 +80136,76 @@ static int parse_unicode_property(REParseState *s, CharRange *cr, } else if (!strcmp(name, "Script_Extensions") || !strcmp(name, "scx")) { script_ext = true; do_script: - cr_init(cr, s->opaque, lre_realloc); - ret = unicode_script(cr, value, script_ext); + re_string_list_init(s, cr); + ret = unicode_script(&cr->cr, value, script_ext); if (ret) { - cr_free(cr); + re_string_list_free(cr); if (ret == -2) return re_parse_error(s, "unknown unicode script"); else goto out_of_memory; } } else if (!strcmp(name, "General_Category") || !strcmp(name, "gc")) { - cr_init(cr, s->opaque, lre_realloc); - ret = unicode_general_category(cr, value); + re_string_list_init(s, cr); + ret = unicode_general_category(&cr->cr, value); if (ret) { - cr_free(cr); + re_string_list_free(cr); if (ret == -2) return re_parse_error(s, "unknown unicode general category"); else goto out_of_memory; } } else if (value[0] == '\0') { - cr_init(cr, s->opaque, lre_realloc); - ret = unicode_general_category(cr, name); + re_string_list_init(s, cr); + ret = unicode_general_category(&cr->cr, name); if (ret == -1) { - cr_free(cr); + re_string_list_free(cr); goto out_of_memory; } if (ret < 0) { - ret = unicode_prop(cr, name); - if (ret) { - cr_free(cr); - if (ret == -2) - goto unknown_property_name; - else - goto out_of_memory; + ret = unicode_prop(&cr->cr, name); + if (ret == -1) { + re_string_list_free(cr); + goto out_of_memory; } } + if (ret < 0 && !is_inv && allow_sequence_prop) { + CharRange cr_tmp; + cr_init(&cr_tmp, s->opaque, lre_realloc); + ret = unicode_sequence_prop(name, seq_prop_cb, cr, &cr_tmp); + cr_free(&cr_tmp); + if (ret == -1) { + re_string_list_free(cr); + goto out_of_memory; + } + } + if (ret < 0) + goto unknown_property_name; } else { unknown_property_name: return re_parse_error(s, "unknown unicode property name"); } + /* the ordering of case folding and inversion differs with + unicode_sets. 'unicode_sets' ordering is more consistent */ + /* XXX: the spec seems incorrect, we do it as the other engines + seem to do it. */ + if (s->ignore_case && s->unicode_sets) { + if (re_string_list_canonicalize(s, cr, s->is_unicode)) { + re_string_list_free(cr); + goto out_of_memory; + } + } if (is_inv) { - if (cr_invert(cr)) { - cr_free(cr); - return -1; + if (cr_invert(&cr->cr)) { + re_string_list_free(cr); + goto out_of_memory; + } + } + if (s->ignore_case && !s->unicode_sets) { + if (re_string_list_canonicalize(s, cr, s->is_unicode)) { + re_string_list_free(cr); + goto out_of_memory; } } *pp = p; @@ -75054,13 +80214,64 @@ static int parse_unicode_property(REParseState *s, CharRange *cr, return re_parse_out_of_memory(s); } +static int get_class_atom(REParseState *s, REStringList *cr, + const uint8_t **pp, bool inclass); + +static int parse_class_string_disjunction(REParseState *s, REStringList *cr, + const uint8_t **pp) +{ + const uint8_t *p; + DynBuf str; + int c; + + p = *pp; + if (*p != '{') + return re_parse_error(s, "expecting '{' after \\q"); + + dbuf_init2(&str, s->opaque, lre_realloc); + re_string_list_init(s, cr); + + p++; + for(;;) { + str.size = 0; + while (*p != '}' && *p != '|') { + c = get_class_atom(s, NULL, &p, true); + if (c < 0) + goto fail; + if (dbuf_put_u32(&str, c)) { + re_parse_out_of_memory(s); + goto fail; + } + } + if (re_string_add(cr, str.size / 4, (uint32_t *)str.buf)) { + re_parse_out_of_memory(s); + goto fail; + } + if (*p == '}') + break; + p++; + } + if (s->ignore_case) { + if (re_string_list_canonicalize(s, cr, true)) + goto fail; + } + p++; /* skip the '}' */ + dbuf_free(&str); + *pp = p; + return 0; + fail: + dbuf_free(&str); + re_string_list_free(cr); + return -1; +} + /* return -1 if error otherwise the character or a class range - (CLASS_RANGE_BASE). In case of class range, 'cr' is + (CLASS_RANGE_BASE) if cr != NULL. In case of class range, 'cr' is initialized. Otherwise, it is ignored. */ -static int get_class_atom(REParseState *s, CharRange *cr, +static int get_class_atom(REParseState *s, REStringList *cr, const uint8_t **pp, bool inclass) { - const uint8_t *p, *p_next; + const uint8_t *p; uint32_t c; int ret; @@ -75092,9 +80303,11 @@ static int get_class_atom(REParseState *s, CharRange *cr, case 'W': c = CHAR_RANGE_W; class_range: + if (!cr) + goto default_escape; if (cr_init_char_range(s, cr, c)) return -1; - c = CLASS_RANGE_BASE; + c += CLASS_RANGE_BASE; break; case 'c': c = *p; @@ -75112,28 +80325,69 @@ static int get_class_atom(REParseState *s, CharRange *cr, c = '\\'; } break; + case '-': + if (!inclass && s->is_unicode) + goto invalid_escape; + break; + case '&': + case '!': + case '#': + case '%': + case ',': + case ':': + case ';': + case '<': + case '=': + case '>': + case '@': + case '`': + case '~': + if (s->is_unicode && (!inclass || !s->unicode_sets)) + /* Only illegal if in unicode mode and not in a class */ + goto invalid_escape; + break; + case '^': + case '$': + case '\\': + case '.': + case '*': + case '+': + case '?': + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case '|': + case '/': + /* always valid to escape these characters */ + break; case 'p': case 'P': - if (s->is_unicode) { - if (parse_unicode_property(s, cr, &p, (c == 'P'))) + if (s->is_unicode && cr) { + if (parse_unicode_property(s, cr, &p, (c == 'P'), s->unicode_sets)) return -1; c = CLASS_RANGE_BASE; break; } - /* fall thru */ + goto default_escape; + case 'q': + if (s->unicode_sets && cr && inclass) { + if (parse_class_string_disjunction(s, cr, &p)) + return -1; + c = CLASS_RANGE_BASE; + break; + } + goto default_escape; default: + default_escape: p--; ret = lre_parse_escape(&p, s->is_unicode * 2); if (ret >= 0) { c = ret; } else { - if (ret == -2 && *p != '\0' && strchr("^$\\.*+?()[]{}|/", *p)) { - /* always valid to escape these characters */ - goto normal_char; - } else if (s->is_unicode) { - // special case: allowed inside [] but not outside - if (ret == -2 && *p == '-' && inclass) - goto normal_char; + if (s->is_unicode) { invalid_escape: return re_parse_error(s, "invalid escape sequence in regular expression"); } else { @@ -75150,20 +80404,59 @@ static int get_class_atom(REParseState *s, CharRange *cr, return re_parse_error(s, "unexpected end"); } /* fall thru */ + goto normal_char; + + case '&': + case '!': + case '#': + case '$': + case '%': + case '*': + case '+': + case ',': + case '.': + case ':': + case ';': + case '<': + case '=': + case '>': + case '?': + case '@': + case '^': + case '`': + case '~': + if (s->unicode_sets && p[1] == c) { + /* forbidden double characters */ + return re_parse_error(s, "invalid class set operation in regular expression"); + } + goto normal_char; + + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case '/': + case '-': + case '|': + if (s->unicode_sets) { + /* invalid characters in unicode sets */ + return re_parse_error(s, "invalid character in class in regular expression"); + } + goto normal_char; + default: normal_char: - p++; - if (c >= 0x80) { - c = utf8_decode(p - 1, &p_next); - if (p_next == p) - return re_parse_error(s, "invalid UTF-8 sequence"); - p = p_next; - if (c > 0xFFFF && !s->is_unicode) { - // TODO(chqrlie): should handle non BMP-1 code points in - // the calling function and no require the source string - // to be CESU-8 encoded if not s->is_unicode + /* normal char */ + if (c >= 128) { + c = utf8_decode_len(p, UTF8_CHAR_LEN_MAX, &p); + if ((unsigned)c > 0xffff && !s->is_unicode) { + /* XXX: should handle non BMP-1 code points */ return re_parse_error(s, "malformed unicode char"); } + } else { + p++; } break; } @@ -75180,8 +80473,6 @@ static int re_emit_range(REParseState *s, const CharRange *cr) if (len >= 65535) return re_parse_error(s, "too many ranges"); if (len == 0) { - /* not sure it can really happen. Emit a match that is always - false */ re_emit_op_u32(s, REOP_char32, -1); } else { high = cr->points[cr->len - 1]; @@ -75190,7 +80481,7 @@ static int re_emit_range(REParseState *s, const CharRange *cr) if (high <= 0xffff) { /* can use 16 bit ranges with the conversion that 0xffff = infinity */ - re_emit_op_u16(s, REOP_range, len); + re_emit_op_u16(s, s->ignore_case ? REOP_range_i : REOP_range, len); for(i = 0; i < cr->len; i += 2) { dbuf_put_u16(&s->byte_code, cr->points[i]); high = cr->points[i + 1] - 1; @@ -75199,7 +80490,7 @@ static int re_emit_range(REParseState *s, const CharRange *cr) dbuf_put_u16(&s->byte_code, high); } } else { - re_emit_op_u16(s, REOP_range32, len); + re_emit_op_u16(s, s->ignore_case ? REOP_range32_i : REOP_range32, len); for(i = 0; i < cr->len; i += 2) { dbuf_put_u32(&s->byte_code, cr->points[i]); dbuf_put_u32(&s->byte_code, cr->points[i + 1] - 1); @@ -75209,254 +80500,419 @@ static int re_emit_range(REParseState *s, const CharRange *cr) return 0; } -// s->unicode turns patterns like []] into syntax errors -// s->unicode_sets turns more patterns into errors, like [a-] or [[] -static int re_parse_char_class(REParseState *s, const uint8_t **pp) +static int re_string_cmp_len(const void *a, const void *b, void *arg) +{ + REString *p1 = *(REString **)a; + REString *p2 = *(REString **)b; + return (p1->len < p2->len) - (p1->len > p2->len); +} + +static void re_emit_char(REParseState *s, int c) +{ + if (c <= 0xffff) + re_emit_op_u16(s, s->ignore_case ? REOP_char_i : REOP_char, c); + else + re_emit_op_u32(s, s->ignore_case ? REOP_char32_i : REOP_char32, c); +} + +static int re_emit_string_list(REParseState *s, const REStringList *sl) +{ + REString **tab, *p; + int i, j, split_pos, last_match_pos, n; + bool has_empty_string, is_last; + + // re_string_list_dump("sl", sl); + if (sl->n_strings == 0) { + /* simple case: only characters */ + if (re_emit_range(s, &sl->cr)) + return -1; + } else { + /* at least one string list is present : match the longest ones first */ + /* XXX: add a new op_switch opcode to compile as a trie */ + tab = lre_realloc(s->opaque, NULL, sizeof(tab[0]) * sl->n_strings); + if (!tab) { + re_parse_out_of_memory(s); + return -1; + } + has_empty_string = false; + n = 0; + for(i = 0; i < sl->hash_size; i++) { + for(p = sl->hash_table[i]; p != NULL; p = p->next) { + if (p->len == 0) { + has_empty_string = true; + } else { + tab[n++] = p; + } + } + } + assert(n <= sl->n_strings); + + rqsort(tab, n, sizeof(tab[0]), re_string_cmp_len, NULL); + + last_match_pos = -1; + for(i = 0; i < n; i++) { + p = tab[i]; + is_last = !has_empty_string && sl->cr.len == 0 && i == (n - 1); + if (!is_last) + split_pos = re_emit_op_u32(s, REOP_split_next_first, 0); + else + split_pos = 0; + for(j = 0; j < p->len; j++) { + re_emit_char(s, p->buf[j]); + } + if (!is_last) { + last_match_pos = re_emit_op_u32(s, REOP_goto, last_match_pos); + /* the positions to patch are only valid if all the byte code + could be emitted */ + if (dbuf_error(&s->byte_code)) + goto out_of_memory; + put_u32(s->byte_code.buf + split_pos, s->byte_code.size - (split_pos + 4)); + } + } + + if (sl->cr.len != 0) { + /* char range */ + is_last = !has_empty_string; + if (!is_last) + split_pos = re_emit_op_u32(s, REOP_split_next_first, 0); + else + split_pos = 0; /* not used */ + if (re_emit_range(s, &sl->cr)) + goto fail; + if (!is_last) { + if (dbuf_error(&s->byte_code)) + goto out_of_memory; + put_u32(s->byte_code.buf + split_pos, s->byte_code.size - (split_pos + 4)); + } + } + + /* patch the 'goto match' */ + if (dbuf_error(&s->byte_code)) + goto out_of_memory; + while (last_match_pos != -1) { + int next_pos = get_u32(s->byte_code.buf + last_match_pos); + put_u32(s->byte_code.buf + last_match_pos, s->byte_code.size - (last_match_pos + 4)); + last_match_pos = next_pos; + } + + lre_realloc(s->opaque, tab, 0); + } + return 0; + out_of_memory: + re_parse_out_of_memory(s); + fail: + lre_realloc(s->opaque, tab, 0); + return -1; +} + +static int re_parse_nested_class(REParseState *s, REStringList *cr, const uint8_t **pp); + +static int re_parse_class_set_operand(REParseState *s, REStringList *cr, const uint8_t **pp) +{ + int c1; + const uint8_t *p = *pp; + + if (*p == '[') { + if (re_parse_nested_class(s, cr, pp)) + return -1; + } else { + c1 = get_class_atom(s, cr, pp, true); + if (c1 < 0) + return -1; + if (c1 < CLASS_RANGE_BASE) { + /* create a range with a single character */ + re_string_list_init(s, cr); + if (s->ignore_case) + c1 = lre_canonicalize(c1, s->is_unicode); + if (cr_union_interval(&cr->cr, c1, c1)) { + re_string_list_free(cr); + return -1; + } + } + } + return 0; +} + +static int re_parse_nested_class(REParseState *s, REStringList *cr, const uint8_t **pp) { const uint8_t *p; uint32_t c1, c2; - CharRange cr_s, *cr = &cr_s; - CharRange cr1_s, *cr1 = &cr1_s; - bool invert; + int ret; + REStringList cr1_s, *cr1 = &cr1_s; + bool invert, is_first; + + if (lre_check_stack_overflow(s->opaque, 0)) + return re_parse_error(s, "stack overflow"); - cr_init(cr, s->opaque, lre_realloc); + re_string_list_init(s, cr); p = *pp; p++; /* skip '[' */ - if (s->unicode_sets) { - static const char verboten[] = - "()[{}/-|" "\0" - "&&!!##$$%%**++,,..::;;<<==>>??@@``~~" "\0" - "^^^_^^"; - const char *s = verboten; - int n = 1; - do { - // not memcmp because some implementations compare word instead - // of byte at a time and will happily read past end of input - if (!strncmp(s, (const char *)p, n)) - if (p[n] == ']') - goto invalid_class_range; - s += n; - if (!*s) { - s++; - n++; - } - } while (n < 4); - } - invert = false; if (*p == '^') { p++; invert = true; } - + + /* handle unions */ + is_first = true; for(;;) { if (*p == ']') break; - c1 = get_class_atom(s, cr1, &p, true); - if ((int)c1 < 0) - goto fail; - if (*p == '-' && p[1] == ']' && s->unicode_sets) { - if (c1 >= CLASS_RANGE_BASE) - cr_free(cr1); - goto invalid_class_range; - } - if (*p == '-' && p[1] != ']') { - const uint8_t *p0 = p + 1; - if (c1 >= CLASS_RANGE_BASE) { - if (s->is_unicode) { - cr_free(cr1); - goto invalid_class_range; - } - /* Annex B: match '-' character */ - goto class_atom; - } - c2 = get_class_atom(s, cr1, &p0, true); - if ((int)c2 < 0) - goto fail; - if (c2 >= CLASS_RANGE_BASE) { - cr_free(cr1); - if (s->is_unicode) { - goto invalid_class_range; - } - /* Annex B: match '-' character */ - goto class_atom; - } - p = p0; - if (c2 < c1) { - invalid_class_range: - re_parse_error(s, "invalid class range"); + if (*p == '[' && s->unicode_sets) { + if (re_parse_nested_class(s, cr1, &p)) goto fail; - } - if (cr_union_interval(cr, c1, c2)) - goto memory_error; + goto class_union; } else { - class_atom: - if (c1 >= CLASS_RANGE_BASE) { - int ret; - ret = cr_union1(cr, cr1->points, cr1->len); - cr_free(cr1); - if (ret) - goto memory_error; + c1 = get_class_atom(s, cr1, &p, true); + if ((int)c1 < 0) + goto fail; + if (*p == '-' && p[1] != ']') { + const uint8_t *p0 = p + 1; + if (p[1] == '-' && s->unicode_sets && is_first) + goto class_atom; /* first character class followed by '--' */ + if (c1 >= CLASS_RANGE_BASE) { + if (s->is_unicode) { + re_string_list_free(cr1); + goto invalid_class_range; + } + /* Annex B: match '-' character */ + goto class_atom; + } + c2 = get_class_atom(s, cr1, &p0, true); + if ((int)c2 < 0) + goto fail; + if (c2 >= CLASS_RANGE_BASE) { + re_string_list_free(cr1); + if (s->is_unicode) { + goto invalid_class_range; + } + /* Annex B: match '-' character */ + goto class_atom; + } + p = p0; + if (c2 < c1) { + invalid_class_range: + re_parse_error(s, "invalid class range"); + goto fail; + } + if (s->ignore_case) { + CharRange cr2_s, *cr2 = &cr2_s; + cr_init(cr2, s->opaque, lre_realloc); + if (cr_add_interval(cr2, c1, c2 + 1) || + cr_regexp_canonicalize(cr2, s->is_unicode) || + cr_op1(&cr->cr, cr2->points, cr2->len, CR_OP_UNION)) { + cr_free(cr2); + goto memory_error; + } + cr_free(cr2); + } else { + if (cr_union_interval(&cr->cr, c1, c2)) + goto memory_error; + } + is_first = false; /* union operation */ } else { - if (cr_union_interval(cr, c1, c1)) - goto memory_error; + class_atom: + if (c1 >= CLASS_RANGE_BASE) { + class_union: + ret = re_string_list_op(cr, cr1, CR_OP_UNION); + re_string_list_free(cr1); + if (ret) + goto memory_error; + } else { + if (s->ignore_case) + c1 = lre_canonicalize(c1, s->is_unicode); + if (cr_union_interval(&cr->cr, c1, c1)) + goto memory_error; + } } } + if (s->unicode_sets && is_first) { + if (*p == '&' && p[1] == '&' && p[2] != '&') { + /* handle '&&' */ + for(;;) { + if (*p == ']') { + break; + } else if (*p == '&' && p[1] == '&' && p[2] != '&') { + p += 2; + } else { + goto invalid_operation; + } + if (re_parse_class_set_operand(s, cr1, &p)) + goto fail; + ret = re_string_list_op(cr, cr1, CR_OP_INTER); + re_string_list_free(cr1); + if (ret) + goto memory_error; + } + } else if (*p == '-' && p[1] == '-') { + /* handle '--' */ + for(;;) { + if (*p == ']') { + break; + } else if (*p == '-' && p[1] == '-') { + p += 2; + } else { + invalid_operation: + re_parse_error(s, "invalid operation in regular expression"); + goto fail; + } + if (re_parse_class_set_operand(s, cr1, &p)) + goto fail; + ret = re_string_list_op(cr, cr1, CR_OP_SUB); + re_string_list_free(cr1); + if (ret) + goto memory_error; + } + } + } + is_first = false; } - if (s->ignore_case) { - if (cr_regexp_canonicalize(cr, s->is_unicode)) - goto memory_error; - } + + p++; /* skip ']' */ + *pp = p; if (invert) { - if (cr_invert(cr)) + /* XXX: add may_contain_string syntax check to be fully + compliant. The test here accepts more input than the + spec. */ + if (cr->n_strings != 0) { + re_parse_error(s, "negated character class with strings in regular expression debugger eval code"); + goto fail; + } + if (cr_invert(&cr->cr)) goto memory_error; } - if (re_emit_range(s, cr)) - goto fail; - cr_free(cr); - p++; /* skip ']' */ - *pp = p; return 0; memory_error: re_parse_out_of_memory(s); fail: - cr_free(cr); + re_string_list_free(cr); return -1; } -/* Return: - - true if the opcodes may not advance the char pointer - - false if the opcodes always advance the char pointer +static int re_parse_char_class(REParseState *s, const uint8_t **pp) +{ + REStringList cr_s, *cr = &cr_s; + + if (re_parse_nested_class(s, cr, pp)) + return -1; + if (re_emit_string_list(s, cr)) + goto fail; + re_string_list_free(cr); + return 0; + fail: + re_string_list_free(cr); + return -1; +} + +/* need_check_adv: false if the opcodes always advance the char pointer + need_capture_init: true if all the captures in the atom are not set */ -static bool re_need_check_advance(const uint8_t *bc_buf, int bc_buf_len) +static bool re_need_check_adv_and_capture_init(bool *pneed_capture_init, + const uint8_t *bc_buf, int bc_buf_len) { int pos, opcode, len; uint32_t val; - bool ret; + bool need_check_adv, need_capture_init; - ret = true; + need_check_adv = true; + need_capture_init = false; pos = 0; - while (pos < bc_buf_len) { opcode = bc_buf[pos]; len = reopcode_info[opcode].size; switch(opcode) { case REOP_range: + case REOP_range_i: val = get_u16(bc_buf + pos + 1); len += val * 4; - goto simple_char; + need_check_adv = false; + break; case REOP_range32: + case REOP_range32_i: val = get_u16(bc_buf + pos + 1); len += val * 8; - goto simple_char; + need_check_adv = false; + break; + case REOP_char: + case REOP_char_i: case REOP_char32: - case REOP_char16: - case REOP_char8: + case REOP_char32_i: case REOP_dot: case REOP_any: - simple_char: - ret = false; + case REOP_space: + case REOP_not_space: + need_check_adv = false; break; case REOP_line_start: + case REOP_line_start_m: case REOP_line_end: - case REOP_push_i32: - case REOP_push_char_pos: - case REOP_drop: + case REOP_line_end_m: + case REOP_set_i32: + case REOP_set_char_pos: case REOP_word_boundary: + case REOP_word_boundary_i: case REOP_not_word_boundary: + case REOP_not_word_boundary_i: case REOP_prev: /* no effect */ break; case REOP_save_start: case REOP_save_end: case REOP_save_reset: + break; case REOP_back_reference: + case REOP_back_reference_i: case REOP_backward_back_reference: + case REOP_backward_back_reference_i: + val = bc_buf[pos + 1]; + len += val; + need_capture_init = true; break; default: - /* safe behvior: we cannot predict the outcome */ - return true; - } - pos += len; - } - return ret; -} - -/* return -1 if a simple quantifier cannot be used. Otherwise return - the number of characters in the atom. */ -static int re_is_simple_quantifier(const uint8_t *bc_buf, int bc_buf_len) -{ - int pos, opcode, len, count; - uint32_t val; - - count = 0; - pos = 0; - while (pos < bc_buf_len) { - opcode = bc_buf[pos]; - len = reopcode_info[opcode].size; - switch(opcode) { - case REOP_range: - val = get_u16(bc_buf + pos + 1); - len += val * 4; - goto simple_char; - case REOP_range32: - val = get_u16(bc_buf + pos + 1); - len += val * 8; - goto simple_char; - case REOP_char32: - case REOP_char16: - case REOP_char8: - case REOP_dot: - case REOP_any: - simple_char: - count++; - break; - case REOP_line_start: - case REOP_line_end: - case REOP_word_boundary: - case REOP_not_word_boundary: - break; - default: - return -1; + /* safe behavior: we cannot predict the outcome */ + need_capture_init = true; + goto done; } pos += len; } - return count; + done: + *pneed_capture_init = need_capture_init; + return need_check_adv; } /* '*pp' is the first char after '<' */ static int re_parse_group_name(char *buf, int buf_size, const uint8_t **pp) { - const uint8_t *p, *p_next; + const uint8_t *p, *p1; uint32_t c, d; char *q; p = *pp; q = buf; for(;;) { - c = *p++; + c = *p; if (c == '\\') { + p++; if (*p != 'u') return -1; c = lre_parse_escape(&p, 2); // accept surrogate pairs - if ((int)c < 0) - return -1; } else if (c == '>') { break; - } else if (c >= 0x80) { - c = utf8_decode(p - 1, &p_next); - if (p_next == p) - return -1; - p = p_next; + } else if (c >= 128) { + c = utf8_decode_len(p, UTF8_CHAR_LEN_MAX, &p); if (is_hi_surrogate(c)) { - d = utf8_decode(p, &p_next); + d = utf8_decode_len(p, UTF8_CHAR_LEN_MAX, &p1); if (is_lo_surrogate(d)) { c = from_surrogate(c, d); - p = p_next; + p = p1; } } + } else { + p++; } + if (c > 0x10FFFF) + return -1; if (q == buf) { if (!lre_js_is_ident_first(c)) return -1; @@ -75466,7 +80922,7 @@ static int re_parse_group_name(char *buf, int buf_size, const uint8_t **pp) } if ((q - buf + UTF8_CHAR_LEN_MAX + 1) > buf_size) return -1; - if (c < 0x80) { + if (c < 128) { *q++ = c; } else { q += utf8_encode((uint8_t*)q, c); @@ -75475,21 +80931,22 @@ static int re_parse_group_name(char *buf, int buf_size, const uint8_t **pp) if (q == buf) return -1; *q = '\0'; + p++; *pp = p; return 0; } /* if capture_name = NULL: return the number of captures + 1. - Otherwise, return the capture index corresponding to capture_name - or -1 if none */ + Otherwise, return the number of matching capture groups */ static int re_parse_captures(REParseState *s, int *phas_named_captures, - const char *capture_name) + const char *capture_name, bool emit_group_index) { const uint8_t *p; - int capture_index; + int capture_index, n; char name[TMP_BUF_SIZE]; capture_index = 1; + n = 0; *phas_named_captures = 0; for (p = s->buf_start; p < s->buf_end; p++) { switch (*p) { @@ -75501,8 +80958,11 @@ static int re_parse_captures(REParseState *s, int *phas_named_captures, if (capture_name) { p += 3; if (re_parse_group_name(name, sizeof(name), &p) == 0) { - if (!strcmp(name, capture_name)) - return capture_index; + if (!strcmp(name, capture_name)) { + if (emit_group_index) + dbuf_putc(&s->byte_code, capture_index); + n++; + } } } capture_index++; @@ -75527,17 +80987,18 @@ static int re_parse_captures(REParseState *s, int *phas_named_captures, } } done: - if (capture_name) - return -1; - else + if (capture_name) { + return n; + } else { return capture_index; + } } static int re_count_captures(REParseState *s) { if (s->total_capture_count < 0) { s->total_capture_count = re_parse_captures(s, &s->has_named_captures, - NULL); + NULL, false); } return s->total_capture_count; } @@ -75549,38 +81010,104 @@ static bool re_has_named_captures(REParseState *s) return s->has_named_captures; } -static int find_group_name(REParseState *s, const char *name) +static int find_group_name(REParseState *s, const char *name, bool emit_group_index) { const char *p, *buf_end; size_t len, name_len; - int capture_index; + int capture_index, n; p = (char *)s->group_names.buf; - if (!p) return -1; + if (!p) + return 0; buf_end = (char *)s->group_names.buf + s->group_names.size; name_len = strlen(name); capture_index = 1; + n = 0; while (p < buf_end) { len = strlen(p); - if (len == name_len && memcmp(name, p, name_len) == 0) - return capture_index; - p += len + 1; + if (len == name_len && memcmp(name, p, name_len) == 0) { + if (emit_group_index) + dbuf_putc(&s->byte_code, capture_index); + n++; + } + p += len + LRE_GROUP_NAME_TRAILER_LEN; capture_index++; } - return -1; + return n; +} + +static bool is_duplicate_group_name(REParseState *s, const char *name, int scope) +{ + const char *p, *buf_end; + size_t len, name_len; + int scope1; + + p = (char *)s->group_names.buf; + if (!p) + return 0; + buf_end = (char *)s->group_names.buf + s->group_names.size; + name_len = strlen(name); + while (p < buf_end) { + len = strlen(p); + if (len == name_len && memcmp(name, p, name_len) == 0) { + scope1 = (uint8_t)p[len + 1]; + if (scope == scope1) + return true; + } + p += len + LRE_GROUP_NAME_TRAILER_LEN; + } + return false; } static int re_parse_disjunction(REParseState *s, bool is_backward_dir); +static int re_parse_modifiers(REParseState *s, const uint8_t **pp) +{ + const uint8_t *p = *pp; + int mask = 0; + int val; + + for(;;) { + if (*p == 'i') { + val = LRE_FLAG_IGNORECASE; + } else if (*p == 'm') { + val = LRE_FLAG_MULTILINE; + } else if (*p == 's') { + val = LRE_FLAG_DOTALL; + } else { + break; + } + if (mask & val) + return re_parse_error(s, "duplicate modifier: '%c'", *p); + mask |= val; + p++; + } + *pp = p; + return mask; +} + +static bool update_modifier(bool val, int add_mask, int remove_mask, + int mask) +{ + if (add_mask & mask) + val = true; + if (remove_mask & mask) + val = false; + return val; +} + static int re_parse_term(REParseState *s, bool is_backward_dir) { const uint8_t *p; int c, last_atom_start, quant_min, quant_max, last_capture_count; - bool greedy, add_zero_advance_check, is_neg, is_backward_lookahead; - CharRange cr_s, *cr = &cr_s; + bool greedy, is_neg, is_backward_lookahead; + REStringList cr_s, *cr = &cr_s; + + /* after a failed allocation the byte code is incomplete and 'buf' may + still be NULL: stop before computing positions inside it */ + if (dbuf_error(&s->byte_code)) + return re_parse_out_of_memory(s); - if (lre_check_size(s)) - return -1; last_atom_start = -1; last_capture_count = 0; p = s->buf_ptr; @@ -75588,11 +81115,11 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) switch(c) { case '^': p++; - re_emit_op(s, REOP_line_start); + re_emit_op(s, s->multi_line ? REOP_line_start_m : REOP_line_start); break; case '$': p++; - re_emit_op(s, REOP_line_end); + re_emit_op(s, s->multi_line ? REOP_line_end_m : REOP_line_end); break; case '.': p++; @@ -75642,6 +81169,44 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) p = s->buf_ptr; if (re_parse_expect(s, &p, ')')) return -1; + } else if (p[2] == 'i' || p[2] == 'm' || p[2] == 's' || p[2] == '-') { + bool saved_ignore_case, saved_multi_line, saved_dotall; + int add_mask, remove_mask; + p += 2; + remove_mask = 0; + add_mask = re_parse_modifiers(s, &p); + if (add_mask < 0) + return -1; + if (*p == '-') { + p++; + remove_mask = re_parse_modifiers(s, &p); + if (remove_mask < 0) + return -1; + } + if ((add_mask == 0 && remove_mask == 0) || + (add_mask & remove_mask) != 0) { + return re_parse_error(s, "invalid modifiers"); + } + if (re_parse_expect(s, &p, ':')) + return -1; + saved_ignore_case = s->ignore_case; + saved_multi_line = s->multi_line; + saved_dotall = s->dotall; + s->ignore_case = update_modifier(s->ignore_case, add_mask, remove_mask, LRE_FLAG_IGNORECASE); + s->multi_line = update_modifier(s->multi_line, add_mask, remove_mask, LRE_FLAG_MULTILINE); + s->dotall = update_modifier(s->dotall, add_mask, remove_mask, LRE_FLAG_DOTALL); + + last_atom_start = s->byte_code.size; + last_capture_count = s->capture_count; + s->buf_ptr = p; + if (re_parse_disjunction(s, is_backward_dir)) + return -1; + p = s->buf_ptr; + if (re_parse_expect(s, &p, ')')) + return -1; + s->ignore_case = saved_ignore_case; + s->multi_line = saved_multi_line; + s->dotall = saved_dotall; } else if ((p[2] == '=' || p[2] == '!')) { is_neg = (p[2] == '!'); is_backward_lookahead = false; @@ -75668,10 +81233,10 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) p = s->buf_ptr; if (re_parse_expect(s, &p, ')')) return -1; - re_emit_op(s, REOP_match); + re_emit_op(s, REOP_lookahead_match + is_neg); /* jump after the 'match' after the lookahead is successful */ if (dbuf_error(&s->byte_code)) - return -1; + return re_parse_out_of_memory(s); put_u32(s->byte_code.buf + pos, s->byte_code.size - (pos + 4)); } else if (p[2] == '<') { p += 3; @@ -75679,12 +81244,16 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) &p)) { return re_parse_error(s, "invalid group name"); } - if (find_group_name(s, s->u.tmp_buf) > 0) { + /* poor's man method to test duplicate group + names. */ + /* XXX: this method does not catch all the errors*/ + if (is_duplicate_group_name(s, s->u.tmp_buf, s->group_name_scope)) { return re_parse_error(s, "duplicate group name"); } /* group name with a trailing zero */ dbuf_put(&s->group_names, (uint8_t *)s->u.tmp_buf, strlen(s->u.tmp_buf) + 1); + dbuf_putc(&s->group_names, s->group_name_scope); s->has_named_captures = 1; goto parse_capture; } else { @@ -75695,6 +81264,7 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) p++; /* capture without group name */ dbuf_putc(&s->group_names, 0); + dbuf_putc(&s->group_names, 0); parse_capture: if (s->capture_count >= CAPTURE_COUNT_MAX) return re_parse_error(s, "too many captures"); @@ -75720,14 +81290,19 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) switch(p[1]) { case 'b': case 'B': - re_emit_op(s, REOP_word_boundary + (p[1] != 'b')); + if (p[1] != 'b') { + re_emit_op(s, s->ignore_case && s->is_unicode ? REOP_not_word_boundary_i : REOP_not_word_boundary); + } else { + re_emit_op(s, s->ignore_case && s->is_unicode ? REOP_word_boundary_i : REOP_word_boundary); + } p += 2; break; case 'k': { const uint8_t *p1; - int dummy_res; - + int dummy_res, n; + bool is_forward; + p1 = p; if (p1[2] != '<') { /* annex B: we tolerate invalid group names in non @@ -75746,21 +81321,33 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) else goto parse_class_atom; } - c = find_group_name(s, s->u.tmp_buf); - if (c < 0) { + is_forward = false; + n = find_group_name(s, s->u.tmp_buf, false); + if (n == 0) { /* no capture name parsed before, try to look after (inefficient, but hopefully not common */ - c = re_parse_captures(s, &dummy_res, s->u.tmp_buf); - if (c < 0) { + n = re_parse_captures(s, &dummy_res, s->u.tmp_buf, false); + if (n == 0) { if (s->is_unicode || re_has_named_captures(s)) return re_parse_error(s, "group name not defined"); else goto parse_class_atom; } + is_forward = true; + } + last_atom_start = s->byte_code.size; + last_capture_count = s->capture_count; + + /* emit back references to all the captures indexes matching the group name */ + re_emit_op_u8(s, REOP_back_reference + 2 * is_backward_dir + s->ignore_case, n); + if (is_forward) { + re_parse_captures(s, &dummy_res, s->u.tmp_buf, true); + } else { + find_group_name(s, s->u.tmp_buf, true); } p = p1; } - goto emit_back_reference; + break; case '0': p += 2; c = 0; @@ -75806,10 +81393,11 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) } return re_parse_error(s, "back reference out of range in regular expression"); } - emit_back_reference: last_atom_start = s->byte_code.size; last_capture_count = s->capture_count; - re_emit_op_u8(s, REOP_back_reference + is_backward_dir, c); + + re_emit_op_u8(s, REOP_back_reference + 2 * is_backward_dir + s->ignore_case, 1); + dbuf_putc(&s->byte_code, c); } break; default: @@ -75842,21 +81430,22 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) if (is_backward_dir) re_emit_op(s, REOP_prev); if (c >= CLASS_RANGE_BASE) { - int ret; - /* Note: canonicalization is not needed */ - ret = re_emit_range(s, cr); - cr_free(cr); + int ret = 0; + /* optimize the common 'space' tests */ + if (c == (CLASS_RANGE_BASE + CHAR_RANGE_s)) { + re_emit_op(s, REOP_space); + } else if (c == (CLASS_RANGE_BASE + CHAR_RANGE_S)) { + re_emit_op(s, REOP_not_space); + } else { + ret = re_emit_string_list(s, cr); + } + re_string_list_free(cr); if (ret) return -1; } else { if (s->ignore_case) c = lre_canonicalize(c, s->is_unicode); - if (c <= 0x7f) - re_emit_op_u8(s, REOP_char8, c); - else if (c <= 0xffff) - re_emit_op_u16(s, REOP_char16, c); - else - re_emit_op_u32(s, REOP_char32, c); + re_emit_char(s, c); } if (is_backward_dir) re_emit_op(s, REOP_prev); @@ -75924,52 +81513,39 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) if (last_atom_start < 0) { return re_parse_error(s, "nothing to repeat"); } - if (greedy) { + { + bool need_capture_init, add_zero_advance_check; int len, pos; - - if (quant_max > 0) { - /* specific optimization for simple quantifiers */ - if (dbuf_error(&s->byte_code)) + + /* the spec tells that if there is no advance when + running the atom after the first quant_min times, + then there is no match. We remove this test when we + are sure the atom always advances the position. */ + add_zero_advance_check = + re_need_check_adv_and_capture_init(&need_capture_init, + s->byte_code.buf + last_atom_start, + s->byte_code.size - last_atom_start); + + /* general case: need to reset the capture at each + iteration. We don't do it if there are no captures + in the atom or if we are sure all captures are + initialized in the atom. If quant_min = 0, we still + need to reset once the captures in case the atom + does not match. */ + if (need_capture_init && last_capture_count != s->capture_count) { + if (dbuf_insert(&s->byte_code, last_atom_start, 3)) goto out_of_memory; - len = re_is_simple_quantifier(s->byte_code.buf + last_atom_start, - s->byte_code.size - last_atom_start); - if (len > 0) { - re_emit_op(s, REOP_match); - - if (dbuf_insert(&s->byte_code, last_atom_start, 17)) - goto out_of_memory; - pos = last_atom_start; - s->byte_code.buf[pos++] = REOP_simple_greedy_quant; - put_u32(&s->byte_code.buf[pos], - s->byte_code.size - last_atom_start - 17); - pos += 4; - put_u32(&s->byte_code.buf[pos], quant_min); - pos += 4; - put_u32(&s->byte_code.buf[pos], quant_max); - pos += 4; - put_u32(&s->byte_code.buf[pos], len); - pos += 4; - goto done; - } + int pos = last_atom_start; + s->byte_code.buf[pos++] = REOP_save_reset; + s->byte_code.buf[pos++] = last_capture_count; + s->byte_code.buf[pos++] = s->capture_count - 1; } - if (dbuf_error(&s->byte_code)) - goto out_of_memory; - } - /* the spec tells that if there is no advance when - running the atom after the first quant_min times, - then there is no match. We remove this test when we - are sure the atom always advances the position. */ - add_zero_advance_check = re_need_check_advance(s->byte_code.buf + last_atom_start, - s->byte_code.size - last_atom_start); - - { - int len, pos; len = s->byte_code.size - last_atom_start; if (quant_min == 0) { /* need to reset the capture in case the atom is not executed */ - if (last_capture_count != s->capture_count) { + if (!need_capture_init && last_capture_count != s->capture_count) { if (dbuf_insert(&s->byte_code, last_atom_start, 3)) goto out_of_memory; s->byte_code.buf[last_atom_start++] = REOP_save_reset; @@ -75980,76 +81556,63 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) s->byte_code.size = last_atom_start; } else if (quant_max == 1 || quant_max == INT32_MAX) { bool has_goto = (quant_max == INT32_MAX); - if (dbuf_insert(&s->byte_code, last_atom_start, 5 + add_zero_advance_check)) + if (dbuf_insert(&s->byte_code, last_atom_start, 5 + add_zero_advance_check * 2)) goto out_of_memory; s->byte_code.buf[last_atom_start] = REOP_split_goto_first + greedy; put_u32(s->byte_code.buf + last_atom_start + 1, - len + 5 * has_goto + add_zero_advance_check * 2); + len + 5 * has_goto + add_zero_advance_check * 2 * 2); if (add_zero_advance_check) { - s->byte_code.buf[last_atom_start + 1 + 4] = REOP_push_char_pos; - re_emit_op(s, REOP_check_advance); + s->byte_code.buf[last_atom_start + 1 + 4] = REOP_set_char_pos; + s->byte_code.buf[last_atom_start + 1 + 4 + 1] = 0; + re_emit_op_u8(s, REOP_check_advance, 0); } if (has_goto) re_emit_goto(s, REOP_goto, last_atom_start); } else { - if (dbuf_insert(&s->byte_code, last_atom_start, 10 + add_zero_advance_check)) + if (dbuf_insert(&s->byte_code, last_atom_start, 11 + add_zero_advance_check * 2)) goto out_of_memory; pos = last_atom_start; - s->byte_code.buf[pos++] = REOP_push_i32; - put_u32(s->byte_code.buf + pos, quant_max); - pos += 4; s->byte_code.buf[pos++] = REOP_split_goto_first + greedy; - put_u32(s->byte_code.buf + pos, len + 5 + add_zero_advance_check * 2); + put_u32(s->byte_code.buf + pos, 6 + add_zero_advance_check * 2 + len + 10); + pos += 4; + + s->byte_code.buf[pos++] = REOP_set_i32; + s->byte_code.buf[pos++] = 0; + put_u32(s->byte_code.buf + pos, quant_max); pos += 4; + last_atom_start = pos; if (add_zero_advance_check) { - s->byte_code.buf[pos++] = REOP_push_char_pos; - re_emit_op(s, REOP_check_advance); + s->byte_code.buf[pos++] = REOP_set_char_pos; + s->byte_code.buf[pos++] = 0; } - re_emit_goto(s, REOP_loop, last_atom_start + 5); - re_emit_op(s, REOP_drop); + re_emit_goto_u8_u32(s, (add_zero_advance_check ? REOP_loop_check_adv_split_next_first : REOP_loop_split_next_first) - greedy, 0, quant_max, last_atom_start); } } else if (quant_min == 1 && quant_max == INT32_MAX && !add_zero_advance_check) { re_emit_goto(s, REOP_split_next_first - greedy, last_atom_start); } else { - if (quant_min == 1) { - /* nothing to add */ - } else { - if (dbuf_insert(&s->byte_code, last_atom_start, 5)) - goto out_of_memory; - s->byte_code.buf[last_atom_start] = REOP_push_i32; - put_u32(s->byte_code.buf + last_atom_start + 1, - quant_min); - last_atom_start += 5; - re_emit_goto(s, REOP_loop, last_atom_start); - re_emit_op(s, REOP_drop); + if (quant_min == quant_max) + add_zero_advance_check = false; + if (dbuf_insert(&s->byte_code, last_atom_start, 6 + add_zero_advance_check * 2)) + goto out_of_memory; + /* Note: we assume the string length is < INT32_MAX */ + pos = last_atom_start; + s->byte_code.buf[pos++] = REOP_set_i32; + s->byte_code.buf[pos++] = 0; + put_u32(s->byte_code.buf + pos, quant_max); + pos += 4; + last_atom_start = pos; + if (add_zero_advance_check) { + s->byte_code.buf[pos++] = REOP_set_char_pos; + s->byte_code.buf[pos++] = 0; } - if (quant_max == INT32_MAX) { - pos = s->byte_code.size; - re_emit_op_u32(s, REOP_split_goto_first + greedy, - len + 5 + add_zero_advance_check * 2); - if (add_zero_advance_check) - re_emit_op(s, REOP_push_char_pos); - /* copy the atom */ - dbuf_put_self(&s->byte_code, last_atom_start, len); - if (add_zero_advance_check) - re_emit_op(s, REOP_check_advance); - re_emit_goto(s, REOP_goto, pos); - } else if (quant_max > quant_min) { - re_emit_op_u32(s, REOP_push_i32, quant_max - quant_min); - pos = s->byte_code.size; - re_emit_op_u32(s, REOP_split_goto_first + greedy, - len + 5 + add_zero_advance_check * 2); - if (add_zero_advance_check) - re_emit_op(s, REOP_push_char_pos); - /* copy the atom */ - dbuf_put_self(&s->byte_code, last_atom_start, len); - if (add_zero_advance_check) - re_emit_op(s, REOP_check_advance); - re_emit_goto(s, REOP_loop, pos); - re_emit_op(s, REOP_drop); + if (quant_min == quant_max) { + /* a simple loop is enough */ + re_emit_goto_u8(s, REOP_loop, 0, last_atom_start); + } else { + re_emit_goto_u8_u32(s, (add_zero_advance_check ? REOP_loop_check_adv_split_next_first : REOP_loop_split_next_first) - greedy, 0, quant_max - quant_min, last_atom_start); } } last_atom_start = -1; @@ -76059,7 +81622,6 @@ static int re_parse_term(REParseState *s, bool is_backward_dir) break; } } - done: s->buf_ptr = p; return 0; out_of_memory: @@ -76072,8 +81634,6 @@ static int re_parse_alternative(REParseState *s, bool is_backward_dir) int ret; size_t start, term_start, end, term_size; - if (lre_check_size(s)) - return -1; start = s->byte_code.size; for(;;) { p = s->buf_ptr; @@ -76091,7 +81651,7 @@ static int re_parse_alternative(REParseState *s, bool is_backward_dir) end = s->byte_code.size; term_size = end - term_start; if (dbuf_claim(&s->byte_code, term_size)) - return -1; + return re_parse_out_of_memory(s); memmove(s->byte_code.buf + start + term_size, s->byte_code.buf + start, end - start); @@ -76108,8 +81668,7 @@ static int re_parse_disjunction(REParseState *s, bool is_backward_dir) if (lre_check_stack_overflow(s->opaque, 0)) return re_parse_error(s, "stack overflow"); - if (lre_check_size(s)) - return -1; + start = s->byte_code.size; if (re_parse_alternative(s, is_backward_dir)) return -1; @@ -76127,18 +81686,27 @@ static int re_parse_disjunction(REParseState *s, bool is_backward_dir) pos = re_emit_op_u32(s, REOP_goto, 0); + if (re_has_named_captures(s) && s->group_name_scope == GROUP_NAME_SCOPE_MAX) + return re_parse_error(s, "too many named groups"); + + s->group_name_scope++; + if (re_parse_alternative(s, is_backward_dir)) return -1; - /* patch the goto */ + /* patch the goto ('pos' is only valid if the placeholder for the + offset could be emitted) */ + if (dbuf_error(&s->byte_code)) + return re_parse_out_of_memory(s); len = s->byte_code.size - (pos + 4); put_u32(s->byte_code.buf + pos, len); } return 0; } -/* the control flow is recursive so the analysis can be linear */ -static int lre_compute_stack_size(const uint8_t *bc_buf, int bc_buf_len) +/* Allocate the registers as a stack. The control flow is recursive so + the analysis can be linear. */ +static int compute_register_count(uint8_t *bc_buf, int bc_buf_len) { int stack_size, stack_size_max, pos, opcode, len; uint32_t val; @@ -76154,34 +81722,64 @@ static int lre_compute_stack_size(const uint8_t *bc_buf, int bc_buf_len) assert(opcode < REOP_COUNT); assert((pos + len) <= bc_buf_len); switch(opcode) { - case REOP_push_i32: - case REOP_push_char_pos: + case REOP_set_i32: + case REOP_set_char_pos: + bc_buf[pos + 1] = stack_size; stack_size++; if (stack_size > stack_size_max) { - if (stack_size > STACK_SIZE_MAX) + if (stack_size > REGISTER_COUNT_MAX) return -1; stack_size_max = stack_size; } break; - case REOP_drop: case REOP_check_advance: + case REOP_loop: + case REOP_loop_split_goto_first: + case REOP_loop_split_next_first: assert(stack_size > 0); stack_size--; + bc_buf[pos + 1] = stack_size; + break; + case REOP_loop_check_adv_split_goto_first: + case REOP_loop_check_adv_split_next_first: + assert(stack_size >= 2); + stack_size -= 2; + bc_buf[pos + 1] = stack_size; break; case REOP_range: + case REOP_range_i: val = get_u16(bc_buf + pos + 1); len += val * 4; break; case REOP_range32: + case REOP_range32_i: val = get_u16(bc_buf + pos + 1); len += val * 8; break; + case REOP_back_reference: + case REOP_back_reference_i: + case REOP_backward_back_reference: + case REOP_backward_back_reference_i: + val = bc_buf[pos + 1]; + len += val; + break; } pos += len; } return stack_size_max; } +static void *lre_bytecode_realloc(void *opaque, void *ptr, size_t size) +{ + if (size > (INT32_MAX / 2)) { + /* the bytecode cannot be larger than 2G. Leave some slack to + avoid some overflows. */ + return NULL; + } else { + return lre_realloc(opaque, ptr, size); + } +} + /* 'buf' must be a zero terminated UTF-8 string of length buf_len. Return NULL if error and allocate an error message in *perror_msg, otherwise the compiled bytecode and its length in plen. @@ -76191,7 +81789,7 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size, void *opaque) { REParseState s_s, *s = &s_s; - int stack_size; + int register_count; bool is_sticky; memset(s, 0, sizeof(*s)); @@ -76200,16 +81798,17 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size, s->buf_end = s->buf_ptr + buf_len; s->buf_start = s->buf_ptr; s->re_flags = re_flags; - s->is_unicode = ((re_flags & LRE_FLAG_UNICODE) != 0); + s->is_unicode = ((re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0); is_sticky = ((re_flags & LRE_FLAG_STICKY) != 0); s->ignore_case = ((re_flags & LRE_FLAG_IGNORECASE) != 0); + s->multi_line = ((re_flags & LRE_FLAG_MULTILINE) != 0); s->dotall = ((re_flags & LRE_FLAG_DOTALL) != 0); s->unicode_sets = ((re_flags & LRE_FLAG_UNICODE_SETS) != 0); s->capture_count = 1; s->total_capture_count = -1; s->has_named_captures = -1; - dbuf_init2(&s->byte_code, opaque, lre_realloc); + dbuf_init2(&s->byte_code, opaque, lre_bytecode_realloc); dbuf_init2(&s->group_names, opaque, lre_realloc); dbuf_put_u16(&s->byte_code, re_flags); /* first element is the flags */ @@ -76251,22 +81850,25 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size, goto error; } - stack_size = lre_compute_stack_size(s->byte_code.buf, s->byte_code.size); - if (stack_size < 0) { + register_count = compute_register_count(s->byte_code.buf, s->byte_code.size); + if (register_count < 0) { re_parse_error(s, "too many imbricated quantifiers"); goto error; } s->byte_code.buf[RE_HEADER_CAPTURE_COUNT] = s->capture_count; - s->byte_code.buf[RE_HEADER_STACK_SIZE] = stack_size; + s->byte_code.buf[RE_HEADER_REGISTER_COUNT] = register_count; put_u32(s->byte_code.buf + RE_HEADER_BYTECODE_LEN, s->byte_code.size - RE_HEADER_LEN); /* add the named groups if needed */ - if (s->group_names.size > (s->capture_count - 1)) { - dbuf_put(&s->byte_code, s->group_names.buf, s->group_names.size); + if (s->group_names.size > (s->capture_count - 1) * LRE_GROUP_NAME_TRAILER_LEN) { + if (dbuf_put(&s->byte_code, s->group_names.buf, s->group_names.size)) { + re_parse_out_of_memory(s); + goto error; + } put_u16(s->byte_code.buf + RE_HEADER_FLAGS, - LRE_FLAG_NAMED_GROUPS | lre_get_flags(s->byte_code.buf)); + lre_get_flags(s->byte_code.buf) | LRE_FLAG_NAMED_GROUPS); } dbuf_free(&s->group_names); @@ -76284,14 +81886,6 @@ static bool is_line_terminator(uint32_t c) return (c == '\n' || c == '\r' || c == CP_LS || c == CP_PS); } -static bool is_word_char(uint32_t c) -{ - return ((c >= '0' && c <= '9') || - (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c == '_')); -} - #define GET_CHAR(c, cptr, cbuf_end, cbuf_type) \ do { \ if (cbuf_type == 0) { \ @@ -76300,11 +81894,11 @@ static bool is_word_char(uint32_t c) const uint16_t *_p = (const uint16_t *)cptr; \ const uint16_t *_end = (const uint16_t *)cbuf_end; \ c = *_p++; \ - if (is_hi_surrogate(c)) \ - if (cbuf_type == 2) \ - if (_p < _end) \ - if (is_lo_surrogate(*_p)) \ - c = from_surrogate(c, *_p++); \ + if (is_hi_surrogate(c) && cbuf_type == 2) { \ + if (_p < _end && is_lo_surrogate(*_p)) { \ + c = from_surrogate(c, *_p++); \ + } \ + } \ cptr = (const void *)_p; \ } \ } while (0) @@ -76317,11 +81911,11 @@ static bool is_word_char(uint32_t c) const uint16_t *_p = (const uint16_t *)cptr; \ const uint16_t *_end = (const uint16_t *)cbuf_end; \ c = *_p++; \ - if (is_hi_surrogate(c)) \ - if (cbuf_type == 2) \ - if (_p < _end) \ - if (is_lo_surrogate(*_p)) \ - c = from_surrogate(c, *_p); \ + if (is_hi_surrogate(c) && cbuf_type == 2) { \ + if (_p < _end && is_lo_surrogate(*_p)) { \ + c = from_surrogate(c, *_p); \ + } \ + } \ } \ } while (0) @@ -76333,11 +81927,11 @@ static bool is_word_char(uint32_t c) const uint16_t *_p = (const uint16_t *)cptr - 1; \ const uint16_t *_start = (const uint16_t *)cbuf_start; \ c = *_p; \ - if (is_lo_surrogate(c)) \ - if (cbuf_type == 2) \ - if (_p > _start) \ - if (is_hi_surrogate(_p[-1])) \ - c = from_surrogate(*--_p, c); \ + if (is_lo_surrogate(c) && cbuf_type == 2) { \ + if (_p > _start && is_hi_surrogate(_p[-1])) { \ + c = from_surrogate(*--_p, c); \ + } \ + } \ } \ } while (0) @@ -76350,11 +81944,11 @@ static bool is_word_char(uint32_t c) const uint16_t *_p = (const uint16_t *)cptr - 1; \ const uint16_t *_start = (const uint16_t *)cbuf_start; \ c = *_p; \ - if (is_lo_surrogate(c)) \ - if (cbuf_type == 2) \ - if (_p > _start) \ - if (is_hi_surrogate(_p[-1])) \ - c = from_surrogate(*--_p, c); \ + if (is_lo_surrogate(c) && cbuf_type == 2) { \ + if (_p > _start && is_hi_surrogate(_p[-1])) { \ + c = from_surrogate(*--_p, c); \ + } \ + } \ cptr = (const void *)_p; \ } \ } while (0) @@ -76366,32 +81960,35 @@ static bool is_word_char(uint32_t c) } else { \ const uint16_t *_p = (const uint16_t *)cptr - 1; \ const uint16_t *_start = (const uint16_t *)cbuf_start; \ - if (is_lo_surrogate(*_p)) \ - if (cbuf_type == 2) \ - if (_p > _start) \ - if (is_hi_surrogate(_p[-1])) \ - _p--; \ + if (is_lo_surrogate(*_p) && cbuf_type == 2) { \ + if (_p > _start && is_hi_surrogate(_p[-1])) { \ + --_p; \ + } \ + } \ cptr = (const void *)_p; \ } \ } while (0) -typedef uintptr_t StackInt; - typedef enum { RE_EXEC_STATE_SPLIT, RE_EXEC_STATE_LOOKAHEAD, RE_EXEC_STATE_NEGATIVE_LOOKAHEAD, - RE_EXEC_STATE_GREEDY_QUANT, } REExecStateEnum; -typedef struct REExecState { - REExecStateEnum type : 8; - uint8_t stack_len; - size_t count; /* only used for RE_EXEC_STATE_GREEDY_QUANT */ - const uint8_t *cptr; - const uint8_t *pc; - void *buf[]; -} REExecState; +#if INTPTR_MAX >= INT64_MAX +#define BP_TYPE_BITS 3 +#else +#define BP_TYPE_BITS 2 +#endif + +typedef union { + uint8_t *ptr; + intptr_t val; /* for bp, the low BP_SHIFT bits store REExecStateEnum */ + struct { + uintptr_t val : sizeof(uintptr_t) * 8 - BP_TYPE_BITS; + uintptr_t type : BP_TYPE_BITS; + } bp; +} StackElem; typedef struct { const uint8_t *cbuf; @@ -76399,57 +81996,15 @@ typedef struct { /* 0 = 8 bit chars, 1 = 16 bit chars, 2 = 16 bit chars, UTF-16 */ int cbuf_type; int capture_count; - int stack_size_max; - bool multi_line; - bool ignore_case; bool is_unicode; int interrupt_counter; void *opaque; /* used for stack overflow check */ - size_t state_size; - uint8_t *state_stack; - size_t state_stack_size; - size_t state_stack_len; + StackElem *stack_buf; + size_t stack_size; + StackElem static_stack_buf[32]; /* static stack to avoid allocation in most cases */ } REExecContext; -static int push_state(REExecContext *s, - uint8_t **capture, - StackInt *stack, size_t stack_len, - const uint8_t *pc, const uint8_t *cptr, - REExecStateEnum type, size_t count) -{ - REExecState *rs; - uint8_t *new_stack; - size_t new_size, i, n; - StackInt *stack_buf; - - if (unlikely((s->state_stack_len + 1) > s->state_stack_size)) { - /* reallocate the stack */ - new_size = s->state_stack_size * 3 / 2; - if (new_size < 8) - new_size = 8; - new_stack = lre_realloc(s->opaque, s->state_stack, new_size * s->state_size); - if (!new_stack) - return -1; - s->state_stack_size = new_size; - s->state_stack = new_stack; - } - rs = (REExecState *)(s->state_stack + s->state_stack_len * s->state_size); - s->state_stack_len++; - rs->type = type; - rs->count = count; - rs->stack_len = stack_len; - rs->cptr = cptr; - rs->pc = pc; - n = 2 * s->capture_count; - for(i = 0; i < n; i++) - rs->buf[i] = capture[i]; - stack_buf = (StackInt *)(rs->buf + n); - for(i = 0; i < stack_len; i++) - stack_buf[i] = stack[i]; - return 0; -} - static int lre_poll_timeout(REExecContext *s) { if (unlikely(--s->interrupt_counter <= 0)) { @@ -76460,111 +82015,196 @@ static int lre_poll_timeout(REExecContext *s) return 0; } +static no_inline int stack_realloc(REExecContext *s, size_t n) +{ + StackElem *new_stack; + size_t new_size; + new_size = s->stack_size * 3 / 2; + if (new_size < n) + new_size = n; + if (s->stack_buf == s->static_stack_buf) { + new_stack = lre_realloc(s->opaque, NULL, new_size * sizeof(StackElem)); + if (!new_stack) + return -1; + /* XXX: could use correct size */ + memcpy(new_stack, s->stack_buf, s->stack_size * sizeof(StackElem)); + } else { + new_stack = lre_realloc(s->opaque, s->stack_buf, new_size * sizeof(StackElem)); + if (!new_stack) + return -1; + } + s->stack_size = new_size; + s->stack_buf = new_stack; + return 0; +} + /* return 1 if match, 0 if not match or < 0 if error. */ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, - StackInt *stack, int stack_len, - const uint8_t *pc, const uint8_t *cptr, - bool no_recurse) + const uint8_t *pc, const uint8_t *cptr) { - int opcode, ret; + int opcode; int cbuf_type; - uint32_t val, c; + uint32_t val, c, idx; const uint8_t *cbuf_end; - + StackElem *sp, *bp, *stack_end; +#ifdef DUMP_EXEC + const uint8_t *pc_start = pc; /* TEST */ +#endif cbuf_type = s->cbuf_type; cbuf_end = s->cbuf_end; + sp = s->stack_buf; + bp = s->stack_buf; + stack_end = s->stack_buf + s->stack_size; + +#define CHECK_STACK_SPACE(n) \ + if (unlikely((stack_end - sp) < (n))) { \ + size_t saved_sp = sp - s->stack_buf; \ + size_t saved_bp = bp - s->stack_buf; \ + if (stack_realloc(s, sp - s->stack_buf + (n))) \ + return LRE_RET_MEMORY_ERROR; \ + stack_end = s->stack_buf + s->stack_size; \ + sp = s->stack_buf + saved_sp; \ + bp = s->stack_buf + saved_bp; \ + } + + /* XXX: could test if the value was saved to reduce the stack size + but slower */ +#define SAVE_CAPTURE(idx, value) \ + { \ + CHECK_STACK_SPACE(2); \ + sp[0].val = idx; \ + sp[1].ptr = capture[idx]; \ + sp += 2; \ + capture[idx] = (value); \ + } + + /* avoid saving the previous value if already saved */ +#define SAVE_CAPTURE_CHECK(idx, value) \ + { \ + StackElem *sp1; \ + sp1 = sp; \ + for(;;) { \ + if (sp1 > bp) { \ + if (sp1[-2].val == idx) \ + break; \ + sp1 -= 2; \ + } else { \ + CHECK_STACK_SPACE(2); \ + sp[0].val = idx; \ + sp[1].ptr = capture[idx]; \ + sp += 2; \ + break; \ + } \ + } \ + capture[idx] = (value); \ + } + + +#ifdef DUMP_EXEC + printf("%5s %5s %5s %5s %s\n", "PC", "CP", "BP", "SP", "OPCODE"); +#endif for(;;) { - // printf("top=%p: pc=%d\n", th_list.top, (int)(pc - (bc_buf + RE_HEADER_LEN))); opcode = *pc++; +#ifdef DUMP_EXEC + printf("%5ld %5ld %5ld %5ld %s\n", + pc - 1 - pc_start, + cbuf_type == 0 ? cptr - s->cbuf : (cptr - s->cbuf) / 2, + bp - s->stack_buf, + sp - s->stack_buf, + reopcode_info[opcode].name); +#endif switch(opcode) { case REOP_match: - { - REExecState *rs; - if (no_recurse) - return (intptr_t)cptr; - ret = 1; - goto recurse; - no_match: - if (no_recurse) + return 1; + no_match: + for(;;) { + REExecStateEnum type; + if (bp == s->stack_buf) return 0; - ret = 0; - recurse: + /* undo the modifications to capture[] */ + while (sp > bp) { + capture[sp[-2].val] = sp[-1].ptr; + sp -= 2; + } + + pc = sp[-3].ptr; + cptr = sp[-2].ptr; + type = sp[-1].bp.type; + bp = s->stack_buf + sp[-1].bp.val; + sp -= 3; + if (type != RE_EXEC_STATE_LOOKAHEAD) + break; + } + if (lre_poll_timeout(s)) + return LRE_RET_TIMEOUT; + break; + case REOP_lookahead_match: + /* pop all the saved states until reaching the start of + the lookahead and keep the updated captures and + variables and the corresponding undo info. */ + { + StackElem *sp1, *sp_top, *next_sp; + REExecStateEnum type; + + sp_top = sp; for(;;) { - if (lre_poll_timeout(s)) - return LRE_RET_TIMEOUT; - if (s->state_stack_len == 0) - return ret; - rs = (REExecState *)(s->state_stack + - (s->state_stack_len - 1) * s->state_size); - if (rs->type == RE_EXEC_STATE_SPLIT) { - if (!ret) { - pop_state: - memcpy(capture, rs->buf, - sizeof(capture[0]) * 2 * s->capture_count); - pop_state1: - pc = rs->pc; - cptr = rs->cptr; - stack_len = rs->stack_len; - memcpy(stack, rs->buf + 2 * s->capture_count, - stack_len * sizeof(stack[0])); - s->state_stack_len--; - break; - } - } else if (rs->type == RE_EXEC_STATE_GREEDY_QUANT) { - if (!ret) { - uint32_t char_count, i; - memcpy(capture, rs->buf, - sizeof(capture[0]) * 2 * s->capture_count); - stack_len = rs->stack_len; - memcpy(stack, rs->buf + 2 * s->capture_count, - stack_len * sizeof(stack[0])); - pc = rs->pc; - cptr = rs->cptr; - /* go backward */ - char_count = get_u32(pc + 12); - for(i = 0; i < char_count; i++) { - PREV_CHAR(cptr, s->cbuf, cbuf_type); - } - pc = (pc + 16) + (int)get_u32(pc); - rs->cptr = cptr; - rs->count--; - if (rs->count == 0) { - s->state_stack_len--; - } - break; - } - } else { - ret = ((rs->type == RE_EXEC_STATE_LOOKAHEAD && ret) || - (rs->type == RE_EXEC_STATE_NEGATIVE_LOOKAHEAD && !ret)); - if (ret) { - /* keep the capture in case of positive lookahead */ - if (rs->type == RE_EXEC_STATE_LOOKAHEAD) - goto pop_state1; - else - goto pop_state; - } + sp1 = sp; + sp = bp; + pc = sp[-3].ptr; + cptr = sp[-2].ptr; + type = sp[-1].bp.type; + bp = s->stack_buf + sp[-1].bp.val; + sp[-1].ptr = (void *)sp1; /* save the next value for the copy step */ + sp -= 3; + if (type == RE_EXEC_STATE_LOOKAHEAD) + break; + } + if (sp != s->stack_buf) { + /* keep the undo info if there is a saved state */ + sp1 = sp; + while (sp1 < sp_top) { + next_sp = (void *)sp1[2].ptr; + sp1 += 3; + while (sp1 < next_sp) + *sp++ = *sp1++; } - s->state_stack_len--; } } break; + case REOP_negative_lookahead_match: + /* pop all the saved states until reaching start of the negative lookahead */ + for(;;) { + REExecStateEnum type; + type = bp[-1].bp.type; + /* undo the modifications to capture[] */ + while (sp > bp) { + capture[sp[-2].val] = sp[-1].ptr; + sp -= 2; + } + pc = sp[-3].ptr; + cptr = sp[-2].ptr; + type = sp[-1].bp.type; + bp = s->stack_buf + sp[-1].bp.val; + sp -= 3; + if (type == RE_EXEC_STATE_NEGATIVE_LOOKAHEAD) + break; + } + goto no_match; case REOP_char32: + case REOP_char32_i: val = get_u32(pc); pc += 4; goto test_char; - case REOP_char16: + case REOP_char: + case REOP_char_i: val = get_u16(pc); pc += 2; - goto test_char; - case REOP_char8: - val = get_u8(pc); - pc += 1; test_char: if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end, cbuf_type); - if (s->ignore_case) { + if (opcode == REOP_char_i || opcode == REOP_char32_i) { c = lre_canonicalize(c, s->is_unicode); } if (val != c) @@ -76583,24 +82223,27 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, pc1 = pc; pc = pc + (int)val; } - ret = push_state(s, capture, stack, stack_len, - pc1, cptr, RE_EXEC_STATE_SPLIT, 0); - if (ret < 0) - return LRE_RET_MEMORY_ERROR; - break; + CHECK_STACK_SPACE(3); + sp[0].ptr = (uint8_t *)pc1; + sp[1].ptr = (uint8_t *)cptr; + sp[2].bp.val = bp - s->stack_buf; + sp[2].bp.type = RE_EXEC_STATE_SPLIT; + sp += 3; + bp = sp; } + break; case REOP_lookahead: case REOP_negative_lookahead: val = get_u32(pc); pc += 4; - ret = push_state(s, capture, stack, stack_len, - pc + (int)val, cptr, - RE_EXEC_STATE_LOOKAHEAD + opcode - REOP_lookahead, - 0); - if (ret < 0) - return LRE_RET_MEMORY_ERROR; + CHECK_STACK_SPACE(3); + sp[0].ptr = (uint8_t *)(pc + (int)val); + sp[1].ptr = (uint8_t *)cptr; + sp[2].bp.val = bp - s->stack_buf; + sp[2].bp.type = RE_EXEC_STATE_LOOKAHEAD + opcode - REOP_lookahead; + sp += 3; + bp = sp; break; - case REOP_goto: val = get_u32(pc); pc += 4 + (int)val; @@ -76608,18 +82251,20 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, return LRE_RET_TIMEOUT; break; case REOP_line_start: + case REOP_line_start_m: if (cptr == s->cbuf) break; - if (!s->multi_line) + if (opcode == REOP_line_start) goto no_match; PEEK_PREV_CHAR(c, cptr, s->cbuf, cbuf_type); if (!is_line_terminator(c)) goto no_match; break; case REOP_line_end: + case REOP_line_end_m: if (cptr == cbuf_end) break; - if (!s->multi_line) + if (opcode == REOP_line_end) goto no_match; PEEK_CHAR(c, cptr, cbuf_end, cbuf_type); if (!is_line_terminator(c)) @@ -76637,12 +82282,27 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, goto no_match; GET_CHAR(c, cptr, cbuf_end, cbuf_type); break; + case REOP_space: + if (cptr == cbuf_end) + goto no_match; + GET_CHAR(c, cptr, cbuf_end, cbuf_type); + if (!lre_is_space(c)) + goto no_match; + break; + case REOP_not_space: + if (cptr == cbuf_end) + goto no_match; + GET_CHAR(c, cptr, cbuf_end, cbuf_type); + if (lre_is_space(c)) + goto no_match; + break; case REOP_save_start: case REOP_save_end: val = *pc++; - if (val >= s->capture_count) + if (val >= (uint32_t)s->capture_count) return LRE_RET_BYTECODE_ERROR; - capture[2 * val + opcode - REOP_save_start] = (uint8_t *)cptr; + idx = 2 * val + opcode - REOP_save_start; + SAVE_CAPTURE(idx, (uint8_t *)cptr); break; case REOP_save_reset: { @@ -76650,106 +82310,194 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, val = pc[0]; val2 = pc[1]; pc += 2; - if (val2 >= s->capture_count) + if (val2 >= (uint32_t)s->capture_count) return LRE_RET_BYTECODE_ERROR; + CHECK_STACK_SPACE(2 * (val2 - val + 1)); while (val <= val2) { - capture[2 * val] = NULL; - capture[2 * val + 1] = NULL; + idx = 2 * val; + SAVE_CAPTURE(idx, NULL); + idx = 2 * val + 1; + SAVE_CAPTURE(idx, NULL); val++; } } break; - case REOP_push_i32: - val = get_u32(pc); - pc += 4; - stack[stack_len++] = val; - break; - case REOP_drop: - stack_len--; + case REOP_set_i32: + idx = 2 * s->capture_count + pc[0]; + val = get_u32(pc + 1); + pc += 5; + SAVE_CAPTURE_CHECK(idx, (void *)(uintptr_t)val); break; case REOP_loop: - val = get_u32(pc); - pc += 4; - if (--stack[stack_len - 1] != 0) { - pc += (int)val; - if (lre_poll_timeout(s)) - return LRE_RET_TIMEOUT; + { + uint32_t val2; + idx = 2 * s->capture_count + pc[0]; + val = get_u32(pc + 1); + pc += 5; + + val2 = (uintptr_t)capture[idx] - 1; + SAVE_CAPTURE_CHECK(idx, (void *)(uintptr_t)val2); + if (val2 != 0) { + pc += (int)val; + if (lre_poll_timeout(s)) + return LRE_RET_TIMEOUT; + } + } + break; + case REOP_loop_split_goto_first: + case REOP_loop_split_next_first: + case REOP_loop_check_adv_split_goto_first: + case REOP_loop_check_adv_split_next_first: + { + const uint8_t *pc1; + uint32_t val2, limit; + idx = 2 * s->capture_count + pc[0]; + limit = get_u32(pc + 1); + val = get_u32(pc + 5); + pc += 9; + + /* decrement the counter */ + val2 = (uintptr_t)capture[idx] - 1; + SAVE_CAPTURE_CHECK(idx, (void *)(uintptr_t)val2); + + if (val2 > limit) { + /* normal loop if counter > limit */ + pc += (int)val; + if (lre_poll_timeout(s)) + return LRE_RET_TIMEOUT; + } else { + /* check advance */ + if ((opcode == REOP_loop_check_adv_split_goto_first || + opcode == REOP_loop_check_adv_split_next_first) && + capture[idx + 1] == cptr && + val2 != limit) { + goto no_match; + } + + /* otherwise conditional split */ + if (val2 != 0) { + if (opcode == REOP_loop_split_next_first || + opcode == REOP_loop_check_adv_split_next_first) { + pc1 = pc + (int)val; + } else { + pc1 = pc; + pc = pc + (int)val; + } + CHECK_STACK_SPACE(3); + sp[0].ptr = (uint8_t *)pc1; + sp[1].ptr = (uint8_t *)cptr; + sp[2].bp.val = bp - s->stack_buf; + sp[2].bp.type = RE_EXEC_STATE_SPLIT; + sp += 3; + bp = sp; + } + } } break; - case REOP_push_char_pos: - stack[stack_len++] = (uintptr_t)cptr; + case REOP_set_char_pos: + idx = 2 * s->capture_count + pc[0]; + pc++; + SAVE_CAPTURE_CHECK(idx, (uint8_t *)cptr); break; case REOP_check_advance: - if (stack[--stack_len] == (uintptr_t)cptr) + idx = 2 * s->capture_count + pc[0]; + pc++; + if (capture[idx] == cptr) goto no_match; break; case REOP_word_boundary: + case REOP_word_boundary_i: case REOP_not_word_boundary: + case REOP_not_word_boundary_i: { bool v1, v2; + int ignore_case = (opcode == REOP_word_boundary_i || opcode == REOP_not_word_boundary_i); + bool is_boundary = (opcode == REOP_word_boundary || opcode == REOP_word_boundary_i); /* char before */ if (cptr == s->cbuf) { v1 = false; } else { PEEK_PREV_CHAR(c, cptr, s->cbuf, cbuf_type); - v1 = is_word_char(c); + if (c < 256) { + v1 = (lre_is_word_byte(c) != 0); + } else { + v1 = ignore_case && (c == 0x017f || c == 0x212a); + } } /* current char */ if (cptr >= cbuf_end) { v2 = false; } else { PEEK_CHAR(c, cptr, cbuf_end, cbuf_type); - v2 = is_word_char(c); + if (c < 256) { + v2 = (lre_is_word_byte(c) != 0); + } else { + v2 = ignore_case && (c == 0x017f || c == 0x212a); + } } - if (v1 ^ v2 ^ (REOP_not_word_boundary - opcode)) + if (v1 ^ v2 ^ is_boundary) goto no_match; } break; case REOP_back_reference: + case REOP_back_reference_i: case REOP_backward_back_reference: + case REOP_backward_back_reference_i: { const uint8_t *cptr1, *cptr1_end, *cptr1_start; + const uint8_t *pc1; uint32_t c1, c2; + int i, n; - val = *pc++; - if (val >= s->capture_count) - goto no_match; - cptr1_start = capture[2 * val]; - cptr1_end = capture[2 * val + 1]; - if (!cptr1_start || !cptr1_end) - break; - if (opcode == REOP_back_reference) { - cptr1 = cptr1_start; - while (cptr1 < cptr1_end) { - if (cptr >= cbuf_end) - goto no_match; - GET_CHAR(c1, cptr1, cptr1_end, cbuf_type); - GET_CHAR(c2, cptr, cbuf_end, cbuf_type); - if (s->ignore_case) { - c1 = lre_canonicalize(c1, s->is_unicode); - c2 = lre_canonicalize(c2, s->is_unicode); - } - if (c1 != c2) - goto no_match; - } - } else { - cptr1 = cptr1_end; - while (cptr1 > cptr1_start) { - if (cptr == s->cbuf) - goto no_match; - GET_PREV_CHAR(c1, cptr1, cptr1_start, cbuf_type); - GET_PREV_CHAR(c2, cptr, s->cbuf, cbuf_type); - if (s->ignore_case) { - c1 = lre_canonicalize(c1, s->is_unicode); - c2 = lre_canonicalize(c2, s->is_unicode); + n = *pc++; + pc1 = pc; + pc += n; + + for(i = 0; i < n; i++) { + val = pc1[i]; + if (val >= s->capture_count) + goto no_match; + cptr1_start = capture[2 * val]; + cptr1_end = capture[2 * val + 1]; + /* test the first not empty capture */ + if (cptr1_start && cptr1_end) { + if (opcode == REOP_back_reference || + opcode == REOP_back_reference_i) { + cptr1 = cptr1_start; + while (cptr1 < cptr1_end) { + if (cptr >= cbuf_end) + goto no_match; + GET_CHAR(c1, cptr1, cptr1_end, cbuf_type); + GET_CHAR(c2, cptr, cbuf_end, cbuf_type); + if (opcode == REOP_back_reference_i) { + c1 = lre_canonicalize(c1, s->is_unicode); + c2 = lre_canonicalize(c2, s->is_unicode); + } + if (c1 != c2) + goto no_match; + } + } else { + cptr1 = cptr1_end; + while (cptr1 > cptr1_start) { + if (cptr == s->cbuf) + goto no_match; + GET_PREV_CHAR(c1, cptr1, cptr1_start, cbuf_type); + GET_PREV_CHAR(c2, cptr, s->cbuf, cbuf_type); + if (opcode == REOP_backward_back_reference_i) { + c1 = lre_canonicalize(c1, s->is_unicode); + c2 = lre_canonicalize(c2, s->is_unicode); + } + if (c1 != c2) + goto no_match; + } } - if (c1 != c2) - goto no_match; + break; } } } break; case REOP_range: + case REOP_range_i: { int n; uint32_t low, high, idx_min, idx_max, idx; @@ -76759,7 +82507,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end, cbuf_type); - if (s->ignore_case) { + if (opcode == REOP_range_i) { c = lre_canonicalize(c, s->is_unicode); } idx_min = 0; @@ -76790,6 +82538,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, } break; case REOP_range32: + case REOP_range32_i: { int n; uint32_t low, high, idx_min, idx_max, idx; @@ -76799,7 +82548,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end, cbuf_type); - if (s->ignore_case) { + if (opcode == REOP_range32_i) { c = lre_canonicalize(c, s->is_unicode); } idx_min = 0; @@ -76832,50 +82581,10 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, goto no_match; PREV_CHAR(cptr, s->cbuf, cbuf_type); break; - case REOP_simple_greedy_quant: - { - uint32_t next_pos, quant_min, quant_max; - size_t q; - intptr_t res; - const uint8_t *pc1; - - next_pos = get_u32(pc); - quant_min = get_u32(pc + 4); - quant_max = get_u32(pc + 8); - pc += 16; - pc1 = pc; - pc += (int)next_pos; - - q = 0; - for(;;) { - if (lre_poll_timeout(s)) - return LRE_RET_TIMEOUT; - res = lre_exec_backtrack(s, capture, stack, stack_len, - pc1, cptr, true); - if (res == LRE_RET_MEMORY_ERROR || - res == LRE_RET_TIMEOUT) - return res; - if (!res) - break; - cptr = (uint8_t *)res; - q++; - if (q >= quant_max && quant_max != INT32_MAX) - break; - } - if (q < quant_min) - goto no_match; - if (q > quant_min) { - /* will examine all matches down to quant_min */ - ret = push_state(s, capture, stack, stack_len, - pc1 - 16, cptr, - RE_EXEC_STATE_GREEDY_QUANT, - q - quant_min); - if (ret < 0) - return LRE_RET_MEMORY_ERROR; - } - } - break; default: +#ifdef DUMP_EXEC + printf("unknown opcode pc=%ld\n", pc - 1 - pc_start); +#endif abort(); } } @@ -76889,15 +82598,12 @@ int lre_exec(uint8_t **capture, int cbuf_type, void *opaque) { REExecContext s_s, *s = &s_s; - int re_flags, i, alloca_size, ret; - StackInt *stack_buf; + int re_flags, i, ret; + const uint8_t *cptr; re_flags = lre_get_flags(bc_buf); - s->multi_line = (re_flags & LRE_FLAG_MULTILINE) != 0; - s->ignore_case = (re_flags & LRE_FLAG_IGNORECASE) != 0; - s->is_unicode = (re_flags & LRE_FLAG_UNICODE) != 0; + s->is_unicode = (re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0; s->capture_count = bc_buf[RE_HEADER_CAPTURE_COUNT]; - s->stack_size_max = bc_buf[RE_HEADER_STACK_SIZE]; s->cbuf = cbuf; s->cbuf_end = cbuf + (clen << cbuf_type); s->cbuf_type = cbuf_type; @@ -76906,23 +82612,47 @@ int lre_exec(uint8_t **capture, s->interrupt_counter = INTERRUPT_COUNTER_INIT; s->opaque = opaque; - s->state_size = sizeof(REExecState) + - s->capture_count * sizeof(capture[0]) * 2 + - s->stack_size_max * sizeof(stack_buf[0]); - s->state_stack = NULL; - s->state_stack_len = 0; - s->state_stack_size = 0; + s->stack_buf = s->static_stack_buf; + s->stack_size = countof(s->static_stack_buf); for(i = 0; i < s->capture_count * 2; i++) capture[i] = NULL; - alloca_size = s->stack_size_max * sizeof(stack_buf[0]); - stack_buf = alloca(alloca_size); - ret = lre_exec_backtrack(s, capture, stack_buf, 0, bc_buf + RE_HEADER_LEN, - cbuf + (cindex << cbuf_type), false); - lre_realloc(s->opaque, s->state_stack, 0); + + cptr = cbuf + (cindex << cbuf_type); + if (0 < cindex && cindex < clen && s->cbuf_type == 2) { + const uint16_t *p = (const uint16_t *)cptr; + if (is_lo_surrogate(*p) && is_hi_surrogate(p[-1])) { + cptr = (const uint8_t *)(p - 1); + } + } + + ret = lre_exec_backtrack(s, capture, bc_buf + RE_HEADER_LEN, cptr); + + if (s->stack_buf != s->static_stack_buf) + lre_realloc(s->opaque, s->stack_buf, 0); return ret; } +int lre_get_alloc_count(const uint8_t *bc_buf) +{ + return bc_buf[RE_HEADER_CAPTURE_COUNT] * 2 + + bc_buf[RE_HEADER_REGISTER_COUNT]; +} + +/* Structurally validate serialized regexp bytecode (e.g. from JS_ReadObject) + before it is executed: a present header and a body length that fits within + the buffer. Returns 0 on success, -1 if the bytecode is malformed. */ +int lre_check_bytecode(const uint8_t *bc_buf, int bc_buf_len) +{ + uint32_t re_bytecode_len; + if (bc_buf_len < RE_HEADER_LEN) + return -1; + re_bytecode_len = get_u32(bc_buf + RE_HEADER_BYTECODE_LEN); + if (re_bytecode_len > (uint32_t)(bc_buf_len - RE_HEADER_LEN)) + return -1; + return 0; +} + int lre_get_capture_count(const uint8_t *bc_buf) { return bc_buf[RE_HEADER_CAPTURE_COUNT]; @@ -76961,13 +82691,13 @@ int main(int argc, char **argv) int len, flags, ret, i; uint8_t *bc; char error_msg[64]; - uint8_t *capture[CAPTURE_COUNT_MAX * 2]; + uint8_t *capture; const char *input; int input_len, capture_count; if (argc < 4) { printf("usage: %s regexp flags input\n", argv[0]); - exit(1); + return 1; } flags = atoi(argv[2]); bc = lre_compile(&len, error_msg, sizeof(error_msg), argv[1], @@ -76980,6 +82710,7 @@ int main(int argc, char **argv) input = argv[3]; input_len = strlen(input); + capture = malloc(sizeof(capture[0]) * lre_get_alloc_count(bc)); ret = lre_exec(capture, bc, (uint8_t *)input, 0, input_len, 0, NULL); printf("ret=%d\n", ret); if (ret == 1) { @@ -76995,6 +82726,7 @@ int main(int argc, char **argv) printf("\n"); } } + free(capture); return 0; } #endif @@ -77492,6 +83224,9 @@ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len, case CR_OP_XOR: is_in = (a_idx & 1) ^ (b_idx & 1); break; + case CR_OP_SUB: + is_in = (a_idx & 1) & ((b_idx & 1) ^ 1); + break; default: abort(); } @@ -77504,14 +83239,14 @@ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len, return 0; } -int cr_union1(CharRange *cr, const uint32_t *b_pt, int b_len) +int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op) { CharRange a = *cr; int ret; cr->len = 0; cr->size = 0; cr->points = NULL; - ret = cr_op(cr, a.points, a.len, b_pt, b_len, CR_OP_UNION); + ret = cr_op(cr, a.points, a.len, b_pt, b_len, op); cr_free(&a); return ret; } @@ -77552,6 +83287,101 @@ bool lre_is_white_space(uint32_t c) sizeof(unicode_prop_White_Space_index) / 3); } +/*---- lre codepoint categorizing functions ----*/ + +#define S UNICODE_C_SPACE +#define D UNICODE_C_DIGIT +#define X UNICODE_C_XDIGIT +#define U UNICODE_C_UPPER +#define L UNICODE_C_LOWER +#define _ UNICODE_C_UNDER +#define d UNICODE_C_DOLLAR + +uint8_t const lre_ctype_bits[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, S, S, S, S, S, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + S, 0, 0, 0, d, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + X|D, X|D, X|D, X|D, X|D, X|D, X|D, X|D, + X|D, X|D, 0, 0, 0, 0, 0, 0, + + 0, X|U, X|U, X|U, X|U, X|U, X|U, U, + U, U, U, U, U, U, U, U, + U, U, U, U, U, U, U, U, + U, U, U, 0, 0, 0, 0, _, + + 0, X|L, X|L, X|L, X|L, X|L, X|L, L, + L, L, L, L, L, L, L, L, + L, L, L, L, L, L, L, L, + L, L, L, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + S, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +}; + +#undef S +#undef D +#undef X +#undef U +#undef L +#undef _ +#undef d + +/* code point ranges for Zs,Zl or Zp property */ +static const uint16_t char_range_space[] = { + 10, + 0x0009, 0x000D + 1, + 0x0020, 0x0020 + 1, + 0x00A0, 0x00A0 + 1, + 0x1680, 0x1680 + 1, + 0x2000, 0x200A + 1, + /* 2028;LINE SEPARATOR;Zl;0;WS;;;;;N;;;;; */ + /* 2029;PARAGRAPH SEPARATOR;Zp;0;B;;;;;N;;;;; */ + 0x2028, 0x2029 + 1, + 0x202F, 0x202F + 1, + 0x205F, 0x205F + 1, + 0x3000, 0x3000 + 1, + /* FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;; */ + 0xFEFF, 0xFEFF + 1, +}; + +int lre_is_space_non_ascii(uint32_t c) +{ + size_t i, n; + + n = countof(char_range_space); + for(i = 5; i < n; i += 2) { + uint32_t low = char_range_space[i]; + uint32_t high = char_range_space[i + 1]; + if (c < low) + return false; + if (c < high) + return true; + } + return false; +} + + #define UNICODE_DECOMP_LEN_MAX 18 typedef enum { @@ -77955,6 +83785,11 @@ int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len, int *buf, buf_len, i, p, starter_pos, cc, last_cc, out_len; bool is_compat; DynBuf dbuf_s, *dbuf = &dbuf_s; + + if (src_len == 0) { + *pdst = NULL; + return 0; + } is_compat = n_type >> 1; @@ -78062,11 +83897,17 @@ int unicode_script(CharRange *cr, CharRange cr2_s = { 0 }, *cr2 = &cr2_s; bool is_common; - script_idx = unicode_find_name(unicode_script_name_table, script_name); - if (script_idx < 0) - return -2; - /* Note: we remove the "Unknown" Script */ - script_idx += UNICODE_SCRIPT_Unknown + 1; + if (!strcmp(script_name, "Unknown") || !strcmp(script_name, "Zzzz")) { + /* "Unknown" (ISO 15924 code "Zzzz") is excluded from the name table; + it matches every character outside any script. */ + script_idx = UNICODE_SCRIPT_Unknown; + } else { + script_idx = unicode_find_name(unicode_script_name_table, script_name); + if (script_idx < 0) + return -2; + /* Note: the name table excludes the "Unknown" Script */ + script_idx += UNICODE_SCRIPT_Unknown + 1; + } is_common = (script_idx == UNICODE_SCRIPT_Common || script_idx == UNICODE_SCRIPT_Inherited); @@ -78101,13 +83942,22 @@ int unicode_script(CharRange *cr, else v = *p++; c1 = c + n + 1; - if (v == script_idx) { + /* only script-bearing ranges (type != 0); for Unknown, match every + such range and invert afterwards */ + if (type != 0 && + (v == script_idx || script_idx == UNICODE_SCRIPT_Unknown)) { if (cr_add_interval(cr1, c, c1)) goto fail; } c = c1; } + if (script_idx == UNICODE_SCRIPT_Unknown) { + /* Unknown is all the characters outside scripts */ + if (cr_invert(cr1)) + goto fail; + } + if (is_ext) { /* add the script extensions */ p = unicode_script_ext_table; @@ -78167,7 +84017,7 @@ int unicode_script(CharRange *cr, cr_free(cr1); cr_free(cr2); } - goto fail; + return -1; } #define M(id) (1U << UNICODE_GC_ ## id) @@ -78586,6 +84436,216 @@ int unicode_general_category(CharRange *cr, const char *gc_name) /* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2 if not found */ +/* Unicode "properties of strings" (e.g. \p{RGI_Emoji}) used by the v flag; + expands a string property into a list of code-point sequences via the + callback. Returns -2 if the property name is unknown. */ +#define SEQ_MAX_LEN 16 + +static int unicode_sequence_prop1(int seq_prop_idx, UnicodeSequencePropCB *cb, void *opaque, + CharRange *cr) +{ + int i, c, j; + uint32_t seq[SEQ_MAX_LEN]; + + switch(seq_prop_idx) { + case UNICODE_SEQUENCE_PROP_Basic_Emoji: + if (unicode_prop1(cr, UNICODE_PROP_Basic_Emoji1) < 0) + return -1; + for(i = 0; i < cr->len; i += 2) { + for(c = cr->points[i]; c < cr->points[i + 1]; c++) { + seq[0] = c; + cb(opaque, seq, 1); + } + } + + cr->len = 0; + + if (unicode_prop1(cr, UNICODE_PROP_Basic_Emoji2) < 0) + return -1; + for(i = 0; i < cr->len; i += 2) { + for(c = cr->points[i]; c < cr->points[i + 1]; c++) { + seq[0] = c; + seq[1] = 0xfe0f; + cb(opaque, seq, 2); + } + } + + break; + case UNICODE_SEQUENCE_PROP_RGI_Emoji_Modifier_Sequence: + if (unicode_prop1(cr, UNICODE_PROP_Emoji_Modifier_Base) < 0) + return -1; + for(i = 0; i < cr->len; i += 2) { + for(c = cr->points[i]; c < cr->points[i + 1]; c++) { + for(j = 0; j < 5; j++) { + seq[0] = c; + seq[1] = 0x1f3fb + j; + cb(opaque, seq, 2); + } + } + } + break; + case UNICODE_SEQUENCE_PROP_RGI_Emoji_Flag_Sequence: + if (unicode_prop1(cr, UNICODE_PROP_RGI_Emoji_Flag_Sequence) < 0) + return -1; + for(i = 0; i < cr->len; i += 2) { + for(c = cr->points[i]; c < cr->points[i + 1]; c++) { + int c0, c1; + c0 = c / 26; + c1 = c % 26; + seq[0] = 0x1F1E6 + c0; + seq[1] = 0x1F1E6 + c1; + cb(opaque, seq, 2); + } + } + break; + case UNICODE_SEQUENCE_PROP_RGI_Emoji_ZWJ_Sequence: + { + int len, code, pres, k, mod, mod_count, mod_pos[2], hc_pos, n_mod, n_hc, mod1; + int mod_idx, hc_idx, i0, i1; + const uint8_t *tab = unicode_rgi_emoji_zwj_sequence; + + for(i = 0; i < countof(unicode_rgi_emoji_zwj_sequence);) { + len = tab[i++]; + k = 0; + mod = 0; + mod_count = 0; + hc_pos = -1; + for(j = 0; j < len; j++) { + code = tab[i++]; + code |= tab[i++] << 8; + pres = code >> 15; + mod1 = (code >> 13) & 3; + code &= 0x1fff; + if (code < 0x1000) { + c = code + 0x2000; + } else { + c = 0x1f000 + (code - 0x1000); + } + if (c == 0x1f9b0) + hc_pos = k; + seq[k++] = c; + if (mod1 != 0) { + assert(mod_count < 2); + mod = mod1; + mod_pos[mod_count++] = k; + seq[k++] = 0; /* will be filled later */ + } + if (pres) { + seq[k++] = 0xfe0f; + } + if (j < len - 1) { + seq[k++] = 0x200d; + } + } + + /* genrate all the variants */ + switch(mod) { + case 1: + n_mod = 5; + break; + case 2: + n_mod = 25; + break; + case 3: + n_mod = 20; + break; + default: + n_mod = 1; + break; + } + if (hc_pos >= 0) + n_hc = 4; + else + n_hc = 1; + for(hc_idx = 0; hc_idx < n_hc; hc_idx++) { + for(mod_idx = 0; mod_idx < n_mod; mod_idx++) { + if (hc_pos >= 0) + seq[hc_pos] = 0x1f9b0 + hc_idx; + + switch(mod) { + case 1: + seq[mod_pos[0]] = 0x1f3fb + mod_idx; + break; + case 2: + case 3: + i0 = mod_idx / 5; + i1 = mod_idx % 5; + /* avoid identical values */ + if (mod == 3 && i0 >= i1) + i0++; + seq[mod_pos[0]] = 0x1f3fb + i0; + seq[mod_pos[1]] = 0x1f3fb + i1; + break; + default: + break; + } +#if 0 + for(j = 0; j < k; j++) + printf(" %04x", seq[j]); + printf("\n"); +#endif + cb(opaque, seq, k); + } + } + } + } + break; + case UNICODE_SEQUENCE_PROP_RGI_Emoji_Tag_Sequence: + { + for(i = 0; i < countof(unicode_rgi_emoji_tag_sequence);) { + j = 0; + seq[j++] = 0x1F3F4; + for(;;) { + c = unicode_rgi_emoji_tag_sequence[i++]; + if (c == 0x00) + break; + seq[j++] = 0xe0000 + c; + } + seq[j++] = 0xe007f; + cb(opaque, seq, j); + } + } + break; + case UNICODE_SEQUENCE_PROP_Emoji_Keycap_Sequence: + if (unicode_prop1(cr, UNICODE_PROP_Emoji_Keycap_Sequence) < 0) + return -1; + for(i = 0; i < cr->len; i += 2) { + for(c = cr->points[i]; c < cr->points[i + 1]; c++) { + seq[0] = c; + seq[1] = 0xfe0f; + seq[2] = 0x20e3; + cb(opaque, seq, 3); + } + } + break; + case UNICODE_SEQUENCE_PROP_RGI_Emoji: + /* all prevous sequences */ + for(i = UNICODE_SEQUENCE_PROP_Basic_Emoji; i <= UNICODE_SEQUENCE_PROP_RGI_Emoji_ZWJ_Sequence; i++) { + int ret; + ret = unicode_sequence_prop1(i, cb, opaque, cr); + if (ret < 0) + return ret; + cr->len = 0; + } + break; + default: + return -2; + } + return 0; +} + +/* build a unicode sequence property */ +/* return -2 if not found, -1 if other error. 'cr' is used as temporary memory. */ +int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque, + CharRange *cr) +{ + int seq_prop_idx; + seq_prop_idx = unicode_find_name(unicode_sequence_prop_name_table, prop_name); + if (seq_prop_idx < 0) + return -2; + return unicode_sequence_prop1(seq_prop_idx, cb, opaque, cr); +} + int unicode_prop(CharRange *cr, const char *prop_name) { int prop_idx, ret; @@ -78900,6 +84960,10 @@ JS_LIBC_EXTERN void js_std_set_worker_new_context_func(JSContext *(*func)(JSRunt #include #endif +#if defined(__COSMOPOLITAN__) +#include +#endif + #if defined(__APPLE__) typedef sig_t sighandler_t; #include @@ -79031,6 +85095,8 @@ typedef struct JSThreadState { #endif // USE_WORKER JSClassID std_file_class_id; JSClassID worker_class_id; + JSInterruptHandler *prev_interrupt_handler; + void *prev_interrupt_opaque; } JSThreadState; static uint64_t os_pending_signals; @@ -79362,8 +85428,12 @@ static int get_bool_option(JSContext *ctx, bool *pbool, return 0; } -static void free_buf(JSRuntime *rt, void *opaque, void *ptr) { - js_free_rt(rt, ptr); +// js_realloc_array_buffer to avoid a name conflict with +// js_array_buffer_realloc from quickjs.c in the amalgamation build +static void *js_realloc_array_buffer(JSRuntime *rt, void *opaque, void *ptr, + size_t size) +{ + return js_realloc_rt(rt, ptr, size); } /* load a file as a UTF-8 encoded string or Uint8Array */ @@ -79392,7 +85462,8 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val, if (!buf) return JS_NULL; if (binary) { - ret = JS_NewUint8Array(ctx, buf, buf_len, free_buf, NULL, false); + ret = JS_NewUint8Array(ctx, buf, buf_len, js_realloc_array_buffer, + NULL, false); } else { ret = JS_NewStringLen(ctx, (char *)buf, buf_len); js_free(ctx, buf); @@ -79663,13 +85734,6 @@ int js_module_check_attributes(JSContext *ctx, void *opaque, return ret; } -// js_free_array_buffer to avoid a name conflict with js_array_buffer_free -// from quickjs.c in the amalgamation build -static void js_free_array_buffer(JSRuntime *rt, void *opaque, void *ptr) -{ - js_free_rt(rt, ptr); -} - enum { JS_IMPORT_TYPE_JS, JS_IMPORT_TYPE_JSON, @@ -79726,7 +85790,9 @@ JSModuleDef *js_module_load(JSContext *ctx, const char *module_name, type = js_module_import_type(ctx, attributes); if (type < 0) return NULL; - if (type != JS_IMPORT_TYPE_BYTES) + /* the .json suffix only selects the JSON type when no type attribute + was given, so that e.g. 'with { type: "text" }' is honored */ + if (type == JS_IMPORT_TYPE_JS) if (js__has_suffix(module_name, ".json")) type = JS_IMPORT_TYPE_JSON; buf = (char *)load_file(ctx, &buf_len, module_name); @@ -79748,7 +85814,8 @@ JSModuleDef *js_module_load(JSContext *ctx, const char *module_name, break; case JS_IMPORT_TYPE_BYTES: val = JS_NewUint8Array(ctx, (uint8_t *)buf, buf_len, - js_free_array_buffer, NULL, /*is_shared*/false); + js_realloc_array_buffer, NULL, + /*is_shared*/false); if (!JS_IsException(val)) { JSValue abuf = JS_GetTypedArrayBuffer(ctx, val, NULL, NULL, NULL); JS_SetImmutableArrayBuffer(abuf, /*immutable*/true); @@ -79916,9 +85983,15 @@ static JSValue js_std_gc(JSContext *ctx, JSValueConst this_val, return JS_UNDEFINED; } -static int interrupt_handler(JSRuntime *rt, void *opaque) +static int interrupt_handler(JSContext *ctx, void *opaque) { - return (os_pending_signals >> SIGINT) & 1; + JSThreadState *ts = opaque; + + if (1 & (os_pending_signals >> SIGINT)) + return 1; + if (ts->prev_interrupt_handler) + return ts->prev_interrupt_handler(ctx, ts->prev_interrupt_opaque); + return 0; } static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val, @@ -79981,7 +86054,14 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val, } if (!ts->recv_pipe && ++ts->eval_script_recurse == 1) { /* install the interrupt handler */ - JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL); + ts->prev_interrupt_handler = + (JSInterruptHandler *)js_std_cmd(/*GetInterruptHandler*/5, rt); + ts->prev_interrupt_opaque = + (void *)js_std_cmd(/*GetInterruptOpaque*/6, rt); + // FIXME(bnoordhuis) Questionable hack to make the REPL interruptible. + // Ideally qjs installs a signal handler + interrupt handler but then + // scripts can intercept it with os.signal(SIGINT). + JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, ts); } flags = compile_module ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL; if (backtrace_barrier) @@ -79999,7 +86079,11 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val, JS_FreeCString(ctx, str); if (!ts->recv_pipe && --ts->eval_script_recurse == 0) { /* remove the interrupt handler */ - JS_SetInterruptHandler(JS_GetRuntime(ctx), NULL, NULL); + JS_SetInterruptHandler(JS_GetRuntime(ctx), + ts->prev_interrupt_handler, + ts->prev_interrupt_opaque); + ts->prev_interrupt_handler = NULL; + ts->prev_interrupt_opaque = NULL; os_pending_signals &= ~((uint64_t)1 << SIGINT); /* convert the uncatchable "interrupted" error into a normal error so that it can be caught by the REPL */ @@ -80753,7 +86837,7 @@ static const JSCFunctionListEntry js_std_funcs[] = { JS_CFUNC_DEF("evalScript", 1, js_evalScript ), JS_CFUNC_DEF("loadScript", 1, js_loadScript ), JS_CFUNC_DEF("getenv", 1, js_std_getenv ), - JS_CFUNC_DEF("setenv", 1, js_std_setenv ), + JS_CFUNC_DEF("setenv", 2, js_std_setenv ), JS_CFUNC_DEF("unsetenv", 1, js_std_unsetenv ), JS_CFUNC_DEF("getenviron", 1, js_std_getenviron ), #if !defined(__wasi__) @@ -81474,7 +87558,7 @@ static void js_waker_signal(JSWaker *w) ret = write(w->write_fd, "", 1); if (ret == 1) break; - if (ret < 0 && (errno != EAGAIN || errno != EINTR)) + if (ret < 0 && errno != EAGAIN && errno != EINTR) break; } } @@ -81667,7 +87751,7 @@ static int js_os_poll_internal(JSContext *ctx, int timeout_ms, int flags) { JSRuntime *rt = JS_GetRuntime(ctx); JSThreadState *ts = js_get_thread_state(rt); - int r, w, ret, nfds, min_delay; + int r, w, ret, nfds, min_delay, poll_nfds; JSOSRWHandler *rh; struct list_head *el; struct pollfd *pfd, *pfds, pfds_local[64]; @@ -81761,12 +87845,14 @@ static int js_os_poll_internal(JSContext *ctx, int timeout_ms, int flags) // linear-ish in practice because we bail out on the first hit, // i.e., it's probably good enough for now ret = 0; + /* poll() returns the number of ready descriptors, not the array length. */ + poll_nfds = nfds; nfds = poll(pfds, nfds, min_delay); if (nfds < 0) { ret = -1; goto done; } - for (pfd = pfds; nfds-- > 0; pfd++) { + for (pfd = pfds; poll_nfds-- > 0; pfd++) { rh = find_rh(ts, pfd->fd); if (rh) { r = (POLLERR|POLLHUP|POLLNVAL|POLLIN) * !JS_IsNull(rh->rw_func[0]); @@ -82931,7 +89017,7 @@ static void worker_func(void *opaque) js_std_add_helpers(ctx, -1, NULL); - val = JS_LoadModule(ctx, args->basename, args->filename); + val = JS_LoadModule(ctx, args->basename, args->filename, JS_UNDEFINED); free(args->filename); free(args->basename); free(args); @@ -83282,7 +89368,7 @@ static const JSCFunctionListEntry js_os_funcs[] = { JS_CFUNC_DEF("sleepAsync", 1, js_os_sleepAsync ), JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ), JS_CFUNC_DEF("getcwd", 0, js_os_getcwd ), - JS_CFUNC_DEF("chdir", 0, js_os_chdir ), + JS_CFUNC_DEF("chdir", 1, js_os_chdir ), JS_CFUNC_DEF("mkdir", 1, js_os_mkdir ), JS_CFUNC_DEF("readdir", 1, js_os_readdir ), #if !defined(_WIN32) && !defined(__wasi__) @@ -83356,7 +89442,29 @@ static int js_os_init(JSContext *ctx, JSModuleDef *m) } #endif /* USE_WORKER */ - return JS_SetModuleExportList(ctx, m, js_os_funcs, countof(js_os_funcs)); + if (JS_SetModuleExportList(ctx, m, js_os_funcs, countof(js_os_funcs))) + return -1; + +#ifdef __COSMOPOLITAN__ + const char *platform; + if (IsLinux()) { + platform = "linux"; + } else if (IsNetbsd()) { + platform = "netbsd"; + } else if (IsOpenbsd()) { + platform = "openbsd"; + } else if (IsWindows()) { + platform = "win32"; + } else if (IsXnu()) { + platform = "darwin"; + } else { + platform = "unknown"; + } + if (JS_SetModuleExport(ctx, m, "platform", JS_NewString(ctx, platform))) + return -1; +#endif /* __COSMOPOLITAN__ */ + + return 0; } JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name) @@ -83554,22 +89662,41 @@ static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val) } } +#define JS_DUMP_ERROR_MAX_CAUSE_DEPTH 10 + static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val) { - JSValue val; + JSValue val, current; bool is_error; - - is_error = JS_IsError(exception_val); - js_dump_obj(ctx, stderr, exception_val); - if (is_error) { - val = JS_GetPropertyStr(ctx, exception_val, "stack"); - } else { - js_std_cmd(/*ErrorBackTrace*/2, ctx, &val); - } - if (!JS_IsUndefined(val)) { - js_dump_obj(ctx, stderr, val); - JS_FreeValue(ctx, val); + int depth; + + current = JS_DupValue(ctx, exception_val); + for (depth = 0; ; depth++) { + is_error = JS_IsError(current); + js_dump_obj(ctx, stderr, current); + if (is_error) { + val = JS_GetPropertyStr(ctx, current, "stack"); + } else if (depth == 0) { + js_std_cmd(/*ErrorBackTrace*/2, ctx, &val); + } else { + val = JS_UNDEFINED; + } + if (!JS_IsUndefined(val)) { + js_dump_obj(ctx, stderr, val); + JS_FreeValue(ctx, val); + } + if (!is_error || depth >= JS_DUMP_ERROR_MAX_CAUSE_DEPTH) + break; + val = JS_GetPropertyStr(ctx, current, "cause"); + if (JS_IsUndefined(val)) { + JS_FreeValue(ctx, val); + break; + } + fputs("Caused by: ", stderr); + JS_FreeValue(ctx, current); + current = val; } + JS_FreeValue(ctx, current); } void js_std_dump_error(JSContext *ctx) diff --git a/src/quickjs/quickjs.h b/src/quickjs/quickjs.h index a44b4565..13240667 100644 --- a/src/quickjs/quickjs.h +++ b/src/quickjs/quickjs.h @@ -68,7 +68,7 @@ extern "C" { # define JS_EXTERN /* nothing */ # endif #else -# ifdef QUICKJS_NG_CC_GNULIKE +# if defined(BUILDING_QJS_SHARED) && defined(QUICKJS_NG_CC_GNULIKE) # define JS_EXTERN __attribute__((visibility("default"))) # else # define JS_EXTERN /* nothing */ @@ -109,7 +109,7 @@ extern "C" { # define JS_MODULE_EXTERN __declspec(dllimport) # endif #else -# ifdef QUICKJS_NG_CC_GNULIKE +# if defined(QUICKJS_NG_MODULE_BUILD) && defined(QUICKJS_NG_CC_GNULIKE) # define JS_MODULE_EXTERN __attribute__((visibility("default"))) # else # define JS_MODULE_EXTERN /* nothing */ @@ -135,13 +135,31 @@ extern "C" { #endif #endif +/* + * `JS_CALL_X` -- macro tree for calling a function with a variable number of arguments. + * This is used for bulk operations such as freeing values or atoms. + */ +#define JS_COUNT_ARGS_(_0, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N +#define JS_COUNT_ARGS(...) JS_COUNT_ARGS_(0, __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +#define JS_CALLX1(F,X,a) do { F(X,a); } while (0) +#define JS_CALLX2(F,X,a,b) do { F(X,a); F(X,b); } while (0) +#define JS_CALLX3(F,X,a,b,c) do { F(X,a); F(X,b); F(X,c); } while (0) +#define JS_CALLX4(F,X,a,b,c,d) do { F(X,a); F(X,b); F(X,c); F(X,d); } while (0) +#define JS_CALLX5(F,X,a,b,c,d,e) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); } while (0) +#define JS_CALLX6(F,X,a,b,c,d,e,f) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); } while (0) +#define JS_CALLX7(F,X,a,b,c,d,e,f,g) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); F(X,g); } while (0) +#define JS_CALLX8(F,X,a,b,c,d,e,f,g,h) do { F(X,a); F(X,b); F(X,c); F(X,d); F(X,e); F(X,f); F(X,g); F(X,h); } while (0) + +#define JS_CALLX__(F, X, N, ...) JS_CALLX##N(F, X, __VA_ARGS__) +#define JS_CALLX_(F, X, N, ...) JS_CALLX__(F, X, N, __VA_ARGS__) + #undef QUICKJS_NG_CC_GNULIKE #undef QUICKJS_NG_PLAT_WIN32 typedef struct JSRuntime JSRuntime; typedef struct JSContext JSContext; typedef struct JSObject JSObject; -typedef struct JSClass JSClass; typedef uint32_t JSClassID; typedef uint32_t JSAtom; @@ -224,12 +242,6 @@ static inline JSValue __JS_NewFloat64(double d) return JS_MKVAL(JS_TAG_FLOAT64, (int)d); } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d) -{ - (void)&ctx; - return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d); -} - static inline bool JS_VALUE_IS_NAN(JSValue v) { (void)&v; @@ -280,12 +292,6 @@ static inline JSValue __JS_NewFloat64(double d) return v; } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d) -{ - (void)&ctx; - return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d); -} - #define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST)) /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */ @@ -309,7 +315,10 @@ static inline bool JS_VALUE_IS_NAN(JSValue v) #else /* !JS_NAN_BOXING */ typedef union JSValueUnion { - int32_t int32; + /* JS_MKVAL() writes all 64 bits even though only 32 are needed: + leaving the upper half uninitialized makes GCC 14+ generate much + slower code on x86-64 */ + uint64_t uint64; double float64; void *ptr; int32_t short_big_int; @@ -323,10 +332,10 @@ typedef struct JSValue { #define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag) /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */ #define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v) -#define JS_VALUE_GET_INT(v) ((v).u.int32) -#define JS_VALUE_GET_BOOL(v) ((v).u.int32) +#define JS_VALUE_GET_INT(v) ((int)(v).u.uint64) +#define JS_VALUE_GET_BOOL(v) ((int)(v).u.uint64) #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64) -#define JS_VALUE_GET_SHORT_BIG_INT(v) ((v).u.short_big_int) +#define JS_VALUE_GET_SHORT_BIG_INT(v) ((int32_t)(v).u.uint64) #define JS_VALUE_GET_PTR(v) ((v).u.ptr) /* msvc doesn't understand designated initializers without /std:c++20 */ @@ -341,7 +350,7 @@ static inline JSValue JS_MKPTR(int64_t tag, void *ptr) static inline JSValue JS_MKVAL(int64_t tag, int32_t int32) { JSValue v; - v.u.int32 = int32; + v.u.uint64 = (uint32_t)int32; v.tag = tag; return v; } @@ -358,7 +367,7 @@ static inline JSValue JS_MKNAN(void) #define JS_NAN JS_MKNAN() /* alas, not a constant expression */ #else #define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag } -#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag } +#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .uint64 = (uint32_t)(val) }, tag } #define JS_NAN (JSValue){ (JSValueUnion){ .float64 = NAN }, JS_TAG_FLOAT64 } #endif @@ -372,15 +381,6 @@ static inline JSValue __JS_NewFloat64(double d) return v; } -static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d) -{ - (void)&ctx; - JSValue v; - v.tag = JS_TAG_SHORT_BIG_INT; - v.u.short_big_int = d; - return v; -} - static inline bool JS_VALUE_IS_NAN(JSValue v) { union { @@ -499,6 +499,7 @@ typedef struct JSMallocFunctions { #define JS_DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */ #define JS_DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */ #define JS_DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */ +#define JS_ABORT_ON_LEAKS 0x10C000 /* abort on atom/object/string leaks; for testing */ // Finalizers run in LIFO order at the very end of JS_FreeRuntime. // Intended for cleanup of associated resources; the runtime itself @@ -560,6 +561,7 @@ JS_EXTERN int JS_AddIntrinsicBigInt(JSContext *ctx); JS_EXTERN int JS_AddIntrinsicWeakRef(JSContext *ctx); JS_EXTERN int JS_AddPerformance(JSContext *ctx); JS_EXTERN int JS_AddIntrinsicDOMException(JSContext *ctx); +JS_EXTERN int JS_AddIntrinsicAToB(JSContext *ctx); /* for equality comparisons and sameness */ JS_EXTERN int JS_IsEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2); @@ -584,7 +586,6 @@ JS_EXTERN void *js_malloc(JSContext *ctx, size_t size); JS_EXTERN void js_free(JSContext *ctx, void *ptr); JS_EXTERN void *js_realloc(JSContext *ctx, void *ptr, size_t size); JS_EXTERN size_t js_malloc_usable_size(JSContext *ctx, const void *ptr); -JS_EXTERN void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack); JS_EXTERN void *js_mallocz(JSContext *ctx, size_t size); JS_EXTERN char *js_strdup(JSContext *ctx, const char *str); JS_EXTERN char *js_strndup(JSContext *ctx, const char *s, size_t n); @@ -618,10 +619,12 @@ JS_EXTERN JSAtom JS_DupAtom(JSContext *ctx, JSAtom v); JS_EXTERN JSAtom JS_DupAtomRT(JSRuntime *rt, JSAtom v); JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v); JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v); +#define JS_FreeAtoms(ctx, ...) JS_CALLX_(JS_FreeAtom, ctx, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) +#define JS_FreeAtomsRT(rt, ...) JS_CALLX_(JS_FreeAtomRT, rt, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom); JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom); JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom); -static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) +static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom) { return JS_AtomToCStringLen(ctx, NULL, atom); } @@ -760,6 +763,17 @@ static inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val) return v; } +static inline JSValue JS_NewUint64(JSContext *ctx, uint64_t val) +{ + JSValue v; + if (val <= INT32_MAX) { + v = JS_NewInt32(ctx, (int32_t)val); + } else { + v = JS_NewFloat64(ctx, (double)val); + } + return v; +} + JS_EXTERN JSValue JS_NewNumber(JSContext *ctx, double d); JS_EXTERN JSValue JS_NewBigInt64(JSContext *ctx, int64_t v); JS_EXTERN JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v); @@ -835,12 +849,14 @@ JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValueConst val); // JS_Throw(ctx, exc); JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx); JS_EXTERN JSValue JS_NewError(JSContext *ctx); +// InternalError cannot be caught by JS code JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); +// InternalError cannot be caught by JS code JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...); @@ -851,6 +867,8 @@ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowDOMException(JSContext *ct JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx); JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v); JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v); +#define JS_FreeValues(ctx, ...) JS_CALLX_(JS_FreeValue, ctx, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) +#define JS_FreeValuesRT(rt, ...) JS_CALLX_(JS_FreeValueRT, rt, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__) JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValueConst v); JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v); JS_EXTERN int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */ @@ -964,6 +982,8 @@ JS_EXTERN JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst this_obj, int64_t idx); JS_EXTERN JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj, const char *prop); +JS_EXTERN JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop); JS_EXTERN int JS_SetProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop, JSValue val); @@ -973,6 +993,9 @@ JS_EXTERN int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj, int64_t idx, JSValue val); JS_EXTERN int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj, const char *prop, JSValue val); +// |flags| is a mix of JS_PROP_* values; when in doubt use JS_PROP_C_W_E +JS_EXTERN int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj, + JSValue prop, JSValue val, int flags); JS_EXTERN int JS_HasProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop); JS_EXTERN int JS_IsExtensible(JSContext *ctx, JSValueConst obj); JS_EXTERN int JS_PreventExtensions(JSContext *ctx, JSValueConst obj); @@ -1055,10 +1078,24 @@ JS_EXTERN JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, JS_EXTERN JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj, JSValueConst replacer, JSValueConst space0); -typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque, void *ptr); +/* Manages the backing memory of an externally created ArrayBuffer. + When 'size' is zero, 'ptr' must be freed and NULL returned. Otherwise the + block must be resized to 'size' bytes and the new pointer returned, or NULL + if that is not possible, indicating a memory error, in which case 'ptr' must + stay valid and the ArrayBuffer keeps its current size. */ +typedef void *JSReallocArrayBufferDataFunc(JSRuntime *rt, void *opaque, + void *ptr, size_t size); +/* Creates an ArrayBuffer backed by 'buf'. Pass a zero 'max_len' for a regular, + fixed length ArrayBuffer; a non-zero 'max_len' makes it resizable and is its + maxByteLength. 'realloc_func' may be NULL if the memory must not be managed + by quickjs at all; such an ArrayBuffer can neither be resized nor + transferred to a different length. It is required for resizable + ArrayBuffers, except for SharedArrayBuffers: those commit their maximum size + upfront and therefore need 'buf' to be at least 'max_len' bytes big. */ JS_EXTERN JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared); + size_t max_len, + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared); JS_EXTERN JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len); JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj); JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj); @@ -1091,8 +1128,8 @@ JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj, size_t *pbyte_length, size_t *pbytes_per_element); JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len, - JSFreeArrayBufferDataFunc *free_func, void *opaque, - bool is_shared); + JSReallocArrayBufferDataFunc *realloc_func, + void *opaque, bool is_shared); /* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */ JS_EXTERN int JS_GetTypedArrayType(JSValueConst obj); JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len); @@ -1113,13 +1150,20 @@ typedef enum JSPromiseStateEnum { } JSPromiseStateEnum; JS_EXTERN JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs); +JS_EXTERN JSValue JS_PromiseThen(JSContext *ctx, JSValueConst promise, + JSValueConst on_fulfilled, + JSValueConst on_rejected); JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValueConst promise); JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValueConst promise); JS_EXTERN bool JS_IsPromise(JSValueConst val); +JS_EXTERN void JS_PromiseMarkAsHandled(JSContext *ctx, JSValueConst promise); JS_EXTERN JSValue JS_NewSettledPromise(JSContext *ctx, bool is_reject, JSValueConst value); +// a symbol that is visible to JavaScript code JS_EXTERN JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global); +// a symbol that is only visible to C code +JS_EXTERN JSValue JS_NewPrivateSymbol(JSContext *ctx, const char *description); typedef enum JSPromiseHookType { JS_PROMISE_HOOK_INIT, // emitted when a new promise is created @@ -1145,7 +1189,7 @@ typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise, JS_EXTERN void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque); /* return != 0 if the JS code needs to be interrupted */ -typedef int JSInterruptHandler(JSRuntime *rt, void *opaque); +typedef int JSInterruptHandler(JSContext *ctx, void *opaque); JS_EXTERN void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque); /* if can_block is true, Atomics.wait() can be used */ JS_EXTERN void JS_SetCanBlock(JSRuntime *rt, bool can_block); @@ -1167,6 +1211,35 @@ typedef char *JSModuleNormalizeFunc2(JSContext *ctx, typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx, const char *module_name, void *opaque); +// Dynamic module loading in detail: +// +// 1. JS code calls `import(url, {with: /*...*/})` +// +// 2. If there are import attributes, JSModuleCheckSupportedImportAttributes +// runs synchronously (with the |opaque| passed to JS_SetModuleLoaderFunc2) +// +// 3. An unfulfilled promise is returned to the JS caller +// +// 4. A job is posted to the microtask queue that runs when something +// calls JS_ExecutePendingJob or js_std_await +// +// 5. The JSDynamicImportFunc callback is called +// +// 6. You, the embedder, load the resource at your convenience and eventually +// call JS_LoadModule(). Pass the result to JS_FinishDynamicImport(). +// +// Step 6 means the non-dynamic module loader needs to be aware of resources +// loaded by the dynamic module loader. How to implement that is up to you. +// +// Ownership of |token| passes to JSDynamicImportFunc. If you need to hold +// on to |basename|, |filename| or |attributes|, strdup or JS_DupValue them. +// +// You must *always* call JS_FinishDynamicImport(), even on error, otherwise +// the resources associated with |token| leak. +typedef void JSDynamicImportFunc(JSContext *ctx, const char *basename, + const char *filename, JSValue token, + JSValueConst attributes, void *opaque); + /* module loader with import attributes support */ typedef JSModuleDef *JSModuleLoaderFunc2(JSContext *ctx, const char *module_name, void *opaque, @@ -1189,10 +1262,18 @@ JS_EXTERN void JS_SetModuleLoaderFunc2(JSRuntime *rt, JSModuleCheckSupportedImportAttributes *module_check_attrs, void *opaque); +JS_EXTERN void JS_SetDynamicImportFunc(JSRuntime *rt, JSDynamicImportFunc *func, + void *opaque); + /* Set an attributes-aware module normalizer. Call after JS_SetModuleLoaderFunc2. */ JS_EXTERN void JS_SetModuleNormalizeFunc2(JSRuntime *rt, JSModuleNormalizeFunc2 *module_normalize); +// pass promise=JS_EXCEPTION and exception= if an exception +// occurred during module loading; pass exception=JS_UNDEFINED otherwise +JS_EXTERN int JS_FinishDynamicImport(JSContext *ctx, JSValue token, + JSValue promise, JSValue exception); + /* return the import.meta object of a module */ JS_EXTERN JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m); JS_EXTERN JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m); @@ -1229,8 +1310,14 @@ JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst ob JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj, int flags, JSSABTab *psab_tab); +/* WARNING: only enable JS_READ_OBJ_BYTECODE on input from a trusted + writer. The bytecode format is not designed to resist a hostile + producer; loading adversarial bytecode can lead to memory corruption. */ #define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */ #define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */ +/* WARNING: serialized SharedArrayBuffers carry a literal host pointer in + the blob; only enable JS_READ_OBJ_SAB on input produced by a trusted + writer in the same process (e.g. another Worker on the same runtime). */ #define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */ #define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */ JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags); @@ -1245,9 +1332,9 @@ JS_EXTERN int JS_ResolveModule(JSContext *ctx, JSValueConst obj); /* only exported for os.Worker() */ JS_EXTERN JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels); -/* only exported for os.Worker() */ +/* don't call this recursively */ JS_EXTERN JSValue JS_LoadModule(JSContext *ctx, const char *basename, - const char *filename); + const char *filename, JSValueConst attributes); /* C function definition */ typedef enum JSCFunctionEnum { /* XXX: should rename for namespace isolation */ @@ -1375,6 +1462,7 @@ typedef struct JSCFunctionListEntry { #define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } } #define JS_CGETSET_DEF2(name, fgetter, fsetter, prop_flags) { name, prop_flags, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } } #define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } } +#define JS_CGETSET_MAGIC_DEF2(name, fgetter, fsetter, magic, prop_flags) { name, prop_flags, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } } #define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } } #define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } } #define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } } @@ -1410,7 +1498,7 @@ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m, /* Version */ #define QJS_VERSION_MAJOR 0 -#define QJS_VERSION_MINOR 14 +#define QJS_VERSION_MINOR 18 #define QJS_VERSION_PATCH 0 #define QJS_VERSION_SUFFIX "" diff --git a/src/quickjs/update.sh b/src/quickjs/update.sh index 49bf342c..39acd347 100755 --- a/src/quickjs/update.sh +++ b/src/quickjs/update.sh @@ -1,11 +1,11 @@ #!/bin/bash # Script to upgrade embedded QuickJS using quickjs-ng amalgamated release. # Usage: ./update.sh [version_tag] -# Example: ./update.sh v0.11.0 +# Example: ./update.sh v0.18.0 set -e -VERSION=${1:-v0.14.0} +VERSION=${1:-v0.18.0} URL="https://github.com/quickjs-ng/quickjs/releases/download/${VERSION}/quickjs-amalgam.zip" TMP_DIR=$(mktemp -d) DEST_DIR=$(dirname "$0")