Bug 1588861 - Part 5: Tests for xdr modules. r=iain

Differential Revision: https://phabricator.services.mozilla.com/D55597

--HG--
extra : moz-landing-system : lando
This commit is contained in:
caroline 2019-12-10 02:34:49 +00:00
Родитель e23b3578db
Коммит c68dbd86f2
4 изменённых файлов: 158 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,20 @@
load(libdir + "dummyModuleResolveHook.js");
const count = 10;
let s = "";
for (let i = 0; i < count; i++)
s += "export let e" + i + " = " + (i * i) + ";\n";
let og = parseModule(s);
let bc = codeModule(og);
let a = moduleRepo['a'] = decodeModule(bc);
og = parseModule("import * as ns from 'a'");
bc = codeModule(og);
let b = moduleRepo['b'] = decodeModule(bc);
b.declarationInstantiation();
b.evaluation();
let ns = a.namespace;
for (let i = 0; i < count; i++)
assertEq(ns["e" + i], i * i);

Просмотреть файл

@ -0,0 +1,28 @@
load(libdir + "dummyModuleResolveHook.js");
const count = 10;
let og = parseModule("export let a = 1;");
let bc = codeModule(og);
let a = moduleRepo['a'] = decodeModule(bc);
let s = "";
for (let i = 0; i < count; i++) {
s += "import { a as i" + i + " } from 'a';\n";
s += "assertEq(i" + i + ", 1);\n";
}
og = parseModule(s);
bc = codeModule(og);
let b = moduleRepo['b'] = decodeModule(bc);
b.declarationInstantiation();
b.evaluation();
og = parseModule("import * as nsa from 'a'; import * as nsb from 'b';");
bc = codeModule(og);
let m = decodeModule(bc);
m.declarationInstantiation();
m.evaluation();

Просмотреть файл

@ -0,0 +1,28 @@
// |jit-test| skip-if: !('oomTest' in this)
// OOM tests for xdr module parsing.
load(libdir + "dummyModuleResolveHook.js");
const sa =
`export default 20;
export let a = 22;
export function f(x, y) { return x + y }
`;
const sb =
`import x from "a";
import { a as y } from "a";
import * as ns from "a";
ns.f(x, y);
`;
oomTest(() => {
let og = parseModule(sa);
let bc = codeModule(og);
let a = moduleRepo['a'] = decodeModule(bc);
og = parseModule(sb);
bc = codeModule(og);
let b = moduleRepo['b'] = decodeModule(bc);
});

Просмотреть файл

@ -0,0 +1,82 @@
load(libdir + "asserts.js");
load(libdir + "dummyModuleResolveHook.js");
function parseAndEvaluate(source) {
let og = parseModule(source);
let bc = codeModule(og);
let m = decodeModule(bc);
m.declarationInstantiation();
m.evaluation();
return m;
}
// Check the evaluation of an empty module succeeds.
parseAndEvaluate("");
// Check evaluation returns evaluation result the first time, then undefined.
let og = parseModule("1");
let bc = codeModule(og);
let m = decodeModule(bc);
m.declarationInstantiation();
assertEq(m.evaluation(), undefined);
assertEq(typeof m.evaluation(), "undefined");
// Check top level variables are initialized by evaluation.
og = parseModule("export var x = 2 + 2;");
bc = codeModule(og);
m = decodeModule(bc);
assertEq(typeof getModuleEnvironmentValue(m, "x"), "undefined");
m.declarationInstantiation();
m.evaluation();
assertEq(getModuleEnvironmentValue(m, "x"), 4);
m = parseAndEvaluate("export let x = 2 * 3;");
assertEq(getModuleEnvironmentValue(m, "x"), 6);
// Set up a module to import from.
og = parseModule(`var x = 1;
export { x };
export default 2;
export function f(x) { return x + 1; }`);
bc = codeModule(og);
a = moduleRepo['a'] = decodeModule(bc);
// Check we can evaluate top level definitions.
parseAndEvaluate("var foo = 1;");
parseAndEvaluate("let foo = 1;");
parseAndEvaluate("const foo = 1");
parseAndEvaluate("function foo() {}");
parseAndEvaluate("class foo { constructor() {} }");
// Check we can evaluate all module-related syntax.
parseAndEvaluate("export var foo = 1;");
parseAndEvaluate("export let foo = 1;");
parseAndEvaluate("export const foo = 1;");
parseAndEvaluate("var x = 1; export { x };");
parseAndEvaluate("export default 1");
parseAndEvaluate("export default function() {};");
parseAndEvaluate("export default function foo() {};");
parseAndEvaluate("import a from 'a';");
parseAndEvaluate("import { x } from 'a';");
parseAndEvaluate("import * as ns from 'a';");
parseAndEvaluate("export * from 'a'");
parseAndEvaluate("export default class { constructor() {} };");
parseAndEvaluate("export default class foo { constructor() {} };");
// Test default import
m = parseAndEvaluate("import a from 'a'; export { a };")
assertEq(getModuleEnvironmentValue(m, "a"), 2);
// Test named import
m = parseAndEvaluate("import { x as y } from 'a'; export { y };")
assertEq(getModuleEnvironmentValue(m, "y"), 1);
// Call exported function
m = parseAndEvaluate("import { f } from 'a'; export let x = f(3);")
assertEq(getModuleEnvironmentValue(m, "x"), 4);
// Import access in functions
m = parseAndEvaluate("import { x } from 'a'; function f() { return x; }")
let f = getModuleEnvironmentValue(m, "f");
assertEq(f(), 1);