|
@@ -1,11 +1,12 @@
|
|
/*
|
|
/*
|
|
- * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
|
|
+ * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
*
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
*/
|
|
|
|
|
|
#include <AK/HashTable.h>
|
|
#include <AK/HashTable.h>
|
|
#include <AK/TypeCasts.h>
|
|
#include <AK/TypeCasts.h>
|
|
|
|
+#include <LibJS/Runtime/AbstractOperations.h>
|
|
#include <LibJS/Runtime/WeakMapPrototype.h>
|
|
#include <LibJS/Runtime/WeakMapPrototype.h>
|
|
|
|
|
|
namespace JS {
|
|
namespace JS {
|
|
@@ -35,9 +36,9 @@ JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::delete_)
|
|
{
|
|
{
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto value = vm.argument(0);
|
|
auto value = vm.argument(0);
|
|
- if (!value.is_object())
|
|
|
|
|
|
+ if (!can_be_held_weakly(value))
|
|
return Value(false);
|
|
return Value(false);
|
|
- return Value(weak_map->values().remove(&value.as_object()));
|
|
|
|
|
|
+ return Value(weak_map->values().remove(&value.as_cell()));
|
|
}
|
|
}
|
|
|
|
|
|
// 24.3.3.3 WeakMap.prototype.get ( key ), https://tc39.es/ecma262/#sec-weakmap.prototype.get
|
|
// 24.3.3.3 WeakMap.prototype.get ( key ), https://tc39.es/ecma262/#sec-weakmap.prototype.get
|
|
@@ -45,10 +46,10 @@ JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::get)
|
|
{
|
|
{
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto value = vm.argument(0);
|
|
auto value = vm.argument(0);
|
|
- if (!value.is_object())
|
|
|
|
|
|
+ if (!can_be_held_weakly(value))
|
|
return js_undefined();
|
|
return js_undefined();
|
|
auto& values = weak_map->values();
|
|
auto& values = weak_map->values();
|
|
- auto result = values.find(&value.as_object());
|
|
|
|
|
|
+ auto result = values.find(&value.as_cell());
|
|
if (result == values.end())
|
|
if (result == values.end())
|
|
return js_undefined();
|
|
return js_undefined();
|
|
return result->value;
|
|
return result->value;
|
|
@@ -59,10 +60,10 @@ JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::has)
|
|
{
|
|
{
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto value = vm.argument(0);
|
|
auto value = vm.argument(0);
|
|
- if (!value.is_object())
|
|
|
|
|
|
+ if (!can_be_held_weakly(value))
|
|
return Value(false);
|
|
return Value(false);
|
|
auto& values = weak_map->values();
|
|
auto& values = weak_map->values();
|
|
- return Value(values.find(&value.as_object()) != values.end());
|
|
|
|
|
|
+ return Value(values.find(&value.as_cell()) != values.end());
|
|
}
|
|
}
|
|
|
|
|
|
// 24.3.3.5 WeakMap.prototype.set ( key, value ), https://tc39.es/ecma262/#sec-weakmap.prototype.set
|
|
// 24.3.3.5 WeakMap.prototype.set ( key, value ), https://tc39.es/ecma262/#sec-weakmap.prototype.set
|
|
@@ -70,9 +71,9 @@ JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::set)
|
|
{
|
|
{
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto* weak_map = TRY(typed_this_object(global_object));
|
|
auto value = vm.argument(0);
|
|
auto value = vm.argument(0);
|
|
- if (!value.is_object())
|
|
|
|
- return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, value.to_string_without_side_effects());
|
|
|
|
- weak_map->values().set(&value.as_object(), vm.argument(1));
|
|
|
|
|
|
+ if (!can_be_held_weakly(value))
|
|
|
|
+ return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, value.to_string_without_side_effects());
|
|
|
|
+ weak_map->values().set(&value.as_cell(), vm.argument(1));
|
|
return weak_map;
|
|
return weak_map;
|
|
}
|
|
}
|
|
|
|
|