|
1 | 1 | import { execFileSync } from "child_process"; |
2 | 2 | import path from "path"; |
3 | 3 |
|
4 | | -// The optimization helpers reach V8 natives syntax, so their behaviour only shows |
5 | | -// up in a process started with --allow-natives-syntax. A function optimized |
6 | | -// without being marked for manual optimization first aborts the process on |
7 | | -// V8 >= 14.6, which no in-process assertion can observe. |
| 4 | +// A function optimized without being marked for manual optimization first aborts |
| 5 | +// the process on V8 >= 14.6, which no in-process assertion can observe. The helpers |
| 6 | +// are exercised through the built package so the bundled natives calls are covered. |
8 | 7 | const runWithNatives = (snippet: string): string => |
9 | 8 | execFileSync(process.execPath, ["--allow-natives-syntax", "-e", snippet], { |
10 | 9 | encoding: "utf8", |
11 | 10 | }); |
12 | 11 |
|
13 | 12 | const corePath = JSON.stringify(path.join(__dirname, "..")); |
14 | 13 |
|
| 14 | +// %GetOptimizationStatus's bitmask layout shifts between V8 versions, so a |
| 15 | +// specific bit isn't safe to assert on. Comparing the status before and after |
| 16 | +// the optimize call instead proves TurboFan recompiled the function, without |
| 17 | +// depending on what any bit means. |
15 | 18 | describe("optimization helpers", () => { |
16 | | - it("should optimize a sync function", () => { |
| 19 | + it("should optimize a sync function without aborting", () => { |
17 | 20 | const stdout = runWithNatives( |
18 | | - `require(${corePath}).optimizeFunctionSync(() => 1); |
19 | | - console.log("done");`, |
| 21 | + `const target = () => 1; |
| 22 | + const before = %GetOptimizationStatus(target); |
| 23 | + require(${corePath}).optimizeFunctionSync(target); |
| 24 | + console.log("done", before, %GetOptimizationStatus(target));`, |
20 | 25 | ); |
| 26 | + const [, before, after] = stdout.match(/done (\d+) (\d+)/) ?? []; |
21 | 27 | expect(stdout).toContain("done"); |
| 28 | + expect(after).not.toEqual(before); |
22 | 29 | }); |
23 | 30 |
|
24 | | - it("should optimize an async function", () => { |
| 31 | + it("should optimize an async function without aborting", () => { |
25 | 32 | const stdout = runWithNatives( |
26 | | - `require(${corePath}) |
27 | | - .optimizeFunction(async () => 1) |
28 | | - .then(() => console.log("done"));`, |
| 33 | + `const target = async () => 1; |
| 34 | + const before = %GetOptimizationStatus(target); |
| 35 | + require(${corePath}) |
| 36 | + .optimizeFunction(target) |
| 37 | + .then(() => console.log("done", before, %GetOptimizationStatus(target)));`, |
29 | 38 | ); |
| 39 | + const [, before, after] = stdout.match(/done (\d+) (\d+)/) ?? []; |
30 | 40 | expect(stdout).toContain("done"); |
| 41 | + expect(after).not.toEqual(before); |
31 | 42 | }); |
32 | 43 | }); |
0 commit comments