use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
object::{internal_methods::get_prototype_from_constructor, JsObject, ObjectData},
property::Attribute,
realm::Realm,
string::utf16,
symbol::JsSymbol,
Context, JsArgs, JsNativeError, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace, WeakMap};
use boa_profiler::Profiler;
#[derive(Debug, Trace, Finalize)]
pub(crate) struct WeakSet;
impl IntrinsicObject for WeakSet {
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
fn init(realm: &Realm) {
let _timer = Profiler::global().start_event(Self::NAME, "init");
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.property(
JsSymbol::to_string_tag(),
Self::NAME,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.method(Self::add, "add", 1)
.method(Self::delete, "delete", 1)
.method(Self::has, "has", 1)
.build();
}
}
impl BuiltInObject for WeakSet {
const NAME: &'static str = "WeakSet";
const ATTRIBUTE: Attribute = Attribute::WRITABLE.union(Attribute::CONFIGURABLE);
}
impl BuiltInConstructor for WeakSet {
const LENGTH: usize = 0;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::weak_set;
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
if new_target.is_undefined() {
return Err(JsNativeError::typ()
.with_message("WeakSet: cannot call constructor without `new`")
.into());
}
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::weak_set, context)?;
let weak_set = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape(),
prototype,
ObjectData::weak_set(WeakMap::new()),
);
let iterable = args.get_or_undefined(0);
if iterable.is_null_or_undefined() {
return Ok(weak_set.into());
}
let adder = weak_set.get(utf16!("add"), context)?;
let adder = adder
.as_callable()
.ok_or_else(|| JsNativeError::typ().with_message("WeakSet: 'add' is not a function"))?;
let mut iterator_record = iterable.clone().get_iterator(context, None, None)?;
while !iterator_record.step(context)? {
let next = iterator_record.value(context)?;
if let Err(status) = adder.call(&weak_set.clone().into(), &[next], context) {
return iterator_record.close(Err(status), context);
}
}
Ok(weak_set.into())
}
}
impl WeakSet {
pub(crate) fn add(
this: &JsValue,
args: &[JsValue],
_context: &mut Context<'_>,
) -> JsResult<JsValue> {
let Some(obj) = this.as_object() else {
return Err(JsNativeError::typ()
.with_message("WeakSet.add: called with non-object value")
.into());
};
let mut obj_borrow = obj.borrow_mut();
let o = obj_borrow.as_weak_set_mut().ok_or_else(|| {
JsNativeError::typ().with_message("WeakSet.add: called with non-object value")
})?;
let value = args.get_or_undefined(0);
let Some(value) = args.get_or_undefined(0).as_object() else {
return Err(JsNativeError::typ()
.with_message(format!(
"WeakSet.add: expected target argument of type `object`, got target of type `{}`",
value.type_of()
)).into());
};
if o.contains_key(value.inner()) {
return Ok(this.clone());
}
o.insert(value.inner(), ());
Ok(this.clone())
}
pub(crate) fn delete(
this: &JsValue,
args: &[JsValue],
_context: &mut Context<'_>,
) -> JsResult<JsValue> {
let Some(obj) = this.as_object() else {
return Err(JsNativeError::typ()
.with_message("WeakSet.delete: called with non-object value")
.into());
};
let mut obj_borrow = obj.borrow_mut();
let o = obj_borrow.as_weak_set_mut().ok_or_else(|| {
JsNativeError::typ().with_message("WeakSet.delete: called with non-object value")
})?;
let value = args.get_or_undefined(0);
let Some(value) = value.as_object() else {
return Ok(false.into());
};
Ok(o.remove(value.inner()).is_some().into())
}
pub(crate) fn has(
this: &JsValue,
args: &[JsValue],
_context: &mut Context<'_>,
) -> JsResult<JsValue> {
let Some(obj) = this.as_object() else {
return Err(JsNativeError::typ()
.with_message("WeakSet.has: called with non-object value")
.into());
};
let obj_borrow = obj.borrow();
let o = obj_borrow.as_weak_set().ok_or_else(|| {
JsNativeError::typ().with_message("WeakSet.has: called with non-object value")
})?;
let value = args.get_or_undefined(0);
let Some(value) = value.as_object() else {
return Ok(false.into());
};
Ok(o.contains_key(value.inner()).into())
}
}