Skip to content

Commit 9465814

Browse files
committed
Trying to fix unsoundness in InternalState
1 parent 143434f commit 9465814

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

boa/src/builtins/console/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ use crate::{
2323
},
2424
exec::Interpreter,
2525
};
26+
use gc::{Finalize, Trace};
2627
use rustc_hash::FxHashMap;
2728
use std::time::SystemTime;
2829

2930
/// This is the internal console object state.
30-
#[derive(Debug, Default)]
31+
#[derive(Debug, Default, Trace, Finalize)]
3132
pub struct ConsoleState {
3233
count_map: FxHashMap<String, u32>,
3334
timer_map: FxHashMap<String, u128>,

boa/src/builtins/function/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ pub enum ConstructorKind {
3939
/// Defines how this references are interpreted within the formal parameters and code body of the function.
4040
///
4141
/// Arrow functions don't define a `this` and thus are lexical, `function`s do define a this and thus are NonLexical
42-
#[derive(Trace, Finalize, Debug, Clone)]
42+
#[derive(Finalize, Copy, Debug, Clone)]
4343
pub enum ThisMode {
4444
Lexical,
4545
NonLexical,
4646
}
4747

48+
unsafe impl Trace for ThisMode {
49+
unsafe_empty_trace!();
50+
}
51+
4852
/// FunctionBody is specific to this interpreter, it will either be Rust code or JavaScript code (AST Node)
4953
#[derive(Clone, Finalize)]
5054
pub enum FunctionBody {

boa/src/builtins/object/internal_state.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
//! Implementations for storing normal rust structs inside any object as internal state.
22
3+
use gc::{unsafe_empty_trace, Finalize, Trace};
34
use std::{
45
any::Any,
56
fmt::{self, Debug},
67
ops::{Deref, DerefMut},
78
rc::Rc,
89
};
910

10-
use gc::{unsafe_empty_trace, Finalize, Trace};
11-
1211
/// Wrapper around `Rc` to implement `Trace` and `Finalize`.
1312
#[derive(Clone)]
1413
pub struct InternalStateCell {
1514
/// The internal state.
16-
state: Rc<dyn Any>,
15+
state: Rc<dyn InternalState + 'static>,
1716
}
1817

1918
impl Finalize for InternalStateCell {}
@@ -23,7 +22,7 @@ unsafe impl Trace for InternalStateCell {
2322
}
2423

2524
impl Deref for InternalStateCell {
26-
type Target = dyn Any;
25+
type Target = dyn InternalState;
2726
fn deref(&self) -> &Self::Target {
2827
Deref::deref(&self.state)
2928
}
@@ -45,20 +44,22 @@ impl Debug for InternalStateCell {
4544

4645
impl InternalStateCell {
4746
/// Create new `InternalStateCell` from a value.
48-
pub fn new<T: Any + InternalState>(value: T) -> Self {
47+
pub fn new<T: 'static + InternalState>(value: T) -> Self {
4948
Self {
5049
state: Rc::new(value),
5150
}
5251
}
52+
5353
/// Get a reference to the stored value and cast it to `T`.
5454
pub fn downcast_ref<T: Any + InternalState>(&self) -> Option<&T> {
5555
self.deref().downcast_ref::<T>()
5656
}
57+
5758
/// Get a mutable reference to the stored value and cast it to `T`.
58-
pub fn downcast_mut<T: Any + InternalState>(&mut self) -> Option<&mut T> {
59-
self.deref_mut().downcast_mut::<T>()
59+
pub fn downcast_mut<T: InternalState>(&mut self) -> Option<&mut T> {
60+
self.state.downcast_mut::<T>()
6061
}
6162
}
6263

6364
/// This trait must be implemented by all structs used for internal state.
64-
pub trait InternalState: Debug {}
65+
pub trait InternalState: Debug + Trace + Any {}

boa/src/builtins/regexp/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
//! [spec]: https://tc39.es/ecma262/#sec-regexp-constructor
1010
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
1111
12-
use std::ops::Deref;
13-
12+
use gc::{unsafe_empty_trace, Finalize, Gc, Trace};
1413
use regex::Regex;
14+
use std::ops::Deref;
1515

1616
use crate::{
1717
builtins::{
@@ -26,7 +26,7 @@ use crate::{
2626
mod tests;
2727

2828
/// The internal representation on a `RegExp` object.
29-
#[derive(Debug)]
29+
#[derive(Debug, Finalize)]
3030
struct RegExp {
3131
/// Regex matcher.
3232
matcher: Regex,
@@ -56,6 +56,12 @@ struct RegExp {
5656
unicode: bool,
5757
}
5858

59+
// FIXME: Maybe `Regex` could at some point implement `Trace`, and we need to take that into
60+
// account.
61+
unsafe impl Trace for RegExp {
62+
unsafe_empty_trace!();
63+
}
64+
5965
impl InternalState for RegExp {}
6066

6167
/// Create a new `RegExp`

0 commit comments

Comments
 (0)