SymbolConstructor.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/SymbolConstructor.h>
  10. namespace JS {
  11. SymbolConstructor::SymbolConstructor(Realm& realm)
  12. : NativeFunction(realm.vm().names.Symbol.as_string(), *realm.intrinsics().function_prototype())
  13. {
  14. }
  15. ThrowCompletionOr<void> SymbolConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  19. // 20.4.2.9 Symbol.prototype, https://tc39.es/ecma262/#sec-symbol.prototype
  20. define_direct_property(vm.names.prototype, realm.intrinsics().symbol_prototype(), 0);
  21. u8 attr = Attribute::Writable | Attribute::Configurable;
  22. define_native_function(realm, vm.names.for_, for_, 1, attr);
  23. define_native_function(realm, vm.names.keyFor, key_for, 1, attr);
  24. #define __JS_ENUMERATE(SymbolName, snake_name) \
  25. define_direct_property(vm.names.SymbolName, vm.well_known_symbol_##snake_name(), 0);
  26. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  27. #undef __JS_ENUMERATE
  28. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  29. return {};
  30. }
  31. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  32. ThrowCompletionOr<Value> SymbolConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. if (vm.argument(0).is_undefined())
  36. return Symbol::create(vm, {}, false);
  37. return Symbol::create(vm, TRY(vm.argument(0).to_string(vm)), false);
  38. }
  39. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  40. ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
  41. {
  42. return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
  43. }
  44. // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
  45. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  46. {
  47. // 1. Let stringKey be ? ToString(key).
  48. auto string_key = TRY(vm.argument(0).to_string(vm));
  49. // 2. For each element e of the GlobalSymbolRegistry List, do
  50. auto result = vm.global_symbol_registry().get(string_key);
  51. if (result.has_value()) {
  52. // a. If SameValue(e.[[Key]], stringKey) is true, return e.[[Symbol]].
  53. return result.value();
  54. }
  55. // 3. Assert: GlobalSymbolRegistry does not currently contain an entry for stringKey.
  56. VERIFY(!result.has_value());
  57. // 4. Let newSymbol be a new unique Symbol value whose [[Description]] value is stringKey.
  58. auto new_symbol = Symbol::create(vm, string_key, true);
  59. // 5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the GlobalSymbolRegistry List.
  60. vm.global_symbol_registry().set(string_key, new_symbol);
  61. // 6. Return newSymbol.
  62. return new_symbol;
  63. }
  64. // 20.4.2.6 Symbol.keyFor ( sym ), https://tc39.es/ecma262/#sec-symbol.keyfor
  65. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  66. {
  67. auto argument = vm.argument(0);
  68. if (!argument.is_symbol())
  69. return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
  70. auto& symbol = argument.as_symbol();
  71. if (symbol.is_global()) {
  72. // NOTE: Global symbols should always have a description string
  73. return PrimitiveString::create(vm, *symbol.description());
  74. }
  75. return js_undefined();
  76. }
  77. }