mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:17:34 +00:00
LibJS: Add spec comments to SymbolConstructor
This commit is contained in:
parent
066133d97b
commit
0ae511edae
3 changed files with 35 additions and 10 deletions
|
@ -34,4 +34,20 @@ ErrorOr<String> Symbol::descriptive_string() const
|
||||||
return String::formatted("Symbol({})", description);
|
return String::formatted("Symbol({})", description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 20.4.5.1 KeyForSymbol ( sym ), https://tc39.es/ecma262/#sec-keyforsymbol
|
||||||
|
Optional<String> Symbol::key() const
|
||||||
|
{
|
||||||
|
// 1. For each element e of the GlobalSymbolRegistry List, do
|
||||||
|
// a. If SameValue(e.[[Symbol]], sym) is true, return e.[[Key]].
|
||||||
|
if (m_is_global) {
|
||||||
|
// NOTE: Global symbols should always have a description string
|
||||||
|
VERIFY(m_description.has_value());
|
||||||
|
return m_description;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Assert: GlobalSymbolRegistry does not currently contain an entry for sym.
|
||||||
|
// 3. Return undefined.
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ public:
|
||||||
bool is_global() const { return m_is_global; }
|
bool is_global() const { return m_is_global; }
|
||||||
|
|
||||||
ErrorOr<String> descriptive_string() const;
|
ErrorOr<String> descriptive_string() const;
|
||||||
|
Optional<String> key() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Symbol(Optional<String>, bool);
|
Symbol(Optional<String>, bool);
|
||||||
|
|
|
@ -42,14 +42,22 @@ ThrowCompletionOr<void> SymbolConstructor::initialize(Realm& realm)
|
||||||
ThrowCompletionOr<Value> SymbolConstructor::call()
|
ThrowCompletionOr<Value> SymbolConstructor::call()
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
if (vm.argument(0).is_undefined())
|
auto description = vm.argument(0);
|
||||||
return Symbol::create(vm, {}, false);
|
|
||||||
return Symbol::create(vm, TRY(vm.argument(0).to_string(vm)), false);
|
// 2. If description is undefined, let descString be undefined.
|
||||||
|
// 3. Else, let descString be ? ToString(description).
|
||||||
|
auto description_string = description.is_undefined()
|
||||||
|
? Optional<String> {}
|
||||||
|
: TRY(description.to_string(vm));
|
||||||
|
|
||||||
|
// 4. Return a new Symbol whose [[Description]] is descString.
|
||||||
|
return Symbol::create(vm, move(description_string), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
|
// 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
|
||||||
ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
|
ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
|
||||||
{
|
{
|
||||||
|
// 1. If NewTarget is not undefined, throw a TypeError exception.
|
||||||
return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
|
return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,16 +91,16 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
|
||||||
JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
|
JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
|
||||||
{
|
{
|
||||||
auto argument = vm.argument(0);
|
auto argument = vm.argument(0);
|
||||||
|
|
||||||
|
// 1. If sym is not a Symbol, throw a TypeError exception.
|
||||||
if (!argument.is_symbol())
|
if (!argument.is_symbol())
|
||||||
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, TRY_OR_THROW_OOM(vm, argument.to_string_without_side_effects()));
|
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, TRY_OR_THROW_OOM(vm, argument.to_string_without_side_effects()));
|
||||||
|
|
||||||
auto& symbol = argument.as_symbol();
|
// 2. Return KeyForSymbol(sym).
|
||||||
if (symbol.is_global()) {
|
auto key = argument.as_symbol().key();
|
||||||
// NOTE: Global symbols should always have a description string
|
return key.has_value()
|
||||||
return PrimitiveString::create(vm, *symbol.description());
|
? PrimitiveString::create(vm, key.release_value())
|
||||||
}
|
: js_undefined();
|
||||||
|
|
||||||
return js_undefined();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue