| 1 |
// |reftest| async |
| 2 |
// Copyright (C) 2023 Igalia, S.L. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-array.fromasync |
| 7 |
description: > |
| 8 |
Order of user-observable operations on a custom this-value and its instances |
| 9 |
includes: [compareArray.js, asyncHelpers.js] |
| 10 |
flags: [async] |
| 11 |
features: [Array.fromAsync] |
| 12 |
---*/ |
| 13 |
|
| 14 |
function formatPropertyName(propertyKey, objectName = "") { |
| 15 |
switch (typeof propertyKey) { |
| 16 |
case "symbol": |
| 17 |
if (Symbol.keyFor(propertyKey) !== undefined) { |
| 18 |
return `${objectName}[Symbol.for('${Symbol.keyFor(propertyKey)}')]`; |
| 19 |
} else if (propertyKey.description.startsWith('Symbol.')) { |
| 20 |
return `${objectName}[${propertyKey.description}]`; |
| 21 |
} else { |
| 22 |
return `${objectName}[Symbol('${propertyKey.description}')]` |
| 23 |
} |
| 24 |
case "string": |
| 25 |
if (propertyKey !== String(Number(propertyKey))) |
| 26 |
return objectName ? `${objectName}.${propertyKey}` : propertyKey; |
| 27 |
// fall through |
| 28 |
default: |
| 29 |
// integer or string integer-index |
| 30 |
return `${objectName}[${propertyKey}]`; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
asyncTest(async function () { |
| 35 |
const expectedCalls = [ |
| 36 |
"construct MyArray", |
| 37 |
"defineProperty A[0]", |
| 38 |
"defineProperty A[1]", |
| 39 |
"set A.length" |
| 40 |
]; |
| 41 |
const actualCalls = []; |
| 42 |
|
| 43 |
function MyArray(...args) { |
| 44 |
actualCalls.push("construct MyArray"); |
| 45 |
return new Proxy(Object.create(null), { |
| 46 |
set(target, key, value) { |
| 47 |
actualCalls.push(`set ${formatPropertyName(key, "A")}`); |
| 48 |
return Reflect.set(target, key, value); |
| 49 |
}, |
| 50 |
defineProperty(target, key, descriptor) { |
| 51 |
actualCalls.push(`defineProperty ${formatPropertyName(key, "A")}`); |
| 52 |
return Reflect.defineProperty(target, key, descriptor); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
let result = await Array.fromAsync.call(MyArray, [1, 2]); |
| 58 |
assert.compareArray(expectedCalls, actualCalls, "order of operations for array argument"); |
| 59 |
|
| 60 |
actualCalls.splice(0); // reset |
| 61 |
|
| 62 |
const expectedCallsForArrayLike = [ |
| 63 |
"construct MyArray", |
| 64 |
"defineProperty A[0]", |
| 65 |
"defineProperty A[1]", |
| 66 |
"set A.length" |
| 67 |
]; |
| 68 |
result = await Array.fromAsync.call(MyArray, { |
| 69 |
length: 2, |
| 70 |
0: 1, |
| 71 |
1: 2 |
| 72 |
}); |
| 73 |
assert.compareArray(expectedCallsForArrayLike, actualCalls, "order of operations for array-like argument"); |
| 74 |
}); |