node/test/parallel/test-vm-module-reevaluate.js
Gus Caplan 0993fbe5b2
vm: add modules
Adds vm.Module, which wraps around ModuleWrap to provide an interface
for developers to work with modules in a more reflective manner.

Co-authored-by: Timothy Gu <timothygu99@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/17560
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2018-01-30 17:00:57 -08:00

50 lines
998 B
JavaScript

'use strict';
// Flags: --experimental-vm-modules
const common = require('../common');
common.crashOnUnhandledRejection();
const assert = require('assert');
const { Module } = require('vm');
const finished = common.mustCall();
(async function main() {
{
const m = new Module('1');
await m.link(common.mustNotCall());
m.instantiate();
assert.strictEqual((await m.evaluate()).result, 1);
assert.strictEqual((await m.evaluate()).result, undefined);
assert.strictEqual((await m.evaluate()).result, undefined);
}
{
const m = new Module('throw new Error()');
await m.link(common.mustNotCall());
m.instantiate();
let threw = false;
try {
await m.evaluate();
} catch (err) {
assert(err instanceof Error);
threw = true;
}
assert(threw);
threw = false;
try {
await m.evaluate();
} catch (err) {
assert(err instanceof Error);
threw = true;
}
assert(threw);
}
finished();
})();