Skip to content
Merged
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
47 changes: 47 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -49057,6 +49057,45 @@ static JSValue js_map_get(JSContext *ctx, JSValueConst this_val,
return js_dup(mr->value);
}

static JSValue js_map_getOrInsert(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
bool computed = magic & 1;
JSClassID class_id = magic >> 1;
JSMapState *s = JS_GetOpaque2(ctx, this_val, class_id);
JSMapRecord *mr;
JSValueConst key;
JSValue value;

if (!s)
return JS_EXCEPTION;
if (computed && !JS_IsFunction(ctx, argv[1]))
return JS_ThrowTypeError(ctx, "not a function");
key = map_normalize_key_const(ctx, argv[0]);
if (s->is_weak && !is_valid_weakref_target(key))
return JS_ThrowTypeError(ctx, "invalid value used as WeakMap key");
mr = map_find_record(ctx, s, key);
if (!mr) {
if (computed) {
value = JS_Call(ctx, argv[1], JS_UNDEFINED, 1, &key);
if (JS_IsException(value))
return JS_EXCEPTION;
mr = map_find_record(ctx, s, key);
if (mr)
map_delete_record(ctx->rt, s, mr);
} else {
value = js_dup(argv[1]);
}
mr = map_add_record(ctx, s, key);
if (!mr) {
JS_FreeValue(ctx, value);
return JS_EXCEPTION;
}
mr->value = value;
}
return js_dup(mr->value);
}

static JSValue js_map_has(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
Expand Down Expand Up @@ -50115,6 +50154,10 @@ static const JSCFunctionListEntry js_set_funcs[] = {
static const JSCFunctionListEntry js_map_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("set", 2, js_map_set, 0 ),
JS_CFUNC_MAGIC_DEF("get", 1, js_map_get, 0 ),
JS_CFUNC_MAGIC_DEF("getOrInsert", 2, js_map_getOrInsert,
JS_CLASS_MAP<<1 | /*computed*/false ),
JS_CFUNC_MAGIC_DEF("getOrInsertComputed", 2, js_map_getOrInsert,
JS_CLASS_MAP<<1 | /*computed*/true ),
JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, 0 ),
JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, 0 ),
JS_CFUNC_MAGIC_DEF("clear", 0, js_map_clear, 0 ),
Expand Down Expand Up @@ -50161,6 +50204,10 @@ static const JSCFunctionListEntry js_set_iterator_proto_funcs[] = {
static const JSCFunctionListEntry js_weak_map_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("set", 2, js_map_set, MAGIC_WEAK ),
JS_CFUNC_MAGIC_DEF("get", 1, js_map_get, MAGIC_WEAK ),
JS_CFUNC_MAGIC_DEF("getOrInsert", 2, js_map_getOrInsert,
JS_CLASS_WEAKMAP<<1 | /*computed*/false ),
JS_CFUNC_MAGIC_DEF("getOrInsertComputed", 2, js_map_getOrInsert,
JS_CLASS_WEAKMAP<<1 | /*computed*/true ),
JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, MAGIC_WEAK ),
JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, MAGIC_WEAK ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "WeakMap", JS_PROP_CONFIGURABLE ),
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Uint32Array
Uint8Array
uint8array-base64=skip
Uint8ClampedArray
upsert=skip
upsert
WeakMap
WeakRef
WeakSet
Expand Down
Loading