StringPrototype.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Function.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/StringObject.h>
  32. #include <LibJS/Runtime/StringPrototype.h>
  33. #include <LibJS/Runtime/Value.h>
  34. namespace JS {
  35. StringPrototype::StringPrototype()
  36. {
  37. put_native_property(
  38. "length", [](Object* this_object) {
  39. ASSERT(this_object);
  40. ASSERT(this_object->is_string_object());
  41. return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
  42. },
  43. nullptr);
  44. put_native_function("charAt", char_at);
  45. put_native_function("repeat", repeat);
  46. }
  47. StringPrototype::~StringPrototype()
  48. {
  49. }
  50. Value StringPrototype::char_at(Interpreter& interpreter)
  51. {
  52. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  53. if (!this_object)
  54. return {};
  55. i32 index = 0;
  56. if (!interpreter.call_frame().arguments.is_empty())
  57. index = interpreter.call_frame().arguments[0].to_i32();
  58. ASSERT(this_object->is_string_object());
  59. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  60. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  61. return js_string(interpreter.heap(), String::empty());
  62. return js_string(interpreter.heap(), underlying_string.substring(index, 1));
  63. }
  64. Value StringPrototype::repeat(Interpreter& interpreter)
  65. {
  66. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  67. if (!this_object)
  68. return {};
  69. ASSERT(this_object->is_string_object());
  70. if (interpreter.call_frame().arguments.is_empty())
  71. return js_string(interpreter.heap(), String::empty());
  72. i32 count = 0;
  73. count = interpreter.call_frame().arguments[0].to_i32();
  74. if (count < 0) {
  75. // FIXME: throw RangeError
  76. return js_undefined();
  77. }
  78. auto* string_object = static_cast<StringObject*>(this_object);
  79. StringBuilder builder;
  80. for (i32 i = 0; i < count; ++i)
  81. builder.append(string_object->primitive_string()->string());
  82. return js_string(interpreter.heap(), builder.to_string());
  83. }
  84. }