From 722987e9689229bd8f311c0906f1b7a3ef463f92 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 21 May 2022 12:48:49 +0200 Subject: [PATCH 1/4] doc: add note regarding `%Array.prototype.concat%` in `primordials.md` --- doc/contributing/primordials.md | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/doc/contributing/primordials.md b/doc/contributing/primordials.md index a847852ed7f3e4..d01660e405e9ad 100644 --- a/doc/contributing/primordials.md +++ b/doc/contributing/primordials.md @@ -269,6 +269,51 @@ ReflectApply(func, null, array);
+%Array.prototype.concat% looks up + @@isConcatSpreadable property of the passed + arguments and the this value. + +```js +{ + // Unsafe code example: + // 1. Lookup @@isConcatSpreadable property on `array` (user-mutable if + // user-provided). + // 2. Lookup @@isConcatSpreadable property on `%Array.prototype% + // (user-mutable). + // 2. Lookup @@isConcatSpreadable property on `%Object.prototype% + // (user-mutable). + const array = []; + ArrayPrototypeConcat(array); +} +``` + +```js +// User-land +Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, { + get() { + this.push(5); + return true; + }, +}); + +// Core +const a = [1, 2]; +const b = [3, 4]; +console.log(ArrayPrototypeConcat(a, b)); // [1, 2, 5, 3, 4, 5] +// Safe example concatenating two arrays as a third array object: +const concatArray = [] +ArrayPrototypePush(concatArray, ...new SafeArrayIterator(a), + ...new SafeArrayIterator(b)); +console.log(concatArray) // [1, 2, 3, 4] +// Safe example for concatenating two arrays into the first one: +ArrayPrototypePushApply(a, b); +console.log(a); // [1, 2, 3, 4] +``` + +
+ +
+ %Object.fromEntries% iterate over an array ```js From 7b8e3e171e15fa0ef101d47db9524ffccce50a23 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 21 May 2022 13:04:19 +0200 Subject: [PATCH 2/4] fixup! doc: add note regarding `%Array.prototype.concat%` in `primordials.md` --- doc/contributing/primordials.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/doc/contributing/primordials.md b/doc/contributing/primordials.md index d01660e405e9ad..9e3dee23030935 100644 --- a/doc/contributing/primordials.md +++ b/doc/contributing/primordials.md @@ -297,17 +297,25 @@ Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, { }); // Core -const a = [1, 2]; -const b = [3, 4]; -console.log(ArrayPrototypeConcat(a, b)); // [1, 2, 5, 3, 4, 5] -// Safe example concatenating two arrays as a third array object: -const concatArray = [] -ArrayPrototypePush(concatArray, ...new SafeArrayIterator(a), - ...new SafeArrayIterator(b)); -console.log(concatArray) // [1, 2, 3, 4] -// Safe example for concatenating two arrays into the first one: -ArrayPrototypePushApply(a, b); -console.log(a); // [1, 2, 3, 4] +{ + const a = [1, 2]; + const b = [3, 4]; + console.log(ArrayPrototypeConcat(a, b)); // [1, 2, 5, 3, 4, 5] +} +{ + const a = [1, 2]; + const b = [3, 4]; + // Using @Array.prototype.push% and `SafeArrayIterator` to get the expected + // outcome: + const concatArray = []; + ArrayPrototypePush(concatArray, ...new SafeArrayIterator(a), + ...new SafeArrayIterator(b)); + console.log(concatArray); // [1, 2, 3, 4] + + // Or using `ArrayPrototypePushApply` is it's OK to mutate the first array: + ArrayPrototypePushApply(a, b); + console.log(a); // [1, 2, 3, 4] +} ```
From f52c172ce66ae028c83315d4e5d79e9db21c8f76 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 22 May 2022 09:40:50 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Livia Medeiros <74449973+LiviaMedeiros@users.noreply.github.com> --- doc/contributing/primordials.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/contributing/primordials.md b/doc/contributing/primordials.md index 9e3dee23030935..2e323eb8f7349a 100644 --- a/doc/contributing/primordials.md +++ b/doc/contributing/primordials.md @@ -305,14 +305,14 @@ Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, { { const a = [1, 2]; const b = [3, 4]; - // Using @Array.prototype.push% and `SafeArrayIterator` to get the expected + // Using %Array.prototype.push% and `SafeArrayIterator` to get the expected // outcome: const concatArray = []; ArrayPrototypePush(concatArray, ...new SafeArrayIterator(a), ...new SafeArrayIterator(b)); console.log(concatArray); // [1, 2, 3, 4] - // Or using `ArrayPrototypePushApply` is it's OK to mutate the first array: + // Or using `ArrayPrototypePushApply` if it's OK to mutate the first array: ArrayPrototypePushApply(a, b); console.log(a); // [1, 2, 3, 4] } From b847a0fc4e04b5bbda78e1759c72c9b125a9a8c9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 22 May 2022 17:37:39 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Darshan Sen --- doc/contributing/primordials.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/contributing/primordials.md b/doc/contributing/primordials.md index 2e323eb8f7349a..9a081418981808 100644 --- a/doc/contributing/primordials.md +++ b/doc/contributing/primordials.md @@ -298,11 +298,13 @@ Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, { // Core { + // Using ArrayPrototypeConcat does not produce the expected result: const a = [1, 2]; const b = [3, 4]; console.log(ArrayPrototypeConcat(a, b)); // [1, 2, 5, 3, 4, 5] } { + // Concatenating two arrays can be achieved safely, e.g.: const a = [1, 2]; const b = [3, 4]; // Using %Array.prototype.push% and `SafeArrayIterator` to get the expected