StringPrototype.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Error.h>
  31. #include <LibJS/Runtime/PrimitiveString.h>
  32. #include <LibJS/Runtime/StringObject.h>
  33. #include <LibJS/Runtime/StringPrototype.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. StringPrototype::StringPrototype()
  37. {
  38. put_native_property("length", length_getter, nullptr);
  39. put_native_function("charAt", char_at, 1);
  40. put_native_function("repeat", repeat, 1);
  41. put_native_function("startsWith", starts_with, 1);
  42. }
  43. StringPrototype::~StringPrototype()
  44. {
  45. }
  46. Value StringPrototype::char_at(Interpreter& interpreter)
  47. {
  48. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  49. if (!this_object)
  50. return {};
  51. i32 index = 0;
  52. if (interpreter.argument_count())
  53. index = interpreter.argument(0).to_i32();
  54. ASSERT(this_object->is_string_object());
  55. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  56. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  57. return js_string(interpreter, String::empty());
  58. return js_string(interpreter, underlying_string.substring(index, 1));
  59. }
  60. Value StringPrototype::repeat(Interpreter& interpreter)
  61. {
  62. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  63. if (!this_object)
  64. return {};
  65. ASSERT(this_object->is_string_object());
  66. if (!interpreter.argument_count())
  67. return js_string(interpreter, String::empty());
  68. i32 count = 0;
  69. count = interpreter.argument(0).to_i32();
  70. if (count < 0) {
  71. // FIXME: throw RangeError
  72. return {};
  73. }
  74. auto* string_object = static_cast<StringObject*>(this_object);
  75. StringBuilder builder;
  76. for (i32 i = 0; i < count; ++i)
  77. builder.append(string_object->primitive_string()->string());
  78. return js_string(interpreter, builder.to_string());
  79. }
  80. Value StringPrototype::starts_with(Interpreter& interpreter)
  81. {
  82. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  83. if (!this_object)
  84. return {};
  85. if (!interpreter.argument_count())
  86. return Value(false);
  87. auto search_string = interpreter.argument(0).to_string();
  88. auto search_string_length = static_cast<i32>(search_string.length());
  89. i32 position = 0;
  90. if (interpreter.argument_count() > 1) {
  91. auto number = interpreter.argument(1).to_number();
  92. if (!number.is_nan())
  93. position = number.to_i32();
  94. }
  95. ASSERT(this_object->is_string_object());
  96. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  97. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  98. auto start = min(max(position, 0), underlying_string_length);
  99. if (start + search_string_length > underlying_string_length)
  100. return Value(false);
  101. if (search_string_length == 0)
  102. return Value(true);
  103. return Value(underlying_string.substring(start, search_string_length) == search_string);
  104. }
  105. Value StringPrototype::length_getter(Interpreter& interpreter)
  106. {
  107. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  108. if (!this_object)
  109. return {};
  110. if (!this_object->is_string_object())
  111. return interpreter.throw_exception<Error>("TypeError", "Not a String object");
  112. return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
  113. }
  114. }