StringPrototype.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <string.h>
  36. namespace JS {
  37. StringPrototype::StringPrototype()
  38. {
  39. put_native_property("length", length_getter, nullptr);
  40. put_native_function("charAt", char_at, 1);
  41. put_native_function("repeat", repeat, 1);
  42. put_native_function("startsWith", starts_with, 1);
  43. put_native_function("indexOf", index_of, 1);
  44. put_native_function("toLowerCase", to_lowercase, 0);
  45. put_native_function("toUpperCase", to_uppercase, 0);
  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.argument_count())
  57. index = interpreter.argument(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, String::empty());
  62. return js_string(interpreter, 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.argument_count())
  71. return js_string(interpreter, String::empty());
  72. i32 count = 0;
  73. count = interpreter.argument(0).to_i32();
  74. if (count < 0) {
  75. // FIXME: throw RangeError
  76. return {};
  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, builder.to_string());
  83. }
  84. Value StringPrototype::starts_with(Interpreter& interpreter)
  85. {
  86. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  87. if (!this_object)
  88. return {};
  89. if (!interpreter.argument_count())
  90. return Value(false);
  91. auto search_string = interpreter.argument(0).to_string();
  92. auto search_string_length = static_cast<i32>(search_string.length());
  93. i32 position = 0;
  94. if (interpreter.argument_count() > 1) {
  95. auto number = interpreter.argument(1).to_number();
  96. if (!number.is_nan())
  97. position = number.to_i32();
  98. }
  99. ASSERT(this_object->is_string_object());
  100. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  101. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  102. auto start = min(max(position, 0), underlying_string_length);
  103. if (start + search_string_length > underlying_string_length)
  104. return Value(false);
  105. if (search_string_length == 0)
  106. return Value(true);
  107. return Value(underlying_string.substring(start, search_string_length) == search_string);
  108. }
  109. Value StringPrototype::index_of(Interpreter& interpreter)
  110. {
  111. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  112. if (!this_object)
  113. return {};
  114. if (!this_object->is_string_object())
  115. return interpreter.throw_exception<Error>("TypeError", "Not a String object");
  116. Value needle_value = js_undefined();
  117. if (interpreter.argument_count() >= 1)
  118. needle_value = interpreter.argument(0);
  119. auto needle = needle_value.to_string();
  120. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  121. // FIXME: We should have a helper in AK::String for this.
  122. auto* ptr = strstr(haystack.characters(), needle.characters());
  123. if (!ptr)
  124. return Value(-1);
  125. return Value((i32)(ptr - haystack.characters()));
  126. }
  127. static StringObject* string_object_from(Interpreter& interpreter)
  128. {
  129. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  130. if (!this_object)
  131. return nullptr;
  132. if (!this_object->is_string_object()) {
  133. interpreter.throw_exception<Error>("TypeError", "Not a String object");
  134. return nullptr;
  135. }
  136. return static_cast<StringObject*>(this_object);
  137. }
  138. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  139. {
  140. auto* string_object = string_object_from(interpreter);
  141. if (!string_object)
  142. return {};
  143. return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
  144. }
  145. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  146. {
  147. auto* string_object = string_object_from(interpreter);
  148. if (!string_object)
  149. return {};
  150. return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
  151. }
  152. Value StringPrototype::length_getter(Interpreter& interpreter)
  153. {
  154. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  155. if (!this_object)
  156. return {};
  157. if (!this_object->is_string_object())
  158. return interpreter.throw_exception<Error>("TypeError", "Not a String object");
  159. return Value((i32) static_cast<const StringObject*>(this_object)->primitive_string()->string().length());
  160. }
  161. }