diff options
author | Linus Groh <mail@linusgroh.de> | 2023-04-12 23:13:24 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-13 13:04:44 +0200 |
commit | 81c6ad047ae7df555011b3dfdcf21d184956001d (patch) | |
tree | 498a4782e0662b3f76344f5a61690727abaab4b1 /Userland/Libraries | |
parent | 0ae511edae2696dcabdbdcc3dbbf0bc6adc1005a (diff) | |
download | serenity-81c6ad047ae7df555011b3dfdcf21d184956001d.zip |
LibJS: Add spec comments to SymbolPrototype
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp index 9fcb5f90fb..1bcdbbab2a 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -42,40 +42,58 @@ ThrowCompletionOr<void> SymbolPrototype::initialize(Realm& realm) // thisSymbolValue ( value ), https://tc39.es/ecma262/#thissymbolvalue static ThrowCompletionOr<Symbol*> this_symbol_value(VM& vm, Value value) { + // 1. If value is a Symbol, return value. if (value.is_symbol()) return &value.as_symbol(); - if (value.is_object() && is<SymbolObject>(value.as_object())) + + // 2. If value is an Object and value has a [[SymbolData]] internal slot, then + if (value.is_object() && is<SymbolObject>(value.as_object())) { + // a. Let s be value.[[SymbolData]]. + // b. Assert: s is a Symbol. + // c. Return s. return &static_cast<SymbolObject&>(value.as_object()).primitive_symbol(); + } + + // 3. Throw a TypeError exception. return vm.throw_completion<TypeError>(ErrorType::NotAnObjectOfType, "Symbol"); } // 20.4.3.2 get Symbol.prototype.description, https://tc39.es/ecma262/#sec-symbol.prototype.description JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::description_getter) { + // 1. Let s be the this value. + // 2. Let sym be ? thisSymbolValue(s). auto* symbol = TRY(this_symbol_value(vm, vm.this_value())); + + // 3. Return sym.[[Description]]. auto& description = symbol->description(); - if (!description.has_value()) - return js_undefined(); - return PrimitiveString::create(vm, *description); + return description.has_value() + ? PrimitiveString::create(vm, *description) + : js_undefined(); } // 20.4.3.3 Symbol.prototype.toString ( ), https://tc39.es/ecma262/#sec-symbol.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::to_string) { + // 1. Let sym be ? thisSymbolValue(this value). auto* symbol = TRY(this_symbol_value(vm, vm.this_value())); + + // 2. Return SymbolDescriptiveString(sym). return PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, symbol->descriptive_string())); } // 20.4.3.4 Symbol.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-symbol.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::value_of) { + // 1. Return ? thisSymbolValue(this value). return TRY(this_symbol_value(vm, vm.this_value())); } // 20.4.3.5 Symbol.prototype [ @@toPrimitive ] ( hint ), https://tc39.es/ecma262/#sec-symbol.prototype-@@toprimitive JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::symbol_to_primitive) { - // The hint argument is ignored. + // 1. Return ? thisSymbolValue(this value). + // NOTE: The argument is ignored. return TRY(this_symbol_value(vm, vm.this_value())); } |