summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2023-04-12 23:12:54 +0200
committerLinus Groh <mail@linusgroh.de>2023-04-13 13:04:44 +0200
commit0ae511edae2696dcabdbdcc3dbbf0bc6adc1005a (patch)
tree32026a6b954d7d1f4393b4b7867826ecd83dd4e4 /Userland
parent066133d97b30b41408c85a4be20123b76fd033b3 (diff)
downloadserenity-0ae511edae2696dcabdbdcc3dbbf0bc6adc1005a.zip
LibJS: Add spec comments to SymbolConstructor
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Symbol.cpp16
-rw-r--r--Userland/Libraries/LibJS/Runtime/Symbol.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp28
3 files changed, 35 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.cpp b/Userland/Libraries/LibJS/Runtime/Symbol.cpp
index 65750d1e6c..0fe3e705ef 100644
--- a/Userland/Libraries/LibJS/Runtime/Symbol.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Symbol.cpp
@@ -34,4 +34,20 @@ ErrorOr<String> Symbol::descriptive_string() const
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 {};
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.h b/Userland/Libraries/LibJS/Runtime/Symbol.h
index 6da84100d7..c75bd9f97f 100644
--- a/Userland/Libraries/LibJS/Runtime/Symbol.h
+++ b/Userland/Libraries/LibJS/Runtime/Symbol.h
@@ -24,6 +24,7 @@ public:
bool is_global() const { return m_is_global; }
ErrorOr<String> descriptive_string() const;
+ Optional<String> key() const;
private:
Symbol(Optional<String>, bool);
diff --git a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
index 5e033bae9f..dec3bf65fe 100644
--- a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
@@ -42,14 +42,22 @@ ThrowCompletionOr<void> SymbolConstructor::initialize(Realm& realm)
ThrowCompletionOr<Value> SymbolConstructor::call()
{
auto& vm = this->vm();
- if (vm.argument(0).is_undefined())
- return Symbol::create(vm, {}, false);
- return Symbol::create(vm, TRY(vm.argument(0).to_string(vm)), false);
+ auto description = vm.argument(0);
+
+ // 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
ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
{
+ // 1. If NewTarget is not undefined, throw a TypeError exception.
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)
{
auto argument = vm.argument(0);
+
+ // 1. If sym is not a Symbol, throw a TypeError exception.
if (!argument.is_symbol())
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, TRY_OR_THROW_OOM(vm, argument.to_string_without_side_effects()));
- auto& symbol = argument.as_symbol();
- if (symbol.is_global()) {
- // NOTE: Global symbols should always have a description string
- return PrimitiveString::create(vm, *symbol.description());
- }
-
- return js_undefined();
+ // 2. Return KeyForSymbol(sym).
+ auto key = argument.as_symbol().key();
+ return key.has_value()
+ ? PrimitiveString::create(vm, key.release_value())
+ : js_undefined();
}
}