diff --git a/boa_engine/src/builtins/async_generator/mod.rs b/boa_engine/src/builtins/async_generator/mod.rs index b524600eb1b..a1bd1e0f11c 100644 --- a/boa_engine/src/builtins/async_generator/mod.rs +++ b/boa_engine/src/builtins/async_generator/mod.rs @@ -19,7 +19,7 @@ use crate::{ vm::GeneratorResumeKind, Context, JsError, JsResult, }; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use boa_profiler::Profiler; use std::collections::VecDeque; @@ -57,7 +57,7 @@ pub struct AsyncGenerator { pub(crate) state: AsyncGeneratorState, /// The `[[AsyncGeneratorContext]]` internal slot. - pub(crate) context: Option>>, + pub(crate) context: Option>>, /// The `[[AsyncGeneratorQueue]]` internal slot. pub(crate) queue: VecDeque, @@ -512,7 +512,7 @@ impl AsyncGenerator { pub(crate) fn resume( generator: &JsObject, state: AsyncGeneratorState, - generator_context: &Gc>, + generator_context: &Gc>, completion: (JsResult, bool), context: &mut Context<'_>, ) { diff --git a/boa_engine/src/builtins/generator/mod.rs b/boa_engine/src/builtins/generator/mod.rs index 7008f65ed7d..99665a61887 100644 --- a/boa_engine/src/builtins/generator/mod.rs +++ b/boa_engine/src/builtins/generator/mod.rs @@ -20,7 +20,7 @@ use crate::{ vm::{CallFrame, GeneratorResumeKind, ReturnType}, Context, JsError, JsResult, }; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use boa_profiler::Profiler; /// Indicates the state of a generator. @@ -52,7 +52,7 @@ pub struct Generator { pub(crate) state: GeneratorState, /// The `[[GeneratorContext]]` internal slot. - pub(crate) context: Option>>, + pub(crate) context: Option>>, } impl BuiltIn for Generator { diff --git a/boa_engine/src/builtins/promise/mod.rs b/boa_engine/src/builtins/promise/mod.rs index 7c678cb92f7..21faa699fd4 100644 --- a/boa_engine/src/builtins/promise/mod.rs +++ b/boa_engine/src/builtins/promise/mod.rs @@ -22,7 +22,7 @@ use crate::{ value::JsValue, Context, JsError, JsResult, }; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use boa_profiler::Profiler; use std::{cell::Cell, rc::Rc}; use tap::{Conv, Pipe}; @@ -146,7 +146,7 @@ impl PromiseCapability { // 2. NOTE: C is assumed to be a constructor function that supports the parameter conventions of the Promise constructor (see 27.2.3.1). // 3. Let promiseCapability be the PromiseCapability Record { [[Promise]]: undefined, [[Resolve]]: undefined, [[Reject]]: undefined }. - let promise_capability = Gc::new(GcCell::new(RejectResolve { + let promise_capability = Gc::new(GcRefCell::new(RejectResolve { reject: JsValue::undefined(), resolve: JsValue::undefined(), })); @@ -459,14 +459,14 @@ impl Promise { #[unsafe_ignore_trace] already_called: Rc>, index: usize, - values: Gc>>, + values: Gc>>, capability_resolve: JsFunction, #[unsafe_ignore_trace] remaining_elements_count: Rc>, } // 1. Let values be a new empty List. - let values = Gc::new(GcCell::new(Vec::new())); + let values = Gc::new(GcRefCell::new(Vec::new())); // 2. Let remainingElementsCount be the Record { [[Value]]: 1 }. let remaining_elements_count = Rc::new(Cell::new(1)); @@ -703,14 +703,14 @@ impl Promise { #[unsafe_ignore_trace] already_called: Rc>, index: usize, - values: Gc>>, + values: Gc>>, capability: JsFunction, #[unsafe_ignore_trace] remaining_elements: Rc>, } // 1. Let values be a new empty List. - let values = Gc::new(GcCell::new(Vec::new())); + let values = Gc::new(GcRefCell::new(Vec::new())); // 2. Let remainingElementsCount be the Record { [[Value]]: 1 }. let remaining_elements_count = Rc::new(Cell::new(1)); @@ -1046,14 +1046,14 @@ impl Promise { #[unsafe_ignore_trace] already_called: Rc>, index: usize, - errors: Gc>>, + errors: Gc>>, capability_reject: JsFunction, #[unsafe_ignore_trace] remaining_elements_count: Rc>, } // 1. Let errors be a new empty List. - let errors = Gc::new(GcCell::new(Vec::new())); + let errors = Gc::new(GcRefCell::new(Vec::new())); // 2. Let remainingElementsCount be the Record { [[Value]]: 1 }. let remaining_elements_count = Rc::new(Cell::new(1)); diff --git a/boa_engine/src/bytecompiler/mod.rs b/boa_engine/src/bytecompiler/mod.rs index 59f098a0024..34e1c7d0d23 100644 --- a/boa_engine/src/bytecompiler/mod.rs +++ b/boa_engine/src/bytecompiler/mod.rs @@ -27,7 +27,7 @@ use boa_ast::{ pattern::Pattern, Declaration, Expression, Statement, StatementList, StatementListItem, }; -use boa_gc::{Gc, GcCell}; +use boa_gc::{Gc, GcRefCell}; use boa_interner::{Interner, Sym}; use rustc_hash::FxHashMap; @@ -244,7 +244,7 @@ impl<'b, 'icu> ByteCompiler<'b, 'icu> { /// Push a compile time environment to the current `CodeBlock` and return it's index. fn push_compile_environment( &mut self, - environment: Gc>, + environment: Gc>, ) -> usize { let index = self.code_block.compile_environments.len(); self.code_block.compile_environments.push(environment); diff --git a/boa_engine/src/environments/compile.rs b/boa_engine/src/environments/compile.rs index 293238839ba..5b7725295b8 100644 --- a/boa_engine/src/environments/compile.rs +++ b/boa_engine/src/environments/compile.rs @@ -2,7 +2,7 @@ use crate::{ environments::runtime::BindingLocator, property::PropertyDescriptor, Context, JsString, JsValue, }; use boa_ast::expression::Identifier; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use rustc_hash::FxHashMap; @@ -22,7 +22,7 @@ struct CompileTimeBinding { /// A compile time environment also indicates, if it is a function environment. #[derive(Debug, Finalize, Trace)] pub(crate) struct CompileTimeEnvironment { - outer: Option>>, + outer: Option>>, environment_index: usize, #[unsafe_ignore_trace] bindings: FxHashMap, @@ -208,7 +208,7 @@ impl Context<'_> { let environment_index = self.realm.compile_env.borrow().environment_index + 1; let outer = self.realm.compile_env.clone(); - self.realm.compile_env = Gc::new(GcCell::new(CompileTimeEnvironment { + self.realm.compile_env = Gc::new(GcRefCell::new(CompileTimeEnvironment { outer: Some(outer), environment_index, bindings: FxHashMap::default(), @@ -225,7 +225,7 @@ impl Context<'_> { /// Panics if there are no more environments that can be pop'ed. pub(crate) fn pop_compile_time_environment( &mut self, - ) -> (usize, Gc>) { + ) -> (usize, Gc>) { let current_env_borrow = self.realm.compile_env.borrow(); if let Some(outer) = ¤t_env_borrow.outer { let outer_clone = outer.clone(); diff --git a/boa_engine/src/environments/runtime.rs b/boa_engine/src/environments/runtime.rs index a9c5da85374..28a54630052 100644 --- a/boa_engine/src/environments/runtime.rs +++ b/boa_engine/src/environments/runtime.rs @@ -2,7 +2,7 @@ use crate::{ environments::CompileTimeEnvironment, error::JsNativeError, object::JsObject, Context, JsValue, }; use boa_ast::expression::Identifier; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use rustc_hash::FxHashSet; use std::cell::Cell; @@ -28,8 +28,8 @@ use std::cell::Cell; /// All poisoned environments have to be checked for added bindings. #[derive(Debug, Trace, Finalize)] pub(crate) struct DeclarativeEnvironment { - bindings: GcCell>>, - compile: Gc>, + bindings: GcRefCell>>, + compile: Gc>, #[unsafe_ignore_trace] poisoned: Cell, slots: Option, @@ -38,13 +38,13 @@ pub(crate) struct DeclarativeEnvironment { /// Describes the different types of internal slot data that an environment can hold. #[derive(Clone, Debug, Trace, Finalize)] pub(crate) enum EnvironmentSlots { - Function(GcCell), + Function(GcRefCell), Global, } impl EnvironmentSlots { /// Return the slots if they are part of a function environment. - pub(crate) const fn as_function_slots(&self) -> Option<&GcCell> { + pub(crate) const fn as_function_slots(&self) -> Option<&GcRefCell> { if let Self::Function(env) = &self { Some(env) } else { @@ -225,10 +225,10 @@ pub struct DeclarativeEnvironmentStack { impl DeclarativeEnvironmentStack { /// Create a new environment stack with the most outer declarative environment. - pub(crate) fn new(global_compile_environment: Gc>) -> Self { + pub(crate) fn new(global_compile_environment: Gc>) -> Self { Self { stack: vec![Gc::new(DeclarativeEnvironment { - bindings: GcCell::new(Vec::new()), + bindings: GcRefCell::new(Vec::new()), compile: global_compile_environment, poisoned: Cell::new(false), slots: Some(EnvironmentSlots::Global), @@ -349,7 +349,7 @@ impl DeclarativeEnvironmentStack { pub(crate) fn push_declarative( &mut self, num_bindings: usize, - compile_environment: Gc>, + compile_environment: Gc>, ) -> usize { let poisoned = self .stack @@ -361,7 +361,7 @@ impl DeclarativeEnvironmentStack { let index = self.stack.len(); self.stack.push(Gc::new(DeclarativeEnvironment { - bindings: GcCell::new(vec![None; num_bindings]), + bindings: GcRefCell::new(vec![None; num_bindings]), compile: compile_environment, poisoned: Cell::new(poisoned), slots: None, @@ -378,7 +378,7 @@ impl DeclarativeEnvironmentStack { pub(crate) fn push_function( &mut self, num_bindings: usize, - compile_environment: Gc>, + compile_environment: Gc>, this: Option, function_object: JsObject, new_target: Option, @@ -402,10 +402,10 @@ impl DeclarativeEnvironmentStack { let this = this.unwrap_or(JsValue::Null); self.stack.push(Gc::new(DeclarativeEnvironment { - bindings: GcCell::new(vec![None; num_bindings]), + bindings: GcRefCell::new(vec![None; num_bindings]), compile: compile_environment, poisoned: Cell::new(poisoned), - slots: Some(EnvironmentSlots::Function(GcCell::new(FunctionSlots { + slots: Some(EnvironmentSlots::Function(GcRefCell::new(FunctionSlots { this, this_binding_status, function_object, @@ -422,7 +422,7 @@ impl DeclarativeEnvironmentStack { pub(crate) fn push_function_inherit( &mut self, num_bindings: usize, - compile_environment: Gc>, + compile_environment: Gc>, ) { let outer = self .stack @@ -433,7 +433,7 @@ impl DeclarativeEnvironmentStack { let slots = outer.slots.clone(); self.stack.push(Gc::new(DeclarativeEnvironment { - bindings: GcCell::new(vec![None; num_bindings]), + bindings: GcRefCell::new(vec![None; num_bindings]), compile: compile_environment, poisoned: Cell::new(poisoned), slots, @@ -480,7 +480,7 @@ impl DeclarativeEnvironmentStack { /// # Panics /// /// Panics if no environment exists on the stack. - pub(crate) fn current_compile_environment(&self) -> Gc> { + pub(crate) fn current_compile_environment(&self) -> Gc> { self.stack .last() .expect("global environment must always exist") diff --git a/boa_engine/src/object/jsobject.rs b/boa_engine/src/object/jsobject.rs index 0f86d4ec6db..e4ce8296400 100644 --- a/boa_engine/src/object/jsobject.rs +++ b/boa_engine/src/object/jsobject.rs @@ -10,7 +10,7 @@ use crate::{ value::PreferredType, Context, JsResult, JsValue, }; -use boa_gc::{self, Finalize, Gc, GcCell, Trace}; +use boa_gc::{self, Finalize, Gc, GcRefCell, Trace}; use rustc_hash::FxHashMap; use std::{ cell::RefCell, @@ -21,15 +21,15 @@ use std::{ }; /// A wrapper type for an immutably borrowed type T. -pub type Ref<'a, T> = boa_gc::GcCellRef<'a, T>; +pub type Ref<'a, T> = boa_gc::GcRef<'a, T>; /// A wrapper type for a mutably borrowed type T. -pub type RefMut<'a, T, U> = boa_gc::GcCellRefMut<'a, T, U>; +pub type RefMut<'a, T, U> = boa_gc::GcRefMut<'a, T, U>; /// Garbage collected `Object`. #[derive(Trace, Finalize, Clone, Default)] pub struct JsObject { - inner: Gc>, + inner: Gc>, } impl JsObject { @@ -69,7 +69,7 @@ impl JsObject { /// [`OrdinaryObjectCreate`]: https://tc39.es/ecma262/#sec-ordinaryobjectcreate pub fn from_proto_and_data>>(prototype: O, data: ObjectData) -> Self { Self { - inner: Gc::new(GcCell::new(Object { + inner: Gc::new(GcRefCell::new(Object { data, prototype: prototype.into(), extensible: true, @@ -755,21 +755,21 @@ Cannot both specify accessors and a value or writable attribute", ) } - pub(crate) const fn inner(&self) -> &Gc> { + pub(crate) const fn inner(&self) -> &Gc> { &self.inner } } -impl AsRef> for JsObject { +impl AsRef> for JsObject { #[inline] - fn as_ref(&self) -> &GcCell { + fn as_ref(&self) -> &GcRefCell { &self.inner } } -impl From>> for JsObject { +impl From>> for JsObject { #[inline] - fn from(inner: Gc>) -> Self { + fn from(inner: Gc>) -> Self { Self { inner } } } diff --git a/boa_engine/src/object/mod.rs b/boa_engine/src/object/mod.rs index fe458d4504e..6bd37c300d8 100644 --- a/boa_engine/src/object/mod.rs +++ b/boa_engine/src/object/mod.rs @@ -54,7 +54,7 @@ use crate::{ Context, JsBigInt, JsString, JsSymbol, JsValue, }; -use boa_gc::{custom_trace, Finalize, GcCell, Trace, WeakGc}; +use boa_gc::{custom_trace, Finalize, GcRefCell, Trace, WeakGc}; use boa_interner::Sym; use rustc_hash::FxHashMap; use std::{ @@ -267,7 +267,7 @@ pub enum ObjectKind { Promise(Promise), /// The `WeakRef` object kind. - WeakRef(WeakGc>), + WeakRef(WeakGc>), /// The `Intl.Collator` object kind. #[cfg(feature = "intl")] @@ -619,7 +619,7 @@ impl ObjectData { } /// Creates the `WeakRef` object data - pub fn weak_ref(weak_ref: WeakGc>) -> Self { + pub fn weak_ref(weak_ref: WeakGc>) -> Self { Self { kind: ObjectKind::WeakRef(weak_ref), internal_methods: &ORDINARY_INTERNAL_METHODS, @@ -1625,7 +1625,7 @@ impl Object { /// Gets the `WeakRef` data if the object is a `WeakRef`. #[inline] - pub const fn as_weak_ref(&self) -> Option<&WeakGc>> { + pub const fn as_weak_ref(&self) -> Option<&WeakGc>> { match self.data { ObjectData { kind: ObjectKind::WeakRef(ref weak_ref), diff --git a/boa_engine/src/realm.rs b/boa_engine/src/realm.rs index 7f7df1824ec..a60b211f064 100644 --- a/boa_engine/src/realm.rs +++ b/boa_engine/src/realm.rs @@ -10,7 +10,7 @@ use crate::{ environments::{CompileTimeEnvironment, DeclarativeEnvironmentStack}, object::{GlobalPropertyMap, JsObject, JsPrototype, ObjectData, PropertyMap}, }; -use boa_gc::{Gc, GcCell}; +use boa_gc::{Gc, GcRefCell}; use boa_profiler::Profiler; /// Representation of a Realm. @@ -23,7 +23,7 @@ pub struct Realm { pub(crate) global_property_map: PropertyMap, pub(crate) global_prototype: JsPrototype, pub(crate) environments: DeclarativeEnvironmentStack, - pub(crate) compile_env: Gc>, + pub(crate) compile_env: Gc>, } impl Realm { @@ -36,7 +36,8 @@ impl Realm { // Allow identification of the global object easily let global_object = JsObject::from_proto_and_data(None, ObjectData::global()); - let global_compile_environment = Gc::new(GcCell::new(CompileTimeEnvironment::new_global())); + let global_compile_environment = + Gc::new(GcRefCell::new(CompileTimeEnvironment::new_global())); Self { global_object, diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index 945f86cb0dd..c4f2b7a024d 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -24,7 +24,7 @@ use crate::{ Context, JsResult, JsString, JsValue, }; use boa_ast::{expression::Identifier, function::FormalParameterList}; -use boa_gc::{Finalize, Gc, GcCell, Trace}; +use boa_gc::{Finalize, Gc, GcRefCell, Trace}; use boa_interner::{Interner, Sym, ToInternedString}; use boa_profiler::Profiler; use std::{collections::VecDeque, convert::TryInto, mem::size_of}; @@ -103,7 +103,7 @@ pub struct CodeBlock { pub(crate) arguments_binding: Option, /// Compile time environments in this function. - pub(crate) compile_environments: Vec>>, + pub(crate) compile_environments: Vec>>, /// The `[[IsClassConstructor]]` internal slot. pub(crate) is_class_constructor: bool, @@ -1083,7 +1083,7 @@ impl JsObject { prototype, ObjectData::generator(Generator { state: GeneratorState::SuspendedStart, - context: Some(Gc::new(GcCell::new(GeneratorContext { + context: Some(Gc::new(GcRefCell::new(GeneratorContext { environments, call_frame, stack, @@ -1227,7 +1227,7 @@ impl JsObject { prototype, ObjectData::async_generator(AsyncGenerator { state: AsyncGeneratorState::SuspendedStart, - context: Some(Gc::new(GcCell::new(GeneratorContext { + context: Some(Gc::new(GcRefCell::new(GeneratorContext { environments, call_frame, stack, diff --git a/boa_engine/src/vm/opcode/await_stm/mod.rs b/boa_engine/src/vm/opcode/await_stm/mod.rs index fb68dc5d92c..e95a27f881b 100644 --- a/boa_engine/src/vm/opcode/await_stm/mod.rs +++ b/boa_engine/src/vm/opcode/await_stm/mod.rs @@ -1,4 +1,4 @@ -use boa_gc::{Gc, GcCell}; +use boa_gc::{Gc, GcRefCell}; use crate::{ builtins::{JsArgs, Promise}, @@ -61,7 +61,7 @@ impl Operation for Await { Ok(JsValue::undefined()) }, - Gc::new(GcCell::new(( + Gc::new(GcRefCell::new(( context.realm.environments.clone(), context.vm.stack.clone(), context.vm.frame().clone(), @@ -104,7 +104,7 @@ impl Operation for Await { Ok(JsValue::undefined()) }, - Gc::new(GcCell::new(( + Gc::new(GcRefCell::new(( context.realm.environments.clone(), context.vm.stack.clone(), context.vm.frame().clone(), diff --git a/boa_examples/src/bin/closures.rs b/boa_examples/src/bin/closures.rs index 5309694193d..44fdfde6830 100644 --- a/boa_examples/src/bin/closures.rs +++ b/boa_examples/src/bin/closures.rs @@ -11,7 +11,7 @@ use boa_engine::{ string::utf16, Context, JsError, JsNativeError, JsString, JsValue, }; -use boa_gc::{Finalize, GcCell, Trace}; +use boa_gc::{Finalize, GcRefCell, Trace}; fn main() -> Result<(), JsError> { // We create a new `Context` to create a new Javascript executor. @@ -96,7 +96,7 @@ fn main() -> Result<(), JsError> { Ok(message.into()) }, // Here is where we move `clone_variable` into the closure. - GcCell::new(clone_variable), + GcRefCell::new(clone_variable), ), ) // And here we assign `createMessage` to the `name` property of the closure. diff --git a/boa_gc/src/cell.rs b/boa_gc/src/cell.rs index 0f2d040fe82..235d908efc5 100644 --- a/boa_gc/src/cell.rs +++ b/boa_gc/src/cell.rs @@ -117,12 +117,12 @@ impl Debug for BorrowFlag { /// that can be used inside of a garbage-collected pointer. /// /// This object is a `RefCell` that can be used inside of a `Gc`. -pub struct GcCell { +pub struct GcRefCell { pub(crate) flags: Cell, pub(crate) cell: UnsafeCell, } -impl GcCell { +impl GcRefCell { /// Creates a new `GcCell` containing `value`. pub const fn new(value: T) -> Self { Self { @@ -137,7 +137,7 @@ impl GcCell { } } -impl GcCell { +impl GcRefCell { /// Immutably borrows the wrapped value. /// /// The borrow lasts until the returned `GcCellRef` exits scope. @@ -146,7 +146,7 @@ impl GcCell { /// # Panics /// /// Panics if the value is currently mutably borrowed. - pub fn borrow(&self) -> GcCellRef<'_, T> { + pub fn borrow(&self) -> GcRef<'_, T> { match self.try_borrow() { Ok(value) => value, Err(e) => panic!("{}", e), @@ -161,7 +161,7 @@ impl GcCell { /// # Panics /// /// Panics if the value is currently borrowed. - pub fn borrow_mut(&self) -> GcCellRefMut<'_, T> { + pub fn borrow_mut(&self) -> GcRefMut<'_, T> { match self.try_borrow_mut() { Ok(value) => value, Err(e) => panic!("{}", e), @@ -179,7 +179,7 @@ impl GcCell { /// # Errors /// /// Returns an `Err` if the value is currently mutably borrowed. - pub fn try_borrow(&self) -> Result, BorrowError> { + pub fn try_borrow(&self) -> Result, BorrowError> { if self.flags.get().borrowed() == BorrowState::Writing { return Err(BorrowError); } @@ -187,7 +187,7 @@ impl GcCell { // SAFETY: calling value on a rooted value may cause Undefined Behavior unsafe { - Ok(GcCellRef { + Ok(GcRef { flags: &self.flags, value: &*self.cell.get(), }) @@ -204,7 +204,7 @@ impl GcCell { /// # Errors /// /// Returns an `Err` if the value is currently borrowed. - pub fn try_borrow_mut(&self) -> Result, BorrowMutError> { + pub fn try_borrow_mut(&self) -> Result, BorrowMutError> { if self.flags.get().borrowed() != BorrowState::Unused { return Err(BorrowMutError); } @@ -219,7 +219,7 @@ impl GcCell { (*self.cell.get()).root(); } - Ok(GcCellRefMut { + Ok(GcRefMut { gc_cell: self, value: &mut *self.cell.get(), }) @@ -247,13 +247,13 @@ impl Display for BorrowMutError { } } -impl Finalize for GcCell {} +impl Finalize for GcRefCell {} // SAFETY: GcCell maintains it's own BorrowState and rootedness. GcCell's implementation // focuses on only continuing Trace based methods while the cell state is not written. // Implementing a Trace while the cell is being written to or incorrectly implementing Trace // on GcCell's value may cause Undefined Behavior -unsafe impl Trace for GcCell { +unsafe impl Trace for GcRefCell { unsafe fn trace(&self) { match self.flags.get().borrowed() { BorrowState::Writing => (), @@ -295,12 +295,12 @@ unsafe impl Trace for GcCell { } /// A wrapper type for an immutably borrowed value from a `GcCell`. -pub struct GcCellRef<'a, T: ?Sized + 'static> { +pub struct GcRef<'a, T: ?Sized + 'static> { pub(crate) flags: &'a Cell, pub(crate) value: &'a T, } -impl<'a, T: ?Sized> GcCellRef<'a, T> { +impl<'a, T: ?Sized> GcRef<'a, T> { /// Copies a `GcCellRef`. /// /// The `GcCell` is already immutably borrowed, so this cannot fail. @@ -311,9 +311,9 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> { /// the contents of a `GcCell`. #[allow(clippy::should_implement_trait)] #[must_use] - pub fn clone(orig: &GcCellRef<'a, T>) -> GcCellRef<'a, T> { + pub fn clone(orig: &GcRef<'a, T>) -> GcRef<'a, T> { orig.flags.set(orig.flags.get().add_reading()); - GcCellRef { + GcRef { flags: orig.flags, value: orig.value, } @@ -326,12 +326,12 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> { /// This is an associated function that needs to be used as `GcCellRef::map(...)`. /// A method would interfere with methods of the same name on the contents /// of a `GcCellRef` used through `Deref`. - pub fn map(orig: Self, f: F) -> GcCellRef<'a, U> + pub fn map(orig: Self, f: F) -> GcRef<'a, U> where U: ?Sized, F: FnOnce(&T) -> &U, { - let ret = GcCellRef { + let ret = GcRef { flags: orig.flags, value: f(orig.value), }; @@ -349,7 +349,7 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> { /// /// This is an associated function that needs to be used as `GcCellRef::map_split(...)`. /// A method would interfere with methods of the same name on the contents of a `GcCellRef` used through `Deref`. - pub fn map_split(orig: Self, f: F) -> (GcCellRef<'a, U>, GcCellRef<'a, V>) + pub fn map_split(orig: Self, f: F) -> (GcRef<'a, U>, GcRef<'a, V>) where U: ?Sized, V: ?Sized, @@ -360,11 +360,11 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> { orig.flags.set(orig.flags.get().add_reading()); let ret = ( - GcCellRef { + GcRef { flags: orig.flags, value: a, }, - GcCellRef { + GcRef { flags: orig.flags, value: b, }, @@ -378,7 +378,7 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> { } } -impl Deref for GcCellRef<'_, T> { +impl Deref for GcRef<'_, T> { type Target = T; fn deref(&self) -> &T { @@ -386,32 +386,32 @@ impl Deref for GcCellRef<'_, T> { } } -impl Drop for GcCellRef<'_, T> { +impl Drop for GcRef<'_, T> { fn drop(&mut self) { debug_assert!(self.flags.get().borrowed() == BorrowState::Reading); self.flags.set(self.flags.get().sub_reading()); } } -impl Debug for GcCellRef<'_, T> { +impl Debug for GcRef<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Debug::fmt(&**self, f) } } -impl Display for GcCellRef<'_, T> { +impl Display for GcRef<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Display::fmt(&**self, f) } } /// A wrapper type for a mutably borrowed value from a `GcCell`. -pub struct GcCellRefMut<'a, T: Trace + ?Sized + 'static, U: ?Sized = T> { - pub(crate) gc_cell: &'a GcCell, +pub struct GcRefMut<'a, T: Trace + ?Sized + 'static, U: ?Sized = T> { + pub(crate) gc_cell: &'a GcRefCell, pub(crate) value: &'a mut U, } -impl<'a, T: Trace + ?Sized, U: ?Sized> GcCellRefMut<'a, T, U> { +impl<'a, T: Trace + ?Sized, U: ?Sized> GcRefMut<'a, T, U> { /// Makes a new `GcCellRefMut` for a component of the borrowed data, e.g., an enum /// variant. /// @@ -420,7 +420,7 @@ impl<'a, T: Trace + ?Sized, U: ?Sized> GcCellRefMut<'a, T, U> { /// This is an associated function that needs to be used as /// `GcCellRefMut::map(...)`. A method would interfere with methods of the same /// name on the contents of a `GcCell` used through `Deref`. - pub fn map(orig: Self, f: F) -> GcCellRefMut<'a, T, V> + pub fn map(orig: Self, f: F) -> GcRefMut<'a, T, V> where V: ?Sized, F: FnOnce(&mut U) -> &mut V, @@ -429,7 +429,7 @@ impl<'a, T: Trace + ?Sized, U: ?Sized> GcCellRefMut<'a, T, U> { // SAFETY: This is safe as `GcCellRefMut` is already borrowed, so the value is rooted. let value = unsafe { &mut *(orig.value as *mut U) }; - let ret = GcCellRefMut { + let ret = GcRefMut { gc_cell: orig.gc_cell, value: f(value), }; @@ -442,7 +442,7 @@ impl<'a, T: Trace + ?Sized, U: ?Sized> GcCellRefMut<'a, T, U> { } } -impl Deref for GcCellRefMut<'_, T, U> { +impl Deref for GcRefMut<'_, T, U> { type Target = U; fn deref(&self) -> &U { @@ -450,13 +450,13 @@ impl Deref for GcCellRefMut<'_, T, U> { } } -impl DerefMut for GcCellRefMut<'_, T, U> { +impl DerefMut for GcRefMut<'_, T, U> { fn deref_mut(&mut self) -> &mut U { self.value } } -impl Drop for GcCellRefMut<'_, T, U> { +impl Drop for GcRefMut<'_, T, U> { fn drop(&mut self) { debug_assert!(self.gc_cell.flags.get().borrowed() == BorrowState::Writing); // Restore the rooted state of the GcCell's contents to the state of the GcCell. @@ -474,45 +474,45 @@ impl Drop for GcCellRefMut<'_, T, U> { } } -impl Debug for GcCellRefMut<'_, T, U> { +impl Debug for GcRefMut<'_, T, U> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Debug::fmt(&**self, f) } } -impl Display for GcCellRefMut<'_, T, U> { +impl Display for GcRefMut<'_, T, U> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Display::fmt(&**self, f) } } // SAFETY: GcCell tracks it's `BorrowState` is `Writing` -unsafe impl Send for GcCell {} +unsafe impl Send for GcRefCell {} -impl Clone for GcCell { +impl Clone for GcRefCell { fn clone(&self) -> Self { Self::new(self.borrow().clone()) } } -impl Default for GcCell { +impl Default for GcRefCell { fn default() -> Self { Self::new(Default::default()) } } #[allow(clippy::inline_always)] -impl PartialEq for GcCell { +impl PartialEq for GcRefCell { #[inline(always)] fn eq(&self, other: &Self) -> bool { *self.borrow() == *other.borrow() } } -impl Eq for GcCell {} +impl Eq for GcRefCell {} #[allow(clippy::inline_always)] -impl PartialOrd for GcCell { +impl PartialOrd for GcRefCell { #[inline(always)] fn partial_cmp(&self, other: &Self) -> Option { (*self.borrow()).partial_cmp(&*other.borrow()) @@ -539,13 +539,13 @@ impl PartialOrd for GcCell { } } -impl Ord for GcCell { +impl Ord for GcRefCell { fn cmp(&self, other: &Self) -> Ordering { (*self.borrow()).cmp(&*other.borrow()) } } -impl Debug for GcCell { +impl Debug for GcRefCell { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.flags.get().borrowed() { BorrowState::Unused | BorrowState::Reading => f diff --git a/boa_gc/src/lib.rs b/boa_gc/src/lib.rs index 909b1df16a1..87a0b9267bb 100644 --- a/boa_gc/src/lib.rs +++ b/boa_gc/src/lib.rs @@ -105,7 +105,7 @@ use std::{ pub use crate::trace::{Finalize, Trace}; pub use boa_macros::{Finalize, Trace}; -pub use cell::{GcCell, GcCellRef, GcCellRefMut}; +pub use cell::{GcRef, GcRefCell, GcRefMut}; pub use internals::GcBox; pub use pointers::{Ephemeron, Gc, WeakGc}; diff --git a/boa_gc/src/test/allocation.rs b/boa_gc/src/test/allocation.rs index 27386999836..93bdab33376 100644 --- a/boa_gc/src/test/allocation.rs +++ b/boa_gc/src/test/allocation.rs @@ -1,10 +1,10 @@ use super::{run_test, Harness}; -use crate::{force_collect, Gc, GcCell}; +use crate::{force_collect, Gc, GcRefCell}; #[test] fn gc_basic_cell_allocation() { run_test(|| { - let gc_cell = Gc::new(GcCell::new(16_u16)); + let gc_cell = Gc::new(GcRefCell::new(16_u16)); force_collect(); Harness::assert_collections(1); diff --git a/boa_gc/src/test/cell.rs b/boa_gc/src/test/cell.rs index 3c067fd3cb6..42222ec4e51 100644 --- a/boa_gc/src/test/cell.rs +++ b/boa_gc/src/test/cell.rs @@ -1,13 +1,13 @@ use super::run_test; -use crate::{Gc, GcCell}; +use crate::{Gc, GcRefCell}; #[test] fn boa_borrow_mut_test() { run_test(|| { - let v = Gc::new(GcCell::new(Vec::new())); + let v = Gc::new(GcRefCell::new(Vec::new())); for _ in 1..=259 { - let cell = Gc::new(GcCell::new([0u8; 10])); + let cell = Gc::new(GcRefCell::new([0u8; 10])); v.borrow_mut().push(cell); } });