Skip to content

Commit

Permalink
Implement String.prototype.at (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis authored Nov 5, 2023
1 parent 7be933e commit 5501834
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 ),
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5501834

Please sign in to comment.