Browse Source

Implement `Symbol.prototype.description` accessor (#1222)

pull/1234/head
Halid Odat 4 years ago committed by GitHub
parent
commit
8a70fdca30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      boa/src/builtins/symbol/mod.rs

43
boa/src/builtins/symbol/mod.rs

@ -21,7 +21,7 @@ mod tests;
use crate::{ use crate::{
builtins::BuiltIn, builtins::BuiltIn,
gc::{Finalize, Trace}, gc::{Finalize, Trace},
object::ConstructorBuilder, object::{ConstructorBuilder, FunctionBuilder},
property::Attribute, property::Attribute,
value::{RcString, RcSymbol, Value}, value::{RcString, RcSymbol, Value},
BoaProfiler, Context, Result, BoaProfiler, Context, Result,
@ -241,6 +241,8 @@ impl BuiltIn for Symbol {
} }
fn init(context: &mut Context) -> (&'static str, Value, Attribute) { 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 // https://tc39.es/ecma262/#sec-well-known-symbols
let well_known_symbols = context.well_known_symbols(); let well_known_symbols = context.well_known_symbols();
@ -258,9 +260,14 @@ impl BuiltIn for Symbol {
let symbol_to_string_tag = well_known_symbols.to_string_tag_symbol(); let symbol_to_string_tag = well_known_symbols.to_string_tag_symbol();
let symbol_unscopables = well_known_symbols.unscopables_symbol(); let symbol_unscopables = well_known_symbols.unscopables_symbol();
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT; let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;
let get_description = FunctionBuilder::new(context, Self::get_description)
.name("get description")
.constructable(false)
.callable(true)
.build();
let symbol_object = ConstructorBuilder::with_standard_object( let symbol_object = ConstructorBuilder::with_standard_object(
context, context,
Self::constructor, Self::constructor,
@ -282,6 +289,12 @@ impl BuiltIn for Symbol {
.static_property("toStringTag", symbol_to_string_tag, attribute) .static_property("toStringTag", symbol_to_string_tag, attribute)
.static_property("unscopables", symbol_unscopables, attribute) .static_property("unscopables", symbol_unscopables, attribute)
.method(Self::to_string, "toString", 0) .method(Self::to_string, "toString", 0)
.accessor(
"description",
Some(get_description),
None,
Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
)
.callable(true) .callable(true)
.constructable(false) .constructable(false)
.build(); .build();
@ -350,7 +363,7 @@ impl Symbol {
/// ///
/// This method returns a string representing the specified `Symbol` object. /// This method returns a string representing the specified `Symbol` object.
/// ///
/// /// More information: /// More information:
/// - [MDN documentation][mdn] /// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec] /// - [ECMAScript reference][spec]
/// ///
@ -362,4 +375,26 @@ impl Symbol {
let description = symbol.description().unwrap_or(""); let description = symbol.description().unwrap_or("");
Ok(Value::from(format!("Symbol({})", description))) Ok(Value::from(format!("Symbol({})", description)))
} }
/// `get Symbol.prototype.description`
///
/// This accessor returns the description of the `Symbol` object.
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-symbol.prototype.description
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/description
pub(crate) fn get_description(
this: &Value,
_: &[Value],
context: &mut Context,
) -> Result<Value> {
if let Some(ref description) = Self::this_symbol_value(this, context)?.description {
Ok(description.clone().into())
} else {
Ok(Value::undefined())
}
}
} }

Loading…
Cancel
Save