ArrayPrototype.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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/Array.h>
  32. #include <LibJS/Runtime/ArrayPrototype.h>
  33. #include <LibJS/Runtime/Error.h>
  34. #include <LibJS/Runtime/Function.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/MarkedValueList.h>
  37. #include <LibJS/Runtime/ObjectPrototype.h>
  38. #include <LibJS/Runtime/Value.h>
  39. namespace JS {
  40. ArrayPrototype::ArrayPrototype()
  41. : Object(interpreter().global_object().object_prototype())
  42. {
  43. u8 attr = Attribute::Writable | Attribute::Configurable;
  44. put_native_function("filter", filter, 1, attr);
  45. put_native_function("forEach", for_each, 1, attr);
  46. put_native_function("map", map, 1, attr);
  47. put_native_function("pop", pop, 0, attr);
  48. put_native_function("push", push, 1, attr);
  49. put_native_function("shift", shift, 0, attr);
  50. put_native_function("toString", to_string, 0, attr);
  51. put_native_function("unshift", unshift, 1, attr);
  52. put_native_function("join", join, 1, attr);
  53. put_native_function("concat", concat, 1, attr);
  54. put_native_function("slice", slice, 2, attr);
  55. put_native_function("indexOf", index_of, 1, attr);
  56. put_native_function("reverse", reverse, 0, attr);
  57. put_native_function("lastIndexOf", last_index_of, 1, attr);
  58. put_native_function("includes", includes, 1, attr);
  59. put_native_function("find", find, 1, attr);
  60. put_native_function("findIndex", find_index, 1, attr);
  61. put_native_function("some", some, 1, attr);
  62. put_native_function("every", every, 1, attr);
  63. put("length", Value(0), Attribute::Configurable);
  64. }
  65. ArrayPrototype::~ArrayPrototype()
  66. {
  67. }
  68. static Function* callback_from_args(Interpreter& interpreter, const String& name)
  69. {
  70. if (interpreter.argument_count() < 1) {
  71. interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
  72. return nullptr;
  73. }
  74. auto callback = interpreter.argument(0);
  75. if (!callback.is_function()) {
  76. interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string_without_side_effects().characters()));
  77. return nullptr;
  78. }
  79. return &callback.as_function();
  80. }
  81. static size_t get_length(Interpreter& interpreter, Object& object)
  82. {
  83. auto length_property = object.get("length");
  84. if (interpreter.exception())
  85. return 0;
  86. return length_property.to_size_t(interpreter);
  87. }
  88. static void for_each_item(Interpreter& interpreter, const String& name, AK::Function<IterationDecision(size_t index, Value value, Value callback_result)> callback, bool skip_empty = true)
  89. {
  90. auto* this_object = interpreter.this_value().to_object(interpreter);
  91. if (!this_object)
  92. return;
  93. auto initial_length = get_length(interpreter, *this_object);
  94. if (interpreter.exception())
  95. return;
  96. auto* callback_function = callback_from_args(interpreter, name);
  97. if (!callback_function)
  98. return;
  99. auto this_value = interpreter.argument(1);
  100. for (size_t i = 0; i < initial_length; ++i) {
  101. auto value = this_object->get_by_index(i);
  102. if (interpreter.exception())
  103. return;
  104. if (value.is_empty()) {
  105. if (skip_empty)
  106. continue;
  107. value = js_undefined();
  108. }
  109. MarkedValueList arguments(interpreter.heap());
  110. arguments.append(value);
  111. arguments.append(Value((i32)i));
  112. arguments.append(this_object);
  113. auto callback_result = interpreter.call(*callback_function, this_value, move(arguments));
  114. if (interpreter.exception())
  115. return;
  116. if (callback(i, value, callback_result) == IterationDecision::Break)
  117. break;
  118. }
  119. }
  120. Value ArrayPrototype::filter(Interpreter& interpreter)
  121. {
  122. auto* new_array = Array::create(interpreter.global_object());
  123. for_each_item(interpreter, "filter", [&](auto, auto value, auto callback_result) {
  124. if (callback_result.to_boolean())
  125. new_array->elements().append(value);
  126. return IterationDecision::Continue;
  127. });
  128. return Value(new_array);
  129. }
  130. Value ArrayPrototype::for_each(Interpreter& interpreter)
  131. {
  132. for_each_item(interpreter, "forEach", [](auto, auto, auto) {
  133. return IterationDecision::Continue;
  134. });
  135. return js_undefined();
  136. }
  137. Value ArrayPrototype::map(Interpreter& interpreter)
  138. {
  139. auto* this_object = interpreter.this_value().to_object(interpreter);
  140. if (!this_object)
  141. return {};
  142. auto initial_length = get_length(interpreter, *this_object);
  143. if (interpreter.exception())
  144. return {};
  145. auto* new_array = Array::create(interpreter.global_object());
  146. new_array->elements().resize(initial_length);
  147. for_each_item(interpreter, "map", [&](auto index, auto, auto callback_result) {
  148. new_array->elements()[index] = callback_result;
  149. return IterationDecision::Continue;
  150. });
  151. return Value(new_array);
  152. }
  153. Value ArrayPrototype::push(Interpreter& interpreter)
  154. {
  155. auto* this_object = interpreter.this_value().to_object(interpreter);
  156. if (!this_object)
  157. return {};
  158. if (this_object->is_array()) {
  159. auto* array = static_cast<Array*>(this_object);
  160. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  161. array->elements().append(interpreter.argument(i));
  162. return Value(array->length());
  163. }
  164. auto length = get_length(interpreter, *this_object);
  165. if (interpreter.exception())
  166. return {};
  167. auto argument_count = interpreter.argument_count();
  168. auto new_length = length + argument_count;
  169. if (new_length > MAX_ARRAY_LIKE_INDEX)
  170. return interpreter.throw_exception<TypeError>("Maximum array size exceeded");
  171. for (size_t i = 0; i < argument_count; ++i)
  172. this_object->put_by_index(length + i, interpreter.argument(i));
  173. auto new_length_value = Value((i32)new_length);
  174. this_object->put("length", new_length_value);
  175. if (interpreter.exception())
  176. return {};
  177. return new_length_value;
  178. }
  179. Value ArrayPrototype::unshift(Interpreter& interpreter)
  180. {
  181. auto* array = array_from(interpreter);
  182. if (!array)
  183. return {};
  184. for (size_t i = 0; i < interpreter.argument_count(); ++i)
  185. array->elements().insert(i, interpreter.argument(i));
  186. return Value(array->length());
  187. }
  188. Value ArrayPrototype::pop(Interpreter& interpreter)
  189. {
  190. auto* this_object = interpreter.this_value().to_object(interpreter);
  191. if (!this_object)
  192. return {};
  193. if (this_object->is_array()) {
  194. auto* array = static_cast<Array*>(this_object);
  195. if (array->elements().is_empty())
  196. return js_undefined();
  197. return array->elements().take_last().value_or(js_undefined());
  198. }
  199. auto length = get_length(interpreter, *this_object);
  200. if (length == 0) {
  201. this_object->put("length", Value(0));
  202. return js_undefined();
  203. }
  204. auto index = length - 1;
  205. auto element = this_object->get_by_index(index).value_or(js_undefined());
  206. if (interpreter.exception())
  207. return {};
  208. this_object->delete_property(PropertyName(index));
  209. this_object->put("length", Value((i32)index));
  210. if (interpreter.exception())
  211. return {};
  212. return element;
  213. }
  214. Value ArrayPrototype::shift(Interpreter& interpreter)
  215. {
  216. auto* array = array_from(interpreter);
  217. if (!array)
  218. return {};
  219. if (array->elements().is_empty())
  220. return js_undefined();
  221. return array->elements().take_first().value_or(js_undefined());
  222. }
  223. Value ArrayPrototype::to_string(Interpreter& interpreter)
  224. {
  225. auto* this_object = interpreter.this_value().to_object(interpreter);
  226. if (!this_object)
  227. return {};
  228. auto join_function = this_object->get("join");
  229. if (interpreter.exception())
  230. return {};
  231. if (!join_function.is_function())
  232. return ObjectPrototype::to_string(interpreter);
  233. return interpreter.call(join_function.as_function(), this_object);
  234. }
  235. Value ArrayPrototype::join(Interpreter& interpreter)
  236. {
  237. auto* this_object = interpreter.this_value().to_object(interpreter);
  238. if (!this_object)
  239. return {};
  240. String separator = ",";
  241. if (interpreter.argument_count()) {
  242. separator = interpreter.argument(0).to_string(interpreter);
  243. if (interpreter.exception())
  244. return {};
  245. }
  246. auto length = get_length(interpreter, *this_object);
  247. if (interpreter.exception())
  248. return {};
  249. StringBuilder builder;
  250. for (size_t i = 0; i < length; ++i) {
  251. if (i > 0)
  252. builder.append(separator);
  253. auto value = this_object->get_by_index(i).value_or(js_undefined());
  254. if (interpreter.exception())
  255. return {};
  256. if (value.is_undefined() || value.is_null())
  257. continue;
  258. auto string = value.to_string(interpreter);
  259. if (interpreter.exception())
  260. return {};
  261. builder.append(string);
  262. }
  263. return js_string(interpreter, builder.to_string());
  264. }
  265. Value ArrayPrototype::concat(Interpreter& interpreter)
  266. {
  267. auto* array = array_from(interpreter);
  268. if (!array)
  269. return {};
  270. auto* new_array = Array::create(interpreter.global_object());
  271. new_array->elements().append(array->elements());
  272. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  273. auto argument = interpreter.argument(i);
  274. if (argument.is_array()) {
  275. auto& argument_object = argument.as_object();
  276. new_array->elements().append(argument_object.elements());
  277. } else {
  278. new_array->elements().append(argument);
  279. }
  280. }
  281. return Value(new_array);
  282. }
  283. Value ArrayPrototype::slice(Interpreter& interpreter)
  284. {
  285. auto* array = array_from(interpreter);
  286. if (!array)
  287. return {};
  288. auto* new_array = Array::create(interpreter.global_object());
  289. if (interpreter.argument_count() == 0) {
  290. new_array->elements().append(array->elements());
  291. return new_array;
  292. }
  293. ssize_t array_size = static_cast<ssize_t>(array->elements().size());
  294. auto start_slice = interpreter.argument(0).to_i32(interpreter);
  295. if (interpreter.exception())
  296. return {};
  297. auto end_slice = array_size;
  298. if (start_slice > array_size)
  299. return new_array;
  300. if (start_slice < 0)
  301. start_slice = end_slice + start_slice;
  302. if (interpreter.argument_count() >= 2) {
  303. end_slice = interpreter.argument(1).to_i32(interpreter);
  304. if (interpreter.exception())
  305. return {};
  306. if (end_slice < 0)
  307. end_slice = array_size + end_slice;
  308. else if (end_slice > array_size)
  309. end_slice = array_size;
  310. }
  311. size_t array_capacity = start_slice + array_size - end_slice;
  312. new_array->elements().ensure_capacity(array_capacity);
  313. for (ssize_t i = start_slice; i < end_slice; ++i) {
  314. new_array->elements().append(array->elements().at(i));
  315. }
  316. return new_array;
  317. }
  318. Value ArrayPrototype::index_of(Interpreter& interpreter)
  319. {
  320. auto* array = array_from(interpreter);
  321. if (!array)
  322. return {};
  323. i32 array_size = static_cast<i32>(array->elements().size());
  324. if (array_size == 0)
  325. return Value(-1);
  326. i32 from_index = 0;
  327. if (interpreter.argument_count() >= 2) {
  328. from_index = interpreter.argument(1).to_i32(interpreter);
  329. if (interpreter.exception())
  330. return {};
  331. if (from_index >= array_size)
  332. return Value(-1);
  333. auto negative_min_index = ((array_size - 1) * -1);
  334. if (from_index < negative_min_index)
  335. from_index = 0;
  336. else if (from_index < 0)
  337. from_index = array_size + from_index;
  338. }
  339. auto search_element = interpreter.argument(0);
  340. for (i32 i = from_index; i < array_size; ++i) {
  341. auto& element = array->elements().at(i);
  342. if (strict_eq(interpreter, element, search_element))
  343. return Value(i);
  344. }
  345. return Value(-1);
  346. }
  347. Value ArrayPrototype::reverse(Interpreter& interpreter)
  348. {
  349. auto* array = array_from(interpreter);
  350. if (!array)
  351. return {};
  352. if (array->elements().size() == 0)
  353. return array;
  354. Vector<Value> array_reverse;
  355. array_reverse.ensure_capacity(array->elements().size());
  356. for (ssize_t i = array->elements().size() - 1; i >= 0; --i)
  357. array_reverse.append(array->elements().at(i));
  358. array->elements() = move(array_reverse);
  359. return array;
  360. }
  361. Value ArrayPrototype::last_index_of(Interpreter& interpreter)
  362. {
  363. auto* array = array_from(interpreter);
  364. if (!array)
  365. return {};
  366. i32 array_size = static_cast<i32>(array->elements().size());
  367. if (array_size == 0)
  368. return Value(-1);
  369. i32 from_index = array_size - 1;
  370. if (interpreter.argument_count() >= 2) {
  371. from_index = interpreter.argument(1).to_i32(interpreter);
  372. if (interpreter.exception())
  373. return {};
  374. if (from_index >= 0)
  375. from_index = min(from_index, array_size - 1);
  376. else
  377. from_index = array_size + from_index;
  378. }
  379. auto search_element = interpreter.argument(0);
  380. for (i32 i = from_index; i >= 0; --i) {
  381. auto& element = array->elements().at(i);
  382. if (strict_eq(interpreter, element, search_element))
  383. return Value(i);
  384. }
  385. return Value(-1);
  386. }
  387. Value ArrayPrototype::includes(Interpreter& interpreter)
  388. {
  389. auto* array = array_from(interpreter);
  390. if (!array)
  391. return {};
  392. i32 array_size = array->elements().size();
  393. if (array_size == 0)
  394. return Value(false);
  395. i32 from_index = 0;
  396. if (interpreter.argument_count() >= 2) {
  397. from_index = interpreter.argument(1).to_i32(interpreter);
  398. if (interpreter.exception())
  399. return {};
  400. if (from_index >= array_size)
  401. return Value(false);
  402. auto negative_min_index = ((array_size - 1) * -1);
  403. if (from_index < negative_min_index)
  404. from_index = 0;
  405. else if (from_index < 0)
  406. from_index = array_size + from_index;
  407. }
  408. auto value_to_find = interpreter.argument(0);
  409. for (i32 i = from_index; i < array_size; ++i) {
  410. auto& element = array->elements().at(i);
  411. if (same_value_zero(interpreter, element, value_to_find))
  412. return Value(true);
  413. }
  414. return Value(false);
  415. }
  416. Value ArrayPrototype::find(Interpreter& interpreter)
  417. {
  418. auto result = js_undefined();
  419. for_each_item(
  420. interpreter, "find", [&](auto, auto value, auto callback_result) {
  421. if (callback_result.to_boolean()) {
  422. result = value;
  423. return IterationDecision::Break;
  424. }
  425. return IterationDecision::Continue;
  426. },
  427. false);
  428. return result;
  429. }
  430. Value ArrayPrototype::find_index(Interpreter& interpreter)
  431. {
  432. auto result_index = -1;
  433. for_each_item(
  434. interpreter, "findIndex", [&](auto index, auto, auto callback_result) {
  435. if (callback_result.to_boolean()) {
  436. result_index = index;
  437. return IterationDecision::Break;
  438. }
  439. return IterationDecision::Continue;
  440. },
  441. false);
  442. return Value(result_index);
  443. }
  444. Value ArrayPrototype::some(Interpreter& interpreter)
  445. {
  446. auto result = false;
  447. for_each_item(interpreter, "some", [&](auto, auto, auto callback_result) {
  448. if (callback_result.to_boolean()) {
  449. result = true;
  450. return IterationDecision::Break;
  451. }
  452. return IterationDecision::Continue;
  453. });
  454. return Value(result);
  455. }
  456. Value ArrayPrototype::every(Interpreter& interpreter)
  457. {
  458. auto result = true;
  459. for_each_item(interpreter, "every", [&](auto, auto, auto callback_result) {
  460. if (!callback_result.to_boolean()) {
  461. result = false;
  462. return IterationDecision::Break;
  463. }
  464. return IterationDecision::Continue;
  465. });
  466. return Value(result);
  467. }
  468. }