ArrayConstructor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.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. JS_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<NonnullGCPtr<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. GCPtr<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. GCPtr<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 ? IteratorStep(iteratorRecord).
  157. auto next = TRY(iterator_step(vm, iterator));
  158. // iv. If next is false, then
  159. if (!next) {
  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. // v. Let nextValue be ? IteratorValue(next).
  166. auto next_value = TRY(iterator_value(vm, *next));
  167. Value mapped_value;
  168. // vi. If mapping is true, then
  169. if (mapfn) {
  170. // 1. Let mappedValue be Completion(Call(mapfn, thisArg, « nextValue, 𝔽(k) »)).
  171. auto mapped_value_or_error = JS::call(vm, *mapfn, this_arg, next_value, Value(k));
  172. // 2. IfAbruptCloseIterator(mappedValue, iteratorRecord).
  173. if (mapped_value_or_error.is_error())
  174. return *TRY(iterator_close(vm, iterator, mapped_value_or_error.release_error()));
  175. mapped_value = mapped_value_or_error.release_value();
  176. }
  177. // vii. Else, let mappedValue be nextValue.
  178. else {
  179. mapped_value = next_value;
  180. }
  181. // viii. Let defineStatus be Completion(CreateDataPropertyOrThrow(A, Pk, mappedValue)).
  182. auto result_or_error = array->create_data_property_or_throw(property_key, mapped_value);
  183. // IfAbruptCloseIterator(defineStatus, iteratorRecord).
  184. if (result_or_error.is_error())
  185. return *TRY(iterator_close(vm, iterator, result_or_error.release_error()));
  186. // x. Set k to k + 1.
  187. }
  188. }
  189. // 6. NOTE: items is not an Iterable so assume it is an array-like object.
  190. // 7. Let arrayLike be ! ToObject(items).
  191. auto array_like = MUST(items.to_object(vm));
  192. // 8. Let len be ? LengthOfArrayLike(arrayLike).
  193. auto length = TRY(length_of_array_like(vm, array_like));
  194. GCPtr<Object> array;
  195. // 9. If IsConstructor(C) is true, then
  196. if (constructor.is_constructor()) {
  197. // a. Let A be ? Construct(C, « 𝔽(len) »).
  198. array = TRY(JS::construct(vm, constructor.as_function(), Value(length)));
  199. } else {
  200. // a. Let A be ? ArrayCreate(len).
  201. array = TRY(Array::create(realm, length));
  202. }
  203. // 11. Let k be 0.
  204. // 12. Repeat, while k < len,
  205. for (size_t k = 0; k < length; ++k) {
  206. // a. Let Pk be ! ToString(𝔽(k)).
  207. auto property_key = PropertyKey { k };
  208. // b. Let kValue be ? Get(arrayLike, Pk).
  209. auto k_value = TRY(array_like->get(property_key));
  210. Value mapped_value;
  211. // c. If mapping is true, then
  212. if (mapfn) {
  213. // i. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
  214. mapped_value = TRY(JS::call(vm, *mapfn, this_arg, k_value, Value(k)));
  215. }
  216. // d. Else, let mappedValue be kValue.
  217. else {
  218. mapped_value = k_value;
  219. }
  220. // e. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
  221. TRY(array->create_data_property_or_throw(property_key, mapped_value));
  222. // f. Set k to k + 1.
  223. }
  224. // 13. Perform ? Set(A, "length", 𝔽(len), true).
  225. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  226. // 14. Return A.
  227. return array;
  228. }
  229. // 2.1.1.1 Array.fromAsync ( asyncItems [ , mapfn [ , thisArg ] ] ), https://tc39.es/proposal-array-from-async/#sec-array.fromAsync
  230. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from_async)
  231. {
  232. auto& realm = *vm.current_realm();
  233. auto async_items = vm.argument(0);
  234. auto mapfn = vm.argument(1);
  235. auto this_arg = vm.argument(2);
  236. // 1. Let C be the this value.
  237. auto constructor = vm.this_value();
  238. // 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  239. auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
  240. // 3. Let fromAsyncClosure be a new Abstract Closure with no parameters that captures C, mapfn, and thisArg and performs the following steps when called:
  241. SafeFunction<Completion()> from_async_closure = [constructor, mapfn, this_arg, &vm, &realm, async_items]() mutable -> Completion {
  242. bool mapping;
  243. // a. If mapfn is undefined, let mapping be false.
  244. if (mapfn.is_undefined()) {
  245. mapping = false;
  246. }
  247. // b. Else,
  248. else {
  249. // i. If IsCallable(mapfn) is false, throw a TypeError exception.
  250. if (!mapfn.is_function())
  251. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, mapfn.to_string_without_side_effects());
  252. // ii. Let mapping be true.
  253. mapping = true;
  254. }
  255. // c. Let usingAsyncIterator be ? GetMethod(asyncItems, @@asyncIterator).
  256. auto using_async_iterator = TRY(async_items.get_method(vm, vm.well_known_symbol_async_iterator()));
  257. GCPtr<FunctionObject> using_sync_iterator;
  258. // d. If usingAsyncIterator is undefined, then
  259. if (!using_async_iterator) {
  260. // i. Let usingSyncIterator be ? GetMethod(asyncItems, @@iterator).
  261. using_sync_iterator = TRY(async_items.get_method(vm, vm.well_known_symbol_iterator()));
  262. }
  263. // e. Let iteratorRecord be undefined.
  264. GCPtr<IteratorRecord> iterator_record;
  265. // f. If usingAsyncIterator is not undefined, then
  266. if (using_async_iterator) {
  267. // i. Set iteratorRecord to ? GetIterator(asyncItems, async, usingAsyncIterator).
  268. // FIXME: The Array.from proposal is out of date - it should be using GetIteratorFromMethod.
  269. iterator_record = TRY(get_iterator_from_method(vm, async_items, *using_async_iterator));
  270. }
  271. // g. Else if usingSyncIterator is not undefined, then
  272. else if (using_sync_iterator) {
  273. // i. Set iteratorRecord to ? CreateAsyncFromSyncIterator(GetIterator(asyncItems, sync, usingSyncIterator)).
  274. // FIXME: The Array.from proposal is out of date - it should be using GetIteratorFromMethod.
  275. iterator_record = create_async_from_sync_iterator(vm, TRY(get_iterator_from_method(vm, async_items, *using_sync_iterator)));
  276. }
  277. // h. If iteratorRecord is not undefined, then
  278. if (iterator_record) {
  279. GCPtr<Object> array;
  280. // i. If IsConstructor(C) is true, then
  281. if (constructor.is_constructor()) {
  282. // 1. Let A be ? Construct(C).
  283. array = TRY(JS::construct(vm, constructor.as_function()));
  284. }
  285. // ii. Else,
  286. else {
  287. // i. Let A be ! ArrayCreate(0).
  288. array = MUST(Array::create(realm, 0));
  289. }
  290. // iii. Let k be 0.
  291. // iv. Repeat,
  292. for (size_t k = 0;; ++k) {
  293. // 1. If k ≥ 2^53 - 1, then
  294. if (k >= MAX_ARRAY_LIKE_INDEX) {
  295. // a. Let error be ThrowCompletion(a newly created TypeError object).
  296. auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
  297. // b. Return ? AsyncIteratorClose(iteratorRecord, error).
  298. return *TRY(async_iterator_close(vm, *iterator_record, move(error)));
  299. }
  300. // 2. Let Pk be ! ToString(𝔽(k)).
  301. auto property_key = PropertyKey { k };
  302. // FIXME: There seems to be a bug here where we are not respecting array mutation. After resolving the first entry, the
  303. // iterator should also take into account any other changes which are made to async_items (which does not seem to
  304. // be happening).
  305. // 3. Let nextResult be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
  306. auto next_result = TRY(JS::call(vm, iterator_record->next_method, iterator_record->iterator));
  307. // 4. Set nextResult to ? Await(nextResult).
  308. next_result = TRY(await(vm, next_result));
  309. // 5. If nextResult is not an Object, throw a TypeError exception.
  310. if (!next_result.is_object())
  311. return vm.throw_completion<TypeError>(ErrorType::IterableNextBadReturn);
  312. // 6. Let done be ? IteratorComplete(nextResult).
  313. auto done = TRY(JS::iterator_complete(vm, next_result.as_object()));
  314. // 7. If done is true,
  315. if (done) {
  316. // a. Perform ? Set(A, "length", 𝔽(k), true).
  317. TRY(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  318. // b. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  319. return Completion { Completion::Type::Return, array, {} };
  320. }
  321. // 8. Let nextValue be ? IteratorValue(nextResult).
  322. auto next_value = TRY(iterator_value(vm, next_result.as_object()));
  323. Value mapped_value;
  324. // 9. If mapping is true, then
  325. if (mapping) {
  326. // a. Let mappedValue be Call(mapfn, thisArg, « nextValue, 𝔽(k) »).
  327. auto mapped_value_or_error = JS::call(vm, mapfn, this_arg, next_value, Value(k));
  328. // b. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  329. if (mapped_value_or_error.is_error()) {
  330. TRY(async_iterator_close(vm, *iterator_record, mapped_value_or_error));
  331. return mapped_value_or_error;
  332. }
  333. // c. Set mappedValue to Await(mappedValue).
  334. mapped_value_or_error = await(vm, mapped_value_or_error.value());
  335. // d. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  336. if (mapped_value_or_error.is_error()) {
  337. TRY(async_iterator_close(vm, *iterator_record, mapped_value_or_error));
  338. return mapped_value_or_error;
  339. }
  340. mapped_value = mapped_value_or_error.value();
  341. }
  342. // 10. Else, let mappedValue be nextValue.
  343. else {
  344. mapped_value = next_value;
  345. }
  346. // 11. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
  347. auto define_status = array->create_data_property_or_throw(property_key, mapped_value);
  348. // 12. If defineStatus is an abrupt completion, return ? AsyncIteratorClose(iteratorRecord, defineStatus).
  349. if (define_status.is_error())
  350. return *TRY(iterator_close(vm, *iterator_record, define_status.release_error()));
  351. // 13. Set k to k + 1.
  352. }
  353. }
  354. // k. Else,
  355. else {
  356. // i. NOTE: asyncItems is neither an AsyncIterable nor an Iterable so assume it is an array-like object.
  357. // ii. Let arrayLike be ! ToObject(asyncItems).
  358. auto array_like = MUST(async_items.to_object(vm));
  359. // iii. Let len be ? LengthOfArrayLike(arrayLike).
  360. auto length = TRY(length_of_array_like(vm, array_like));
  361. GCPtr<Object> array;
  362. // iv. If IsConstructor(C) is true, then
  363. if (constructor.is_constructor()) {
  364. // 1. Let A be ? Construct(C, « 𝔽(len) »).
  365. array = TRY(JS::construct(vm, constructor.as_function(), Value(length)));
  366. }
  367. // v. Else,
  368. else {
  369. // 1. Let A be ? ArrayCreate(len).
  370. array = TRY(Array::create(realm, length));
  371. }
  372. // vi. Let k be 0.
  373. // vii. Repeat, while k < len,
  374. for (size_t k = 0; k < length; ++k) {
  375. // 1. Let Pk be ! ToString(𝔽(k)).
  376. auto property_key = PropertyKey { k };
  377. // 2. Let kValue be ? Get(arrayLike, Pk).
  378. auto k_value = TRY(array_like->get(property_key));
  379. // 3. Set kValue to ? Await(kValue).
  380. k_value = TRY(await(vm, k_value));
  381. Value mapped_value;
  382. // 4. If mapping is true, then
  383. if (mapping) {
  384. // a. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
  385. mapped_value = TRY(JS::call(vm, mapfn, this_arg, k_value, Value(k)));
  386. // b. Set mappedValue to ? Await(mappedValue).
  387. mapped_value = TRY(await(vm, mapped_value));
  388. }
  389. // 5. Else, let mappedValue be kValue.
  390. else {
  391. mapped_value = k_value;
  392. }
  393. // 6. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
  394. TRY(array->create_data_property_or_throw(property_key, mapped_value));
  395. // 7. Set k to k + 1.
  396. }
  397. // viii. Perform ? Set(A, "length", 𝔽(len), true).
  398. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  399. // ix. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  400. return Completion { Completion::Type::Return, array, {} };
  401. }
  402. };
  403. // 4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
  404. async_function_start(vm, promise_capability, from_async_closure);
  405. // 5. Return promiseCapability.[[Promise]].
  406. return promise_capability->promise();
  407. }
  408. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  409. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  410. {
  411. auto arg = vm.argument(0);
  412. // 1. Return ? IsArray(arg).
  413. return Value(TRY(arg.is_array(vm)));
  414. }
  415. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  416. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  417. {
  418. auto& realm = *vm.current_realm();
  419. // 1. Let len be the number of elements in items.
  420. auto len = vm.argument_count();
  421. // 2. Let lenNumber be 𝔽(len).
  422. auto len_number = Value(len);
  423. // 3. Let C be the this value.
  424. auto constructor = vm.this_value();
  425. GCPtr<Object> array;
  426. // 4. If IsConstructor(C) is true, then
  427. if (constructor.is_constructor()) {
  428. // a. Let A be ? Construct(C, « lenNumber »).
  429. array = TRY(JS::construct(vm, constructor.as_function(), Value(vm.argument_count())));
  430. } else {
  431. // a. Let A be ? ArrayCreate(len).
  432. array = TRY(Array::create(realm, len));
  433. }
  434. // 6. Let k be 0.
  435. // 7. Repeat, while k < len,
  436. for (size_t k = 0; k < len; ++k) {
  437. // a. Let kValue be items[k].
  438. auto k_value = vm.argument(k);
  439. // b. Let Pk be ! ToString(𝔽(k)).
  440. auto property_key = PropertyKey { k };
  441. // c. Perform ? CreateDataPropertyOrThrow(A, Pk, kValue).
  442. TRY(array->create_data_property_or_throw(property_key, k_value));
  443. // d. Set k to k + 1.
  444. }
  445. // 8. Perform ? Set(A, "length", lenNumber, true).
  446. TRY(array->set(vm.names.length, len_number, Object::ShouldThrowExceptions::Yes));
  447. // 9. Return A.
  448. return array;
  449. }
  450. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  451. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
  452. {
  453. // 1. Return the this value.
  454. return vm.this_value();
  455. }
  456. }