From 55018345ed3df5f1c45f3584fee6bf34ae973314 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 5 Nov 2023 11:08:27 +0100 Subject: [PATCH] Implement String.prototype.at (#12) --- quickjs.c | 31 +++++++++++++++++++++++++++++++ test262.conf | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 781358b..3edd140 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40441,6 +40441,36 @@ static JSValue js_string___isSpace(JSContext *ctx, JSValueConst this_val, } #endif +static JSValue js_string_at(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + JSValue val, ret; + JSString *p; + int idx, c; + + val = JS_ToStringCheckObject(ctx, this_val); + if (JS_IsException(val)) + return val; + p = JS_VALUE_GET_STRING(val); + if (JS_ToInt32Sat(ctx, &idx, argv[0])) { + JS_FreeValue(ctx, val); + return JS_EXCEPTION; + } + if (idx < 0) + idx = p->len + idx; + if (idx < 0 || idx >= p->len) { + ret = JS_UNDEFINED; + } else { + if (p->is_wide_char) + c = p->u.str16[idx]; + else + c = p->u.str8[idx]; + ret = js_new_string_char(ctx, c); + } + JS_FreeValue(ctx, val); + return ret; +} + static JSValue js_string_charCodeAt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -41744,6 +41774,7 @@ static const JSCFunctionListEntry js_string_funcs[] = { static const JSCFunctionListEntry js_string_proto_funcs[] = { JS_PROP_INT32_DEF("length", 0, JS_PROP_CONFIGURABLE ), + JS_CFUNC_DEF("at", 1, js_string_at ), JS_CFUNC_DEF("charCodeAt", 1, js_string_charCodeAt ), JS_CFUNC_DEF("charAt", 1, js_string_charAt ), JS_CFUNC_DEF("concat", 1, js_string_concat ), diff --git a/test262.conf b/test262.conf index d5db06f..3f39cfc 100644 --- a/test262.conf +++ b/test262.conf @@ -166,7 +166,7 @@ ShadowRealm=skip SharedArrayBuffer string-trimming String.fromCodePoint -String.prototype.at=skip +String.prototype.at String.prototype.endsWith String.prototype.includes String.prototype.isWellFormed=skip