node/test/parallel/test-vm-module-basic.js
Gus Caplan 2bdcdfc3cb
vm: rename vm.Module to vm.SourceTextModule
At the last TC39 meeting, a new type of Module Records backed by
JavaScript source called Dynamic Module Records was discussed, and
it is now at Stage 1. Regardless of whether that proposal makes it
all the way into the spec, SourceTextModule is indeed a more
descriptive and accurate name for what this class represents.

PR-URL: https://github.com/nodejs/node/pull/22007
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
2018-07-31 10:23:03 -05:00

53 lines
1.4 KiB
JavaScript

'use strict';
// Flags: --experimental-vm-modules
const common = require('../common');
const assert = require('assert');
const { SourceTextModule, createContext } = require('vm');
(async function test1() {
const context = createContext({
foo: 'bar',
baz: undefined,
typeofProcess: undefined,
});
const m = new SourceTextModule(
'baz = foo; typeofProcess = typeof process; typeof Object;',
{ context }
);
assert.strictEqual(m.status, 'uninstantiated');
await m.link(common.mustNotCall());
m.instantiate();
assert.strictEqual(m.status, 'instantiated');
const result = await m.evaluate();
assert.strictEqual(m.status, 'evaluated');
assert.strictEqual(Object.getPrototypeOf(result), null);
assert.deepStrictEqual(context, {
foo: 'bar',
baz: 'bar',
typeofProcess: 'undefined'
});
assert.strictEqual(result.result, 'function');
}());
(async () => {
const m = new SourceTextModule(
'global.vmResult = "foo"; Object.prototype.toString.call(process);'
);
await m.link(common.mustNotCall());
m.instantiate();
const { result } = await m.evaluate();
assert.strictEqual(global.vmResult, 'foo');
assert.strictEqual(result, '[object process]');
delete global.vmResult;
})();
(async () => {
const m = new SourceTextModule('while (true) {}');
await m.link(common.mustNotCall());
m.instantiate();
await m.evaluate({ timeout: 500 })
.then(() => assert(false), () => {});
})();