|
| 1 | +// Gap: a spread method call whose RECEIVER is an imported binding (#7191). |
| 2 | +// |
| 3 | +// `arr.map(...[cb])` where `arr` comes from another module was wrong four ways |
| 4 | +// out of four — and two of them were silently wrong rather than throwing, which |
| 5 | +// is why it survived: `arr.slice(...[1,3])` returned the whole array and |
| 6 | +// `nums.includes(...[20])` returned false, because the spread argument list |
| 7 | +// arrived as a single array sitting in the builtin's first slot. |
| 8 | +// |
| 9 | +// The cause is that `Expr::ExternFuncRef` is not one shape. It is both an |
| 10 | +// imported FUNCTION — where a method-apply dispatch would be wrong, which is |
| 11 | +// why the generic `CallSpread` tail skips it — and an imported VALUE, where |
| 12 | +// method-apply is exactly what is needed. The skip did not distinguish them, so |
| 13 | +// a spread call on any imported receiver never dispatched the receiver's |
| 14 | +// method. `ctx.imported_vars` already records which imports are variables |
| 15 | +// rather than functions. |
| 16 | +// |
| 17 | +// `test_gap_6718_spread_call_native_method` covers inline literals, local |
| 18 | +// receivers and `queueMicrotask` — not imported receivers — so this shape had |
| 19 | +// never been under test. Both halves are asserted here: the imported-value |
| 20 | +// receivers that were broken, and the imported-function/class/object receivers |
| 21 | +// the skip exists to protect, which must keep working. |
| 22 | + |
| 23 | +import { arr, nums, fn, obj, K } from "./spread_call_imported_receiver_7191_helper.ts"; |
| 24 | + |
| 25 | +const cb = (x: number) => x * 2; |
| 26 | + |
| 27 | +// The four cases from the report, in their original order. |
| 28 | +console.log(JSON.stringify(arr.map(...([cb] as [any])))); |
| 29 | +console.log(JSON.stringify(arr.slice(...([1, 3] as [any, any])))); |
| 30 | +console.log(JSON.stringify(nums.join(...(["-"] as [any])))); |
| 31 | +console.log(JSON.stringify(nums.includes(...([20] as [any])))); |
| 32 | + |
| 33 | +// More imported-array receivers, including one whose spread arg is itself an |
| 34 | +// array (so "the args became one array" cannot pass by accident). |
| 35 | +console.log("indexOf:", arr.indexOf(...([2] as [any]))); |
| 36 | +console.log("concat:", JSON.stringify(arr.concat(...([[9]] as [any])))); |
| 37 | +console.log("at:", nums.at(...([-1] as [any]))); |
| 38 | + |
| 39 | +// The shapes the skip protects: an imported FUNCTION reached through |
| 40 | +// `.call` / `.apply`, and a direct spread call on it. |
| 41 | +console.log("fn.call:", fn.call(...([null, 1, 2] as [any, any, any]))); |
| 42 | +console.log("fn.apply:", fn.apply(...([null, [3, 4]] as [any, any]))); |
| 43 | +console.log("fn direct:", fn(...([5, 6] as [any, any]))); |
| 44 | + |
| 45 | +// An imported object's method, and an imported class's static and instance |
| 46 | +// methods — all reached with a spread argument list. |
| 47 | +console.log("obj.m:", obj.m(...([2] as [any]))); |
| 48 | +console.log("K.s:", K.s(...([1] as [any]))); |
| 49 | +console.log("K#m:", new K().m(...([10] as [any]))); |
| 50 | + |
| 51 | +// A local receiver must be unaffected by any of this. |
| 52 | +const local = [3, 1, 2]; |
| 53 | +console.log("local-map:", JSON.stringify(local.map(...([cb] as [any])))); |
| 54 | +console.log("local-slice:", JSON.stringify(local.slice(...([1, 3] as [any, any])))); |
0 commit comments