From 0720b068ca3969e0ac50ebf56041edda69ba91e8 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 11 Nov 2023 13:47:22 +0100 Subject: [PATCH] Implement String.prototype.isWellFormed (#41) --- quickjs.c | 41 +++++++++++++++++++++++++++++++++++++++++ test262.conf | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 85f2648..f13b259 100644 --- a/quickjs.c +++ b/quickjs.c @@ -38206,6 +38206,46 @@ static int64_t string_advance_index(JSString *p, int64_t index, BOOL unicode) return index; } +static JSValue js_string_isWellFormed(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue str; + JSValue ret; + JSString *p; + uint32_t i, n, hi, lo; + + ret = JS_TRUE; + str = JS_ToStringCheckObject(ctx, this_val); + if (JS_IsException(str)) + return JS_EXCEPTION; + + p = JS_VALUE_GET_STRING(str); + if (p->is_wide_char) { + for (i = 0, n = p->len; i < n; i++) { + hi = p->u.str16[i]; + if (hi < 0xD800 || hi > 0xDFFF) + continue; + if (hi > 0xDBFF) { + ret = JS_FALSE; + break; + } + i++; + if (i == n) { + ret = JS_FALSE; + break; + } + lo = p->u.str16[i]; + if (lo < 0xDC00 || lo > 0xDFFF) { + ret = JS_FALSE; + break; + } + } + } + + JS_FreeValue(ctx, str); + return ret; +} + static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int lastIndexOf) { @@ -39357,6 +39397,7 @@ static const JSCFunctionListEntry js_string_proto_funcs[] = { JS_CFUNC_DEF("charAt", 1, js_string_charAt ), JS_CFUNC_DEF("concat", 1, js_string_concat ), JS_CFUNC_DEF("codePointAt", 1, js_string_codePointAt ), + JS_CFUNC_DEF("isWellFormed", 0, js_string_isWellFormed ), JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ), JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ), JS_CFUNC_MAGIC_DEF("includes", 1, js_string_includes, 0 ), diff --git a/test262.conf b/test262.conf index c340438..36172e4 100644 --- a/test262.conf +++ b/test262.conf @@ -169,7 +169,7 @@ String.fromCodePoint String.prototype.at String.prototype.endsWith String.prototype.includes -String.prototype.isWellFormed=skip +String.prototype.isWellFormed String.prototype.matchAll String.prototype.replaceAll String.prototype.toWellFormed=skip