| 1 |
// |jit-test| |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
async function parseAndEvaluate(source) { |
| 6 |
let stencil = compileToStencilXDR(source, {module: true}); |
| 7 |
let m = instantiateModuleStencilXDR(stencil); |
| 8 |
moduleLink(m); |
| 9 |
await moduleEvaluate(m); |
| 10 |
return m; |
| 11 |
} |
| 12 |
|
| 13 |
(async () => { |
| 14 |
// Check the evaluation of an empty module succeeds. |
| 15 |
await parseAndEvaluate(""); |
| 16 |
})(); |
| 17 |
|
| 18 |
(async () => { |
| 19 |
// Check that evaluation returns evaluation promise, |
| 20 |
// and promise is always the same. |
| 21 |
let stencil = compileToStencilXDR("1", {module: true}); |
| 22 |
let m = instantiateModuleStencilXDR(stencil); |
| 23 |
moduleLink(m); |
| 24 |
assertEq(typeof moduleEvaluate(m), "object"); |
| 25 |
assertEq(moduleEvaluate(m) instanceof Promise, true); |
| 26 |
assertEq(moduleEvaluate(m), moduleEvaluate(m)); |
| 27 |
})(); |
| 28 |
|
| 29 |
(async () => { |
| 30 |
// Check top level variables are initialized by evaluation. |
| 31 |
let stencil = compileToStencilXDR("export var x = 2 + 2;", {module: true}); |
| 32 |
let m = instantiateModuleStencilXDR(stencil); |
| 33 |
assertEq(typeof getModuleEnvironmentValue(m, "x"), "undefined"); |
| 34 |
moduleLink(m); |
| 35 |
await moduleEvaluate(m); |
| 36 |
assertEq(getModuleEnvironmentValue(m, "x"), 4); |
| 37 |
})(); |
| 38 |
|
| 39 |
(async () => { |
| 40 |
let m = await parseAndEvaluate("export let x = 2 * 3;"); |
| 41 |
assertEq(getModuleEnvironmentValue(m, "x"), 6); |
| 42 |
|
| 43 |
// Set up a module to import from. |
| 44 |
let stencil = compileToStencilXDR( |
| 45 |
`var x = 1; |
| 46 |
export { x }; |
| 47 |
export default 2; |
| 48 |
export function f(x) { return x + 1; }`, |
| 49 |
{module: true}); |
| 50 |
let mod = instantiateModuleStencilXDR(stencil); |
| 51 |
let a = registerModule('a', mod); |
| 52 |
|
| 53 |
// Check we can evaluate top level definitions. |
| 54 |
await parseAndEvaluate("var foo = 1;"); |
| 55 |
await parseAndEvaluate("let foo = 1;"); |
| 56 |
await parseAndEvaluate("const foo = 1"); |
| 57 |
await parseAndEvaluate("function foo() {}"); |
| 58 |
await parseAndEvaluate("class foo { constructor() {} }"); |
| 59 |
|
| 60 |
// Check we can evaluate all module-related syntax. |
| 61 |
await parseAndEvaluate("export var foo = 1;"); |
| 62 |
await parseAndEvaluate("export let foo = 1;"); |
| 63 |
await parseAndEvaluate("export const foo = 1;"); |
| 64 |
await parseAndEvaluate("var x = 1; export { x };"); |
| 65 |
await parseAndEvaluate("export default 1"); |
| 66 |
await parseAndEvaluate("export default function() {};"); |
| 67 |
await parseAndEvaluate("export default function foo() {};"); |
| 68 |
await parseAndEvaluate("import a from 'a';"); |
| 69 |
await parseAndEvaluate("import { x } from 'a';"); |
| 70 |
await parseAndEvaluate("import * as ns from 'a';"); |
| 71 |
await parseAndEvaluate("export * from 'a'"); |
| 72 |
await parseAndEvaluate("export default class { constructor() {} };"); |
| 73 |
await parseAndEvaluate("export default class foo { constructor() {} };"); |
| 74 |
|
| 75 |
// Test default import |
| 76 |
m = await parseAndEvaluate("import a from 'a'; export { a };") |
| 77 |
assertEq(getModuleEnvironmentValue(m, "a"), 2); |
| 78 |
|
| 79 |
// Test named import |
| 80 |
m = await parseAndEvaluate("import { x as y } from 'a'; export { y };") |
| 81 |
assertEq(getModuleEnvironmentValue(m, "y"), 1); |
| 82 |
|
| 83 |
// Call exported function |
| 84 |
m = await parseAndEvaluate("import { f } from 'a'; export let x = f(3);") |
| 85 |
assertEq(getModuleEnvironmentValue(m, "x"), 4); |
| 86 |
|
| 87 |
// Import access in functions |
| 88 |
m = await parseAndEvaluate("import { x } from 'a'; function f() { return x; }") |
| 89 |
let f = getModuleEnvironmentValue(m, "f"); |
| 90 |
assertEq(f(), 1); |
| 91 |
})(); |
| 92 |
drainJobQueue(); |