StringPrototype.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibJS/Heap/Heap.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/Error.h>
  32. #include <LibJS/Runtime/GlobalObject.h>
  33. #include <LibJS/Runtime/PrimitiveString.h>
  34. #include <LibJS/Runtime/StringObject.h>
  35. #include <LibJS/Runtime/StringIterator.h>
  36. #include <LibJS/Runtime/StringPrototype.h>
  37. #include <LibJS/Runtime/Value.h>
  38. #include <string.h>
  39. namespace JS {
  40. static StringObject* typed_this(Interpreter& interpreter, GlobalObject& global_object)
  41. {
  42. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  43. if (!this_object)
  44. return nullptr;
  45. if (!this_object->is_string_object()) {
  46. interpreter.throw_exception<TypeError>(ErrorType::NotA, "String");
  47. return nullptr;
  48. }
  49. return static_cast<StringObject*>(this_object);
  50. }
  51. static String ak_string_from(Interpreter& interpreter, GlobalObject& global_object)
  52. {
  53. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  54. if (!this_object)
  55. return {};
  56. return Value(this_object).to_string(interpreter);
  57. }
  58. StringPrototype::StringPrototype(GlobalObject& global_object)
  59. : StringObject(*js_string(interpreter(), String::empty()), *global_object.object_prototype())
  60. {
  61. }
  62. void StringPrototype::initialize(GlobalObject& global_object)
  63. {
  64. StringObject::initialize(global_object);
  65. u8 attr = Attribute::Writable | Attribute::Configurable;
  66. define_native_property("length", length_getter, nullptr, 0);
  67. define_native_function("charAt", char_at, 1, attr);
  68. define_native_function("charCodeAt", char_code_at, 1, attr);
  69. define_native_function("repeat", repeat, 1, attr);
  70. define_native_function("startsWith", starts_with, 1, attr);
  71. define_native_function("indexOf", index_of, 1, attr);
  72. define_native_function("toLowerCase", to_lowercase, 0, attr);
  73. define_native_function("toUpperCase", to_uppercase, 0, attr);
  74. define_native_function("toString", to_string, 0, attr);
  75. define_native_function("padStart", pad_start, 1, attr);
  76. define_native_function("padEnd", pad_end, 1, attr);
  77. define_native_function("trim", trim, 0, attr);
  78. define_native_function("trimStart", trim_start, 0, attr);
  79. define_native_function("trimEnd", trim_end, 0, attr);
  80. define_native_function("concat", concat, 1, attr);
  81. define_native_function("substring", substring, 2, attr);
  82. define_native_function("includes", includes, 1, attr);
  83. define_native_function("slice", slice, 2, attr);
  84. define_native_function("lastIndexOf", last_index_of, 1, attr);
  85. define_native_function(global_object.interpreter().well_known_symbol_iterator(), symbol_iterator, 0, attr);
  86. }
  87. StringPrototype::~StringPrototype()
  88. {
  89. }
  90. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at)
  91. {
  92. auto string = ak_string_from(interpreter, global_object);
  93. if (string.is_null())
  94. return {};
  95. i32 index = 0;
  96. if (interpreter.argument_count()) {
  97. index = interpreter.argument(0).to_i32(interpreter);
  98. if (interpreter.exception())
  99. return {};
  100. }
  101. if (index < 0 || index >= static_cast<i32>(string.length()))
  102. return js_string(interpreter, String::empty());
  103. // FIXME: This should return a character corresponding to the i'th UTF-16 code point.
  104. return js_string(interpreter, string.substring(index, 1));
  105. }
  106. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_code_at)
  107. {
  108. auto string = ak_string_from(interpreter, global_object);
  109. if (string.is_null())
  110. return {};
  111. i32 index = 0;
  112. if (interpreter.argument_count()) {
  113. index = interpreter.argument(0).to_i32(interpreter);
  114. if (interpreter.exception())
  115. return {};
  116. }
  117. if (index < 0 || index >= static_cast<i32>(string.length()))
  118. return js_nan();
  119. // FIXME: This should return the i'th UTF-16 code point.
  120. return Value((i32)string[index]);
  121. }
  122. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::repeat)
  123. {
  124. auto string = ak_string_from(interpreter, global_object);
  125. if (string.is_null())
  126. return {};
  127. if (!interpreter.argument_count())
  128. return js_string(interpreter, String::empty());
  129. auto count_value = interpreter.argument(0).to_number(interpreter);
  130. if (interpreter.exception())
  131. return {};
  132. if (count_value.as_double() < 0) {
  133. interpreter.throw_exception<RangeError>(ErrorType::StringRepeatCountMustBe, "positive");
  134. return {};
  135. }
  136. if (count_value.is_infinity()) {
  137. interpreter.throw_exception<RangeError>(ErrorType::StringRepeatCountMustBe, "finite");
  138. return {};
  139. }
  140. auto count = count_value.to_size_t(interpreter);
  141. if (interpreter.exception())
  142. return {};
  143. StringBuilder builder;
  144. for (size_t i = 0; i < count; ++i)
  145. builder.append(string);
  146. return js_string(interpreter, builder.to_string());
  147. }
  148. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::starts_with)
  149. {
  150. auto string = ak_string_from(interpreter, global_object);
  151. if (string.is_null())
  152. return {};
  153. if (!interpreter.argument_count())
  154. return Value(false);
  155. auto search_string = interpreter.argument(0).to_string(interpreter);
  156. if (interpreter.exception())
  157. return {};
  158. auto string_length = string.length();
  159. auto search_string_length = search_string.length();
  160. size_t start = 0;
  161. if (interpreter.argument_count() > 1) {
  162. auto number = interpreter.argument(1).to_number(interpreter);
  163. if (interpreter.exception())
  164. return {};
  165. if (!number.is_nan())
  166. start = min(number.to_size_t(interpreter), string_length);
  167. }
  168. if (start + search_string_length > string_length)
  169. return Value(false);
  170. if (search_string_length == 0)
  171. return Value(true);
  172. return Value(string.substring(start, search_string_length) == search_string);
  173. }
  174. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
  175. {
  176. auto string = ak_string_from(interpreter, global_object);
  177. if (string.is_null())
  178. return {};
  179. auto needle = interpreter.argument(0).to_string(interpreter);
  180. if (interpreter.exception())
  181. return {};
  182. return Value((i32)string.index_of(needle).value_or(-1));
  183. }
  184. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_lowercase)
  185. {
  186. auto string = ak_string_from(interpreter, global_object);
  187. if (string.is_null())
  188. return {};
  189. return js_string(interpreter, string.to_lowercase());
  190. }
  191. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_uppercase)
  192. {
  193. auto string = ak_string_from(interpreter, global_object);
  194. if (string.is_null())
  195. return {};
  196. return js_string(interpreter, string.to_uppercase());
  197. }
  198. JS_DEFINE_NATIVE_GETTER(StringPrototype::length_getter)
  199. {
  200. auto* string_object = typed_this(interpreter, global_object);
  201. if (!string_object)
  202. return {};
  203. return Value((i32)string_object->primitive_string().string().length());
  204. }
  205. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_string)
  206. {
  207. auto* string_object = typed_this(interpreter, global_object);
  208. if (!string_object)
  209. return {};
  210. return js_string(interpreter, string_object->primitive_string().string());
  211. }
  212. enum class PadPlacement {
  213. Start,
  214. End,
  215. };
  216. static Value pad_string(Interpreter& interpreter, const String& string, PadPlacement placement)
  217. {
  218. auto max_length = interpreter.argument(0).to_size_t(interpreter);
  219. if (interpreter.exception())
  220. return {};
  221. if (max_length <= string.length())
  222. return js_string(interpreter, string);
  223. String fill_string = " ";
  224. if (!interpreter.argument(1).is_undefined()) {
  225. fill_string = interpreter.argument(1).to_string(interpreter);
  226. if (interpreter.exception())
  227. return {};
  228. if (fill_string.is_empty())
  229. return js_string(interpreter, string);
  230. }
  231. auto fill_length = max_length - string.length();
  232. StringBuilder filler_builder;
  233. while (filler_builder.length() < fill_length)
  234. filler_builder.append(fill_string);
  235. auto filler = filler_builder.build().substring(0, fill_length);
  236. if (placement == PadPlacement::Start)
  237. return js_string(interpreter, String::format("%s%s", filler.characters(), string.characters()));
  238. return js_string(interpreter, String::format("%s%s", string.characters(), filler.characters()));
  239. }
  240. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_start)
  241. {
  242. auto string = ak_string_from(interpreter, global_object);
  243. if (string.is_null())
  244. return {};
  245. return pad_string(interpreter, string, PadPlacement::Start);
  246. }
  247. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_end)
  248. {
  249. auto string = ak_string_from(interpreter, global_object);
  250. if (string.is_null())
  251. return {};
  252. return pad_string(interpreter, string, PadPlacement::End);
  253. }
  254. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim)
  255. {
  256. auto string = ak_string_from(interpreter, global_object);
  257. if (string.is_null())
  258. return {};
  259. return js_string(interpreter, string.trim_whitespace(String::TrimMode::Both));
  260. }
  261. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_start)
  262. {
  263. auto string = ak_string_from(interpreter, global_object);
  264. if (string.is_null())
  265. return {};
  266. return js_string(interpreter, string.trim_whitespace(String::TrimMode::Left));
  267. }
  268. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_end)
  269. {
  270. auto string = ak_string_from(interpreter, global_object);
  271. if (string.is_null())
  272. return {};
  273. return js_string(interpreter, string.trim_whitespace(String::TrimMode::Right));
  274. }
  275. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::concat)
  276. {
  277. auto string = ak_string_from(interpreter, global_object);
  278. if (string.is_null())
  279. return {};
  280. StringBuilder builder;
  281. builder.append(string);
  282. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  283. auto string_argument = interpreter.argument(i).to_string(interpreter);
  284. if (interpreter.exception())
  285. return {};
  286. builder.append(string_argument);
  287. }
  288. return js_string(interpreter, builder.to_string());
  289. }
  290. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::substring)
  291. {
  292. auto string = ak_string_from(interpreter, global_object);
  293. if (string.is_null())
  294. return {};
  295. if (interpreter.argument_count() == 0)
  296. return js_string(interpreter, string);
  297. // FIXME: index_start and index_end should index a UTF-16 code_point view of the string.
  298. auto string_length = string.length();
  299. auto index_start = min(interpreter.argument(0).to_size_t(interpreter), string_length);
  300. if (interpreter.exception())
  301. return {};
  302. auto index_end = string_length;
  303. if (interpreter.argument_count() >= 2) {
  304. index_end = min(interpreter.argument(1).to_size_t(interpreter), string_length);
  305. if (interpreter.exception())
  306. return {};
  307. }
  308. if (index_start == index_end)
  309. return js_string(interpreter, String(""));
  310. if (index_start > index_end) {
  311. if (interpreter.argument_count() == 1)
  312. return js_string(interpreter, String(""));
  313. auto temp_index_start = index_start;
  314. index_start = index_end;
  315. index_end = temp_index_start;
  316. }
  317. auto part_length = index_end - index_start;
  318. auto string_part = string.substring(index_start, part_length);
  319. return js_string(interpreter, string_part);
  320. }
  321. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::includes)
  322. {
  323. auto string = ak_string_from(interpreter, global_object);
  324. if (string.is_null())
  325. return {};
  326. auto search_string = interpreter.argument(0).to_string(interpreter);
  327. if (interpreter.exception())
  328. return {};
  329. // FIXME: position should index a UTF-16 code_point view of the string.
  330. size_t position = 0;
  331. if (interpreter.argument_count() >= 2) {
  332. position = interpreter.argument(1).to_size_t(interpreter);
  333. if (interpreter.exception())
  334. return {};
  335. if (position >= string.length())
  336. return Value(false);
  337. }
  338. if (position == 0)
  339. return Value(string.contains(search_string));
  340. auto substring_length = string.length() - position;
  341. auto substring_search = string.substring(position, substring_length);
  342. return Value(substring_search.contains(search_string));
  343. }
  344. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice)
  345. {
  346. auto string = ak_string_from(interpreter, global_object);
  347. if (string.is_null())
  348. return {};
  349. if (interpreter.argument_count() == 0)
  350. return js_string(interpreter, string);
  351. // FIXME: index_start and index_end should index a UTF-16 code_point view of the string.
  352. auto string_length = static_cast<i32>(string.length());
  353. auto index_start = interpreter.argument(0).to_i32(interpreter);
  354. if (interpreter.exception())
  355. return {};
  356. auto index_end = string_length;
  357. auto negative_min_index = -(string_length - 1);
  358. if (index_start < negative_min_index)
  359. index_start = 0;
  360. else if (index_start < 0)
  361. index_start = string_length + index_start;
  362. if (interpreter.argument_count() >= 2) {
  363. index_end = interpreter.argument(1).to_i32(interpreter);
  364. if (interpreter.exception())
  365. return {};
  366. if (index_end < negative_min_index)
  367. return js_string(interpreter, String::empty());
  368. if (index_end > string_length)
  369. index_end = string_length;
  370. else if (index_end < 0)
  371. index_end = string_length + index_end;
  372. }
  373. if (index_start >= index_end)
  374. return js_string(interpreter, String::empty());
  375. auto part_length = index_end - index_start;
  376. auto string_part = string.substring(index_start, part_length);
  377. return js_string(interpreter, string_part);
  378. }
  379. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of)
  380. {
  381. auto string = ak_string_from(interpreter, global_object);
  382. if (string.is_null())
  383. return {};
  384. if (interpreter.argument_count() == 0)
  385. return Value(-1);
  386. auto search_string = interpreter.argument(0).to_string(interpreter);
  387. if (interpreter.exception())
  388. return {};
  389. if (search_string.length() > string.length())
  390. return Value(-1);
  391. auto max_index = string.length() - search_string.length();
  392. auto from_index = max_index;
  393. if (interpreter.argument_count() >= 2) {
  394. // FIXME: from_index should index a UTF-16 code_point view of the string.
  395. from_index = min(interpreter.argument(1).to_size_t(interpreter), max_index);
  396. if (interpreter.exception())
  397. return {};
  398. }
  399. for (i32 i = from_index; i >= 0; --i) {
  400. auto part_view = string.substring_view(i, search_string.length());
  401. if (part_view == search_string)
  402. return Value(i);
  403. }
  404. return Value(-1);
  405. }
  406. JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
  407. {
  408. auto this_object = interpreter.this_value(global_object);
  409. if (this_object.is_undefined() || this_object.is_null()) {
  410. interpreter.throw_exception<TypeError>(ErrorType::ToObjectNullOrUndef);
  411. return {};
  412. }
  413. auto string = this_object.to_string(interpreter);
  414. if (interpreter.exception())
  415. return {};
  416. return StringIterator::create(global_object, string);
  417. }
  418. }