SymbolConstructor.cpp 3.1 KB

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