mirror of
https://github.com/nodejs/node.git
synced 2025-05-21 08:57:07 +00:00

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>
50 lines
998 B
JavaScript
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();
|
|
})();
|