Skip to content

Commit b5dea9c

Browse files
authored
Introduce js_empty_string (#1147)
Replace the `JS_AtomToString(ctx, JS_ATOM_empty_string)` pattern with a dedicated function.
1 parent 3c5f8ab commit b5dea9c

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

quickjs.c

Lines changed: 24 additions & 18 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
{
@@ -39362,7 +39368,7 @@ static JSValue js_function_bind(JSContext *ctx, JSValueConst this_val,
3936239368
goto exception;
3936339369
if (!JS_IsString(name1)) {
3936439370
JS_FreeValue(ctx, name1);
39365-
name1 = JS_AtomToString(ctx, JS_ATOM_empty_string);
39371+
name1 = js_empty_string(ctx->rt);
3936639372
}
3936739373
name1 = JS_ConcatString3(ctx, "bound ", name1, "");
3936839374
if (JS_IsException(name1))
@@ -39413,7 +39419,7 @@ static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
3941339419
suff = "() {\n [native code]\n}";
3941439420
name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
3941539421
if (JS_IsUndefined(name))
39416-
name = JS_AtomToString(ctx, JS_ATOM_empty_string);
39422+
name = js_empty_string(ctx->rt);
3941739423
return JS_ConcatString3(ctx, pref, name, suff);
3941839424
}
3941939425
}
@@ -39575,7 +39581,7 @@ static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val,
3957539581

3957639582
msg = JS_GetProperty(ctx, this_val, JS_ATOM_message);
3957739583
if (JS_IsUndefined(msg))
39578-
msg = JS_AtomToString(ctx, JS_ATOM_empty_string);
39584+
msg = js_empty_string(ctx->rt);
3957939585
else
3958039586
msg = JS_ToStringFree(ctx, msg);
3958139587
if (JS_IsException(msg)) {
@@ -43051,7 +43057,7 @@ static JSValue js_string_constructor(JSContext *ctx, JSValueConst new_target,
4305143057
{
4305243058
JSValue val, obj;
4305343059
if (argc == 0) {
43054-
val = JS_AtomToString(ctx, JS_ATOM_empty_string);
43060+
val = js_empty_string(ctx->rt);
4305543061
} else {
4305643062
if (JS_IsUndefined(new_target) && JS_IsSymbol(argv[0])) {
4305743063
JSAtomStruct *p = JS_VALUE_GET_PTR(argv[0]);
@@ -43301,7 +43307,7 @@ static JSValue js_string_charAt(JSContext *ctx, JSValueConst this_val,
4330143307
return JS_EXCEPTION;
4330243308
}
4330343309
if (idx < 0 || idx >= p->len) {
43304-
ret = JS_AtomToString(ctx, JS_ATOM_empty_string);
43310+
ret = js_empty_string(ctx->rt);
4330543311
} else {
4330643312
c = string_get(p, idx);
4330743313
ret = js_new_string_char(ctx, c);
@@ -45331,7 +45337,7 @@ static JSValue js_regexp_constructor(JSContext *ctx, JSValueConst new_target,
4533145337
flags = js_dup(flags1);
4533245338
}
4533345339
if (JS_IsUndefined(pattern)) {
45334-
pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
45340+
pattern = js_empty_string(ctx->rt);
4533545341
} else {
4533645342
val = pattern;
4533745343
pattern = JS_ToString(ctx, val);
@@ -45373,7 +45379,7 @@ static JSValue js_regexp_compile(JSContext *ctx, JSValueConst this_val,
4537345379
} else {
4537445380
bc = JS_UNDEFINED;
4537545381
if (JS_IsUndefined(pattern1))
45376-
pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
45382+
pattern = js_empty_string(ctx->rt);
4537745383
else
4537845384
pattern = JS_ToString(ctx, pattern1);
4537945385
if (JS_IsException(pattern))
@@ -45531,7 +45537,7 @@ static JSValue js_regexp_get_flags(JSContext *ctx, JSValueConst this_val)
4553145537
if (res)
4553245538
*p++ = 'y';
4553345539
if (p == str)
45534-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
45540+
return js_empty_string(ctx->rt);
4553545541
return js_new_string8_len(ctx, str, p - str);
4553645542

4553745543
exception:
@@ -47256,7 +47262,7 @@ JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
4725647262
jsc->property_list = JS_UNDEFINED;
4725747263
jsc->gap = JS_UNDEFINED;
4725847264
jsc->b = &b_s;
47259-
jsc->empty = JS_AtomToString(ctx, JS_ATOM_empty_string);
47265+
jsc->empty = js_empty_string(ctx->rt);
4726047266
ret = JS_UNDEFINED;
4726147267
wrapper = JS_UNDEFINED;
4726247268

@@ -52300,7 +52306,7 @@ static JSValue get_date_string(JSContext *ctx, JSValueConst this_val,
5230052306
}
5230152307
if (!pos) {
5230252308
// XXX: should throw exception?
52303-
return JS_AtomToString(ctx, JS_ATOM_empty_string);
52309+
return js_empty_string(ctx->rt);
5230452310
}
5230552311
return js_new_string8_len(ctx, buf, pos);
5230652312
}
@@ -53320,7 +53326,7 @@ static void JS_AddIntrinsicBasicObjects(JSContext *ctx)
5332053326
JS_NewAtomString(ctx, native_error_name[i]),
5332153327
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
5332253328
JS_DefinePropertyValue(ctx, proto, JS_ATOM_message,
53323-
JS_AtomToString(ctx, JS_ATOM_empty_string),
53329+
js_empty_string(ctx->rt),
5332453330
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
5332553331
ctx->native_error_proto[i] = proto;
5332653332
}
@@ -53515,7 +53521,7 @@ void JS_AddIntrinsicBaseObjects(JSContext *ctx)
5351553521
/* String */
5351653522
ctx->class_proto[JS_CLASS_STRING] = JS_NewObjectProtoClass(ctx, ctx->class_proto[JS_CLASS_OBJECT],
5351753523
JS_CLASS_STRING);
53518-
JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], JS_AtomToString(ctx, JS_ATOM_empty_string));
53524+
JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], js_empty_string(ctx->rt));
5351953525
obj = JS_NewGlobalCConstructor(ctx, "String", js_string_constructor, 1,
5352053526
ctx->class_proto[JS_CLASS_STRING]);
5352153527
JS_SetPropertyFunctionList(ctx, obj, js_string_funcs,
@@ -57838,7 +57844,7 @@ static JSValue js_domexception_constructor0(JSContext *ctx, JSValueConst new_tar
5783857844
if (!JS_IsUndefined(argv[0]))
5783957845
message = JS_ToString(ctx, argv[0]);
5784057846
else
57841-
message = JS_AtomToString(ctx, JS_ATOM_empty_string);
57847+
message = js_empty_string(ctx->rt);
5784257848
if (JS_IsException(message))
5784357849
goto fail1;
5784457850
if (!JS_IsUndefined(argv[1]))

0 commit comments

Comments
 (0)