tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

module-evaluation.js (4026B)


      1 // |jit-test|
      2 // Exercise ModuleEvaluation() concrete method.
      3 
      4 load(libdir + "asserts.js");
      5 
      6 async function parseAndEvaluate(source) {
      7    let m = parseModule(source);
      8    moduleLink(m);
      9    await moduleEvaluate(m);
     10    return m;
     11 }
     12 
     13 // Check the evaluation of an empty module succeeds.
     14 (async () => {
     15  await parseAndEvaluate("");
     16 })();
     17 
     18 (async () => {
     19  // Check that evaluation returns evaluation promise,
     20  // and promise is always the same.
     21  let m = parseModule("1");
     22  moduleLink(m);
     23  assertEq(typeof moduleEvaluate(m), "object");
     24  assertEq(moduleEvaluate(m) instanceof Promise, true);
     25  assertEq(moduleEvaluate(m), moduleEvaluate(m));
     26  await moduleEvaluate(m);
     27 })();
     28 
     29 (async () => {
     30  // Check top level variables are initialized by evaluation.
     31  let m = parseModule("export var x = 2 + 2;");
     32  assertEq(typeof getModuleEnvironmentValue(m, "x"), "undefined");
     33  moduleLink(m);
     34  await moduleEvaluate(m);
     35  assertEq(getModuleEnvironmentValue(m, "x"), 4);
     36 })();
     37 
     38 (async () => {
     39  let m = parseModule("export let x = 2 * 3;");
     40  moduleLink(m);
     41  await moduleEvaluate(m);
     42  assertEq(getModuleEnvironmentValue(m, "x"), 6);
     43 })();
     44 
     45 // Set up a module to import from.
     46 let a = registerModule('a',
     47    parseModule(`var x = 1;
     48                 export { x };
     49                 export default 2;
     50                 export function f(x) { return x + 1; }`));
     51 
     52 (async () => {
     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 
     76 (async () => {
     77  // Test default import
     78  let m = parseModule("import a from 'a'; export { a };")
     79  moduleLink(m);
     80  await moduleEvaluate(m)
     81  assertEq(getModuleEnvironmentValue(m, "a"), 2);
     82 })();
     83 
     84 (async () => {
     85  // Test named import
     86  let m = parseModule("import { x as y } from 'a'; export { y };")
     87  moduleLink(m);
     88  await moduleEvaluate(m);
     89  assertEq(getModuleEnvironmentValue(m, "y"), 1);
     90 })();
     91 
     92 (async () => {
     93  // Call exported function
     94  let m = parseModule("import { f } from 'a'; export let x = f(3);")
     95  moduleLink(m);
     96  await moduleEvaluate(m);
     97  assertEq(getModuleEnvironmentValue(m, "x"), 4);
     98 })();
     99 
    100 (async () => {
    101  // Test importing an indirect export
    102  registerModule('b', parseModule("export { x as z } from 'a';"));
    103  let m = await parseAndEvaluate("import { z } from 'b'; export { z }");
    104  assertEq(getModuleEnvironmentValue(m, "z"), 1);
    105 })();
    106 
    107 (async () => {
    108  // Test cyclic dependencies
    109  registerModule('c1', parseModule("export var x = 1; export {y} from 'c2'"));
    110  registerModule('c2', parseModule("export var y = 2; export {x} from 'c1'"));
    111  let m = await parseAndEvaluate(`import { x as x1, y as y1 } from 'c1';
    112                        import { x as x2, y as y2 } from 'c2';
    113                        export let z = [x1, y1, x2, y2]`);
    114  assertDeepEq(getModuleEnvironmentValue(m, "z"), [1, 2, 1, 2]);
    115 })();
    116 
    117 (async () => {
    118  // Import access in functions
    119  let m = await parseModule("import { x } from 'a'; function f() { return x; }")
    120  moduleLink(m);
    121  moduleEvaluate(m);
    122  let f = getModuleEnvironmentValue(m, "f");
    123  assertEq(f(), 1);
    124 })();
    125 drainJobQueue();