SymbolConstructor.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibJS/Runtime/Error.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibJS/Runtime/SymbolConstructor.h>
  29. #include <LibJS/Runtime/SymbolObject.h>
  30. namespace JS {
  31. SymbolConstructor::SymbolConstructor(GlobalObject& global_object)
  32. : NativeFunction("Symbol", *global_object.function_prototype())
  33. {
  34. }
  35. void SymbolConstructor::initialize(GlobalObject& global_object)
  36. {
  37. NativeFunction::initialize(global_object);
  38. define_property("prototype", global_object.symbol_prototype(), 0);
  39. define_property("length", Value(0), Attribute::Configurable);
  40. define_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable);
  41. define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable);
  42. #define __JS_ENUMERATE(SymbolName, snake_name) \
  43. define_property(#SymbolName, global_object.vm().well_known_symbol_##snake_name(), 0);
  44. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  45. #undef __JS_ENUMERATE
  46. }
  47. SymbolConstructor::~SymbolConstructor()
  48. {
  49. }
  50. Value SymbolConstructor::call()
  51. {
  52. if (!vm().argument_count())
  53. return js_symbol(heap(), "", false);
  54. return js_symbol(heap(), vm().argument(0).to_string(global_object()), false);
  55. }
  56. Value SymbolConstructor::construct(Function&)
  57. {
  58. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "Symbol");
  59. return {};
  60. }
  61. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  62. {
  63. String description;
  64. if (!vm.argument_count()) {
  65. description = "undefined";
  66. } else {
  67. description = vm.argument(0).to_string(global_object);
  68. }
  69. return global_object.vm().get_global_symbol(description);
  70. }
  71. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  72. {
  73. auto argument = vm.argument(0);
  74. if (!argument.is_symbol()) {
  75. vm.throw_exception<TypeError>(global_object, ErrorType::NotASymbol, argument.to_string_without_side_effects().characters());
  76. return {};
  77. }
  78. auto& symbol = argument.as_symbol();
  79. if (symbol.is_global())
  80. return js_string(vm, symbol.description());
  81. return js_undefined();
  82. }
  83. }