StringConstructor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 <AK/StringBuilder.h>
  27. #include <LibJS/Interpreter.h>
  28. #include <LibJS/Runtime/Array.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/StringConstructor.h>
  32. #include <LibJS/Runtime/StringObject.h>
  33. namespace JS {
  34. StringConstructor::StringConstructor()
  35. : NativeFunction("String", *interpreter().global_object().function_prototype())
  36. {
  37. put("prototype", interpreter().global_object().string_prototype(), 0);
  38. put("length", Value(1), Attribute::Configurable);
  39. put_native_function("raw", raw, 0, Attribute::Writable | Attribute::Configurable);
  40. }
  41. StringConstructor::~StringConstructor()
  42. {
  43. }
  44. Value StringConstructor::call(Interpreter& interpreter)
  45. {
  46. if (!interpreter.argument_count())
  47. return js_string(interpreter, "");
  48. auto* string = interpreter.argument(0).to_primitive_string(interpreter);
  49. if (interpreter.exception())
  50. return {};
  51. return string;
  52. }
  53. Value StringConstructor::construct(Interpreter& interpreter)
  54. {
  55. PrimitiveString* primitive_string = nullptr;
  56. if (!interpreter.argument_count())
  57. primitive_string = js_string(interpreter, "");
  58. else
  59. primitive_string = interpreter.argument(0).to_primitive_string(interpreter);
  60. if (!primitive_string)
  61. return {};
  62. return StringObject::create(interpreter.global_object(), *primitive_string);
  63. }
  64. Value StringConstructor::raw(Interpreter& interpreter)
  65. {
  66. auto* template_object = interpreter.argument(0).to_object(interpreter);
  67. if (interpreter.exception())
  68. return {};
  69. auto raw = template_object->get("raw");
  70. if (raw.is_empty() || raw.is_undefined() || raw.is_null()) {
  71. interpreter.throw_exception<TypeError>(String::format("Cannot convert property 'raw' to object from %s", raw.is_null() ? "null" : "undefined"));
  72. return {};
  73. }
  74. if (!raw.is_array())
  75. return js_string(interpreter, "");
  76. auto& raw_array_elements = static_cast<Array*>(raw.to_object(interpreter))->elements();
  77. StringBuilder builder;
  78. for (size_t i = 0; i < raw_array_elements.size(); ++i) {
  79. builder.append(raw_array_elements.at(i).to_string(interpreter));
  80. if (interpreter.exception())
  81. return {};
  82. if (i + 1 < interpreter.argument_count() && i < raw_array_elements.size() - 1) {
  83. builder.append(interpreter.argument(i + 1).to_string(interpreter));
  84. if (interpreter.exception())
  85. return {};
  86. }
  87. }
  88. return js_string(interpreter, builder.build());
  89. }
  90. }