StringConstructor.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return js_string(interpreter, interpreter.argument(0).to_string());
  49. }
  50. Value StringConstructor::construct(Interpreter& interpreter)
  51. {
  52. PrimitiveString* primitive_string = nullptr;
  53. if (!interpreter.argument_count())
  54. primitive_string = js_string(interpreter, "");
  55. else
  56. primitive_string = js_string(interpreter, interpreter.argument(0).to_string());
  57. if (!primitive_string)
  58. return {};
  59. return StringObject::create(interpreter.global_object(), *primitive_string);
  60. }
  61. Value StringConstructor::raw(Interpreter& interpreter)
  62. {
  63. auto* template_object = interpreter.argument(0).to_object(interpreter.heap());
  64. if (interpreter.exception())
  65. return {};
  66. auto raw = template_object->get("raw");
  67. if (raw.is_empty() || raw.is_undefined() || raw.is_null()) {
  68. interpreter.throw_exception<TypeError>(String::format("Cannot convert property 'raw' to object from %s", raw.is_null() ? "null" : "undefined"));
  69. return {};
  70. }
  71. if (!raw.is_array())
  72. return js_string(interpreter, "");
  73. auto& raw_array_elements = static_cast<Array*>(raw.to_object(interpreter.heap()))->elements();
  74. StringBuilder builder;
  75. for (size_t i = 0; i < raw_array_elements.size(); ++i) {
  76. builder.append(raw_array_elements.at(i).to_string());
  77. if (i + 1 < interpreter.argument_count() && i < raw_array_elements.size() - 1)
  78. builder.append(interpreter.argument(i + 1).to_string());
  79. }
  80. return js_string(interpreter, builder.build());
  81. }
  82. }