|
|
@ -21,10 +21,23 @@ use crate::{ |
|
|
|
gc::{Finalize, Trace}, |
|
|
|
gc::{Finalize, Trace}, |
|
|
|
value::RcString, |
|
|
|
value::RcString, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
use std::{ |
|
|
|
|
|
|
|
cell::Cell, |
|
|
|
|
|
|
|
hash::{Hash, Hasher}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
pub use rcsymbol::RcSymbol; |
|
|
|
pub use rcsymbol::RcSymbol; |
|
|
|
|
|
|
|
|
|
|
|
/// A structure that contains the JavaScript well known symbols.
|
|
|
|
/// A structure that contains the JavaScript well known symbols.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
///# use boa::symbol::WellKnownSymbols;
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let iterator = WellKnownSymbols::iterator();
|
|
|
|
|
|
|
|
/// assert_eq!(iterator.description(), Some("Symbol.iterator"));
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// This is equivalent to `let iterator = Symbol.iterator` in JavaScript.
|
|
|
|
#[derive(Debug, Clone)] |
|
|
|
#[derive(Debug, Clone)] |
|
|
|
pub struct WellKnownSymbols { |
|
|
|
pub struct WellKnownSymbols { |
|
|
|
async_iterator: RcSymbol, |
|
|
|
async_iterator: RcSymbol, |
|
|
@ -42,39 +55,54 @@ pub struct WellKnownSymbols { |
|
|
|
unscopables: RcSymbol, |
|
|
|
unscopables: RcSymbol, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Reserved number of symbols.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This is where the well known symbol live
|
|
|
|
|
|
|
|
/// and internal engine symbols.
|
|
|
|
|
|
|
|
const RESERVED_SYMBOL_HASHES: u64 = 128; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread_local! { |
|
|
|
|
|
|
|
/// Cached well known symbols
|
|
|
|
|
|
|
|
static WELL_KNOW_SYMBOLS: WellKnownSymbols = WellKnownSymbols::new(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Symbol hash.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// For now this is an incremented u64 number.
|
|
|
|
|
|
|
|
static SYMBOL_HASH_COUNT: Cell<u64> = Cell::new(RESERVED_SYMBOL_HASHES); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl WellKnownSymbols { |
|
|
|
impl WellKnownSymbols { |
|
|
|
pub(crate) fn new() -> (Self, u64) { |
|
|
|
/// Create the well known symbols.
|
|
|
|
|
|
|
|
fn new() -> Self { |
|
|
|
let mut count = 0; |
|
|
|
let mut count = 0; |
|
|
|
|
|
|
|
|
|
|
|
let async_iterator = Symbol::new(count, Some("Symbol.asyncIterator".into())).into(); |
|
|
|
let async_iterator = Symbol::with_hash(count, Some("Symbol.asyncIterator".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let has_instance = Symbol::new(count, Some("Symbol.hasInstance".into())).into(); |
|
|
|
let has_instance = Symbol::with_hash(count, Some("Symbol.hasInstance".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let is_concat_spreadable = |
|
|
|
let is_concat_spreadable = |
|
|
|
Symbol::new(count, Some("Symbol.isConcatSpreadable".into())).into(); |
|
|
|
Symbol::with_hash(count, Some("Symbol.isConcatSpreadable".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let iterator = Symbol::new(count, Some("Symbol.iterator".into())).into(); |
|
|
|
let iterator = Symbol::with_hash(count, Some("Symbol.iterator".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let match_ = Symbol::new(count, Some("Symbol.match".into())).into(); |
|
|
|
let match_ = Symbol::with_hash(count, Some("Symbol.match".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let match_all = Symbol::new(count, Some("Symbol.matchAll".into())).into(); |
|
|
|
let match_all = Symbol::with_hash(count, Some("Symbol.matchAll".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let replace = Symbol::new(count, Some("Symbol.replace".into())).into(); |
|
|
|
let replace = Symbol::with_hash(count, Some("Symbol.replace".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let search = Symbol::new(count, Some("Symbol.search".into())).into(); |
|
|
|
let search = Symbol::with_hash(count, Some("Symbol.search".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let species = Symbol::new(count, Some("Symbol.species".into())).into(); |
|
|
|
let species = Symbol::with_hash(count, Some("Symbol.species".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let split = Symbol::new(count, Some("Symbol.split".into())).into(); |
|
|
|
let split = Symbol::with_hash(count, Some("Symbol.split".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let to_primitive = Symbol::new(count, Some("Symbol.toPrimitive".into())).into(); |
|
|
|
let to_primitive = Symbol::with_hash(count, Some("Symbol.toPrimitive".into())).into(); |
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
let to_string_tag = Symbol::new(count, Some("Symbol.toStringTag".into())).into(); |
|
|
|
let to_string_tag = Symbol::with_hash(count, Some("Symbol.toStringTag".into())).into(); |
|
|
|
count += 1; |
|
|
|
|
|
|
|
let unscopables = Symbol::new(count, Some("Symbol.unscopables".into())).into(); |
|
|
|
|
|
|
|
count += 1; |
|
|
|
count += 1; |
|
|
|
|
|
|
|
let unscopables = Symbol::with_hash(count, Some("Symbol.unscopables".into())).into(); |
|
|
|
|
|
|
|
|
|
|
|
( |
|
|
|
|
|
|
|
Self { |
|
|
|
Self { |
|
|
|
async_iterator, |
|
|
|
async_iterator, |
|
|
|
has_instance, |
|
|
|
has_instance, |
|
|
@ -89,9 +117,7 @@ impl WellKnownSymbols { |
|
|
|
to_primitive, |
|
|
|
to_primitive, |
|
|
|
to_string_tag, |
|
|
|
to_string_tag, |
|
|
|
unscopables, |
|
|
|
unscopables, |
|
|
|
}, |
|
|
|
} |
|
|
|
count, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.asyncIterator` well known symbol.
|
|
|
|
/// The `Symbol.asyncIterator` well known symbol.
|
|
|
@ -99,8 +125,8 @@ impl WellKnownSymbols { |
|
|
|
/// A method that returns the default AsyncIterator for an object.
|
|
|
|
/// A method that returns the default AsyncIterator for an object.
|
|
|
|
/// Called by the semantics of the `for-await-of` statement.
|
|
|
|
/// Called by the semantics of the `for-await-of` statement.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn async_iterator_symbol(&self) -> RcSymbol { |
|
|
|
pub fn async_iterator() -> RcSymbol { |
|
|
|
self.async_iterator.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.async_iterator.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.hasInstance` well known symbol.
|
|
|
|
/// The `Symbol.hasInstance` well known symbol.
|
|
|
@ -109,8 +135,8 @@ impl WellKnownSymbols { |
|
|
|
/// recognizes an object as one of the `constructor`'s instances.
|
|
|
|
/// recognizes an object as one of the `constructor`'s instances.
|
|
|
|
/// Called by the semantics of the instanceof operator.
|
|
|
|
/// Called by the semantics of the instanceof operator.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn has_instance_symbol(&self) -> RcSymbol { |
|
|
|
pub fn has_instance() -> RcSymbol { |
|
|
|
self.has_instance.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.has_instance.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.isConcatSpreadable` well known symbol.
|
|
|
|
/// The `Symbol.isConcatSpreadable` well known symbol.
|
|
|
@ -119,8 +145,8 @@ impl WellKnownSymbols { |
|
|
|
/// an object should be flattened to its array elements
|
|
|
|
/// an object should be flattened to its array elements
|
|
|
|
/// by `Array.prototype.concat`.
|
|
|
|
/// by `Array.prototype.concat`.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn is_concat_spreadable_symbol(&self) -> RcSymbol { |
|
|
|
pub fn is_concat_spreadable() -> RcSymbol { |
|
|
|
self.is_concat_spreadable.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.is_concat_spreadable.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.iterator` well known symbol.
|
|
|
|
/// The `Symbol.iterator` well known symbol.
|
|
|
@ -128,8 +154,8 @@ impl WellKnownSymbols { |
|
|
|
/// A method that returns the default Iterator for an object.
|
|
|
|
/// A method that returns the default Iterator for an object.
|
|
|
|
/// Called by the semantics of the `for-of` statement.
|
|
|
|
/// Called by the semantics of the `for-of` statement.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn iterator_symbol(&self) -> RcSymbol { |
|
|
|
pub fn iterator() -> RcSymbol { |
|
|
|
self.iterator.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.iterator.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.match` well known symbol.
|
|
|
|
/// The `Symbol.match` well known symbol.
|
|
|
@ -137,8 +163,8 @@ impl WellKnownSymbols { |
|
|
|
/// A regular expression method that matches the regular expression
|
|
|
|
/// A regular expression method that matches the regular expression
|
|
|
|
/// against a string. Called by the `String.prototype.match` method.
|
|
|
|
/// against a string. Called by the `String.prototype.match` method.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn match_symbol(&self) -> RcSymbol { |
|
|
|
pub fn match_() -> RcSymbol { |
|
|
|
self.match_.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.match_.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.matchAll` well known symbol.
|
|
|
|
/// The `Symbol.matchAll` well known symbol.
|
|
|
@ -147,8 +173,8 @@ impl WellKnownSymbols { |
|
|
|
/// matches of the regular expression against a string.
|
|
|
|
/// matches of the regular expression against a string.
|
|
|
|
/// Called by the `String.prototype.matchAll` method.
|
|
|
|
/// Called by the `String.prototype.matchAll` method.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn match_all_symbol(&self) -> RcSymbol { |
|
|
|
pub fn match_all() -> RcSymbol { |
|
|
|
self.match_all.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.match_all.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.replace` well known symbol.
|
|
|
|
/// The `Symbol.replace` well known symbol.
|
|
|
@ -156,8 +182,8 @@ impl WellKnownSymbols { |
|
|
|
/// A regular expression method that replaces matched substrings
|
|
|
|
/// A regular expression method that replaces matched substrings
|
|
|
|
/// of a string. Called by the `String.prototype.replace` method.
|
|
|
|
/// of a string. Called by the `String.prototype.replace` method.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn replace_symbol(&self) -> RcSymbol { |
|
|
|
pub fn replace() -> RcSymbol { |
|
|
|
self.replace.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.replace.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.search` well known symbol.
|
|
|
|
/// The `Symbol.search` well known symbol.
|
|
|
@ -166,8 +192,8 @@ impl WellKnownSymbols { |
|
|
|
/// string that matches the regular expression.
|
|
|
|
/// string that matches the regular expression.
|
|
|
|
/// Called by the `String.prototype.search` method.
|
|
|
|
/// Called by the `String.prototype.search` method.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn search_symbol(&self) -> RcSymbol { |
|
|
|
pub fn search() -> RcSymbol { |
|
|
|
self.search.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.search.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.species` well known symbol.
|
|
|
|
/// The `Symbol.species` well known symbol.
|
|
|
@ -175,8 +201,8 @@ impl WellKnownSymbols { |
|
|
|
/// A function valued property that is the `constructor` function
|
|
|
|
/// A function valued property that is the `constructor` function
|
|
|
|
/// that is used to create derived objects.
|
|
|
|
/// that is used to create derived objects.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn species_symbol(&self) -> RcSymbol { |
|
|
|
pub fn species() -> RcSymbol { |
|
|
|
self.species.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.species.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.split` well known symbol.
|
|
|
|
/// The `Symbol.split` well known symbol.
|
|
|
@ -185,8 +211,8 @@ impl WellKnownSymbols { |
|
|
|
/// that match the regular expression.
|
|
|
|
/// that match the regular expression.
|
|
|
|
/// Called by the `String.prototype.split` method.
|
|
|
|
/// Called by the `String.prototype.split` method.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn split_symbol(&self) -> RcSymbol { |
|
|
|
pub fn split() -> RcSymbol { |
|
|
|
self.split.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.split.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.toPrimitive` well known symbol.
|
|
|
|
/// The `Symbol.toPrimitive` well known symbol.
|
|
|
@ -194,8 +220,8 @@ impl WellKnownSymbols { |
|
|
|
/// A method that converts an object to a corresponding primitive value.
|
|
|
|
/// A method that converts an object to a corresponding primitive value.
|
|
|
|
/// Called by the `ToPrimitive` (`Value::to_primitve`) abstract operation.
|
|
|
|
/// Called by the `ToPrimitive` (`Value::to_primitve`) abstract operation.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn to_primitive_symbol(&self) -> RcSymbol { |
|
|
|
pub fn to_primitive() -> RcSymbol { |
|
|
|
self.to_primitive.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.to_primitive.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.toStringTag` well known symbol.
|
|
|
|
/// The `Symbol.toStringTag` well known symbol.
|
|
|
@ -204,8 +230,8 @@ impl WellKnownSymbols { |
|
|
|
/// string description of an object.
|
|
|
|
/// string description of an object.
|
|
|
|
/// Accessed by the built-in method `Object.prototype.toString`.
|
|
|
|
/// Accessed by the built-in method `Object.prototype.toString`.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn to_string_tag_symbol(&self) -> RcSymbol { |
|
|
|
pub fn to_string_tag() -> RcSymbol { |
|
|
|
self.to_string_tag.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.to_string_tag.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The `Symbol.unscopables` well known symbol.
|
|
|
|
/// The `Symbol.unscopables` well known symbol.
|
|
|
@ -213,19 +239,31 @@ impl WellKnownSymbols { |
|
|
|
/// An object valued property whose own and inherited property names are property
|
|
|
|
/// An object valued property whose own and inherited property names are property
|
|
|
|
/// names that are excluded from the `with` environment bindings of the associated object.
|
|
|
|
/// names that are excluded from the `with` environment bindings of the associated object.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn unscopables_symbol(&self) -> RcSymbol { |
|
|
|
pub fn unscopables() -> RcSymbol { |
|
|
|
self.unscopables.clone() |
|
|
|
WELL_KNOW_SYMBOLS.with(|symbols| symbols.unscopables.clone()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Finalize, Trace, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
|
|
|
#[derive(Debug, Finalize, Trace, Clone, Eq, PartialOrd, Ord)] |
|
|
|
pub struct Symbol { |
|
|
|
pub struct Symbol { |
|
|
|
pub(crate) hash: u64, |
|
|
|
pub(crate) hash: u64, |
|
|
|
pub(crate) description: Option<RcString>, |
|
|
|
pub(crate) description: Option<RcString>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Symbol { |
|
|
|
impl Symbol { |
|
|
|
pub(crate) fn new(hash: u64, description: Option<RcString>) -> Self { |
|
|
|
/// Create a new symbol with a specified hash and description.
|
|
|
|
|
|
|
|
fn with_hash(hash: u64, description: Option<RcString>) -> Self { |
|
|
|
|
|
|
|
Self { hash, description } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new symbol.
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
pub fn new(description: Option<RcString>) -> Self { |
|
|
|
|
|
|
|
let hash = SYMBOL_HASH_COUNT.with(|count| { |
|
|
|
|
|
|
|
let hash = count.get(); |
|
|
|
|
|
|
|
count.set(hash + 1); |
|
|
|
|
|
|
|
hash |
|
|
|
|
|
|
|
}); |
|
|
|
Self { hash, description } |
|
|
|
Self { hash, description } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -236,8 +274,24 @@ impl Symbol { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns the `Symbol`s hash.
|
|
|
|
/// Returns the `Symbol`s hash.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// The hash is guaranteed to be unique.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn hash(&self) -> u64 { |
|
|
|
pub fn hash(&self) -> u64 { |
|
|
|
self.hash |
|
|
|
self.hash |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl PartialEq for Symbol { |
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
|
|
|
|
|
self.hash == other.hash |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Hash for Symbol { |
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) { |
|
|
|
|
|
|
|
self.hash.hash(state); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|