StringPrototype.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. : StringObject(js_string(interpreter(), String::empty()))
  39. {
  40. set_prototype(interpreter().object_prototype());
  41. put_native_property("length", length_getter, nullptr);
  42. put_native_function("charAt", char_at, 1);
  43. put_native_function("repeat", repeat, 1);
  44. put_native_function("startsWith", starts_with, 1);
  45. put_native_function("indexOf", index_of, 1);
  46. put_native_function("toLowerCase", to_lowercase, 0);
  47. put_native_function("toUpperCase", to_uppercase, 0);
  48. put_native_function("toString", to_string, 0);
  49. }
  50. StringPrototype::~StringPrototype()
  51. {
  52. }
  53. Value StringPrototype::char_at(Interpreter& interpreter)
  54. {
  55. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  56. if (!this_object)
  57. return {};
  58. i32 index = 0;
  59. if (interpreter.argument_count())
  60. index = interpreter.argument(0).to_i32();
  61. ASSERT(this_object->is_string_object());
  62. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  63. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  64. return js_string(interpreter, String::empty());
  65. return js_string(interpreter, underlying_string.substring(index, 1));
  66. }
  67. Value StringPrototype::repeat(Interpreter& interpreter)
  68. {
  69. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  70. if (!this_object)
  71. return {};
  72. ASSERT(this_object->is_string_object());
  73. if (!interpreter.argument_count())
  74. return js_string(interpreter, String::empty());
  75. i32 count = 0;
  76. count = interpreter.argument(0).to_i32();
  77. if (count < 0) {
  78. // FIXME: throw RangeError
  79. return {};
  80. }
  81. auto* string_object = static_cast<StringObject*>(this_object);
  82. StringBuilder builder;
  83. for (i32 i = 0; i < count; ++i)
  84. builder.append(string_object->primitive_string()->string());
  85. return js_string(interpreter, builder.to_string());
  86. }
  87. Value StringPrototype::starts_with(Interpreter& interpreter)
  88. {
  89. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  90. if (!this_object)
  91. return {};
  92. if (!interpreter.argument_count())
  93. return Value(false);
  94. auto search_string = interpreter.argument(0).to_string();
  95. auto search_string_length = static_cast<i32>(search_string.length());
  96. i32 position = 0;
  97. if (interpreter.argument_count() > 1) {
  98. auto number = interpreter.argument(1).to_number();
  99. if (!number.is_nan())
  100. position = number.to_i32();
  101. }
  102. ASSERT(this_object->is_string_object());
  103. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  104. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  105. auto start = min(max(position, 0), underlying_string_length);
  106. if (start + search_string_length > underlying_string_length)
  107. return Value(false);
  108. if (search_string_length == 0)
  109. return Value(true);
  110. return Value(underlying_string.substring(start, search_string_length) == search_string);
  111. }
  112. Value StringPrototype::index_of(Interpreter& interpreter)
  113. {
  114. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  115. if (!this_object)
  116. return {};
  117. if (!this_object->is_string_object())
  118. return interpreter.throw_exception<TypeError>("Not a String object");
  119. Value needle_value = js_undefined();
  120. if (interpreter.argument_count() >= 1)
  121. needle_value = interpreter.argument(0);
  122. auto needle = needle_value.to_string();
  123. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  124. // FIXME: We should have a helper in AK::String for this.
  125. auto* ptr = strstr(haystack.characters(), needle.characters());
  126. if (!ptr)
  127. return Value(-1);
  128. return Value((i32)(ptr - haystack.characters()));
  129. }
  130. static StringObject* string_object_from(Interpreter& interpreter)
  131. {
  132. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  133. if (!this_object)
  134. return nullptr;
  135. if (!this_object->is_string_object()) {
  136. interpreter.throw_exception<TypeError>("Not a String object");
  137. return nullptr;
  138. }
  139. return static_cast<StringObject*>(this_object);
  140. }
  141. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  142. {
  143. auto* string_object = string_object_from(interpreter);
  144. if (!string_object)
  145. return {};
  146. return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
  147. }
  148. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  149. {
  150. auto* string_object = string_object_from(interpreter);
  151. if (!string_object)
  152. return {};
  153. return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
  154. }
  155. Value StringPrototype::length_getter(Interpreter& interpreter)
  156. {
  157. auto* string_object = string_object_from(interpreter);
  158. if (!string_object)
  159. return {};
  160. return Value((i32) string_object->primitive_string()->string().length());
  161. }
  162. Value StringPrototype::to_string(Interpreter& interpreter)
  163. {
  164. auto* string_object = string_object_from(interpreter);
  165. if (!string_object)
  166. return {};
  167. return js_string(interpreter, string_object->primitive_string()->string());
  168. }
  169. }