mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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>
This commit is contained in:
1 parent
2033a9f436
commit
0993fbe5b2
16 files changed
+1281
-35
No files matched your search
@@ -0,0 +1,54 @@
|
||||
'use strict';
|
||||
|
||||
// Flags: --experimental-vm-modules
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { Module, createContext } = require('vm');
|
||||
|
||||
common.crashOnUnhandledRejection();
|
||||
|
||||
(async function test1() {
|
||||
const context = createContext({
|
||||
foo: 'bar',
|
||||
baz: undefined,
|
||||
typeofProcess: undefined,
|
||||
});
|
||||
const m = new Module(
|
||||
'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 Module(
|
||||
'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 Module('while (true) {}');
|
||||
await m.link(common.mustNotCall());
|
||||
m.instantiate();
|
||||
await m.evaluate({ timeout: 500 })
|
||||
.then(() => assert(false), () => {});
|
||||
})();
|
||||
Reference in new issue
Block a user