Skip to content

Commit 72a63ad

Browse files
committed
Introduce js_empty_string
Replace the `JS_AtomToString(ctx, JS_ATOM_empty_string)` pattern with a dedicated function.
1 parent 5299e09 commit 72a63ad

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

quickjs.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,12 @@ int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def)
36223622
return ret;
36233623
}
36243624

3625+
static inline JSValue js_empty_string(JSRuntime *rt)
3626+
{
3627+
JSAtomStruct *p = rt->atom_array[JS_ATOM_empty_string];
3628+
return js_dup(JS_MKPTR(JS_TAG_STRING, p));
3629+
}
3630+
36253631
// XXX: `buf` contains raw 8-bit data, no UTF-8 decoding is performed
36263632
// XXX: no special case for len == 0
36273633
static JSValue js_new_string8_len(JSContext *ctx, const char *buf, int len)
@@ -3670,7 +3676,7 @@ static JSValue js_sub_string(JSContext *ctx, JSString *p, int start, int end)
36703676
return js_dup(JS_MKPTR(JS_TAG_STRING, p));
36713677
}
36723678
if (len <= 0) {
3673-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
3679+
return js_empty_string(ctx->rt);
36743680
}
36753681
if (p->is_wide_char) {
36763682
JSString *str;
@@ -4007,7 +4013,7 @@ static JSValue string_buffer_end(StringBuffer *s)
40074013
if (s->len == 0) {
40084014
js_free(s->ctx, str);
40094015
s->str = NULL;
4010-
return JS_AtomToString(s->ctx, JS_ATOM_empty_string);
4016+
return js_empty_string(s->ctx->rt);
40114017
}
40124018
if (s->len < s->size) {
40134019
/* smaller size so js_realloc should not fail, but OK if it does */
@@ -4038,7 +4044,7 @@ JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len)
40384044
int kind;
40394045

40404046
if (buf_len <= 0) {
4041-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
4047+
return js_empty_string(ctx->rt);
40424048
}
40434049
/* Compute string kind and length: 7-bit, 8-bit, 16-bit, 16-bit UTF-16 */
40444050
kind = utf8_scan(buf, buf_len, &len);
@@ -4078,7 +4084,7 @@ JSValue JS_NewTwoByteString(JSContext *ctx, const uint16_t *buf, size_t len)
40784084
JSString *str;
40794085

40804086
if (!len)
4081-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
4087+
return js_empty_string(ctx->rt);
40824088
str = js_alloc_string(ctx, len, 1);
40834089
if (!str)
40844090
return JS_EXCEPTION;
@@ -16614,7 +16620,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1661416620
goto exception;
1661516621
BREAK;
1661616622
CASE(OP_push_empty_string):
16617-
*sp++ = JS_AtomToString(ctx, JS_ATOM_empty_string);
16623+
*sp++ = js_empty_string(rt);
1661816624
BREAK;
1661916625
CASE(OP_get_length):
1662016626
{
@@ -39361,7 +39367,7 @@ static JSValue js_function_bind(JSContext *ctx, JSValueConst this_val,
3936139367
goto exception;
3936239368
if (!JS_IsString(name1)) {
3936339369
JS_FreeValue(ctx, name1);
39364-
name1 = JS_AtomToString(ctx, JS_ATOM_empty_string);
39370+
name1 = js_empty_string(ctx->rt);
3936539371
}
3936639372
name1 = JS_ConcatString3(ctx, "bound ", name1, "");
3936739373
if (JS_IsException(name1))
@@ -39412,7 +39418,7 @@ static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
3941239418
suff = "() {\n [native code]\n}";
3941339419
name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
3941439420
if (JS_IsUndefined(name))
39415-
name = JS_AtomToString(ctx, JS_ATOM_empty_string);
39421+
name = js_empty_string(ctx->rt);
3941639422
return JS_ConcatString3(ctx, pref, name, suff);
3941739423
}
3941839424
}
@@ -39574,7 +39580,7 @@ static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val,
3957439580

3957539581
msg = JS_GetProperty(ctx, this_val, JS_ATOM_message);
3957639582
if (JS_IsUndefined(msg))
39577-
msg = JS_AtomToString(ctx, JS_ATOM_empty_string);
39583+
msg = js_empty_string(ctx->rt);
3957839584
else
3957939585
msg = JS_ToStringFree(ctx, msg);
3958039586
if (JS_IsException(msg)) {
@@ -43050,7 +43056,7 @@ static JSValue js_string_constructor(JSContext *ctx, JSValueConst new_target,
4305043056
{
4305143057
JSValue val, obj;
4305243058
if (argc == 0) {
43053-
val = JS_AtomToString(ctx, JS_ATOM_empty_string);
43059+
val = js_empty_string(ctx->rt);
4305443060
} else {
4305543061
if (JS_IsUndefined(new_target) && JS_IsSymbol(argv[0])) {
4305643062
JSAtomStruct *p = JS_VALUE_GET_PTR(argv[0]);
@@ -43300,7 +43306,7 @@ static JSValue js_string_charAt(JSContext *ctx, JSValueConst this_val,
4330043306
return JS_EXCEPTION;
4330143307
}
4330243308
if (idx < 0 || idx >= p->len) {
43303-
ret = JS_AtomToString(ctx, JS_ATOM_empty_string);
43309+
ret = js_empty_string(ctx->rt);
4330443310
} else {
4330543311
c = string_get(p, idx);
4330643312
ret = js_new_string_char(ctx, c);
@@ -45330,7 +45336,7 @@ static JSValue js_regexp_constructor(JSContext *ctx, JSValueConst new_target,
4533045336
flags = js_dup(flags1);
4533145337
}
4533245338
if (JS_IsUndefined(pattern)) {
45333-
pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
45339+
pattern = js_empty_string(ctx->rt);
4533445340
} else {
4533545341
val = pattern;
4533645342
pattern = JS_ToString(ctx, val);
@@ -45372,7 +45378,7 @@ static JSValue js_regexp_compile(JSContext *ctx, JSValueConst this_val,
4537245378
} else {
4537345379
bc = JS_UNDEFINED;
4537445380
if (JS_IsUndefined(pattern1))
45375-
pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
45381+
pattern = js_empty_string(ctx->rt);
4537645382
else
4537745383
pattern = JS_ToString(ctx, pattern1);
4537845384
if (JS_IsException(pattern))
@@ -45530,7 +45536,7 @@ static JSValue js_regexp_get_flags(JSContext *ctx, JSValueConst this_val)
4553045536
if (res)
4553145537
*p++ = 'y';
4553245538
if (p == str)
45533-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
45539+
return js_empty_string(ctx->rt);
4553445540
return js_new_string8_len(ctx, str, p - str);
4553545541

4553645542
exception:
@@ -47255,7 +47261,7 @@ JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
4725547261
jsc->property_list = JS_UNDEFINED;
4725647262
jsc->gap = JS_UNDEFINED;
4725747263
jsc->b = &b_s;
47258-
jsc->empty = JS_AtomToString(ctx, JS_ATOM_empty_string);
47264+
jsc->empty = js_empty_string(ctx->rt);
4725947265
ret = JS_UNDEFINED;
4726047266
wrapper = JS_UNDEFINED;
4726147267

@@ -52299,7 +52305,7 @@ static JSValue get_date_string(JSContext *ctx, JSValueConst this_val,
5229952305
}
5230052306
if (!pos) {
5230152307
// XXX: should throw exception?
52302-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
52308+
return js_empty_string(ctx->rt);
5230352309
}
5230452310
return js_new_string8_len(ctx, buf, pos);
5230552311
}
@@ -53319,7 +53325,7 @@ static void JS_AddIntrinsicBasicObjects(JSContext *ctx)
5331953325
JS_NewAtomString(ctx, native_error_name[i]),
5332053326
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
5332153327
JS_DefinePropertyValue(ctx, proto, JS_ATOM_message,
53322-
JS_AtomToString(ctx, JS_ATOM_empty_string),
53328+
js_empty_string(ctx->rt),
5332353329
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
5332453330
ctx->native_error_proto[i] = proto;
5332553331
}
@@ -53514,7 +53520,7 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
5351453520
/* String */
5351553521
ctx->class_proto[JS_CLASS_STRING] = JS_NewObjectProtoClass(ctx, ctx->class_proto[JS_CLASS_OBJECT],
5351653522
JS_CLASS_STRING);
53517-
JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], JS_AtomToString(ctx, JS_ATOM_empty_string));
53523+
JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], js_empty_string(ctx->rt));
5351853524
obj = JS_NewGlobalCConstructor(ctx, "String", js_string_constructor, 1,
5351953525
ctx->class_proto[JS_CLASS_STRING]);
5352053526
JS_SetPropertyFunctionList(ctx, obj, js_string_funcs,

0 commit comments

Comments
 (0)