Skip to content

Commit 5501834

Browse files
authored
Implement String.prototype.at (#12)
1 parent 7be933e commit 5501834

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

quickjs.c

+31
Original file line numberDiff line numberDiff line change
@@ -40441,6 +40441,36 @@ static JSValue js_string___isSpace(JSContext *ctx, JSValueConst this_val,
4044140441
}
4044240442
#endif
4044340443

40444+
static JSValue js_string_at(JSContext *ctx, JSValueConst this_val,
40445+
int argc, JSValueConst *argv)
40446+
{
40447+
JSValue val, ret;
40448+
JSString *p;
40449+
int idx, c;
40450+
40451+
val = JS_ToStringCheckObject(ctx, this_val);
40452+
if (JS_IsException(val))
40453+
return val;
40454+
p = JS_VALUE_GET_STRING(val);
40455+
if (JS_ToInt32Sat(ctx, &idx, argv[0])) {
40456+
JS_FreeValue(ctx, val);
40457+
return JS_EXCEPTION;
40458+
}
40459+
if (idx < 0)
40460+
idx = p->len + idx;
40461+
if (idx < 0 || idx >= p->len) {
40462+
ret = JS_UNDEFINED;
40463+
} else {
40464+
if (p->is_wide_char)
40465+
c = p->u.str16[idx];
40466+
else
40467+
c = p->u.str8[idx];
40468+
ret = js_new_string_char(ctx, c);
40469+
}
40470+
JS_FreeValue(ctx, val);
40471+
return ret;
40472+
}
40473+
4044440474
static JSValue js_string_charCodeAt(JSContext *ctx, JSValueConst this_val,
4044540475
int argc, JSValueConst *argv)
4044640476
{
@@ -41744,6 +41774,7 @@ static const JSCFunctionListEntry js_string_funcs[] = {
4174441774

4174541775
static const JSCFunctionListEntry js_string_proto_funcs[] = {
4174641776
JS_PROP_INT32_DEF("length", 0, JS_PROP_CONFIGURABLE ),
41777+
JS_CFUNC_DEF("at", 1, js_string_at ),
4174741778
JS_CFUNC_DEF("charCodeAt", 1, js_string_charCodeAt ),
4174841779
JS_CFUNC_DEF("charAt", 1, js_string_charAt ),
4174941780
JS_CFUNC_DEF("concat", 1, js_string_concat ),

test262.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ ShadowRealm=skip
166166
SharedArrayBuffer
167167
string-trimming
168168
String.fromCodePoint
169-
String.prototype.at=skip
169+
String.prototype.at
170170
String.prototype.endsWith
171171
String.prototype.includes
172172
String.prototype.isWellFormed=skip

0 commit comments

Comments
 (0)