use crate::{
builtins::{
map::add_entries_from_iterable, BuiltInBuilder, BuiltInConstructor, BuiltInObject,
IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
js_string,
object::{internal_methods::get_prototype_from_constructor, ErasedVTableObject, JsObject},
property::Attribute,
realm::Realm,
string::StaticJsStrings,
symbol::JsSymbol,
Context, JsArgs, JsNativeError, JsResult, JsString, JsValue,
};
use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
type NativeWeakMap = boa_gc::WeakMap<ErasedVTableObject, JsValue>;
#[derive(Debug, Trace, Finalize)]
pub(crate) struct WeakMap;
impl IntrinsicObject for WeakMap {
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
fn init(realm: &Realm) {
let _timer = Profiler::global().start_event(std::any::type_name::<Self>(), "init");
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.property(
JsSymbol::to_string_tag(),
Self::NAME,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.method(Self::delete, js_string!("delete"), 1)
.method(Self::get, js_string!("get"), 1)
.method(Self::has, js_string!("has"), 1)
.method(Self::set, js_string!("set"), 2)
.build();
}
}
impl BuiltInObject for WeakMap {
const NAME: JsString = StaticJsStrings::WEAK_MAP;
const ATTRIBUTE: Attribute = Attribute::WRITABLE.union(Attribute::CONFIGURABLE);
}
impl BuiltInConstructor for WeakMap {
const LENGTH: usize = 0;
const P: usize = 5;
const SP: usize = 0;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::weak_map;
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
if new_target.is_undefined() {
return Err(JsNativeError::typ()
.with_message("WeakMap: cannot call constructor without `new`")
.into());
}
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::weak_map, context)?;
let map = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape(),
prototype,
NativeWeakMap::new(),
);
let iterable = args.get_or_undefined(0);
if iterable.is_null_or_undefined() {
return Ok(map.into());
}
let adder = map
.get(js_string!("set"), context)?
.as_function()
.ok_or_else(|| JsNativeError::typ().with_message("WeakMap: 'add' is not a function"))?;
add_entries_from_iterable(&map, iterable, &adder, context)
}
}
impl WeakMap {
pub(crate) fn delete(
this: &JsValue,
args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
let mut map = this
.as_object()
.and_then(JsObject::downcast_mut::<NativeWeakMap>)
.ok_or_else(|| {
JsNativeError::typ().with_message("WeakMap.delete: called with non-object value")
})?;
let Some(key) = args.get_or_undefined(0).as_object() else {
return Ok(false.into());
};
Ok(map.remove(key.inner()).is_some().into())
}
pub(crate) fn get(
this: &JsValue,
args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
let map = this
.as_object()
.and_then(JsObject::downcast_ref::<NativeWeakMap>)
.ok_or_else(|| {
JsNativeError::typ().with_message("WeakMap.get: called with non-object value")
})?;
let Some(key) = args.get_or_undefined(0).as_object() else {
return Ok(JsValue::undefined());
};
Ok(map.get(key.inner()).unwrap_or_default())
}
pub(crate) fn has(
this: &JsValue,
args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
let map = this
.as_object()
.and_then(JsObject::downcast_ref::<NativeWeakMap>)
.ok_or_else(|| {
JsNativeError::typ().with_message("WeakMap.has: called with non-object value")
})?;
let Some(key) = args.get_or_undefined(0).as_object() else {
return Ok(false.into());
};
Ok(map.contains_key(key.inner()).into())
}
pub(crate) fn set(
this: &JsValue,
args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
let mut map = this
.as_object()
.and_then(JsObject::downcast_mut::<NativeWeakMap>)
.ok_or_else(|| {
JsNativeError::typ().with_message("WeakMap.set: called with non-object value")
})?;
let key = args.get_or_undefined(0);
let Some(key) = key.as_object() else {
return Err(JsNativeError::typ()
.with_message(format!(
"WeakMap.set: expected target argument of type `object`, got target of type `{}`",
key.type_of()
)).into());
};
map.insert(key.inner(), args.get_or_undefined(1).clone());
Ok(this.clone())
}
}