summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/modules/module-evaluation.js
blob: 64b64c51a23f012e9e87a377e72b98c768910b5b (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
// |jit-test| --enable-top-level-await;
// Exercise ModuleEvaluation() concrete method.

load(libdir + "asserts.js");

async function parseAndEvaluate(source) {
    let m = parseModule(source);
    m.declarationInstantiation();
    await m.evaluation();
    return m;
}

// Check the evaluation of an empty module succeeds.
(async () => {
  await parseAndEvaluate("");
})();

(async () => {
  // Check that evaluation returns evaluation promise,
  // and promise is always the same.
  let m = parseModule("1");
  m.declarationInstantiation();
  assertEq(typeof m.evaluation(), "object");
  assertEq(m.evaluation() instanceof Promise, true);
  assertEq(m.evaluation(), m.evaluation());
  await m.evaluation();
})();

(async () => {
  // Check top level variables are initialized by evaluation.
  let m = parseModule("export var x = 2 + 2;");
  assertEq(typeof getModuleEnvironmentValue(m, "x"), "undefined");
  m.declarationInstantiation();
  await m.evaluation();
  assertEq(getModuleEnvironmentValue(m, "x"), 4);
})();

(async () => {
  let m = parseModule("export let x = 2 * 3;");
  m.declarationInstantiation();
  await m.evaluation();
  assertEq(getModuleEnvironmentValue(m, "x"), 6);
})();

// Set up a module to import from.
let a = registerModule('a',
    parseModule(`var x = 1;
                 export { x };
                 export default 2;
                 export function f(x) { return x + 1; }`));

(async () => {
  // Check we can evaluate top level definitions.
  await parseAndEvaluate("var foo = 1;");
  await parseAndEvaluate("let foo = 1;");
  await parseAndEvaluate("const foo = 1");
  await parseAndEvaluate("function foo() {}");
  await parseAndEvaluate("class foo { constructor() {} }");

  // Check we can evaluate all module-related syntax.
  await parseAndEvaluate("export var foo = 1;");
  await parseAndEvaluate("export let foo = 1;");
  await parseAndEvaluate("export const foo = 1;");
  await parseAndEvaluate("var x = 1; export { x };");
  await parseAndEvaluate("export default 1");
  await parseAndEvaluate("export default function() {};");
  await parseAndEvaluate("export default function foo() {};");
  await parseAndEvaluate("import a from 'a';");
  await parseAndEvaluate("import { x } from 'a';");
  await parseAndEvaluate("import * as ns from 'a';");
  await parseAndEvaluate("export * from 'a'");
  await parseAndEvaluate("export default class { constructor() {} };");
  await parseAndEvaluate("export default class foo { constructor() {} };");
})();

(async () => {
  // Test default import
  let m = parseModule("import a from 'a'; export { a };")
  m.declarationInstantiation();
  await m.evaluation()
  assertEq(getModuleEnvironmentValue(m, "a"), 2);
})();

(async () => {
  // Test named import
  let m = parseModule("import { x as y } from 'a'; export { y };")
  m.declarationInstantiation();
  await m.evaluation();
  assertEq(getModuleEnvironmentValue(m, "y"), 1);
})();

(async () => {
  // Call exported function
  let m = parseModule("import { f } from 'a'; export let x = f(3);")
  m.declarationInstantiation();
  await m.evaluation();
  assertEq(getModuleEnvironmentValue(m, "x"), 4);
})();

(async () => {
  // Test importing an indirect export
  registerModule('b', parseModule("export { x as z } from 'a';"));
  let m = await parseAndEvaluate("import { z } from 'b'; export { z }");
  assertEq(getModuleEnvironmentValue(m, "z"), 1);
})();

(async () => {
  // Test cyclic dependencies
  registerModule('c1', parseModule("export var x = 1; export {y} from 'c2'"));
  registerModule('c2', parseModule("export var y = 2; export {x} from 'c1'"));
  let m = await parseAndEvaluate(`import { x as x1, y as y1 } from 'c1';
                        import { x as x2, y as y2 } from 'c2';
                        export let z = [x1, y1, x2, y2]`);
  assertDeepEq(getModuleEnvironmentValue(m, "z"), [1, 2, 1, 2]);
})();

(async () => {
  // Import access in functions
  let m = await parseModule("import { x } from 'a'; function f() { return x; }")
  m.declarationInstantiation();
  m.evaluation();
  let f = getModuleEnvironmentValue(m, "f");
  assertEq(f(), 1);
})();
drainJobQueue();