SymbolConstructor.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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(GlobalObject& global_object)
  11. : NativeFunction(vm().names.Symbol, *global_object.function_prototype())
  12. {
  13. }
  14. void SymbolConstructor::initialize(GlobalObject& global_object)
  15. {
  16. auto& vm = this->vm();
  17. NativeFunction::initialize(global_object);
  18. // 20.4.2.9 Symbol.prototype, https://tc39.es/ecma262/#sec-symbol.prototype
  19. define_property(vm.names.prototype, global_object.symbol_prototype(), 0);
  20. define_property(vm.names.length, Value(0), Attribute::Configurable);
  21. u8 attr = Attribute::Writable | Attribute::Configurable;
  22. define_native_function(vm.names.for_, for_, 1, attr);
  23. define_native_function(vm.names.keyFor, key_for, 1, attr);
  24. #define __JS_ENUMERATE(SymbolName, snake_name) \
  25. define_property(vm.names.SymbolName, vm.well_known_symbol_##snake_name(), 0);
  26. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  27. #undef __JS_ENUMERATE
  28. }
  29. SymbolConstructor::~SymbolConstructor()
  30. {
  31. }
  32. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  33. Value SymbolConstructor::call()
  34. {
  35. if (!vm().argument_count())
  36. return js_symbol(heap(), "", false);
  37. return js_symbol(heap(), vm().argument(0).to_string(global_object()), false);
  38. }
  39. // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
  40. Value SymbolConstructor::construct(Function&)
  41. {
  42. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "Symbol");
  43. return {};
  44. }
  45. // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
  46. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  47. {
  48. String description;
  49. if (!vm.argument_count()) {
  50. description = "undefined";
  51. } else {
  52. description = vm.argument(0).to_string(global_object);
  53. }
  54. return global_object.vm().get_global_symbol(description);
  55. }
  56. // 20.4.2.6 Symbol.keyFor ( sym ), https://tc39.es/ecma262/#sec-symbol.keyfor
  57. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  58. {
  59. auto argument = vm.argument(0);
  60. if (!argument.is_symbol()) {
  61. vm.throw_exception<TypeError>(global_object, ErrorType::NotASymbol, argument.to_string_without_side_effects());
  62. return {};
  63. }
  64. auto& symbol = argument.as_symbol();
  65. if (symbol.is_global())
  66. return js_string(vm, symbol.description());
  67. return js_undefined();
  68. }
  69. }