diff --git a/nova_vm/Cargo.toml b/nova_vm/Cargo.toml index c2abba55b..103c74895 100644 --- a/nova_vm/Cargo.toml +++ b/nova_vm/Cargo.toml @@ -23,12 +23,13 @@ sonic-rs = { workspace = true, optional = true } wtf8 = { workspace = true } [features] -default = ["math", "json", "date", "array-buffer", "shared-array-buffer"] +default = ["math", "json", "date", "array-buffer", "shared-array-buffer", "weak-refs"] math = [] json = ["sonic-rs"] date = [] array-buffer = [] shared-array-buffer = [] +weak-refs = [] typescript = [] [build-dependencies] diff --git a/nova_vm/src/ecmascript/builtins.rs b/nova_vm/src/ecmascript/builtins.rs index 1042f8a0f..09f4fef00 100644 --- a/nova_vm/src/ecmascript/builtins.rs +++ b/nova_vm/src/ecmascript/builtins.rs @@ -45,8 +45,11 @@ pub(crate) mod structured_data; pub(crate) mod text_processing; #[cfg(feature = "array-buffer")] pub(crate) mod typed_array; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_map; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_ref; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_set; pub(crate) use arguments::*; diff --git a/nova_vm/src/ecmascript/builtins/keyed_collections.rs b/nova_vm/src/ecmascript/builtins/keyed_collections.rs index c1c82987a..85dacf765 100644 --- a/nova_vm/src/ecmascript/builtins/keyed_collections.rs +++ b/nova_vm/src/ecmascript/builtins/keyed_collections.rs @@ -4,5 +4,7 @@ pub(crate) mod map_objects; pub(crate) mod set_objects; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_map_objects; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_set_objects; diff --git a/nova_vm/src/ecmascript/builtins/managing_memory.rs b/nova_vm/src/ecmascript/builtins/managing_memory.rs index 46f1a06b0..aa1d635a0 100644 --- a/nova_vm/src/ecmascript/builtins/managing_memory.rs +++ b/nova_vm/src/ecmascript/builtins/managing_memory.rs @@ -3,4 +3,5 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. pub(crate) mod finalization_registry_objects; +#[cfg(feature = "weak-refs")] pub(crate) mod weak_ref_objects; diff --git a/nova_vm/src/ecmascript/builtins/ordinary.rs b/nova_vm/src/ecmascript/builtins/ordinary.rs index 1a223535f..66dd63dc3 100644 --- a/nova_vm/src/ecmascript/builtins/ordinary.rs +++ b/nova_vm/src/ecmascript/builtins/ordinary.rs @@ -42,15 +42,17 @@ use super::{ promise::data::PromiseHeapData, regexp::RegExpHeapData, set::data::SetHeapData, - weak_map::data::WeakMapHeapData, - weak_ref::data::WeakRefHeapData, - weak_set::data::WeakSetHeapData, ArrayHeapData, }; #[cfg(feature = "array-buffer")] use super::{ data_view::data::DataViewHeapData, typed_array::data::TypedArrayHeapData, ArrayBufferHeapData, }; +#[cfg(feature = "weak-refs")] +use super::{ + weak_map::data::WeakMapHeapData, weak_ref::data::WeakRefHeapData, + weak_set::data::WeakSetHeapData, +}; impl Index for Agent { type Output = ObjectHeapData; @@ -971,8 +973,11 @@ pub(crate) fn ordinary_object_create_with_intrinsics( .heap .create(TypedArrayHeapData::default()) .into_object(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakMap => agent.heap.create(WeakMapHeapData::default()).into_object(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakRef => agent.heap.create(WeakRefHeapData::default()).into_object(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakSet => agent.heap.create(WeakSetHeapData::default()).into_object(), }; @@ -1110,8 +1115,11 @@ pub(crate) fn get_prototype_from_constructor( #[cfg(feature = "array-buffer")] ProtoIntrinsics::Uint8Array => Some(intrinsics.uint8_array().into_function()), ProtoIntrinsics::UriError => Some(intrinsics.uri_error().into_function()), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakMap => Some(intrinsics.weak_map().into_function()), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakRef => Some(intrinsics.weak_ref().into_function()), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakSet => Some(intrinsics.weak_set().into_function()), }; if Some(constructor) == intrinsic_constructor { diff --git a/nova_vm/src/ecmascript/execution/realm.rs b/nova_vm/src/ecmascript/execution/realm.rs index 352bd814c..1f7f4a8d8 100644 --- a/nova_vm/src/ecmascript/execution/realm.rs +++ b/nova_vm/src/ecmascript/execution/realm.rs @@ -941,40 +941,42 @@ pub(crate) fn set_default_global_bindings( define_property_or_throw(agent, global, name, desc)?; // 19.3.38 WeakMap ( . . . ) - let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakMap); - let value = agent.get_realm(realm_id).intrinsics().weak_map(); - let desc = PropertyDescriptor { - value: Some(value.into_value()), - writable: Some(true), - enumerable: Some(false), - configurable: Some(true), - ..Default::default() - }; - define_property_or_throw(agent, global, name, desc)?; - - // 19.3.39 WeakRef ( . . . ) - let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakRef); - let value = agent.get_realm(realm_id).intrinsics().weak_ref(); - let desc = PropertyDescriptor { - value: Some(value.into_value()), - writable: Some(true), - enumerable: Some(false), - configurable: Some(true), - ..Default::default() - }; - define_property_or_throw(agent, global, name, desc)?; + #[cfg(feature = "weak-refs")] + { + let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakMap); + let value = agent.get_realm(realm_id).intrinsics().weak_map(); + let desc = PropertyDescriptor { + value: Some(value.into_value()), + writable: Some(true), + enumerable: Some(false), + configurable: Some(true), + ..Default::default() + }; + define_property_or_throw(agent, global, name, desc)?; + // 19.3.39 WeakRef ( . . . ) + let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakRef); + let value = agent.get_realm(realm_id).intrinsics().weak_ref(); + let desc = PropertyDescriptor { + value: Some(value.into_value()), + writable: Some(true), + enumerable: Some(false), + configurable: Some(true), + ..Default::default() + }; + define_property_or_throw(agent, global, name, desc)?; - // 19.3.40 WeakSet ( . . . ) - let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakSet); - let value = agent.get_realm(realm_id).intrinsics().weak_set(); - let desc = PropertyDescriptor { - value: Some(value.into_value()), - writable: Some(true), - enumerable: Some(false), - configurable: Some(true), - ..Default::default() - }; - define_property_or_throw(agent, global, name, desc)?; + // 19.3.40 WeakSet ( . . . ) + let name = PropertyKey::from(BUILTIN_STRING_MEMORY.WeakSet); + let value = agent.get_realm(realm_id).intrinsics().weak_set(); + let desc = PropertyDescriptor { + value: Some(value.into_value()), + writable: Some(true), + enumerable: Some(false), + configurable: Some(true), + ..Default::default() + }; + define_property_or_throw(agent, global, name, desc)?; + } } // 19.4 Other Properties of the Global Object diff --git a/nova_vm/src/ecmascript/execution/realm/intrinsics.rs b/nova_vm/src/ecmascript/execution/realm/intrinsics.rs index 94211bc0e..ecd2e83bd 100644 --- a/nova_vm/src/ecmascript/execution/realm/intrinsics.rs +++ b/nova_vm/src/ecmascript/execution/realm/intrinsics.rs @@ -3,6 +3,19 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. use super::RealmIdentifier; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::keyed_collections::{ + weak_map_objects::{ + weak_map_constructor::WeakMapConstructor, weak_map_prototype::WeakMapPrototype, + }, + weak_set_objects::{ + weak_set_constructor::WeakSetConstructor, weak_set_prototype::WeakSetPrototype, + }, +}; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::managing_memory::weak_ref_objects::{ + weak_ref_constructor::WeakRefConstructor, weak_ref_prototype::WeakRefPrototype, +}; #[cfg(feature = "date")] use crate::ecmascript::builtins::numbers_and_dates::date_objects::{ date_constructor::DateConstructor, date_prototype::DatePrototype, @@ -76,21 +89,10 @@ use crate::{ set_iterator_objects::set_iterator_prototype::SetIteratorPrototype, set_prototype::SetPrototype, }, - weak_map_objects::{ - weak_map_constructor::WeakMapConstructor, weak_map_prototype::WeakMapPrototype, - }, - weak_set_objects::{ - weak_set_constructor::WeakSetConstructor, weak_set_prototype::WeakSetPrototype, - }, }, - managing_memory::{ - finalization_registry_objects::{ - finalization_registry_constructor::FinalizationRegistryConstructor, - finalization_registry_prototype::FinalizationRegistryPrototype, - }, - weak_ref_objects::{ - weak_ref_constructor::WeakRefConstructor, weak_ref_prototype::WeakRefPrototype, - }, + managing_memory::finalization_registry_objects::{ + finalization_registry_constructor::FinalizationRegistryConstructor, + finalization_registry_prototype::FinalizationRegistryPrototype, }, primitive_objects::PrimitiveObject, reflection::{proxy_constructor::ProxyConstructor, reflect_object::ReflectObject}, @@ -218,8 +220,11 @@ pub enum ProtoIntrinsics { #[cfg(feature = "array-buffer")] Uint8Array, UriError, + #[cfg(feature = "weak-refs")] WeakMap, + #[cfg(feature = "weak-refs")] WeakRef, + #[cfg(feature = "weak-refs")] WeakSet, } @@ -304,9 +309,13 @@ impl Intrinsics { SetPrototype::create_intrinsic(agent, realm); SetConstructor::create_intrinsic(agent, realm); SetIteratorPrototype::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakMapPrototype::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakMapConstructor::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakSetPrototype::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakSetConstructor::create_intrinsic(agent, realm); #[cfg(feature = "array-buffer")] ArrayBufferPrototype::create_intrinsic(agent, realm); @@ -323,7 +332,9 @@ impl Intrinsics { AtomicsObject::create_intrinsic(agent, realm); #[cfg(feature = "json")] JSONObject::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakRefPrototype::create_intrinsic(agent, realm); + #[cfg(feature = "weak-refs")] WeakRefConstructor::create_intrinsic(agent, realm); FinalizationRegistryPrototype::create_intrinsic(agent, realm); FinalizationRegistryConstructor::create_intrinsic(agent, realm); @@ -410,8 +421,11 @@ impl Intrinsics { ProtoIntrinsics::Uint32Array => self.uint32_array_prototype().into(), #[cfg(feature = "array-buffer")] ProtoIntrinsics::Uint8Array => self.uint8_array_prototype().into(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakMap => self.weak_map_prototype().into(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakRef => self.weak_ref_prototype().into(), + #[cfg(feature = "weak-refs")] ProtoIntrinsics::WeakSet => self.weak_set_prototype().into(), } } @@ -1518,6 +1532,7 @@ impl Intrinsics { } /// %WeakMap.prototype% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_map_prototype(&self) -> OrdinaryObject { IntrinsicObjectIndexes::WeakMapPrototype .get_object_index(self.object_index_base) @@ -1525,17 +1540,20 @@ impl Intrinsics { } /// %WeakMap% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_map(&self) -> BuiltinFunction { IntrinsicConstructorIndexes::WeakMap .get_builtin_function_index(self.builtin_function_index_base) .into() } + #[cfg(feature = "weak-refs")] pub(crate) fn weak_map_base_object(&self) -> ObjectIndex { IntrinsicConstructorIndexes::WeakMap.get_object_index(self.object_index_base) } /// %WeakRef.prototype% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_ref_prototype(&self) -> OrdinaryObject { IntrinsicObjectIndexes::WeakRefPrototype .get_object_index(self.object_index_base) @@ -1543,17 +1561,20 @@ impl Intrinsics { } /// %WeakRef% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_ref(&self) -> BuiltinFunction { IntrinsicConstructorIndexes::WeakRef .get_builtin_function_index(self.builtin_function_index_base) .into() } + #[cfg(feature = "weak-refs")] pub(crate) fn weak_ref_base_object(&self) -> ObjectIndex { IntrinsicConstructorIndexes::WeakRef.get_object_index(self.object_index_base) } /// %WeakSet.prototype% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_set_prototype(&self) -> OrdinaryObject { IntrinsicObjectIndexes::WeakSetPrototype .get_object_index(self.object_index_base) @@ -1561,12 +1582,14 @@ impl Intrinsics { } /// %WeakSet% + #[cfg(feature = "weak-refs")] pub(crate) fn weak_set(&self) -> BuiltinFunction { IntrinsicConstructorIndexes::WeakSet .get_builtin_function_index(self.builtin_function_index_base) .into() } + #[cfg(feature = "weak-refs")] pub(crate) fn weak_set_base_object(&self) -> ObjectIndex { IntrinsicConstructorIndexes::WeakSet.get_object_index(self.object_index_base) } @@ -1735,11 +1758,17 @@ impl HeapMarkAndSweep for Intrinsics { self.unescape().mark_values(queues); self.uri_error_prototype().mark_values(queues); self.uri_error().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_map_prototype().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_map().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_ref_prototype().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_ref().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_set_prototype().mark_values(queues); + #[cfg(feature = "weak-refs")] self.weak_set().mark_values(queues); } diff --git a/nova_vm/src/ecmascript/types/language/object.rs b/nova_vm/src/ecmascript/types/language/object.rs index db8a1b97b..f1fb02af8 100644 --- a/nova_vm/src/ecmascript/types/language/object.rs +++ b/nova_vm/src/ecmascript/types/language/object.rs @@ -23,6 +23,8 @@ use super::value::{ UINT_16_ARRAY_DISCRIMINANT, UINT_32_ARRAY_DISCRIMINANT, UINT_8_ARRAY_DISCRIMINANT, UINT_8_CLAMPED_ARRAY_DISCRIMINANT, }; +#[cfg(feature = "weak-refs")] +use super::value::{WEAK_MAP_DISCRIMINANT, WEAK_REF_DISCRIMINANT, WEAK_SET_DISCRIMINANT}; use super::{ value::{ ARGUMENTS_DISCRIMINANT, ARRAY_DISCRIMINANT, ARRAY_ITERATOR_DISCRIMINANT, @@ -35,16 +37,16 @@ use super::{ FINALIZATION_REGISTRY_DISCRIMINANT, GENERATOR_DISCRIMINANT, ITERATOR_DISCRIMINANT, MAP_DISCRIMINANT, MAP_ITERATOR_DISCRIMINANT, MODULE_DISCRIMINANT, OBJECT_DISCRIMINANT, PRIMITIVE_OBJECT_DISCRIMINANT, PROMISE_DISCRIMINANT, PROXY_DISCRIMINANT, - REGEXP_DISCRIMINANT, SET_DISCRIMINANT, SET_ITERATOR_DISCRIMINANT, WEAK_MAP_DISCRIMINANT, - WEAK_REF_DISCRIMINANT, WEAK_SET_DISCRIMINANT, + REGEXP_DISCRIMINANT, SET_DISCRIMINANT, SET_ITERATOR_DISCRIMINANT, }, Function, IntoValue, Value, }; - #[cfg(feature = "date")] use crate::ecmascript::builtins::date::Date; #[cfg(feature = "shared-array-buffer")] use crate::ecmascript::builtins::shared_array_buffer::SharedArrayBuffer; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::{weak_map::WeakMap, weak_ref::WeakRef, weak_set::WeakSet}; #[cfg(feature = "array-buffer")] use crate::{ ecmascript::builtins::{data_view::DataView, typed_array::TypedArray, ArrayBuffer}, @@ -73,9 +75,6 @@ use crate::{ proxy::Proxy, regexp::RegExp, set::Set, - weak_map::WeakMap, - weak_ref::WeakRef, - weak_set::WeakSet, ArgumentsList, Array, BuiltinConstructorFunction, BuiltinFunction, ECMAScriptFunction, }, execution::{Agent, JsResult}, @@ -129,8 +128,11 @@ pub enum Object { Set(Set) = SET_DISCRIMINANT, #[cfg(feature = "shared-array-buffer")] SharedArrayBuffer(SharedArrayBuffer) = SHARED_ARRAY_BUFFER_DISCRIMINANT, + #[cfg(feature = "weak-refs")] WeakMap(WeakMap) = WEAK_MAP_DISCRIMINANT, + #[cfg(feature = "weak-refs")] WeakRef(WeakRef) = WEAK_REF_DISCRIMINANT, + #[cfg(feature = "weak-refs")] WeakSet(WeakSet) = WEAK_SET_DISCRIMINANT, #[cfg(feature = "array-buffer")] Int8Array(TypedArrayIndex) = INT_8_ARRAY_DISCRIMINANT, @@ -200,8 +202,11 @@ impl IntoValue for Object { Object::Set(data) => Value::Set(data), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => Value::SharedArrayBuffer(data), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => Value::WeakMap(data), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => Value::WeakRef(data), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => Value::WeakSet(data), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => Value::Int8Array(data), @@ -391,8 +396,11 @@ impl From for Value { Object::Set(data) => Value::Set(data), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => Value::SharedArrayBuffer(data), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => Value::WeakMap(data), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => Value::WeakRef(data), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => Value::WeakSet(data), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => Value::Int8Array(data), @@ -473,8 +481,11 @@ impl TryFrom for Object { Value::Set(data) => Ok(Object::Set(data)), #[cfg(feature = "shared-array-buffer")] Value::SharedArrayBuffer(data) => Ok(Object::SharedArrayBuffer(data)), + #[cfg(feature = "weak-refs")] Value::WeakMap(data) => Ok(Object::WeakMap(data)), + #[cfg(feature = "weak-refs")] Value::WeakRef(data) => Ok(Object::WeakRef(data)), + #[cfg(feature = "weak-refs")] Value::WeakSet(data) => Ok(Object::WeakSet(data)), #[cfg(feature = "array-buffer")] Value::Int8Array(data) => Ok(Object::Int8Array(data)), @@ -552,8 +563,11 @@ impl Hash for Object { Object::Set(data) => data.get_index().hash(state), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.get_index().hash(state), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.get_index().hash(state), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.get_index().hash(state), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.get_index().hash(state), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => data.into_index().hash(state), @@ -632,8 +646,11 @@ impl InternalSlots for Object { Object::Set(data) => data.internal_extensible(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_extensible(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => TypedArray::Int8Array(data).internal_extensible(agent), @@ -706,8 +723,11 @@ impl InternalSlots for Object { Object::Set(data) => data.internal_set_extensible(agent, value), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_set_extensible(agent, value), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_set_extensible(agent, value), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_set_extensible(agent, value), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_set_extensible(agent, value), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -794,8 +814,11 @@ impl InternalSlots for Object { Object::Set(data) => data.internal_prototype(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_prototype(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_prototype(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_prototype(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_prototype(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => TypedArray::Int8Array(data).internal_prototype(agent), @@ -870,8 +893,11 @@ impl InternalSlots for Object { Object::Set(data) => data.internal_set_prototype(agent, prototype), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_set_prototype(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_set_prototype(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_set_prototype(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_set_prototype(agent, prototype), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -960,8 +986,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_get_prototype_of(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_get_prototype_of(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_get_prototype_of(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_get_prototype_of(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_get_prototype_of(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => TypedArray::Int8Array(data).internal_get_prototype_of(agent), @@ -1054,8 +1083,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_set_prototype_of(agent, prototype), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_set_prototype_of(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_set_prototype_of(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_set_prototype_of(agent, prototype), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_set_prototype_of(agent, prototype), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1142,8 +1174,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_is_extensible(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_is_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_is_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_is_extensible(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_is_extensible(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => TypedArray::Int8Array(data).internal_is_extensible(agent), @@ -1224,8 +1259,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_prevent_extensions(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_prevent_extensions(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_prevent_extensions(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_prevent_extensions(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_prevent_extensions(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1322,8 +1360,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_get_own_property(agent, property_key), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_get_own_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_get_own_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_get_own_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_get_own_property(agent, property_key), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1455,12 +1496,15 @@ impl InternalMethods for Object { Object::SharedArrayBuffer(data) => { data.internal_define_own_property(agent, property_key, property_descriptor) } + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => { data.internal_define_own_property(agent, property_key, property_descriptor) } + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => { data.internal_define_own_property(agent, property_key, property_descriptor) } + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => { data.internal_define_own_property(agent, property_key, property_descriptor) } @@ -1566,8 +1610,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_has_property(agent, property_key), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_has_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_has_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_has_property(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_has_property(agent, property_key), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1663,8 +1710,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_get(agent, property_key, receiver), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_get(agent, property_key, receiver), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_get(agent, property_key, receiver), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_get(agent, property_key, receiver), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_get(agent, property_key, receiver), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1771,8 +1821,11 @@ impl InternalMethods for Object { Object::SharedArrayBuffer(data) => { data.internal_set(agent, property_key, value, receiver) } + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_set(agent, property_key, value, receiver), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_set(agent, property_key, value, receiver), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_set(agent, property_key, value, receiver), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1864,8 +1917,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_delete(agent, property_key), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_delete(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_delete(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_delete(agent, property_key), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_delete(agent, property_key), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -1952,8 +2008,11 @@ impl InternalMethods for Object { Object::Set(data) => data.internal_own_property_keys(agent), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.internal_own_property_keys(agent), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.internal_own_property_keys(agent), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.internal_own_property_keys(agent), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.internal_own_property_keys(agent), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => { @@ -2079,8 +2138,11 @@ impl HeapMarkAndSweep for Object { Object::Set(data) => data.mark_values(queues), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.mark_values(queues), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => data.mark_values(queues), @@ -2145,8 +2207,11 @@ impl HeapMarkAndSweep for Object { Object::Set(data) => data.sweep_values(compactions), #[cfg(feature = "shared-array-buffer")] Object::SharedArrayBuffer(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Object::WeakMap(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Object::WeakRef(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Object::WeakSet(data) => data.sweep_values(compactions), #[cfg(feature = "array-buffer")] Object::Int8Array(data) => data.sweep_values(compactions), diff --git a/nova_vm/src/ecmascript/types/language/value.rs b/nova_vm/src/ecmascript/types/language/value.rs index bf96245b6..51006e55f 100644 --- a/nova_vm/src/ecmascript/types/language/value.rs +++ b/nova_vm/src/ecmascript/types/language/value.rs @@ -13,6 +13,8 @@ use super::{ use crate::ecmascript::builtins::date::Date; #[cfg(feature = "shared-array-buffer")] use crate::ecmascript::builtins::shared_array_buffer::SharedArrayBuffer; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::{weak_map::WeakMap, weak_ref::WeakRef, weak_set::WeakSet}; #[cfg(feature = "array-buffer")] use crate::{ ecmascript::builtins::{data_view::DataView, ArrayBuffer}, @@ -44,9 +46,6 @@ use crate::{ proxy::Proxy, regexp::RegExp, set::Set, - weak_map::WeakMap, - weak_ref::WeakRef, - weak_set::WeakSet, Array, BuiltinConstructorFunction, BuiltinFunction, ECMAScriptFunction, }, execution::{Agent, JsResult}, @@ -164,8 +163,11 @@ pub enum Value { Set(Set), #[cfg(feature = "shared-array-buffer")] SharedArrayBuffer(SharedArrayBuffer), + #[cfg(feature = "weak-refs")] WeakMap(WeakMap), + #[cfg(feature = "weak-refs")] WeakRef(WeakRef), + #[cfg(feature = "weak-refs")] WeakSet(WeakSet), // TypedArrays @@ -286,8 +288,11 @@ pub(crate) const SET_DISCRIMINANT: u8 = value_discriminant(Value::Set(Set::_def( #[cfg(feature = "shared-array-buffer")] pub(crate) const SHARED_ARRAY_BUFFER_DISCRIMINANT: u8 = value_discriminant(Value::SharedArrayBuffer(SharedArrayBuffer::_def())); +#[cfg(feature = "weak-refs")] pub(crate) const WEAK_MAP_DISCRIMINANT: u8 = value_discriminant(Value::WeakMap(WeakMap::_def())); +#[cfg(feature = "weak-refs")] pub(crate) const WEAK_REF_DISCRIMINANT: u8 = value_discriminant(Value::WeakRef(WeakRef::_def())); +#[cfg(feature = "weak-refs")] pub(crate) const WEAK_SET_DISCRIMINANT: u8 = value_discriminant(Value::WeakSet(WeakSet::_def())); #[cfg(feature = "array-buffer")] pub(crate) const INT_8_ARRAY_DISCRIMINANT: u8 = @@ -653,14 +658,17 @@ impl Value { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakMap(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakRef(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakSet(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); @@ -870,14 +878,17 @@ impl Value { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakMap(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakRef(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); } + #[cfg(feature = "weak-refs")] Value::WeakSet(data) => { discriminant.hash(hasher); data.get_index().hash(hasher); @@ -1094,8 +1105,11 @@ impl HeapMarkAndSweep for Value { Value::Set(data) => data.mark_values(queues), #[cfg(feature = "shared-array-buffer")] Value::SharedArrayBuffer(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Value::WeakMap(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Value::WeakRef(data) => data.mark_values(queues), + #[cfg(feature = "weak-refs")] Value::WeakSet(data) => data.mark_values(queues), #[cfg(feature = "array-buffer")] Value::Int8Array(data) => data.mark_values(queues), @@ -1173,8 +1187,11 @@ impl HeapMarkAndSweep for Value { Value::Set(data) => data.sweep_values(compactions), #[cfg(feature = "shared-array-buffer")] Value::SharedArrayBuffer(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Value::WeakMap(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Value::WeakRef(data) => data.sweep_values(compactions), + #[cfg(feature = "weak-refs")] Value::WeakSet(data) => data.sweep_values(compactions), #[cfg(feature = "array-buffer")] Value::Int8Array(data) => data.sweep_values(compactions), diff --git a/nova_vm/src/engine/bytecode/vm.rs b/nova_vm/src/engine/bytecode/vm.rs index b1812a905..86c8ab4d4 100644 --- a/nova_vm/src/engine/bytecode/vm.rs +++ b/nova_vm/src/engine/bytecode/vm.rs @@ -2176,9 +2176,6 @@ fn typeof_operator(_: &mut Agent, val: Value) -> String { Value::Map(_) | Value::Promise(_) | Value::Set(_) | - Value::WeakMap(_) | - Value::WeakRef(_) | - Value::WeakSet(_) | Value::AsyncFromSyncIterator | Value::AsyncIterator | Value::Iterator | @@ -2188,6 +2185,10 @@ fn typeof_operator(_: &mut Agent, val: Value) -> String { Value::Generator(_) | Value::Module(_) | Value::EmbedderObject(_) => BUILTIN_STRING_MEMORY.object, + #[cfg(feature = "weak-refs")] + Value::WeakMap(_) | + Value::WeakRef(_) | + Value::WeakSet(_) => BUILTIN_STRING_MEMORY.object, #[cfg(feature = "shared-array-buffer")] Value::SharedArrayBuffer(_) => BUILTIN_STRING_MEMORY.object, #[cfg(feature = "array-buffer")] diff --git a/nova_vm/src/heap.rs b/nova_vm/src/heap.rs index 26b6256ce..46eee04c1 100644 --- a/nova_vm/src/heap.rs +++ b/nova_vm/src/heap.rs @@ -29,6 +29,31 @@ use self::{ }, indexes::{NumberIndex, ObjectIndex, StringIndex}, }; +#[cfg(feature = "date")] +use crate::ecmascript::builtins::date::data::DateHeapData; +#[cfg(feature = "shared-array-buffer")] +use crate::ecmascript::builtins::shared_array_buffer::data::SharedArrayBufferHeapData; +#[cfg(feature = "array-buffer")] +use crate::ecmascript::builtins::{ + data_view::data::DataViewHeapData, typed_array::data::TypedArrayHeapData, ArrayBufferHeapData, +}; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::{ + weak_map::data::WeakMapHeapData, weak_ref::data::WeakRefHeapData, + weak_set::data::WeakSetHeapData, +}; +use crate::ecmascript::{ + builtins::ArrayHeapData, + execution::{Environments, Realm, RealmIdentifier}, + scripts_and_modules::{ + module::ModuleIdentifier, + script::{Script, ScriptIdentifier}, + }, + types::{ + BigIntHeapData, BoundFunctionHeapData, BuiltinFunctionHeapData, ECMAScriptFunctionHeapData, + NumberHeapData, Object, ObjectHeapData, String, StringHeapData, SymbolHeapData, Value, + }, +}; use crate::ecmascript::{ builtins::{ control_abstraction_objects::{ @@ -54,9 +79,6 @@ use crate::ecmascript::{ proxy::data::ProxyHeapData, regexp::RegExpHeapData, set::data::SetHeapData, - weak_map::data::WeakMapHeapData, - weak_ref::data::WeakRefHeapData, - weak_set::data::WeakSetHeapData, }, scripts_and_modules::source_code::SourceCodeHeapData, types::{ @@ -64,27 +86,6 @@ use crate::ecmascript::{ BUILTIN_STRINGS_LIST, }, }; - -#[cfg(feature = "date")] -use crate::ecmascript::builtins::date::data::DateHeapData; -#[cfg(feature = "shared-array-buffer")] -use crate::ecmascript::builtins::shared_array_buffer::data::SharedArrayBufferHeapData; -#[cfg(feature = "array-buffer")] -use crate::ecmascript::builtins::{ - data_view::data::DataViewHeapData, typed_array::data::TypedArrayHeapData, ArrayBufferHeapData, -}; -use crate::ecmascript::{ - builtins::ArrayHeapData, - execution::{Environments, Realm, RealmIdentifier}, - scripts_and_modules::{ - module::ModuleIdentifier, - script::{Script, ScriptIdentifier}, - }, - types::{ - BigIntHeapData, BoundFunctionHeapData, BuiltinFunctionHeapData, ECMAScriptFunctionHeapData, - NumberHeapData, Object, ObjectHeapData, String, StringHeapData, SymbolHeapData, Value, - }, -}; pub(crate) use heap_bits::{CompactionLists, HeapMarkAndSweep, WorkQueues}; #[derive(Debug)] @@ -131,8 +132,11 @@ pub struct Heap { pub symbols: Vec>, #[cfg(feature = "array-buffer")] pub typed_arrays: Vec>, + #[cfg(feature = "weak-refs")] pub weak_maps: Vec>, + #[cfg(feature = "weak-refs")] pub weak_refs: Vec>, + #[cfg(feature = "weak-refs")] pub weak_sets: Vec>, pub modules: Vec>, pub scripts: Vec>, @@ -227,8 +231,11 @@ impl Heap { symbols: Vec::with_capacity(1024), #[cfg(feature = "array-buffer")] typed_arrays: Vec::with_capacity(0), + #[cfg(feature = "weak-refs")] weak_maps: Vec::with_capacity(0), + #[cfg(feature = "weak-refs")] weak_refs: Vec::with_capacity(0), + #[cfg(feature = "weak-refs")] weak_sets: Vec::with_capacity(0), }; diff --git a/nova_vm/src/heap/heap_bits.rs b/nova_vm/src/heap/heap_bits.rs index c6f9e43de..bc1243a5a 100644 --- a/nova_vm/src/heap/heap_bits.rs +++ b/nova_vm/src/heap/heap_bits.rs @@ -16,6 +16,8 @@ use crate::ecmascript::builtins::date::Date; use crate::ecmascript::builtins::shared_array_buffer::SharedArrayBuffer; #[cfg(feature = "array-buffer")] use crate::ecmascript::builtins::{data_view::DataView, ArrayBuffer}; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::{weak_map::WeakMap, weak_ref::WeakRef, weak_set::WeakSet}; use crate::ecmascript::{ builtins::{ bound_function::BoundFunction, @@ -42,9 +44,6 @@ use crate::ecmascript::{ proxy::Proxy, regexp::RegExp, set::Set, - weak_map::WeakMap, - weak_ref::WeakRef, - weak_set::WeakSet, Array, BuiltinConstructorFunction, BuiltinFunction, ECMAScriptFunction, }, execution::{ @@ -112,8 +111,11 @@ pub struct HeapBits { pub symbols: Box<[bool]>, #[cfg(feature = "array-buffer")] pub typed_arrays: Box<[bool]>, + #[cfg(feature = "weak-refs")] pub weak_maps: Box<[bool]>, + #[cfg(feature = "weak-refs")] pub weak_refs: Box<[bool]>, + #[cfg(feature = "weak-refs")] pub weak_sets: Box<[bool]>, } @@ -171,8 +173,11 @@ pub(crate) struct WorkQueues { pub symbols: Vec, #[cfg(feature = "array-buffer")] pub typed_arrays: Vec, + #[cfg(feature = "weak-refs")] pub weak_maps: Vec, + #[cfg(feature = "weak-refs")] pub weak_refs: Vec, + #[cfg(feature = "weak-refs")] pub weak_sets: Vec, } @@ -230,8 +235,11 @@ impl HeapBits { let symbols = vec![false; heap.symbols.len()]; #[cfg(feature = "array-buffer")] let typed_arrays = vec![false; heap.typed_arrays.len()]; + #[cfg(feature = "weak-refs")] let weak_maps = vec![false; heap.weak_maps.len()]; + #[cfg(feature = "weak-refs")] let weak_refs = vec![false; heap.weak_refs.len()]; + #[cfg(feature = "weak-refs")] let weak_sets = vec![false; heap.weak_sets.len()]; Self { #[cfg(feature = "array-buffer")] @@ -286,8 +294,11 @@ impl HeapBits { symbols: symbols.into_boxed_slice(), #[cfg(feature = "array-buffer")] typed_arrays: typed_arrays.into_boxed_slice(), + #[cfg(feature = "weak-refs")] weak_maps: weak_maps.into_boxed_slice(), + #[cfg(feature = "weak-refs")] weak_refs: weak_refs.into_boxed_slice(), + #[cfg(feature = "weak-refs")] weak_sets: weak_sets.into_boxed_slice(), } } @@ -350,8 +361,11 @@ impl WorkQueues { symbols: Vec::with_capacity((heap.symbols.len() / 4).max(13)), #[cfg(feature = "array-buffer")] typed_arrays: Vec::with_capacity(heap.typed_arrays.len() / 4), + #[cfg(feature = "weak-refs")] weak_maps: Vec::with_capacity(heap.weak_maps.len() / 4), + #[cfg(feature = "weak-refs")] weak_refs: Vec::with_capacity(heap.weak_refs.len() / 4), + #[cfg(feature = "weak-refs")] weak_sets: Vec::with_capacity(heap.weak_sets.len() / 4), } } @@ -433,8 +447,11 @@ impl WorkQueues { symbols, #[cfg(feature = "array-buffer")] typed_arrays, + #[cfg(feature = "weak-refs")] weak_maps, + #[cfg(feature = "weak-refs")] weak_refs, + #[cfg(feature = "weak-refs")] weak_sets, } = self; @@ -448,6 +465,12 @@ impl WorkQueues { let typed_arrays: &[bool; 0] = &[]; #[cfg(not(feature = "shared-array-buffer"))] let shared_array_buffers: &[bool; 0] = &[]; + #[cfg(not(feature = "weak-refs"))] + let weak_maps: &[bool; 0] = &[]; + #[cfg(not(feature = "weak-refs"))] + let weak_refs: &[bool; 0] = &[]; + #[cfg(not(feature = "weak-refs"))] + let weak_sets: &[bool; 0] = &[]; array_buffers.is_empty() && arrays.is_empty() @@ -693,8 +716,11 @@ pub(crate) struct CompactionLists { pub symbols: CompactionList, #[cfg(feature = "array-buffer")] pub typed_arrays: CompactionList, + #[cfg(feature = "weak-refs")] pub weak_maps: CompactionList, + #[cfg(feature = "weak-refs")] pub weak_refs: CompactionList, + #[cfg(feature = "weak-refs")] pub weak_sets: CompactionList, } @@ -766,8 +792,11 @@ impl CompactionLists { data_views: CompactionList::from_mark_bits(&bits.data_views), finalization_registrys: CompactionList::from_mark_bits(&bits.finalization_registrys), proxys: CompactionList::from_mark_bits(&bits.proxys), + #[cfg(feature = "weak-refs")] weak_maps: CompactionList::from_mark_bits(&bits.weak_maps), + #[cfg(feature = "weak-refs")] weak_refs: CompactionList::from_mark_bits(&bits.weak_refs), + #[cfg(feature = "weak-refs")] weak_sets: CompactionList::from_mark_bits(&bits.weak_sets), #[cfg(feature = "array-buffer")] typed_arrays: CompactionList::from_mark_bits(&bits.typed_arrays), diff --git a/nova_vm/src/heap/heap_gc.rs b/nova_vm/src/heap/heap_gc.rs index 5a4d6a804..31cd035b4 100644 --- a/nova_vm/src/heap/heap_gc.rs +++ b/nova_vm/src/heap/heap_gc.rs @@ -23,6 +23,8 @@ use crate::ecmascript::builtins::date::Date; use crate::ecmascript::builtins::shared_array_buffer::SharedArrayBuffer; #[cfg(feature = "array-buffer")] use crate::ecmascript::builtins::{data_view::DataView, ArrayBuffer}; +#[cfg(feature = "weak-refs")] +use crate::ecmascript::builtins::{weak_map::WeakMap, weak_ref::WeakRef, weak_set::WeakSet}; use crate::ecmascript::{ builtins::{ bound_function::BoundFunction, @@ -49,9 +51,6 @@ use crate::ecmascript::{ proxy::Proxy, regexp::RegExp, set::Set, - weak_map::WeakMap, - weak_ref::WeakRef, - weak_set::WeakSet, Array, BuiltinConstructorFunction, BuiltinFunction, ECMAScriptFunction, }, execution::{ @@ -150,8 +149,11 @@ pub fn heap_gc(heap: &mut Heap, root_realms: &mut [Option]) { symbols, #[cfg(feature = "array-buffer")] typed_arrays, + #[cfg(feature = "weak-refs")] weak_maps, + #[cfg(feature = "weak-refs")] weak_refs, + #[cfg(feature = "weak-refs")] weak_sets, } = heap; let Environments { @@ -716,45 +718,49 @@ pub fn heap_gc(heap: &mut Heap, root_realms: &mut [Option]) { } }); } - let mut weak_map_marks: Box<[WeakMap]> = queues.weak_maps.drain(..).collect(); - weak_map_marks.sort(); - weak_map_marks.iter().for_each(|&idx| { - let index = idx.get_index(); - if let Some(marked) = bits.weak_maps.get_mut(index) { - if *marked { - // Already marked, ignore - return; + #[cfg(feature = "weak-refs")] + { + let mut weak_map_marks: Box<[WeakMap]> = queues.weak_maps.drain(..).collect(); + weak_map_marks.sort(); + weak_map_marks.iter().for_each(|&idx| { + let index = idx.get_index(); + if let Some(marked) = bits.weak_maps.get_mut(index) { + if *marked { + // Already marked, ignore + return; + } + *marked = true; + weak_maps.get(index).mark_values(&mut queues); } - *marked = true; - weak_maps.get(index).mark_values(&mut queues); - } - }); - let mut weak_ref_marks: Box<[WeakRef]> = queues.weak_refs.drain(..).collect(); - weak_ref_marks.sort(); - weak_ref_marks.iter().for_each(|&idx| { - let index = idx.get_index(); - if let Some(marked) = bits.weak_refs.get_mut(index) { - if *marked { - // Already marked, ignore - return; + }); + let mut weak_ref_marks: Box<[WeakRef]> = queues.weak_refs.drain(..).collect(); + weak_ref_marks.sort(); + weak_ref_marks.iter().for_each(|&idx| { + let index = idx.get_index(); + if let Some(marked) = bits.weak_refs.get_mut(index) { + if *marked { + // Already marked, ignore + return; + } + *marked = true; + weak_refs.get(index).mark_values(&mut queues); } - *marked = true; - weak_refs.get(index).mark_values(&mut queues); - } - }); - let mut weak_set_marks: Box<[WeakSet]> = queues.weak_sets.drain(..).collect(); - weak_set_marks.sort(); - weak_set_marks.iter().for_each(|&idx| { - let index = idx.get_index(); - if let Some(marked) = bits.weak_sets.get_mut(index) { - if *marked { - // Already marked, ignore - return; + }); + let mut weak_set_marks: Box<[WeakSet]> = queues.weak_sets.drain(..).collect(); + weak_set_marks.sort(); + weak_set_marks.iter().for_each(|&idx| { + let index = idx.get_index(); + if let Some(marked) = bits.weak_sets.get_mut(index) { + if *marked { + // Already marked, ignore + return; + } + *marked = true; + weak_sets.get(index).mark_values(&mut queues); } - *marked = true; - weak_sets.get(index).mark_values(&mut queues); - } - }); + }); + } + let mut e_2_4_marks: Box<[(ElementIndex, u32)]> = queues.e_2_4.drain(..).collect(); e_2_4_marks.sort(); e_2_4_marks.iter().for_each(|&(idx, len)| { @@ -979,8 +985,11 @@ fn sweep(heap: &mut Heap, bits: &HeapBits, root_realms: &mut [Option; pub type SymbolIndex = BaseIndex; #[cfg(feature = "array-buffer")] pub type TypedArrayIndex = BaseIndex; +#[cfg(feature = "weak-refs")] pub type WeakMapIndex = BaseIndex; +#[cfg(feature = "weak-refs")] pub type WeakRefIndex = BaseIndex; +#[cfg(feature = "weak-refs")] pub type WeakSetIndex = BaseIndex; // Implement Default for ElementIndex: This is done to support Default