From f5f1b3c2d46c96eeb63fbc419c5f47613815b6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 15 Jun 2026 14:25:05 +0200 Subject: [PATCH] Expose `JS_NewSymbol()` --- quickjs.c | 28 ++++++++++++++++++++++++---- quickjs.h | 1 + 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/quickjs.c b/quickjs.c index fa5a7a9d0..54040acc7 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; @@ -3535,7 +3535,27 @@ 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); +} + +JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global) +{ + JSAtom atom; + int atom_type; + JSValue symbol; + + if (description == NULL) { + if (!is_global) + return JS_NewSymbolInternal(ctx, NULL, JS_ATOM_TYPE_SYMBOL); + description = "undefined"; + } + atom = JS_NewAtom(ctx, description); + if (atom == JS_ATOM_NULL) + return JS_EXCEPTION; + atom_type = is_global ? JS_ATOM_TYPE_GLOBAL_SYMBOL : JS_ATOM_TYPE_SYMBOL; + symbol = JS_NewSymbolFromAtom(ctx, atom, atom_type); + JS_FreeAtom(ctx, atom); + return symbol; } #define ATOM_GET_STR_BUF_SIZE 64 @@ -51354,7 +51374,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) @@ -51426,7 +51446,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 476d73513..b5badd5ad 100644 --- a/quickjs.h +++ b/quickjs.h @@ -746,6 +746,7 @@ static inline JSValue JS_NewString(JSContext *ctx, const char *str) return JS_NewStringLen(ctx, str, strlen(str)); } JSValue JS_NewAtomString(JSContext *ctx, const char *str); +JSValue JS_NewSymbol(JSContext *ctx, const char *description, JS_BOOL is_global); JSValue JS_ToString(JSContext *ctx, JSValueConst val); JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val); const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, JS_BOOL cesu8);