From: Fabrice Bellard Date: Sat, 26 Sep 2026 10:46:23 +0000 (+0200) Subject: added JS_NewSymbol() X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=87dc7cbaa8b50fabf7f1432101d68bfb71156964;p=quickjs.git added JS_NewSymbol() --- diff --git a/quickjs.c b/quickjs.c index e3716a4..4ca6831 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3514,7 +3514,7 @@ static JSAtom JS_NewAtomInt64(JSContext *ctx, int64_t n) } /* 'p' is freed */ -static JSValue JS_NewSymbol(JSContext *ctx, JSString *p, int atom_type) +static JSValue JS_NewSymbolInternal(JSContext *ctx, JSString *p, int atom_type) { JSRuntime *rt = ctx->rt; JSAtom atom; @@ -3524,6 +3524,28 @@ static JSValue JS_NewSymbol(JSContext *ctx, JSString *p, int atom_type) return JS_MKPTR(JS_TAG_SYMBOL, rt->atom_array[atom]); } +/* description is UTF-8 encoded or NULL */ +JSValue JS_NewSymbol(JSContext *ctx, const char *description, BOOL is_global) +{ + JSValue str; + int atom_type; + + if (description == NULL) { + if (!is_global) { + /* Local symbol without description: Symbol() */ + return JS_NewSymbolInternal(ctx, NULL, JS_ATOM_TYPE_SYMBOL); + } + /* Global symbol without description: Symbol.for() + Per ES spec, ToString(undefined) becomes "undefined" */ + description = "undefined"; + } + str = JS_NewString(ctx, description); + if (JS_IsException(str)) + return JS_EXCEPTION; + atom_type = is_global ? JS_ATOM_TYPE_GLOBAL_SYMBOL : JS_ATOM_TYPE_SYMBOL; + return JS_NewSymbolInternal(ctx, JS_VALUE_GET_STRING(str), atom_type); +} + /* descr must be a non-numeric string atom */ static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr, int atom_type) @@ -3535,7 +3557,7 @@ static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr, assert(descr < rt->atom_size); p = rt->atom_array[descr]; JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p)); - return JS_NewSymbol(ctx, p, atom_type); + return JS_NewSymbolInternal(ctx, p, atom_type); } #define ATOM_GET_STR_BUF_SIZE 64 @@ -51581,7 +51603,7 @@ static JSValue js_symbol_constructor(JSContext *ctx, JSValueConst new_target, return JS_EXCEPTION; p = JS_VALUE_GET_STRING(str); } - return JS_NewSymbol(ctx, p, JS_ATOM_TYPE_SYMBOL); + return JS_NewSymbolInternal(ctx, p, JS_ATOM_TYPE_SYMBOL); } static JSValue js_thisSymbolValue(JSContext *ctx, JSValueConst this_val) @@ -51653,7 +51675,7 @@ static JSValue js_symbol_for(JSContext *ctx, JSValueConst this_val, str = JS_ToString(ctx, argv[0]); if (JS_IsException(str)) return JS_EXCEPTION; - return JS_NewSymbol(ctx, JS_VALUE_GET_STRING(str), JS_ATOM_TYPE_GLOBAL_SYMBOL); + return JS_NewSymbolInternal(ctx, JS_VALUE_GET_STRING(str), JS_ATOM_TYPE_GLOBAL_SYMBOL); } static JSValue js_symbol_keyFor(JSContext *ctx, JSValueConst this_val, diff --git a/quickjs.h b/quickjs.h index 476d735..d349d95 100644 --- a/quickjs.h +++ b/quickjs.h @@ -759,6 +759,8 @@ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1) } void JS_FreeCString(JSContext *ctx, const char *ptr); +JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global); + JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto, JSClassID class_id); JSValue JS_NewObjectClass(JSContext *ctx, int class_id); JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto);