SymbolConstructor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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()
  33. : NativeFunction("Symbol", *interpreter().global_object().function_prototype())
  34. {
  35. define_property("prototype", interpreter().global_object().symbol_prototype(), 0);
  36. define_property("length", Value(0), Attribute::Configurable);
  37. define_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable);
  38. define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable);
  39. SymbolObject::initialize_well_known_symbols(interpreter());
  40. define_property("iterator", SymbolObject::well_known_iterator(), 0);
  41. define_property("asyncIterator", SymbolObject::well_known_async_terator(), 0);
  42. define_property("match", SymbolObject::well_known_match(), 0);
  43. define_property("matchAll", SymbolObject::well_known_match_all(), 0);
  44. define_property("replace", SymbolObject::well_known_replace(), 0);
  45. define_property("search", SymbolObject::well_known_search(), 0);
  46. define_property("split", SymbolObject::well_known_split(), 0);
  47. define_property("hasInstance", SymbolObject::well_known_has_instance(), 0);
  48. define_property("isConcatSpreadable", SymbolObject::well_known_is_concat_spreadable(), 0);
  49. define_property("unscopables", SymbolObject::well_known_unscopables(), 0);
  50. define_property("species", SymbolObject::well_known_species(), 0);
  51. define_property("toPrimitive", SymbolObject::well_known_to_primtive(), 0);
  52. define_property("toStringTag", SymbolObject::well_known_to_string_tag(), 0);
  53. }
  54. SymbolConstructor::~SymbolConstructor()
  55. {
  56. }
  57. Value SymbolConstructor::call(Interpreter& interpreter)
  58. {
  59. if (!interpreter.argument_count())
  60. return js_symbol(interpreter, "", false);
  61. return js_symbol(interpreter, interpreter.argument(0).to_string(interpreter), false);
  62. }
  63. Value SymbolConstructor::construct(Interpreter& interpreter)
  64. {
  65. interpreter.throw_exception<TypeError>(ErrorType::NotACtor, "Symbol");
  66. return {};
  67. }
  68. Value SymbolConstructor::for_(Interpreter& interpreter)
  69. {
  70. String description;
  71. if (!interpreter.argument_count()) {
  72. description = "undefined";
  73. } else {
  74. description = interpreter.argument(0).to_string(interpreter);
  75. }
  76. return SymbolObject::get_global(interpreter, description);
  77. }
  78. Value SymbolConstructor::key_for(Interpreter& interpreter)
  79. {
  80. auto argument = interpreter.argument(0);
  81. if (!argument.is_symbol()) {
  82. interpreter.throw_exception<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects().characters());
  83. return {};
  84. }
  85. auto& symbol = argument.as_symbol();
  86. if (symbol.is_global())
  87. return js_string(interpreter, symbol.description());
  88. return js_undefined();
  89. }
  90. }