StringPrototype.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. put_native_function("padStart", pad_start, 1);
  50. put_native_function("padEnd", pad_end, 1);
  51. }
  52. StringPrototype::~StringPrototype()
  53. {
  54. }
  55. Value StringPrototype::char_at(Interpreter& interpreter)
  56. {
  57. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  58. if (!this_object)
  59. return {};
  60. i32 index = 0;
  61. if (interpreter.argument_count())
  62. index = interpreter.argument(0).to_i32();
  63. ASSERT(this_object->is_string_object());
  64. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  65. if (index < 0 || index >= static_cast<i32>(underlying_string.length()))
  66. return js_string(interpreter, String::empty());
  67. return js_string(interpreter, underlying_string.substring(index, 1));
  68. }
  69. Value StringPrototype::repeat(Interpreter& interpreter)
  70. {
  71. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  72. if (!this_object)
  73. return {};
  74. ASSERT(this_object->is_string_object());
  75. if (!interpreter.argument_count())
  76. return js_string(interpreter, String::empty());
  77. i32 count = 0;
  78. count = interpreter.argument(0).to_i32();
  79. if (count < 0) {
  80. // FIXME: throw RangeError
  81. return {};
  82. }
  83. auto* string_object = static_cast<StringObject*>(this_object);
  84. StringBuilder builder;
  85. for (i32 i = 0; i < count; ++i)
  86. builder.append(string_object->primitive_string()->string());
  87. return js_string(interpreter, builder.to_string());
  88. }
  89. Value StringPrototype::starts_with(Interpreter& interpreter)
  90. {
  91. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  92. if (!this_object)
  93. return {};
  94. if (!interpreter.argument_count())
  95. return Value(false);
  96. auto search_string = interpreter.argument(0).to_string();
  97. auto search_string_length = static_cast<i32>(search_string.length());
  98. i32 position = 0;
  99. if (interpreter.argument_count() > 1) {
  100. auto number = interpreter.argument(1).to_number();
  101. if (!number.is_nan())
  102. position = number.to_i32();
  103. }
  104. ASSERT(this_object->is_string_object());
  105. auto underlying_string = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  106. auto underlying_string_length = static_cast<i32>(underlying_string.length());
  107. auto start = min(max(position, 0), underlying_string_length);
  108. if (start + search_string_length > underlying_string_length)
  109. return Value(false);
  110. if (search_string_length == 0)
  111. return Value(true);
  112. return Value(underlying_string.substring(start, search_string_length) == search_string);
  113. }
  114. Value StringPrototype::index_of(Interpreter& interpreter)
  115. {
  116. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  117. if (!this_object)
  118. return {};
  119. if (!this_object->is_string_object())
  120. return interpreter.throw_exception<TypeError>("Not a String object");
  121. Value needle_value = js_undefined();
  122. if (interpreter.argument_count() >= 1)
  123. needle_value = interpreter.argument(0);
  124. auto needle = needle_value.to_string();
  125. auto haystack = static_cast<const StringObject*>(this_object)->primitive_string()->string();
  126. // FIXME: We should have a helper in AK::String for this.
  127. auto* ptr = strstr(haystack.characters(), needle.characters());
  128. if (!ptr)
  129. return Value(-1);
  130. return Value((i32)(ptr - haystack.characters()));
  131. }
  132. static StringObject* string_object_from(Interpreter& interpreter)
  133. {
  134. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  135. if (!this_object)
  136. return nullptr;
  137. if (!this_object->is_string_object()) {
  138. interpreter.throw_exception<TypeError>("Not a String object");
  139. return nullptr;
  140. }
  141. return static_cast<StringObject*>(this_object);
  142. }
  143. Value StringPrototype::to_lowercase(Interpreter& interpreter)
  144. {
  145. auto* string_object = string_object_from(interpreter);
  146. if (!string_object)
  147. return {};
  148. return js_string(interpreter, string_object->primitive_string()->string().to_lowercase());
  149. }
  150. Value StringPrototype::to_uppercase(Interpreter& interpreter)
  151. {
  152. auto* string_object = string_object_from(interpreter);
  153. if (!string_object)
  154. return {};
  155. return js_string(interpreter, string_object->primitive_string()->string().to_uppercase());
  156. }
  157. Value StringPrototype::length_getter(Interpreter& interpreter)
  158. {
  159. auto* string_object = string_object_from(interpreter);
  160. if (!string_object)
  161. return {};
  162. return Value((i32)string_object->primitive_string()->string().length());
  163. }
  164. Value StringPrototype::to_string(Interpreter& interpreter)
  165. {
  166. auto* string_object = string_object_from(interpreter);
  167. if (!string_object)
  168. return {};
  169. return js_string(interpreter, string_object->primitive_string()->string());
  170. }
  171. enum class PadPlacement {
  172. Start,
  173. End,
  174. };
  175. static Value pad_string(Interpreter& interpreter, Object* object, PadPlacement placement)
  176. {
  177. auto string = object->to_string().as_string()->string();
  178. if (interpreter.argument(0).to_number().is_nan()
  179. || interpreter.argument(0).to_number().is_undefined()
  180. || interpreter.argument(0).to_number().to_i32() < 0) {
  181. return js_string(interpreter, string);
  182. }
  183. auto max_length = static_cast<size_t>(interpreter.argument(0).to_i32());
  184. if (max_length <= string.length())
  185. return js_string(interpreter, string);
  186. String fill_string = " ";
  187. if (!interpreter.argument(1).is_undefined())
  188. fill_string = interpreter.argument(1).to_string();
  189. if (fill_string.is_empty())
  190. return js_string(interpreter, string);
  191. auto fill_length = max_length - string.length();
  192. StringBuilder filler_builder;
  193. while (filler_builder.length() < fill_length)
  194. filler_builder.append(fill_string);
  195. auto filler = filler_builder.build().substring(0, fill_length);
  196. if (placement == PadPlacement::Start)
  197. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  198. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  199. }
  200. Value StringPrototype::pad_start(Interpreter& interpreter)
  201. {
  202. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  203. if (!this_object)
  204. return {};
  205. return pad_string(interpreter, this_object, PadPlacement::Start);
  206. }
  207. Value StringPrototype::pad_end(Interpreter& interpreter)
  208. {
  209. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  210. if (!this_object)
  211. return {};
  212. return pad_string(interpreter, this_object, PadPlacement::End);
  213. }
  214. }