SymbolConstructor.cpp 3.7 KB

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