Skip to content

Commit

Permalink
Support Symbol as WeakMap key
Browse files Browse the repository at this point in the history
  • Loading branch information
LanderlYoung committed Jul 31, 2024
1 parent eb1f2e9 commit 9f5b1ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 0 additions & 2 deletions doc/quickjs.texi
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ The following features are not supported yet:

@item FinalizationRegistry objects

@item Symbols as WeakMap keys

@end itemize

@subsection ECMA402
Expand Down
17 changes: 9 additions & 8 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,10 +1309,12 @@ static void js_init_weak_record(struct JSWeakRecord *wr, const struct JSWeakReco
static BOOL js_add_weak_ref(JSRuntime *rt, struct list_head **weak_ref_list, JSWeakRecord *wr);
static void js_unlink_weak_ref(JSWeakRecord *wr);
static void js_reset_weak_ref(JSRuntime *rt, struct list_head **weak_ref_list);
/*
static inline BOOL js_object_add_weak_ref(JSRuntime *rt, JSObject *p, JSWeakRecord *wr)
{
return js_add_weak_ref(rt, &p->weak_ref_list, wr);
}
*/
static inline void js_object_unlink_weak_ref(JSObject *p, JSWeakRecord *wr)
{
assert(p->weak_ref_list);
Expand Down Expand Up @@ -47504,7 +47506,7 @@ static const JSWeakRecordOperations js_map_weak_operations = {
};

static JSMapRecord *map_add_record(JSContext *ctx, JSMapState *s,
JSValueConst key)
JSValueConst key, JSValue *exception)
{
uint32_t h;
JSMapRecord *mr;
Expand All @@ -47516,10 +47518,10 @@ static JSMapRecord *map_add_record(JSContext *ctx, JSMapState *s,
mr->map = s;
mr->empty = FALSE;
if (s->is_weak) {
JSObject *p = JS_VALUE_GET_OBJ(key);
/* Add the weak reference */
struct list_head **weak_ref_list = js_get_target_weak_ref_list(ctx, key, exception);
js_init_weak_record(&mr->weak_record, &js_map_weak_operations);
if (!js_object_add_weak_ref(ctx->rt, p, &mr->weak_record)) {
/* Add the weak reference */
if (!weak_ref_list || !js_add_weak_ref(ctx->rt, weak_ref_list, &mr->weak_record)) {
js_free(ctx, mr);
return NULL;
}
Expand Down Expand Up @@ -47602,8 +47604,6 @@ static JSValue js_map_set(JSContext *ctx, JSValueConst this_val,
if (!s)
return JS_EXCEPTION;
key = map_normalize_key(ctx, argv[0]);
if (s->is_weak && !JS_IsObject(key))
return JS_ThrowTypeErrorNotAnObject(ctx);
if (magic & MAGIC_SET)
value = JS_UNDEFINED;
else
Expand All @@ -47612,9 +47612,10 @@ static JSValue js_map_set(JSContext *ctx, JSValueConst this_val,
if (mr) {
JS_FreeValue(ctx, mr->value);
} else {
mr = map_add_record(ctx, s, key);
JSValue exception = JS_EXCEPTION;
mr = map_add_record(ctx, s, key, &exception);
if (!mr)
return JS_EXCEPTION;
return exception;
}
mr->value = JS_DupValue(ctx, value);
return JS_DupValue(ctx, this_val);
Expand Down
11 changes: 11 additions & 0 deletions tests/test_builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,17 @@ function test_weak_map()
tab[i][0] = null; /* should remove the object from the WeakMap too */
}
/* the WeakMap should be empty here */

// test symbol as key
var symbol_key = Symbol("key");
a = new WeakMap();
a.set(symbol_key, "hello");
assert(a.has(symbol_key), true);
assert(a.get(symbol_key), "hello");

symbol_key = undefined;
assert(a.has(symbol_key), false);
assert(a.get(symbol_key), undefined);
}

function test_weak_ref()
Expand Down

0 comments on commit 9f5b1ef

Please sign in to comment.