}
/* '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;
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)
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
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)
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,
}
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);