SymbolConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. void SymbolConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. Base::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. auto description = vm.argument(0);
  35. // 2. If description is undefined, let descString be undefined.
  36. // 3. Else, let descString be ? ToString(description).
  37. auto description_string = description.is_undefined()
  38. ? Optional<String> {}
  39. : TRY(description.to_string(vm));
  40. // 4. Return a new Symbol whose [[Description]] is descString.
  41. return Symbol::create(vm, move(description_string), false);
  42. }
  43. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  44. ThrowCompletionOr<NonnullGCPtr<Object>> SymbolConstructor::construct(FunctionObject&)
  45. {
  46. // 1. If NewTarget is not undefined, throw a TypeError exception.
  47. return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
  48. }
  49. // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
  50. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  51. {
  52. // 1. Let stringKey be ? ToString(key).
  53. auto string_key = TRY(vm.argument(0).to_string(vm));
  54. // 2. For each element e of the GlobalSymbolRegistry List, do
  55. auto result = vm.global_symbol_registry().get(string_key);
  56. if (result.has_value()) {
  57. // a. If SameValue(e.[[Key]], stringKey) is true, return e.[[Symbol]].
  58. return result.value();
  59. }
  60. // 3. Assert: GlobalSymbolRegistry does not currently contain an entry for stringKey.
  61. VERIFY(!result.has_value());
  62. // 4. Let newSymbol be a new unique Symbol value whose [[Description]] value is stringKey.
  63. auto new_symbol = Symbol::create(vm, string_key, true);
  64. // 5. Append the Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the GlobalSymbolRegistry List.
  65. vm.global_symbol_registry().set(string_key, new_symbol);
  66. // 6. Return newSymbol.
  67. return new_symbol;
  68. }
  69. // 20.4.2.6 Symbol.keyFor ( sym ), https://tc39.es/ecma262/#sec-symbol.keyfor
  70. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  71. {
  72. auto argument = vm.argument(0);
  73. // 1. If sym is not a Symbol, throw a TypeError exception.
  74. if (!argument.is_symbol())
  75. return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
  76. // 2. Return KeyForSymbol(sym).
  77. auto key = argument.as_symbol().key();
  78. return key.has_value()
  79. ? PrimitiveString::create(vm, key.release_value())
  80. : js_undefined();
  81. }
  82. }