SymbolConstructor.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022, 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. void SymbolConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. 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. }
  30. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  31. ThrowCompletionOr<Value> SymbolConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. if (vm.argument(0).is_undefined())
  35. return Symbol::create(vm, {}, false);
  36. return Symbol::create(vm, TRY(vm.argument(0).to_string(vm)), false);
  37. }
  38. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  39. ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
  40. {
  41. return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
  42. }
  43. // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
  44. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  45. {
  46. // 1. Let stringKey be ? ToString(key).
  47. auto string_key = TRY(vm.argument(0).to_string(vm));
  48. // 2. For each element e of the GlobalSymbolRegistry List, do
  49. auto result = vm.global_symbol_registry().get(string_key);
  50. if (result.has_value()) {
  51. // a. If SameValue(e.[[Key]], stringKey) is true, return e.[[Symbol]].
  52. return result.value();
  53. }
  54. // 3. Assert: GlobalSymbolRegistry does not currently contain an entry for stringKey.
  55. VERIFY(!result.has_value());
  56. // 4. Let newSymbol be a new unique Symbol value whose [[Description]] value is stringKey.
  57. auto new_symbol = Symbol::create(vm, string_key, true);
  58. // 5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the GlobalSymbolRegistry List.
  59. vm.global_symbol_registry().set(string_key, new_symbol);
  60. // 6. Return newSymbol.
  61. return new_symbol;
  62. }
  63. // 20.4.2.6 Symbol.keyFor ( sym ), https://tc39.es/ecma262/#sec-symbol.keyfor
  64. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  65. {
  66. auto argument = vm.argument(0);
  67. if (!argument.is_symbol())
  68. return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
  69. auto& symbol = argument.as_symbol();
  70. if (symbol.is_global())
  71. return PrimitiveString::create(vm, symbol.description());
  72. return js_undefined();
  73. }
  74. }