Skip to content

Truly independent web browser

LibJS: Give each Private Name a private symbol

Shannon Booth2da18d58f88b
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
index bbf1372a8a7..1e11b49446b 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
@@ -825,7 +825,7 @@ pub fn has_private_id(
pub fn add_private_name(vm: &Vm, pc: u32, instruction: &op::AddPrivateName) -> SlowPathControl {
let name = vm.current_executable().get_identifier(instruction.name).clone();
- running_private_environment(vm).add_private_name(name);
+ running_private_environment(vm).add_private_name(vm, name);
SlowPathControl::continue_at(pc + op::AddPrivateName::LENGTH)
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 8e1f46844fc..143b0e7e1e9 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -586,7 +586,6 @@ pub struct Vm {
module_async_evaluation_count: Cell<u64>, // [[ModuleAsyncEvaluationCount]]
/// The id the next PrivateEnvironment gives its names. It starts at one such that 0 can be invalid / default
/// initialized.
- next_private_environment_id: Cell<u64>,
/// The properties defined with Object::define_intrinsic_accessor that have not been read yet, by the address of
/// their object. The objects are weak: the sweep callback forgets the ones that die.
intrinsic_accessors: RefCell<IntrinsicAccessorMap>,
@@ -734,7 +733,6 @@ impl Vm {
loaded_modules: OnceCell::new(),
module_execution_depth: Cell::new(0),
module_async_evaluation_count: Cell::new(0),
- next_private_environment_id: Cell::new(1),
intrinsic_accessors: RefCell::new(HashMap::default()),
debugger: RefCell::new(None),
last_throw: RefCell::new(None),
@@ -1754,10 +1752,6 @@ impl Vm {
.expect("the VM allocates its list of loaded modules")
}
- pub fn next_private_environment_id(&self) -> &Cell<u64> {
- &self.next_private_environment_id
- }
-
pub fn string_to_atom_cache(&self) -> &RefCell<Box<[StringToAtomCacheEntry; STRING_TO_ATOM_CACHE_SIZE]>> {
&self.string_to_atom_cache
}
diff --git a/Libraries/LibJS/Rust/src/runtime/class_construction.rs b/Libraries/LibJS/Rust/src/runtime/class_construction.rs
index 597ac058a91..0fd657a33c4 100644
--- a/Libraries/LibJS/Rust/src/runtime/class_construction.rs
+++ b/Libraries/LibJS/Rust/src/runtime/class_construction.rs
@@ -337,7 +337,7 @@ pub fn construct_class(
ClassFieldInitializerName::PropertyKey(key.clone())
}
ClassElementName::PrivateName(name) => {
- ClassFieldInitializerName::PrivateName(name.clone())
+ ClassFieldInitializerName::PrivateName(name.description.clone())
}
});
}
diff --git a/Libraries/LibJS/Rust/src/runtime/class_field_definition.rs b/Libraries/LibJS/Rust/src/runtime/class_field_definition.rs
index b9a9bc13038..4e62b8f90dc 100644
--- a/Libraries/LibJS/Rust/src/runtime/class_field_definition.rs
+++ b/Libraries/LibJS/Rust/src/runtime/class_field_definition.rs
@@ -16,7 +16,7 @@ use crate::runtime::property_key::PropertyKey;
#[derive(Clone, Debug, PartialEq, Eq, Trace)]
pub enum ClassElementName {
PropertyKey(PropertyKey),
- PrivateName(#[gc(untraced)] PrivateName),
+ PrivateName(PrivateName),
}
/// [[Initializer]]: Variant<GC::Ref<ECMAScriptFunctionObject>, Value, Empty>.
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index a2c60d4856c..90bd05eeae2 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -71,7 +71,6 @@ pub enum PrivateElementKind {
#[derive(Clone, Debug, Trace)]
pub struct PrivateElement {
- #[gc(untraced)]
pub key: PrivateName,
#[gc(untraced)]
pub kind: PrivateElementKind,
diff --git a/Libraries/LibJS/Rust/src/runtime/private_environment.rs b/Libraries/LibJS/Rust/src/runtime/private_environment.rs
index cabaad2b6f3..5dedd6a0362 100644
--- a/Libraries/LibJS/Rust/src/runtime/private_environment.rs
+++ b/Libraries/LibJS/Rust/src/runtime/private_environment.rs
@@ -4,20 +4,24 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use core::cell::RefCell;
-
use ak::Utf16FlyString;
+use libjs_runtime_macros::Trace;
use crate::gc::class::{GcCell, define_cell};
+use crate::gc::gc_ref_cell::GcRefCell;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
pub use crate::layout::environment::PrivateEnvironment;
use crate::layout::member::NullableMember;
+use crate::runtime::property_key::PropertyKey;
+use crate::runtime::symbol::Symbol;
-#[derive(Clone, Default)]
+/// A Private Name, whose private elements are the properties an object stores under its symbol, which nothing outside
+/// the engine ever sees.
+#[derive(Clone)]
pub struct PrivateName {
- pub unique_id: u64,
+ pub symbol: Gc<Symbol>,
pub description: Utf16FlyString,
}
@@ -29,7 +33,7 @@ fn fly_strings_are_equal(lhs: &Utf16FlyString, rhs: &Utf16FlyString) -> bool {
impl PartialEq for PrivateName {
fn eq(&self, other: &Self) -> bool {
- self.unique_id == other.unique_id && fly_strings_are_equal(&self.description, &other.description)
+ self.symbol == other.symbol
}
}
@@ -39,50 +43,54 @@ impl core::fmt::Debug for PrivateName {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
formatter,
- "PrivateName({}, #{})",
- self.unique_id,
+ "PrivateName({:p}, #{})",
+ self.symbol.as_ptr(),
crate::utf16::Utf16View::of_fly_string(&self.description).to_utf8()
)
}
}
impl PrivateName {
- pub fn new(unique_id: u64, description: Utf16FlyString) -> Self {
- Self { unique_id, description }
+ /// The key of the name's private elements in the storage of objects.
+ pub fn property_key(&self) -> PropertyKey {
+ PropertyKey::from_symbol(self.symbol)
+ }
+}
+
+// SAFETY: The symbol is the only cell a private name holds.
+unsafe impl Trace for PrivateName {
+ fn trace(&self, visitor: &mut Visitor) {
+ self.symbol.trace(visitor);
}
}
/// The parts of a private environment the interpreter does not read.
+#[derive(Trace)]
pub struct PrivateEnvironmentStorage {
/// [[Names]]
- private_names: RefCell<Vec<PrivateName>>,
- unique_id: u64,
+ private_names: GcRefCell<Vec<PrivateName>>,
}
-const _: () = assert!(size_of::<PrivateEnvironment>() == 56);
+const _: () = assert!(size_of::<PrivateEnvironment>() == 48);
define_cell!(PrivateEnvironment, Other);
-// SAFETY: The outer environment is the only cell a private environment reaches; names hold no cells.
+// SAFETY: Visits the outer environment and the names.
unsafe impl Trace for PrivateEnvironment {
fn trace(&self, visitor: &mut Visitor) {
self.outer.trace(visitor);
+ self.storage.trace(visitor);
}
}
impl PrivateEnvironment {
/// The constructor, which only NewPrivateEnvironment calls.
pub(crate) fn create(vm: &Vm, parent: Option<Gc<PrivateEnvironment>>) -> Gc<PrivateEnvironment> {
- // FIXME: We might want to delay getting the next unique id until required.
- let unique_id = vm.next_private_environment_id().get();
- assert!(unique_id != u64::MAX);
- vm.next_private_environment_id().set(unique_id + 1);
vm.heap().allocate(|_| PrivateEnvironment {
header: CellHeader::for_class(Self::CLASS),
outer: NullableMember::new(parent),
storage: PrivateEnvironmentStorage {
- private_names: RefCell::new(Vec::new()),
- unique_id,
+ private_names: GcRefCell::new(Vec::new()),
},
})
}
@@ -119,15 +127,16 @@ impl PrivateEnvironment {
.is_some_and(|outer| outer.contains_private_identifier(identifier))
}
- pub fn add_private_name(&self, description: Utf16FlyString) {
+ pub fn add_private_name(&self, vm: &Vm, description: Utf16FlyString) {
if self.find_private_name(&description).is_some() {
return;
}
+ let symbol = Symbol::create_private(vm);
self.storage
.private_names
- .borrow_mut()
- .push(PrivateName::new(self.storage.unique_id, description));
+ .borrow_mut(vm)
+ .push(PrivateName { symbol, description });
}
pub fn outer_environment(&self) -> Option<Gc<PrivateEnvironment>> {
diff --git a/Libraries/LibJS/Rust/src/runtime/reference.rs b/Libraries/LibJS/Rust/src/runtime/reference.rs
index f0fa737120a..8ef92f875dc 100644
--- a/Libraries/LibJS/Rust/src/runtime/reference.rs
+++ b/Libraries/LibJS/Rust/src/runtime/reference.rs
@@ -48,7 +48,7 @@ pub struct Reference {
strict: Strict,
}
-// SAFETY: Visits the base, the property key and the this value, as Reference::visit_edges does.
+// SAFETY: Visits the base, the name and the this value, as Reference::visit_edges does.
unsafe impl Trace for Reference {
fn trace(&self, visitor: &mut Visitor) {
match self.base_type {
@@ -58,8 +58,7 @@ unsafe impl Trace for Reference {
}
match &self.name {
ReferencedName::PropertyKey(key) => key.trace(visitor),
- // No GC pointers.
- ReferencedName::PrivateName(_) => {}
+ ReferencedName::PrivateName(name) => name.trace(visitor),
}
self.this_value.trace(visitor);
}
diff --git a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
index e39dc347f99..a3368686ba1 100644
--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
@@ -30,7 +30,6 @@ use crate::layout::function_object::CallEnvironmentTemplate;
pub use crate::layout::function_object::{SharedFunctionInstanceData, asm_call_metadata};
use crate::layout::member::NullableMember;
use crate::runtime::environment_shape::{EnvironmentShape, EnvironmentShapeCache};
-use crate::runtime::private_environment::PrivateName;
use crate::runtime::property_key::PropertyKey;
use crate::runtime::shape::Shape;
use crate::source_code::SourceCode;
@@ -56,11 +55,12 @@ pub enum ConstructorKind {
Derived,
}
-/// [[ClassFieldInitializerName]]: Variant<PropertyKey, PrivateName, Empty>.
+/// [[ClassFieldInitializerName]]: Variant<PropertyKey, PrivateName, Empty>. Only whether there is a name matters, so a
+/// Private Name is kept as its description.
#[derive(Clone, Debug, Default, Trace)]
pub enum ClassFieldInitializerName {
PropertyKey(PropertyKey),
- PrivateName(#[gc(untraced)] PrivateName),
+ PrivateName(#[gc(untraced)] Utf16FlyString),
#[default]
Empty,
}
@@ -143,7 +143,7 @@ pub struct SharedFunctionInstanceDataStorage {
}
// NB: Every function has these bytes.
-const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 272);
+const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 264);
/// The most properties that constructions make room for ahead (the largest inline capacity of plain objects).
const MAX_CONSTRUCT_RESERVE: u32 = 16;
@@ -664,7 +664,7 @@ impl SharedFunctionInstanceData {
pub fn set_class_field_initializer_name_from_utf16(&self, name: &[u16], is_private: bool) {
let name = Utf16FlyString::from_utf16(name);
self.set_class_field_initializer_name(if is_private {
- ClassFieldInitializerName::PrivateName(PrivateName::new(0, name))
+ ClassFieldInitializerName::PrivateName(name)
} else {
ClassFieldInitializerName::PropertyKey(PropertyKey::from(name))
});