Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading