ArrayConstructor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Function.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Array.h>
  11. #include <LibJS/Runtime/ArrayConstructor.h>
  12. #include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
  13. #include <LibJS/Runtime/Completion.h>
  14. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  15. #include <LibJS/Runtime/Error.h>
  16. #include <LibJS/Runtime/GlobalObject.h>
  17. #include <LibJS/Runtime/Iterator.h>
  18. #include <LibJS/Runtime/PromiseCapability.h>
  19. #include <LibJS/Runtime/PromiseConstructor.h>
  20. #include <LibJS/Runtime/Shape.h>
  21. namespace JS {
  22. GC_DEFINE_ALLOCATOR(ArrayConstructor);
  23. ArrayConstructor::ArrayConstructor(Realm& realm)
  24. : NativeFunction(realm.vm().names.Array.as_string(), realm.intrinsics().function_prototype())
  25. {
  26. }
  27. void ArrayConstructor::initialize(Realm& realm)
  28. {
  29. auto& vm = this->vm();
  30. Base::initialize(realm);
  31. // 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
  32. define_direct_property(vm.names.prototype, realm.intrinsics().array_prototype(), 0);
  33. u8 attr = Attribute::Writable | Attribute::Configurable;
  34. define_native_function(realm, vm.names.from, from, 1, attr);
  35. define_native_function(realm, vm.names.fromAsync, from_async, 1, attr);
  36. define_native_function(realm, vm.names.isArray, is_array, 1, attr);
  37. define_native_function(realm, vm.names.of, of, 0, attr);
  38. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  39. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  40. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  41. }
  42. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  43. ThrowCompletionOr<Value> ArrayConstructor::call()
  44. {
  45. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  46. return TRY(construct(*this));
  47. }
  48. // 23.1.1.1 Array ( ...values ), https://tc39.es/ecma262/#sec-array
  49. ThrowCompletionOr<GC::Ref<Object>> ArrayConstructor::construct(FunctionObject& new_target)
  50. {
  51. auto& vm = this->vm();
  52. auto& realm = *vm.current_realm();
  53. // 2. Let proto be ? GetPrototypeFromConstructor(newTarget, "%Array.prototype%").
  54. auto* proto = TRY(get_prototype_from_constructor(vm, new_target, &Intrinsics::array_prototype));
  55. // 3. Let numberOfArgs be the number of elements in values.
  56. // 4. If numberOfArgs = 0, then
  57. if (vm.argument_count() == 0) {
  58. // a. Return ! ArrayCreate(0, proto).
  59. return MUST(Array::create(realm, 0, proto));
  60. }
  61. // 5. Else if numberOfArgs = 1, then
  62. if (vm.argument_count() == 1) {
  63. // a. Let len be values[0].
  64. auto length = vm.argument(0);
  65. // b. Let array be ! ArrayCreate(0, proto).
  66. auto array = MUST(Array::create(realm, 0, proto));
  67. size_t int_length;
  68. // c. If len is not a Number, then
  69. if (!length.is_number()) {
  70. // i. Perform ! CreateDataPropertyOrThrow(array, "0", len).
  71. MUST(array->create_data_property_or_throw(0, length));
  72. // ii. Let intLen be 1𝔽.
  73. int_length = 1;
  74. }
  75. // d. Else,
  76. else {
  77. // i. Let intLen be ! ToUint32(len).
  78. int_length = MUST(length.to_u32(vm));
  79. // ii. If SameValueZero(intLen, len) is false, throw a RangeError exception.
  80. if (int_length != length.as_double())
  81. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "array");
  82. }
  83. // e. Perform ! Set(array, "length", intLen, true).
  84. TRY(array->set(vm.names.length, Value(int_length), Object::ShouldThrowExceptions::Yes));
  85. // f. Return array.
  86. return array;
  87. }
  88. // 6. Else,
  89. // a. Assert: numberOfArgs ≥ 2.
  90. VERIFY(vm.argument_count() >= 2);
  91. // b. Let array be ? ArrayCreate(numberOfArgs, proto).
  92. auto array = TRY(Array::create(realm, vm.argument_count(), proto));
  93. // c. Let k be 0.
  94. // d. Repeat, while k < numberOfArgs,
  95. for (size_t k = 0; k < vm.argument_count(); ++k) {
  96. // i. Let Pk be ! ToString(𝔽(k)).
  97. auto property_key = PropertyKey { k };
  98. // ii. Let itemK be values[k].
  99. auto item_k = vm.argument(k);
  100. // iii. Perform ! CreateDataPropertyOrThrow(array, Pk, itemK).
  101. MUST(array->create_data_property_or_throw(property_key, item_k));
  102. // iv. Set k to k + 1.
  103. }
  104. // e. Assert: The mathematical value of array's "length" property is numberOfArgs.
  105. // f. Return array.
  106. return array;
  107. }
  108. // 23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-array.from
  109. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
  110. {
  111. auto& realm = *vm.current_realm();
  112. auto items = vm.argument(0);
  113. auto mapfn_value = vm.argument(1);
  114. auto this_arg = vm.argument(2);
  115. // 1. Let C be the this value.
  116. auto constructor = vm.this_value();
  117. // 2. If mapfn is undefined, let mapping be false.
  118. GC::Ptr<FunctionObject> mapfn;
  119. // 3. Else,
  120. if (!mapfn_value.is_undefined()) {
  121. // a. If IsCallable(mapfn) is false, throw a TypeError exception.
  122. if (!mapfn_value.is_function())
  123. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, mapfn_value.to_string_without_side_effects());
  124. // b. Let mapping be true.
  125. mapfn = &mapfn_value.as_function();
  126. }
  127. // 4. Let usingIterator be ? GetMethod(items, @@iterator).
  128. auto using_iterator = TRY(items.get_method(vm, vm.well_known_symbol_iterator()));
  129. // 5. If usingIterator is not undefined, then
  130. if (using_iterator) {
  131. GC::Ptr<Object> array;
  132. // a. If IsConstructor(C) is true, then
  133. if (constructor.is_constructor()) {
  134. // i. Let A be ? Construct(C).
  135. array = TRY(JS::construct(vm, constructor.as_function()));
  136. }
  137. // b. Else,
  138. else {
  139. // i. Let A be ! ArrayCreate(0).
  140. array = MUST(Array::create(realm, 0));
  141. }
  142. // c. Let iteratorRecord be ? GetIteratorFromMethod(items, usingIterator).
  143. auto iterator = TRY(get_iterator_from_method(vm, items, *using_iterator));
  144. // d. Let k be 0.
  145. // e. Repeat,
  146. for (size_t k = 0;; ++k) {
  147. // i. If k ≥ 2^53 - 1, then
  148. if (k >= MAX_ARRAY_LIKE_INDEX) {
  149. // 1. Let error be ThrowCompletion(a newly created TypeError object).
  150. auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
  151. // 2. Return ? IteratorClose(iteratorRecord, error).
  152. return *TRY(iterator_close(vm, iterator, move(error)));
  153. }
  154. // ii. Let Pk be ! ToString(𝔽(k)).
  155. auto property_key = PropertyKey { k };
  156. // iii. Let next be ? IteratorStepValue(iteratorRecord).
  157. auto next = TRY(iterator_step_value(vm, iterator));
  158. // iv. If next is DONE, then
  159. if (!next.has_value()) {
  160. // 1. Perform ? Set(A, "length", 𝔽(k), true).
  161. TRY(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  162. // 2. Return A.
  163. return array;
  164. }
  165. Value mapped_value;
  166. // v. If mapping is true, then
  167. if (mapfn) {
  168. // 1. Let mappedValue be Completion(Call(mapfn, thisArg, « nextValue, 𝔽(k) »)).
  169. auto mapped_value_or_error = JS::call(vm, *mapfn, this_arg, next.release_value(), Value(k));
  170. // 2. IfAbruptCloseIterator(mappedValue, iteratorRecord).
  171. if (mapped_value_or_error.is_error())
  172. return *TRY(iterator_close(vm, iterator, mapped_value_or_error.release_error()));
  173. mapped_value = mapped_value_or_error.release_value();
  174. }
  175. // vi. Else, let mappedValue be nextValue.
  176. else {
  177. mapped_value = next.release_value();
  178. }
  179. // vii. Let defineStatus be Completion(CreateDataPropertyOrThrow(A, Pk, mappedValue)).
  180. auto result_or_error = array->create_data_property_or_throw(property_key, mapped_value);
  181. // viii. IfAbruptCloseIterator(defineStatus, iteratorRecord).
  182. if (result_or_error.is_error())
  183. return *TRY(iterator_close(vm, iterator, result_or_error.release_error()));
  184. // ix. Set k to k + 1.
  185. }
  186. }
  187. // 6. NOTE: items is not an Iterable so assume it is an array-like object.
  188. // 7. Let arrayLike be ! ToObject(items).
  189. auto array_like = MUST(items.to_object(vm));
  190. // 8. Let len be ? LengthOfArrayLike(arrayLike).
  191. auto length = TRY(length_of_array_like(vm, array_like));
  192. GC::Ptr<Object> array;
  193. // 9. If IsConstructor(C) is true, then
  194. if (constructor.is_constructor()) {
  195. // a. Let A be ? Construct(C, « 𝔽(len) »).
  196. array = TRY(JS::construct(vm, constructor.as_function(), Value(length)));
  197. } else {
  198. // a. Let A be ? ArrayCreate(len).
  199. array = TRY(Array::create(realm, length));
  200. }
  201. // 11. Let k be 0.
  202. // 12. Repeat, while k < len,
  203. for (size_t k = 0; k < length; ++k) {
  204. // a. Let Pk be ! ToString(𝔽(k)).
  205. auto property_key = PropertyKey { k };
  206. // b. Let kValue be ? Get(arrayLike, Pk).
  207. auto k_value = TRY(array_like->get(property_key));
  208. Value mapped_value;
  209. // c. If mapping is true, then
  210. if (mapfn) {
  211. // i. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
  212. mapped_value = TRY(JS::call(vm, *mapfn, this_arg, k_value, Value(k)));
  213. }
  214. // d. Else, let mappedValue be kValue.
  215. else {
  216. mapped_value = k_value;
  217. }
  218. // e. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
  219. TRY(array->create_data_property_or_throw(property_key, mapped_value));
  220. // f. Set k to k + 1.
  221. }
  222. // 13. Perform ? Set(A, "length", 𝔽(len), true).
  223. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  224. // 14. Return A.
  225. return array;
  226. }
  227. // 2.1.1.1 Array.fromAsync ( asyncItems [ , mapfn [ , thisArg ] ] ), https://tc39.es/proposal-array-from-async/#sec-array.fromAsync
  228. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from_async)
  229. {
  230. auto& realm = *vm.current_realm();
  231. auto async_items = vm.argument(0);
  232. auto mapfn = vm.argument(1);
  233. auto this_arg = vm.argument(2);
  234. // 1. Let C be the this value.
  235. auto constructor = vm.this_value();
  236. // 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  237. auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
  238. // 3. Let fromAsyncClosure be a new Abstract Closure with no parameters that captures C, mapfn, and thisArg and performs the following steps when called:
  239. auto from_async_closure = GC::create_function(realm.heap(), [constructor, mapfn, this_arg, &vm, &realm, async_items]() mutable -> Completion {
  240. bool mapping;
  241. // a. If mapfn is undefined, let mapping be false.
  242. if (mapfn.is_undefined()) {
  243. mapping = false;
  244. }
  245. // b. Else,
  246. else {
  247. // i. If IsCallable(mapfn) is false, throw a TypeError exception.
  248. if (!mapfn.is_function())
  249. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, mapfn.to_string_without_side_effects());
  250. // ii. Let mapping be true.
  251. mapping = true;
  252. }
  253. // c. Let usingAsyncIterator be ? GetMethod(asyncItems, @@asyncIterator).
  254. auto using_async_iterator = TRY(async_items.get_method(vm, vm.well_known_symbol_async_iterator()));
  255. GC::Ptr<FunctionObject> using_sync_iterator;
  256. // d. If usingAsyncIterator is undefined, then
  257. if (!using_async_iterator) {
  258. // i. Let usingSyncIterator be ? GetMethod(asyncItems, @@iterator).
  259. using_sync_iterator = TRY(async_items.get_method(vm, vm.well_known_symbol_iterator()));
  260. }
  261. // e. Let iteratorRecord be undefined.
  262. GC::Ptr<IteratorRecord> iterator_record;
  263. // f. If usingAsyncIterator is not undefined, then
  264. if (using_async_iterator) {
  265. // i. Set iteratorRecord to ? GetIterator(asyncItems, async, usingAsyncIterator).
  266. // FIXME: The Array.from proposal is out of date - it should be using GetIteratorFromMethod.
  267. iterator_record = TRY(get_iterator_from_method(vm, async_items, *using_async_iterator));
  268. }
  269. // g. Else if usingSyncIterator is not undefined, then
  270. else if (using_sync_iterator) {
  271. // i. Set iteratorRecord to ? CreateAsyncFromSyncIterator(GetIterator(asyncItems, sync, usingSyncIterator)).
  272. // FIXME: The Array.from proposal is out of date - it should be using GetIteratorFromMethod.
  273. iterator_record = create_async_from_sync_iterator(vm, TRY(get_iterator_from_method(vm, async_items, *using_sync_iterator)));
  274. }
  275. // h. If iteratorRecord is not undefined, then
  276. if (iterator_record) {
  277. GC::Ptr<Object> array;
  278. // i. If IsConstructor(C) is true, then
  279. if (constructor.is_constructor()) {
  280. // 1. Let A be ? Construct(C).
  281. array = TRY(JS::construct(vm, constructor.as_function()));
  282. }
  283. // ii. Else,
  284. else {
  285. // i. Let A be ! ArrayCreate(0).
  286. array = MUST(Array::create(realm, 0));
  287. }
  288. // iii. Let k be 0.
  289. // iv. Repeat,
  290. for (size_t k = 0;; ++k) {
  291. // 1. If k ≥ 2^53 - 1, then
  292. if (k >= MAX_ARRAY_LIKE_INDEX) {
  293. // a. Let error be ThrowCompletion(a newly created TypeError object).
  294. auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
  295. // b. Return ? AsyncIteratorClose(iteratorRecord, error).
  296. return *TRY(async_iterator_close(vm, *iterator_record, move(error)));
  297. }
  298. // 2. Let Pk be ! ToString(𝔽(k)).
  299. auto property_key = PropertyKey { k };
  300. // FIXME: There seems to be a bug here where we are not respecting array mutation. After resolving the first entry, the
  301. // iterator should also take into account any other changes which are made to async_items (which does not seem to
  302. // be happening).
  303. // 3. Let nextResult be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
  304. auto next_result = TRY(JS::call(vm, iterator_record->next_method, iterator_record->iterator));
  305. // 4. Set nextResult to ? Await(nextResult).
  306. next_result = TRY(await(vm, next_result));
  307. // 5. If nextResult is not an Object, throw a TypeError exception.
  308. if (!next_result.is_object())
  309. return vm.throw_completion<TypeError>(ErrorType::IterableNextBadReturn);
  310. // 6. Let done be ? IteratorComplete(nextResult).
  311. auto done = TRY(JS::iterator_complete(vm, next_result.as_object()));
  312. // 7. If done is true,
  313. if (done) {
  314. // a. Perform ? Set(A, "length", 𝔽(k), true).
  315. TRY(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  316. // b. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  317. return Completion { Completion::Type::Return, array };
  318. }
  319. // 8. Let nextValue be ? IteratorValue(nextResult).
  320. auto next_value = TRY(iterator_value(vm, next_result.as_object()));
  321. Value mapped_value;
  322. // 9. If mapping is true, then
  323. if (mapping) {
  324. // a. Let mappedValue be Call(mapfn, thisArg, « nextValue, 𝔽(k) »).
  325. auto mapped_value_or_error = JS::call(vm, mapfn, this_arg, next_value, Value(k));
  326. // b. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  327. if (mapped_value_or_error.is_error()) {
  328. TRY(async_iterator_close(vm, *iterator_record, mapped_value_or_error));
  329. return mapped_value_or_error;
  330. }
  331. // c. Set mappedValue to Await(mappedValue).
  332. mapped_value_or_error = await(vm, mapped_value_or_error.value());
  333. // d. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  334. if (mapped_value_or_error.is_error()) {
  335. TRY(async_iterator_close(vm, *iterator_record, mapped_value_or_error));
  336. return mapped_value_or_error;
  337. }
  338. mapped_value = mapped_value_or_error.value();
  339. }
  340. // 10. Else, let mappedValue be nextValue.
  341. else {
  342. mapped_value = next_value;
  343. }
  344. // 11. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
  345. auto define_status = array->create_data_property_or_throw(property_key, mapped_value);
  346. // 12. If defineStatus is an abrupt completion, return ? AsyncIteratorClose(iteratorRecord, defineStatus).
  347. if (define_status.is_error())
  348. return *TRY(iterator_close(vm, *iterator_record, define_status.release_error()));
  349. // 13. Set k to k + 1.
  350. }
  351. }
  352. // k. Else,
  353. else {
  354. // i. NOTE: asyncItems is neither an AsyncIterable nor an Iterable so assume it is an array-like object.
  355. // ii. Let arrayLike be ! ToObject(asyncItems).
  356. auto array_like = MUST(async_items.to_object(vm));
  357. // iii. Let len be ? LengthOfArrayLike(arrayLike).
  358. auto length = TRY(length_of_array_like(vm, array_like));
  359. GC::Ptr<Object> array;
  360. // iv. If IsConstructor(C) is true, then
  361. if (constructor.is_constructor()) {
  362. // 1. Let A be ? Construct(C, « 𝔽(len) »).
  363. array = TRY(JS::construct(vm, constructor.as_function(), Value(length)));
  364. }
  365. // v. Else,
  366. else {
  367. // 1. Let A be ? ArrayCreate(len).
  368. array = TRY(Array::create(realm, length));
  369. }
  370. // vi. Let k be 0.
  371. // vii. Repeat, while k < len,
  372. for (size_t k = 0; k < length; ++k) {
  373. // 1. Let Pk be ! ToString(𝔽(k)).
  374. auto property_key = PropertyKey { k };
  375. // 2. Let kValue be ? Get(arrayLike, Pk).
  376. auto k_value = TRY(array_like->get(property_key));
  377. // 3. Set kValue to ? Await(kValue).
  378. k_value = TRY(await(vm, k_value));
  379. Value mapped_value;
  380. // 4. If mapping is true, then
  381. if (mapping) {
  382. // a. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
  383. mapped_value = TRY(JS::call(vm, mapfn, this_arg, k_value, Value(k)));
  384. // b. Set mappedValue to ? Await(mappedValue).
  385. mapped_value = TRY(await(vm, mapped_value));
  386. }
  387. // 5. Else, let mappedValue be kValue.
  388. else {
  389. mapped_value = k_value;
  390. }
  391. // 6. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
  392. TRY(array->create_data_property_or_throw(property_key, mapped_value));
  393. // 7. Set k to k + 1.
  394. }
  395. // viii. Perform ? Set(A, "length", 𝔽(len), true).
  396. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  397. // ix. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  398. return Completion { Completion::Type::Return, array };
  399. }
  400. });
  401. // 4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
  402. async_function_start(vm, promise_capability, *from_async_closure);
  403. // 5. Return promiseCapability.[[Promise]].
  404. return promise_capability->promise();
  405. }
  406. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  407. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  408. {
  409. auto arg = vm.argument(0);
  410. // 1. Return ? IsArray(arg).
  411. return Value(TRY(arg.is_array(vm)));
  412. }
  413. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  414. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  415. {
  416. auto& realm = *vm.current_realm();
  417. // 1. Let len be the number of elements in items.
  418. auto len = vm.argument_count();
  419. // 2. Let lenNumber be 𝔽(len).
  420. auto len_number = Value(len);
  421. // 3. Let C be the this value.
  422. auto constructor = vm.this_value();
  423. GC::Ptr<Object> array;
  424. // 4. If IsConstructor(C) is true, then
  425. if (constructor.is_constructor()) {
  426. // a. Let A be ? Construct(C, « lenNumber »).
  427. array = TRY(JS::construct(vm, constructor.as_function(), Value(vm.argument_count())));
  428. } else {
  429. // a. Let A be ? ArrayCreate(len).
  430. array = TRY(Array::create(realm, len));
  431. }
  432. // 6. Let k be 0.
  433. // 7. Repeat, while k < len,
  434. for (size_t k = 0; k < len; ++k) {
  435. // a. Let kValue be items[k].
  436. auto k_value = vm.argument(k);
  437. // b. Let Pk be ! ToString(𝔽(k)).
  438. auto property_key = PropertyKey { k };
  439. // c. Perform ? CreateDataPropertyOrThrow(A, Pk, kValue).
  440. TRY(array->create_data_property_or_throw(property_key, k_value));
  441. // d. Set k to k + 1.
  442. }
  443. // 8. Perform ? Set(A, "length", lenNumber, true).
  444. TRY(array->set(vm.names.length, len_number, Object::ShouldThrowExceptions::Yes));
  445. // 9. Return A.
  446. return array;
  447. }
  448. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  449. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
  450. {
  451. // 1. Return the this value.
  452. return vm.this_value();
  453. }
  454. }