ArrayConstructor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/IteratorOperations.h>
  18. #include <LibJS/Runtime/PromiseCapability.h>
  19. #include <LibJS/Runtime/PromiseConstructor.h>
  20. #include <LibJS/Runtime/Shape.h>
  21. namespace JS {
  22. ArrayConstructor::ArrayConstructor(Realm& realm)
  23. : NativeFunction(realm.vm().names.Array.as_string(), realm.intrinsics().function_prototype())
  24. {
  25. }
  26. ThrowCompletionOr<void> ArrayConstructor::initialize(Realm& realm)
  27. {
  28. auto& vm = this->vm();
  29. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  30. // 23.1.2.4 Array.prototype, https://tc39.es/ecma262/#sec-array.prototype
  31. define_direct_property(vm.names.prototype, realm.intrinsics().array_prototype(), 0);
  32. u8 attr = Attribute::Writable | Attribute::Configurable;
  33. define_native_function(realm, vm.names.from, from, 1, attr);
  34. define_native_function(realm, vm.names.fromAsync, from_async, 1, attr);
  35. define_native_function(realm, vm.names.isArray, is_array, 1, attr);
  36. define_native_function(realm, vm.names.of, of, 0, attr);
  37. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  38. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  39. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  40. return {};
  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, TRY_OR_THROW_OOM(vm, 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 ? GetIterator(items, sync, usingIterator).
  143. auto iterator = TRY(get_iterator(vm, items, IteratorHint::Sync, 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, TRY_OR_THROW_OOM(vm, 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. GCPtr<Object> array;
  264. // e. If IsConstructor(C) is true, then
  265. if (constructor.is_constructor()) {
  266. // i. Let A be ? Construct(C).
  267. array = TRY(JS::construct(vm, constructor.as_function()));
  268. }
  269. // f. Else,
  270. else {
  271. // i. Let A be ! ArrayCreate(0).
  272. array = MUST(Array::create(realm, 0));
  273. }
  274. // g. Let iteratorRecord be undefined.
  275. Optional<IteratorRecord> iterator_record;
  276. // h. If usingAsyncIterator is not undefined, then
  277. if (using_async_iterator) {
  278. // i. Set iteratorRecord to ? GetIterator(asyncItems, async, usingAsyncIterator).
  279. iterator_record = TRY(get_iterator(vm, async_items, IteratorHint::Async, using_async_iterator));
  280. }
  281. // i. Else if usingSyncIterator is not undefined, then
  282. else if (using_sync_iterator) {
  283. // i. Set iteratorRecord to ? CreateAsyncFromSyncIterator(GetIterator(asyncItems, sync, usingSyncIterator)).
  284. iterator_record = create_async_from_sync_iterator(vm, TRY(get_iterator(vm, async_items, IteratorHint::Sync, using_sync_iterator)));
  285. }
  286. // j. If iteratorRecord is not undefined, then
  287. if (iterator_record.has_value()) {
  288. // i. Let k be 0.
  289. // ii. 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.value(), move(error)));
  297. }
  298. // 2. Let Pk be ! ToString(𝔽(k)).
  299. auto property_key = PropertyKey { k };
  300. // FIXME: Spec bug: https://github.com/tc39/proposal-array-from-async/issues/33.
  301. // Implementing the following would cause an infinite loop.
  302. //
  303. // 3. Let next be ? Await(IteratorStep(iteratorRecord)).
  304. // 4. If next is false, then
  305. // a. Perform ? Set(A, "length", 𝔽(k), true).
  306. // b. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  307. // 5. Let nextValue be ? IteratorValue(next).
  308. //
  309. // Below implements the suggested fix: https://github.com/tc39/proposal-array-from-async/issues/33#issuecomment-1279296963
  310. //
  311. // There also seems to be a second issue here where we are not respecting array mutation. After resolving the first entry, the
  312. // iterator should also take into account any other changes which are made to async_items (which does not seem to be happening).
  313. // Let nextResult be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
  314. auto next_result = TRY(JS::call(vm, iterator_record->next_method, iterator_record->iterator));
  315. // Set nextResult to ? Await(nextResult).
  316. next_result = TRY(await(vm, next_result));
  317. // If nextResult is not an Object, throw a TypeError exception.
  318. if (!next_result.is_object())
  319. return vm.throw_completion<TypeError>(ErrorType::IterableNextBadReturn);
  320. // Let done be ? IteratorComplete(nextResult).
  321. auto done = TRY(JS::iterator_complete(vm, next_result.as_object()));
  322. // If done is true,
  323. if (done) {
  324. // Perform ? Set(A, "length", 𝔽(k), true).
  325. TRY(array->set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes));
  326. // Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  327. return Completion { Completion::Type::Return, array, {} };
  328. }
  329. // Let nextValue be ? IteratorValue(nextResult).
  330. auto next_value = TRY(iterator_value(vm, next_result.as_object()));
  331. // (spec deviation ends here)
  332. Value mapped_value;
  333. // 6. If mapping is true, then
  334. if (mapping) {
  335. // a. Let mappedValue be Call(mapfn, thisArg, « nextValue, 𝔽(k) »).
  336. auto mapped_value_or_error = JS::call(vm, mapfn, this_arg, next_value, Value(k));
  337. // b. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  338. if (mapped_value_or_error.is_error()) {
  339. TRY(async_iterator_close(vm, iterator_record.value(), mapped_value_or_error));
  340. return mapped_value_or_error;
  341. }
  342. // c. Set mappedValue to Await(mappedValue).
  343. mapped_value_or_error = await(vm, mapped_value_or_error.value());
  344. // d. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
  345. if (mapped_value_or_error.is_error()) {
  346. TRY(async_iterator_close(vm, iterator_record.value(), mapped_value_or_error));
  347. return mapped_value_or_error;
  348. }
  349. mapped_value = mapped_value_or_error.value();
  350. }
  351. // 7. Else, let mappedValue be nextValue.
  352. else {
  353. mapped_value = next_value;
  354. }
  355. // 8. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
  356. auto define_status = array->create_data_property_or_throw(property_key, mapped_value);
  357. // 9. If defineStatus is an abrupt completion, return ? AsyncIteratorClose(iteratorRecord, defineStatus).
  358. if (define_status.is_error())
  359. return *TRY(iterator_close(vm, iterator_record.value(), define_status.release_error()));
  360. // 10. Set k to k + 1.
  361. }
  362. }
  363. // k. Else,
  364. else {
  365. // i. NOTE: asyncItems is neither an AsyncIterable nor an Iterable so assume it is an array-like object.
  366. // ii. Let arrayLike be ! ToObject(asyncItems).
  367. auto array_like = MUST(async_items.to_object(vm));
  368. // iii. Let len be ? LengthOfArrayLike(arrayLike).
  369. auto length = TRY(length_of_array_like(vm, array_like));
  370. // iv. If IsConstructor(C) is true, then
  371. if (constructor.is_constructor()) {
  372. // 1. Let A be ? Construct(C, « 𝔽(len) »).
  373. array = TRY(JS::construct(vm, constructor.as_function(), Value(length)));
  374. }
  375. // v. Else,
  376. else {
  377. // 1. Let A be ? ArrayCreate(len).
  378. array = TRY(Array::create(realm, length));
  379. }
  380. // vi. Let k be 0.
  381. // vii. Repeat, while k < len,
  382. for (size_t k = 0; k < length; ++k) {
  383. // 1. Let Pk be ! ToString(𝔽(k)).
  384. auto property_key = PropertyKey { k };
  385. // 2. Let kValue be ? Get(arrayLike, Pk).
  386. auto k_value = TRY(array_like->get(property_key));
  387. // 3. Set kValue to ? Await(kValue).
  388. k_value = TRY(await(vm, k_value));
  389. Value mapped_value;
  390. // 4. If mapping is true, then
  391. if (mapping) {
  392. // a. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
  393. mapped_value = TRY(JS::call(vm, mapfn, this_arg, k_value, Value(k)));
  394. // b. Set mappedValue to ? Await(mappedValue).
  395. mapped_value = TRY(await(vm, mapped_value));
  396. }
  397. // 5. Else, let mappedValue be kValue.
  398. else {
  399. mapped_value = k_value;
  400. }
  401. // 6. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
  402. TRY(array->create_data_property_or_throw(property_key, mapped_value));
  403. // 7. Set k to k + 1.
  404. }
  405. // viii. Perform ? Set(A, "length", 𝔽(len), true).
  406. TRY(array->set(vm.names.length, Value(length), Object::ShouldThrowExceptions::Yes));
  407. // ix. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
  408. return Completion { Completion::Type::Return, array, {} };
  409. }
  410. };
  411. // 4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
  412. async_function_start(vm, promise_capability, from_async_closure);
  413. // 5. Return promiseCapability.[[Promise]].
  414. return promise_capability->promise();
  415. }
  416. // 23.1.2.2 Array.isArray ( arg ), https://tc39.es/ecma262/#sec-array.isarray
  417. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
  418. {
  419. auto arg = vm.argument(0);
  420. // 1. Return ? IsArray(arg).
  421. return Value(TRY(arg.is_array(vm)));
  422. }
  423. // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of
  424. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
  425. {
  426. auto& realm = *vm.current_realm();
  427. // 1. Let len be the number of elements in items.
  428. auto len = vm.argument_count();
  429. // 2. Let lenNumber be 𝔽(len).
  430. auto len_number = Value(len);
  431. // 3. Let C be the this value.
  432. auto constructor = vm.this_value();
  433. GCPtr<Object> array;
  434. // 4. If IsConstructor(C) is true, then
  435. if (constructor.is_constructor()) {
  436. // a. Let A be ? Construct(C, « lenNumber »).
  437. array = TRY(JS::construct(vm, constructor.as_function(), Value(vm.argument_count())));
  438. } else {
  439. // a. Let A be ? ArrayCreate(len).
  440. array = TRY(Array::create(realm, len));
  441. }
  442. // 6. Let k be 0.
  443. // 7. Repeat, while k < len,
  444. for (size_t k = 0; k < len; ++k) {
  445. // a. Let kValue be items[k].
  446. auto k_value = vm.argument(k);
  447. // b. Let Pk be ! ToString(𝔽(k)).
  448. auto property_key = PropertyKey { k };
  449. // c. Perform ? CreateDataPropertyOrThrow(A, Pk, kValue).
  450. TRY(array->create_data_property_or_throw(property_key, k_value));
  451. // d. Set k to k + 1.
  452. }
  453. // 8. Perform ? Set(A, "length", lenNumber, true).
  454. TRY(array->set(vm.names.length, len_number, Object::ShouldThrowExceptions::Yes));
  455. // 9. Return A.
  456. return array;
  457. }
  458. // 23.1.2.5 get Array [ @@species ], https://tc39.es/ecma262/#sec-get-array-@@species
  459. JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::symbol_species_getter)
  460. {
  461. // 1. Return the this value.
  462. return vm.this_value();
  463. }
  464. }