summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/parser/stencil-scope.js
blob: 0dc562147af43475b9859f141337550113a64fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const optionsFull = {
    fileName: "compileToStencil-DATA.js",
    lineNumber: 1,
    eagerDelazificationStrategy: "ParseEverythingEagerly",
};

const optionsLazy = {
    fileName: "compileToStencil-DATA.js",
    lineNumber: 1,
    eagerDelazificationStrategy: "OnDemandOnly",
};

const optionsLazyCache = {
    fileName: "compileToStencil-DATA.js",
    lineNumber: 1,
    eagerDelazificationStrategy: "ConcurrentDepthFirst",
};

const optionsLazyCache2 = {
    fileName: "compileToStencil-DATA.js",
    lineNumber: 1,
    eagerDelazificationStrategy: "ConcurrentLargeFirst",
};

let result = 0;

function testMainThread(script_str) {
    const stencil = compileToStencil(script_str, optionsFull);
    result = evalStencil(stencil, optionsFull);
    assertEq(result, 1);
}

function testMainThreadDelazifyAll(script_str) {
    if (isLcovEnabled()) {
      // Code-coverage implies forceFullParse = true, and as such it cannot be
      // used while testing to incrementally delazify.
      return;
    }
    const stencil = compileToStencil(script_str, optionsLazy);
    result = evalStencil(stencil, optionsLazy);
    assertEq(result, 1);
}

function testMainThreadCacheAll(script_str) {
  if (isLcovEnabled() || helperThreadCount() === 0) {
    // Code-coverage implies forceFullParse = true, and as such it cannot be
    // used while testing to incrementally delazify.
    // Similarly, concurrent delazification requires off-threads processing.
    return;
  }
  const stencil = compileToStencil(script_str, optionsLazyCache);
  result = evalStencil(stencil, optionsLazyCache);
  assertEq(result, 1);
}

function testMainThreadCacheAll2(script_str) {
  if (isLcovEnabled() || helperThreadCount() === 0) {
    // Code-coverage implies forceFullParse = true, and as such it cannot be
    // used while testing to incrementally delazify.
    // Similarly, concurrent delazification requires off-threads processing.
    return;
  }
  const stencil = compileToStencil(script_str, optionsLazyCache2);
  result = evalStencil(stencil, optionsLazyCache2);
  assertEq(result, 1);
}

function testOffThread(script_str) {
    const job = offThreadCompileToStencil(script_str, optionsFull);
    const stencil = finishOffThreadStencil(job);
    result = evalStencil(stencil, optionsFull);
    assertEq(result, 1);
}

// These patches are meant to wrap the inner code given as argument into one
// kind of scope. The freeVars specify one way to retrieve the variable name
// added in this process if any.
const scopeCases = [
    { code: inner => `{ ${inner} }`, freeVars: [] },
    { code: inner => `{ var v = 1; ${inner} }`, freeVars: ["v"] },
    { code: inner => `{ let l = 1; ${inner} }`, freeVars: ["l"] },
    { code: inner => `{ const c = 1; ${inner} }`, freeVars: ["c"] },
    { code: inner => `with ({ p: 1 }) { ${inner} }`, freeVars: ["p"],
      inClass: false },
    { code: inner => `(a => { ${inner} })(1)`, freeVars: ["a"] },
    { code: inner => `function fun(a) { ${inner} }; fun(1)`, freeVars: ["a"],
      inClass: false},
    { code: inner => `try { ${inner} } catch(unused) { }`, freeVars: [] },
    { code: inner => `try { throw 1; } catch(t) { ${inner} }`, freeVars: ["t"] },
    { code: inner => `{ class C { #m = 1; constructor() { ${inner} }}; new C() }`,
      freeVars: ["this.#m"], isClass: true },
];

// This function is used to generate code which mostly exercise the various kind
// of scopes to cover ScopeContext class in CompilationStencil.h
function generateCode(seed) {
    let start = inner => `
      ${inner};
      result
    `;

    let prog = [start];
    let freeVars = ["1"];
    let inClass = false;

    while (seed >= freeVars.length) {
        let index = seed % scopeCases.length;
        seed = (seed / scopeCases.length) | 0;
        let scope = scopeCases[index];
        if (inClass && !(scope.inClass ?? false)) {
            // Skip illegal code (non-strict) or code which might not accept
            // this to work.
            continue;
        }
        inClass ||= scope.isClass ?? false;
        prog.push(scope.code);
        freeVars = freeVars.concat(scope.freeVars);
    }

    let name = freeVars[seed];
    return prog.reduceRight((inner, f) => f(inner), `result = ${name}`);
}

for (let s = 0; s < 3000; s++) {
    let code = generateCode(s);
    // console.log(s, ":", code);
    testMainThread(code);
    testMainThreadDelazifyAll(code);
    testMainThreadCacheAll(code);
    testMainThreadCacheAll2(code);
    if (helperThreadCount() > 0) {
        testOffThread(code);
    }
}