Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross realm symbols #1243

Merged
merged 4 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
gc::{Finalize, Trace},
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result,
};

Expand Down Expand Up @@ -126,7 +127,7 @@ impl ArrayIterator {
make_builtin_fn(Self::next, "next", &array_iterator, 0, context);
array_iterator.set_prototype_instance(iterator_prototype);

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();
let to_string_tag_property = DataDescriptor::new("Array Iterator", Attribute::CONFIGURABLE);
array_iterator.insert(to_string_tag, to_string_tag_property);
array_iterator
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
gc::GcObject,
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
value::{same_value_zero, IntegerOrInfinity, Value},
BoaProfiler, Context, Result,
};
Expand All @@ -42,7 +43,7 @@ impl BuiltIn for Array {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let symbol_iterator = context.well_known_symbols().iterator_symbol();
let symbol_iterator = WellKnownSymbols::iterator();

let values_function = FunctionBuilder::new(context, Self::values)
.name("values")
Expand Down
6 changes: 3 additions & 3 deletions boa/src/builtins/iterable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
builtins::MapIterator,
object::{GcObject, ObjectInitializer},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
};

Expand Down Expand Up @@ -79,8 +80,7 @@ pub fn create_iter_result_object(context: &mut Context, value: Value, done: bool

/// Get an iterator record
pub fn get_iterator(context: &mut Context, iterable: Value) -> Result<IteratorRecord> {
let iterator_function =
iterable.get_field(context.well_known_symbols().iterator_symbol(), context)?;
let iterator_function = iterable.get_field(WellKnownSymbols::iterator(), context)?;
if iterator_function.is_null_or_undefined() {
return Err(context.construct_type_error("Not an iterable"));
}
Expand All @@ -101,7 +101,7 @@ pub fn get_iterator(context: &mut Context, iterable: Value) -> Result<IteratorRe
fn create_iterator_prototype(context: &mut Context) -> GcObject {
let _timer = BoaProfiler::global().start_event("Iterator Prototype", "init");

let symbol_iterator = context.well_known_symbols().iterator_symbol();
let symbol_iterator = WellKnownSymbols::iterator();
let iterator_prototype = ObjectInitializer::new(context)
.function(
|v, _, _| Ok(v.clone()),
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
//! [json]: https://www.json.org/json-en.html
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

use crate::object::Object;
use crate::{
builtins::BuiltIn,
object::ObjectInitializer,
property::{Attribute, DataDescriptor, PropertyKey},
value::IntegerOrInfinity,
BoaProfiler, Context, Result, Value,
};
use crate::{object::Object, symbol::WellKnownSymbols};
use serde::Serialize;
use serde_json::{self, ser::PrettyFormatter, Serializer, Value as JSONValue};

Expand All @@ -41,7 +41,7 @@ impl BuiltIn for Json {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();

let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE;

Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, Value},
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result,
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -146,7 +147,7 @@ impl MapIterator {
make_builtin_fn(Self::next, "next", &map_iterator, 0, context);
map_iterator.set_prototype_instance(iterator_prototype);

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();
let to_string_tag_property = DataDescriptor::new("Map Iterator", Attribute::CONFIGURABLE);
map_iterator.insert(to_string_tag, to_string_tag_property);
map_iterator
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
};
use ordered_map::OrderedMap;
Expand All @@ -28,7 +29,7 @@ impl BuiltIn for Map {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let iterator_symbol = context.well_known_symbols().iterator_symbol();
let iterator_symbol = WellKnownSymbols::iterator();

let entries_function = FunctionBuilder::new(context, Self::entries)
.name("entries")
Expand Down
6 changes: 3 additions & 3 deletions boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

use crate::{
builtins::BuiltIn, object::ObjectInitializer, property::Attribute, BoaProfiler, Context,
Result, Value,
builtins::BuiltIn, object::ObjectInitializer, property::Attribute, symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
};

#[cfg(test)]
Expand All @@ -34,7 +34,7 @@ impl BuiltIn for Math {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
use std::f64;

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();

let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;
let object = ObjectInitializer::new(context)
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/object/for_in_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::property::PropertyKey;
use crate::value::RcString;
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object},
Expand All @@ -7,6 +6,7 @@ use crate::{
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result, Value,
};
use crate::{property::PropertyKey, symbol::WellKnownSymbols};
use rustc_hash::FxHashSet;
use std::collections::VecDeque;

Expand Down Expand Up @@ -133,7 +133,7 @@ impl ForInIterator {
make_builtin_fn(Self::next, "next", &for_in_iterator, 0, context);
for_in_iterator.set_prototype_instance(iterator_prototype);

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();
let to_string_tag_property =
DataDescriptor::new("For In Iterator", Attribute::CONFIGURABLE);
for_in_iterator.insert(to_string_tag, to_string_tag_property);
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
property::Attribute,
property::DataDescriptor,
property::PropertyDescriptor,
symbol::WellKnownSymbols,
value::{same_value, Type, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -432,7 +433,7 @@ impl Object {
};

let tag = o.get(
&context.well_known_symbols().to_string_tag_symbol().into(),
&WellKnownSymbols::to_string_tag().into(),
o.clone().into(),
context,
)?;
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/reflect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
builtins::{self, BuiltIn},
object::{Object, ObjectData, ObjectInitializer},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
};

Expand All @@ -34,7 +35,7 @@ impl BuiltIn for Reflect {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();

let object = ObjectInitializer::new(context)
.function(Self::apply, "apply", 3)
Expand Down
5 changes: 3 additions & 2 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
builtins::{string::string_iterator::StringIterator, Array, BuiltIn, RegExp},
object::{ConstructorBuilder, Object, ObjectData},
property::Attribute,
symbol::WellKnownSymbols,
value::{RcString, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -94,7 +95,7 @@ impl BuiltIn for String {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let symbol_iterator = context.well_known_symbols().iterator_symbol();
let symbol_iterator = WellKnownSymbols::iterator();

let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;
let string_object = ConstructorBuilder::with_standard_object(
Expand Down Expand Up @@ -1215,7 +1216,7 @@ impl String {
if let Some(result) = separator
.and_then(|separator| separator.as_object())
.and_then(|separator| {
let key = context.well_known_symbols().split_symbol();
let key = WellKnownSymbols::split();

match separator.get_method(context, key) {
Ok(splitter) => splitter.map(|splitter| {
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/string/string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
gc::{Finalize, Trace},
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
BoaProfiler, Context, Result, Value,
};

Expand Down Expand Up @@ -77,7 +78,7 @@ impl StringIterator {
make_builtin_fn(Self::next, "next", &array_iterator, 0, context);
array_iterator.set_prototype_instance(iterator_prototype);

let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag = WellKnownSymbols::to_string_tag();
let to_string_tag_property =
DataDescriptor::new("String Iterator", Attribute::CONFIGURABLE);
array_iterator.insert(to_string_tag, to_string_tag_property);
Expand Down
31 changes: 14 additions & 17 deletions boa/src/builtins/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, FunctionBuilder},
property::Attribute,
symbol::RcSymbol,
symbol::{RcSymbol, WellKnownSymbols},
value::Value,
BoaProfiler, Context, Result,
};
Expand All @@ -40,22 +40,19 @@ impl BuiltIn for Symbol {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

// https://tc39.es/ecma262/#sec-well-known-symbols
let well_known_symbols = context.well_known_symbols();

let symbol_async_iterator = well_known_symbols.async_iterator_symbol();
let symbol_has_instance = well_known_symbols.has_instance_symbol();
let symbol_is_concat_spreadable = well_known_symbols.is_concat_spreadable_symbol();
let symbol_iterator = well_known_symbols.iterator_symbol();
let symbol_match = well_known_symbols.match_symbol();
let symbol_match_all = well_known_symbols.match_all_symbol();
let symbol_replace = well_known_symbols.replace_symbol();
let symbol_search = well_known_symbols.search_symbol();
let symbol_species = well_known_symbols.species_symbol();
let symbol_split = well_known_symbols.split_symbol();
let symbol_to_primitive = well_known_symbols.to_primitive_symbol();
let symbol_to_string_tag = well_known_symbols.to_string_tag_symbol();
let symbol_unscopables = well_known_symbols.unscopables_symbol();
let symbol_async_iterator = WellKnownSymbols::async_iterator();
let symbol_has_instance = WellKnownSymbols::has_instance();
let symbol_is_concat_spreadable = WellKnownSymbols::is_concat_spreadable();
let symbol_iterator = WellKnownSymbols::iterator();
let symbol_match = WellKnownSymbols::match_();
let symbol_match_all = WellKnownSymbols::match_all();
let symbol_replace = WellKnownSymbols::replace();
let symbol_search = WellKnownSymbols::search();
let symbol_species = WellKnownSymbols::species();
let symbol_split = WellKnownSymbols::split();
let symbol_to_primitive = WellKnownSymbols::to_primitive();
let symbol_to_string_tag = WellKnownSymbols::to_string_tag();
let symbol_unscopables = WellKnownSymbols::unscopables();

let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;

Expand Down
41 changes: 2 additions & 39 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
object::{GcObject, Object, PROTOTYPE},
property::{Attribute, DataDescriptor, PropertyKey},
realm::Realm,
symbol::{RcSymbol, Symbol, WellKnownSymbols},
symbol::{RcSymbol, Symbol},
syntax::{
ast::{
node::{
Expand Down Expand Up @@ -215,18 +215,10 @@ pub struct Context {
/// The current executor.
executor: Interpreter,

/// Symbol hash.
///
/// For now this is an incremented u64 number.
symbol_count: u64,

/// console object state.
#[cfg(feature = "console")]
console: Console,

/// Cached well known symbols
well_known_symbols: WellKnownSymbols,

/// Cached iterator prototypes.
iterator_prototypes: IteratorPrototypes,

Expand All @@ -241,14 +233,11 @@ impl Default for Context {
fn default() -> Self {
let realm = Realm::create();
let executor = Interpreter::new();
let (well_known_symbols, symbol_count) = WellKnownSymbols::new();
let mut context = Self {
realm,
executor,
symbol_count,
#[cfg(feature = "console")]
console: Console::default(),
well_known_symbols,
iterator_prototypes: IteratorPrototypes::default(),
standard_objects: Default::default(),
trace: false,
Expand Down Expand Up @@ -306,20 +295,10 @@ impl Context {
builtins::init(self);
}

/// Generates a new `Symbol` internal hash.
///
/// This currently is an incremented value.
#[inline]
fn generate_hash(&mut self) -> u64 {
let hash = self.symbol_count;
self.symbol_count += 1;
hash
}

/// Construct a new `Symbol` with an optional description.
#[inline]
pub fn construct_symbol(&mut self, description: Option<RcString>) -> RcSymbol {
RcSymbol::from(Symbol::new(self.generate_hash(), description))
RcSymbol::from(Symbol::new(description))
}

/// Construct an empty object.
Expand Down Expand Up @@ -722,22 +701,6 @@ impl Context {
result
}

/// Returns a structure that contains the JavaScript well known symbols.
///
/// # Examples
/// ```
///# use boa::Context;
/// let mut context = Context::new();
///
/// let iterator = context.well_known_symbols().iterator_symbol();
/// assert_eq!(iterator.description(), Some("Symbol.iterator"));
/// ```
/// This is equivalent to `let iterator = Symbol.iterator` in JavaScript.
#[inline]
pub fn well_known_symbols(&self) -> &WellKnownSymbols {
&self.well_known_symbols
}

/// Return the cached iterator prototypes.
#[inline]
pub fn iterator_prototypes(&self) -> &IteratorPrototypes {
Expand Down
Loading