Skip to content

Commit 0319d76

Browse files
committed
improve: use ctor when map string primitive to string object
1 parent 049ed4d commit 0319d76

File tree

7 files changed

+13
-16
lines changed

7 files changed

+13
-16
lines changed

quickjs/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set(MI_OVERRIDE OFF)
1515
# interprocedural optimization (IPO/LTO)
1616
include(CheckIPOSupported)
1717
check_ipo_supported(RESULT supported OUTPUT error)
18-
if( supported )
18+
if( supported AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
1919
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
2020
endif()
2121

quickjs/scripts/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -x
55
rm -rf build bin lib
66
mkdir build
77
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -B build -S .
8-
cmake --build ./build --target qjs -j 8
8+
cmake --build ./build --target qjs run-test262 -j 8

quickjs/scripts/test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ if [ ! -d test262 ]; then
77
fi
88

99
cd test262
10-
git checkout ac1c3546c393d89b37483c3a32eddfe7dd1903a7
11-
patch -p1 < ../tests/test262.patch
10+
# git checkout ac1c3546c393d89b37483c3a32eddfe7dd1903a7
11+
# patch -p1 < ../tests/test262.patch
1212
cd ..
1313
touch test262_errors.txt
1414
./bin/run-test262 -m -c test262.conf -a

quickjs/src/core/builtins/js-object.c

+2-12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../runtime.h"
3131
#include "../string.h"
3232
#include "js-operator.h"
33+
#include "js-string.h"
3334

3435
void js_object_data_finalizer(JSRuntime* rt, JSValue val) {
3536
JSObject* p = JS_VALUE_GET_OBJ(val);
@@ -72,18 +73,7 @@ JSValue JS_ToObject(JSContext* ctx, JSValueConst val) {
7273
obj = JS_NewObjectClass(ctx, JS_CLASS_NUMBER);
7374
goto set_value;
7475
case JS_TAG_STRING:
75-
/* XXX: should call the string constructor */
76-
{
77-
JSString* p1 = JS_VALUE_GET_STRING(val);
78-
obj = JS_NewObjectClass(ctx, JS_CLASS_STRING);
79-
JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, JS_NewInt32(ctx, p1->len), 0);
80-
81-
// set u.array.count to make it array-like
82-
// we check `idx` and `len` in JS_GetPropertyValue
83-
JSObject* p = JS_VALUE_GET_OBJ(obj);
84-
p->u.array.count = p1->len;
85-
}
86-
goto set_value;
76+
return js_string_constructor(ctx, ctx->string_ctor, 1, (JSValueConst *)&val);
8777
case JS_TAG_BOOL:
8878
obj = JS_NewObjectClass(ctx, JS_CLASS_BOOLEAN);
8979
goto set_value;

quickjs/src/core/builtins/js-string.c

+5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ JSValue js_string_constructor(JSContext* ctx, JSValueConst new_target, int argc,
139139
if (!JS_IsException(obj)) {
140140
JS_SetObjectData(ctx, obj, val);
141141
JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, JS_NewInt32(ctx, p1->len), 0);
142+
143+
// set u.array.count to make it array-like
144+
// See: `idx` and `len` in JS_GetPropertyValue
145+
JSObject* p = JS_VALUE_GET_OBJ(obj);
146+
p->u.array.count = p1->len;
142147
}
143148
return obj;
144149
} else {

quickjs/src/core/runtime.c

+1
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,7 @@ void JS_AddIntrinsicBaseObjects(JSContext* ctx) {
25582558
ctx->class_proto[JS_CLASS_STRING] = JS_NewObjectProtoClass(ctx, ctx->class_proto[JS_CLASS_OBJECT], JS_CLASS_STRING);
25592559
JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], JS_AtomToString(ctx, JS_ATOM_empty_string));
25602560
obj = JS_NewGlobalCConstructor(ctx, "String", js_string_constructor, 1, ctx->class_proto[JS_CLASS_STRING]);
2561+
ctx->string_ctor = JS_DupValue(ctx, obj);
25612562
JS_SetPropertyFunctionList(ctx, obj, js_string_funcs, countof(js_string_funcs));
25622563
JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_STRING], js_string_proto_funcs, countof(js_string_proto_funcs));
25632564

quickjs/src/core/types.h

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ struct JSContext {
356356
JSValue *class_proto;
357357
JSValue function_proto;
358358
JSValue function_ctor;
359+
JSValue string_ctor;
359360
JSValue array_ctor;
360361
JSValue regexp_ctor;
361362
JSValue promise_ctor;

0 commit comments

Comments
 (0)