ArrayPrototype.cpp 17 KB

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