SymbolConstructor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/Interpreter.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/SymbolConstructor.h>
  30. #include <LibJS/Runtime/SymbolObject.h>
  31. namespace JS {
  32. SymbolConstructor::SymbolConstructor(GlobalObject& global_object)
  33. : NativeFunction("Symbol", *global_object.function_prototype())
  34. {
  35. }
  36. void SymbolConstructor::initialize(GlobalObject& global_object)
  37. {
  38. NativeFunction::initialize(global_object);
  39. define_property("prototype", global_object.symbol_prototype(), 0);
  40. define_property("length", Value(0), Attribute::Configurable);
  41. define_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable);
  42. define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable);
  43. #define __JS_ENUMERATE(SymbolName, snake_name) \
  44. define_property(#SymbolName, global_object.vm().well_known_symbol_##snake_name(), 0);
  45. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  46. #undef __JS_ENUMERATE
  47. }
  48. SymbolConstructor::~SymbolConstructor()
  49. {
  50. }
  51. Value SymbolConstructor::call()
  52. {
  53. if (!vm().argument_count())
  54. return js_symbol(heap(), "", false);
  55. return js_symbol(heap(), vm().argument(0).to_string(interpreter()), false);
  56. }
  57. Value SymbolConstructor::construct(Interpreter& interpreter, Function&)
  58. {
  59. interpreter.vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "Symbol");
  60. return {};
  61. }
  62. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
  63. {
  64. String description;
  65. if (!interpreter.argument_count()) {
  66. description = "undefined";
  67. } else {
  68. description = interpreter.argument(0).to_string(interpreter);
  69. }
  70. return global_object.vm().get_global_symbol(description);
  71. }
  72. JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
  73. {
  74. auto argument = interpreter.argument(0);
  75. if (!argument.is_symbol()) {
  76. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotASymbol, argument.to_string_without_side_effects().characters());
  77. return {};
  78. }
  79. auto& symbol = argument.as_symbol();
  80. if (symbol.is_global())
  81. return js_string(interpreter, symbol.description());
  82. return js_undefined();
  83. }
  84. }