Skip to content

Truly independent web browser

LibJS: Name environment shape cache slots by their owner

Andreas Kling1e81912f179b
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 562a7e5d1e9..1c754cb4895 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -1961,10 +1961,12 @@ impl Executable {
}
pub fn environment_shape_cache(&self, index: u32) -> EnvironmentShapeCache {
- let slot = &self.environment_shape_caches[index as usize];
- // SAFETY: Executables are only reached through the heap, and the slot is part of this one's caches, which
- // live as long as it does.
- unsafe { EnvironmentShapeCache::new(Gc::from_ref(self), slot) }
+ assert!((index as usize) < self.environment_shape_caches.len());
+ EnvironmentShapeCache::Executable(Gc::from_ref(self), index)
+ }
+
+ pub(crate) fn environment_shape_cache_slot(&self, index: u32) -> &Cell<Option<Gc<EnvironmentShape>>> {
+ &self.environment_shape_caches[index as usize]
}
pub fn name(&self) -> Utf16FlyString {
diff --git a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs b/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
index b800e4ffee3..3b435042686 100644
--- a/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/environment_shape.rs
@@ -5,19 +5,20 @@
*/
use core::cell::Cell;
-use core::ptr::NonNull;
use std::collections::HashMap;
use ak::Utf16FlyString;
use libjs_runtime_macros::Trace;
-use crate::gc::class::{Extends, Finalize, GcCell, define_cell};
+use crate::bytecode::executable::Executable;
+use crate::gc::class::{Finalize, GcCell, define_cell};
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::layout::buffer::InterpreterBuffer;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::environment::BINDING_FLAG_MUTABLE;
pub use crate::layout::environment::EnvironmentShape;
+use crate::runtime::shared_function_instance_data::SharedFunctionInstanceData;
/// The names of an environment shape's bindings, whose flags the interpreter reads from the shape itself.
pub struct EnvironmentShapeStorage {
@@ -101,34 +102,32 @@ impl EnvironmentShape {
}
}
-/// The slot where the environments one piece of code creates find their shape: an entry of an executable's
-/// environment shape caches, or the function or var environment shape of a function's shared data. Along with a
-/// pointer to the slot, this keeps the cell that owns the slot alive, so that the pointer cannot dangle.
+/// The slot where the environments one piece of code creates find their shape, which this keeps alive along with the
+/// cell that owns it.
#[derive(Clone, Copy, Trace)]
-pub struct EnvironmentShapeCache {
- owner: Gc<CellHeader>,
- #[gc(untraced)]
- slot: NonNull<Cell<Option<Gc<EnvironmentShape>>>>,
+pub enum EnvironmentShapeCache {
+ /// An entry of an executable's environment shape caches.
+ Executable(Gc<Executable>, #[gc(untraced)] u32),
+ /// The function environment shape of a function's shared data.
+ FunctionEnvironment(Gc<SharedFunctionInstanceData>),
+ /// The var environment shape of a function's shared data.
+ VarEnvironment(Gc<SharedFunctionInstanceData>),
}
impl EnvironmentShapeCache {
- /// # Safety
- ///
- /// `slot` must be part of the cell `owner`, or of memory that `owner` owns and only frees when it is destroyed.
- pub unsafe fn new<Owner: Extends<CellHeader>>(owner: Gc<Owner>, slot: &Cell<Option<Gc<EnvironmentShape>>>) -> Self {
- Self {
- owner: owner.upcast(),
- slot: NonNull::from(slot),
+ fn slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {
+ match self {
+ Self::Executable(executable, index) => executable.environment_shape_cache_slot(*index),
+ Self::FunctionEnvironment(shared_data) => shared_data.function_environment_shape_slot(),
+ Self::VarEnvironment(shared_data) => shared_data.var_environment_shape_slot(),
}
}
pub fn shape(&self) -> Option<Gc<EnvironmentShape>> {
- // SAFETY: The owner keeps the slot allocated, and this keeps the owner alive.
- unsafe { self.slot.as_ref() }.get()
+ self.slot().get()
}
pub fn set_shape(&self, shape: Gc<EnvironmentShape>) {
- // SAFETY: As above.
- unsafe { self.slot.as_ref() }.set(Some(shape));
+ self.slot().set(Some(shape));
}
}
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 e05cd5d97b2..06c4264ae53 100644
--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
@@ -781,14 +781,20 @@ impl SharedFunctionInstanceData {
/// Where the function environments of calls to the function find their shape.
pub fn function_environment_shape_cache(&self) -> EnvironmentShapeCache {
- // SAFETY: The slot is part of this shared data, which only exists as a cell.
- unsafe { EnvironmentShapeCache::new(Gc::from_ref(self), &self.storage.function_environment_shape) }
+ EnvironmentShapeCache::FunctionEnvironment(Gc::from_ref(self))
}
/// Where the separate var environments of calls to the function find their shape.
pub fn var_environment_shape_cache(&self) -> EnvironmentShapeCache {
- // SAFETY: As above.
- unsafe { EnvironmentShapeCache::new(Gc::from_ref(self), &self.storage.var_environment_shape) }
+ EnvironmentShapeCache::VarEnvironment(Gc::from_ref(self))
+ }
+
+ pub(crate) fn function_environment_shape_slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {
+ &self.storage.function_environment_shape
+ }
+
+ pub(crate) fn var_environment_shape_slot(&self) -> &Cell<Option<Gc<EnvironmentShape>>> {
+ &self.storage.var_environment_shape
}
/// RustIntegration::compile_function(): the function's executable, from the bytecode compiled ahead of time if