diff --git a/quickjs.c b/quickjs.c index 3bf342e31..8d3e10297 100644 --- a/quickjs.c +++ b/quickjs.c @@ -59867,6 +59867,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; @@ -59941,6 +59943,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, @@ -60822,6 +60828,15 @@ static const unsigned char b64_enc[64] = { '+','/' }; +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] = { @@ -60833,6 +60848,7 @@ static const uint8_t b64_val[256] = { ['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] = { @@ -60850,31 +60866,47 @@ static const char b64_flags[256] = { ['+']=K_VAL,['/']=K_VAL, }; -static size_t b64_encode(const uint8_t *src, size_t len, char *dst) +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] = b64_enc[(v >> 18) & 63]; - dst[j + 1] = b64_enc[(v >> 12) & 63]; - dst[j + 2] = b64_enc[(v >> 6) & 63]; - dst[j + 3] = b64_enc[v & 63]; + 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++] = b64_enc[(v >> 18) & 63]; - dst[j++] = b64_enc[(v >> 12) & 63]; + 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++] = b64_enc[(v >> 18) & 63]; - dst[j++] = b64_enc[(v >> 12) & 63]; - dst[j++] = b64_enc[(v >> 6) & 63]; + dst[j++] = alpha[(v >> 18) & 63]; + dst[j++] = alpha[(v >> 12) & 63]; + dst[j++] = alpha[(v >> 6) & 63]; dst[j++] = '='; } return j; @@ -60986,7 +61018,7 @@ static JSValue js_btoa(JSContext *ctx, JSValueConst this_val, goto fail; outp = str8(ostr); - written = b64_encode(in8, len, (char *)outp); + written = b64_encode(in8, len, (char *)outp, b64_enc); outp[written] = '\0'; ostr->len = out_len; ret = JS_MKPTR(JS_TAG_STRING, ostr); @@ -61079,6 +61111,689 @@ static const JSCFunctionListEntry js_base64_funcs[] = { 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 (!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 (!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)) { diff --git a/test262.conf b/test262.conf index bb85580e7..b310069c6 100644 --- a/test262.conf +++ b/test262.conf @@ -230,7 +230,7 @@ u180e Uint16Array Uint32Array Uint8Array -uint8array-base64=skip +uint8array-base64 Uint8ClampedArray upsert WeakMap