Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: add experimental NodeRealm implementation #47855

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2186,3 +2186,28 @@ The externally maintained libraries used by Node.js are:
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

- synchronous-worker, located at lib/internal/vm/localworker.js, is licensed as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated to point to the correct location via the license builder.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you point me at the docs for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there are docs for it, but I think you can change this line to lib/internal/vm/localworker.js where it currently says lib/worker_threads.js, rerun the license builder, and this should be updated.

"""
The MIT License (MIT)

Copyright (c) 2020 Anna Henningsen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
17 changes: 17 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,22 @@ changes:
Specify the `module` of a custom experimental [ECMAScript module loader][].
`module` may be any string accepted as an [`import` specifier][].

### `--experimental-noderealm`
mcollina marked this conversation as resolved.
Show resolved Hide resolved
mcollina marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

Enable experimental support for `vm.NodeRealm`.

### `--no-experimental-noderealm`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually document one or the other, never both IIRC


<!-- YAML
added: REPLACEME
-->

Disable experimental support for `vm.NodeRealm`.

### `--experimental-network-imports`

<!-- YAML
Expand Down Expand Up @@ -2113,6 +2129,7 @@ Node.js options that are allowed are:
* `--experimental-import-meta-resolve`
* `--experimental-json-modules`
* `--experimental-loader`
* `--experimental-noderealm`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think test/parallel/test-process-env-allowed-flags-are-documented.js needs to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What update will be necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is failing in this PR (you can see at the bottom of this view). Looking closer though, I think the issue is that this list needs to be in sort order, and this new flag is in the wrong position. It should go between --experimental-network-imports and --experimental-permission.

* `--experimental-modules`
* `--experimental-network-imports`
* `--experimental-permission`
Expand Down
72 changes: 72 additions & 0 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,78 @@ inside a `vm.Context`, functions passed to them will be added to global queues,
which are shared by all contexts. Therefore, callbacks passed to those functions
are not controllable through the timeout either.

### Class: `NodeRealm`

> Stability: 1 - Experimental. Use `--experimental-noderealm` CLI flag to enable this feature.

<!-- YAML
added: REPLACEME
-->

* Extends: {EventEmitter}

A `NodeRealm` is effectively a Node.js environment that runs within the
same thread.

```mjs
import { NodeRealm } from 'node:vm';
const noderealm = new NodeRealm();
const myAsyncFunction = noderealm.createImport(import.meta.url)('my-module');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be simplified to noderealm.import('my-module')? As in, are we able to infer the module parent (import.meta.url) from within createImport, and if so, is there ever a reason we wouldn’t want to import relative to that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think retrieving a function caller's location reliably is possible.

console.log(await myAsyncFunction());
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think the docs should clarify the difference between this and a ShadowRealm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to understand the differences (and similarities) between this and a worker. Because they look very similar. For example, does a realm have an event loop? Does it share globals? (I'm assuming yes and no?)


#### `new NodeRealm()`

<!-- YAML
added: REPLACEME
-->

#### `noderealm.stop()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### `noderealm.stop()`
#### `nodeRealm.stop()`

And for all following.


<!-- YAML
added: REPLACEME
-->

mcollina marked this conversation as resolved.
Show resolved Hide resolved
* Returns: <Promise>

This will render the inner Node.js instance unusable.
and is generally comparable to running `process.exit()`.

This method returns a promise that will be resolved when all resources
associated with this Node.js instance are released. This promise resolves on
the event loop of the _outer_ Node.js instance.

#### `noderealm.createImport(filename)`

<!-- YAML
added: REPLACEME
-->

* `specifier` {string} A module specifier like './file.js' or 'my-package'

Create a function that can be used for loading
mcollina marked this conversation as resolved.
Show resolved Hide resolved
modules inside the inner Node.js instance.

#### `noderealm.globalThis`

<!-- YAML
added: REPLACEME
-->

* Type: {Object}

Returns a reference to the global object of the inner Node.js instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be clarified whether this value is mutable. e.g. is it possible to localworker.globalThis.foo = 1 and have that value reflected within the local worker.


#### `noderealm.process`

<!-- YAML
added: REPLACEME
-->

* Type: {Object}

Returns a reference to the `process` object of the inner Node.js instance.

[Cyclic Module Record]: https://tc39.es/ecma262/#sec-cyclic-module-records
[ECMAScript Module Loader]: esm.md#modules-ecmascript-modules
[Evaluate() concrete method]: https://tc39.es/ecma262/#sec-moduleevaluation
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function prepareExecution(options) {
setupInspectorHooks();
setupWarningHandler();
setupFetch();
setupNodeRealm();
setupWebCrypto();
setupCustomEvent();
setupCodeCoverage();
Expand Down Expand Up @@ -267,6 +268,16 @@ function setupFetch() {
});
}

function setupNodeRealm() {
// Patch the vm module when --experimental-noderealm is on.
// Please update the comments in vm.js when this block changes.
if (getOptionValue('--experimental-noderealm')) {
const NodeRealm = require('internal/vm/noderealm');
const vm = require('vm');
vm.NodeRealm = NodeRealm;
}
}

// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupWebCrypto() {
Expand Down
125 changes: 125 additions & 0 deletions lib/internal/vm/noderealm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
'use strict';

// NodeRealm was originally a separate module developed by
// Anna Henningsen and published separately on npm as the
// synchronous-worker module under the MIT license. It has been
// incorporated into Node.js with Anna's permission.
// See the LICENSE file for LICENSE and copyright attribution.

const {
Promise,
} = primordials;

const {
NodeRealm: NodeRealmImpl,
} = internalBinding('contextify');

const EventEmitter = require('events');
const { setTimeout } = require('timers');
const { pathToFileURL } = require('url');

let debug = require('internal/util/debuglog').debuglog('noderealm', (fn) => {
debug = fn;
});

class NodeRealm extends EventEmitter {
#handle = undefined;
#process = undefined;
#global = undefined;
#stoppedPromise = undefined;
#loader = undefined;

/**
*/
constructor() {
super();
mcollina marked this conversation as resolved.
Show resolved Hide resolved
this.#handle = new NodeRealmImpl();
this.#handle.onexit = (code) => {
this.stop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wait for the Promise from stop() to settle first?

this.emit('exit', code);
};
try {
this.#handle.start();
this.#handle.load((process, nativeRequire, globalThis) => {
this.#process = process;
this.#global = globalThis;
process.on('uncaughtException', (err) => {
if (process.listenerCount('uncaughtException') === 1) {
// If we are stopping, silence all errors
if (!this.#stoppedPromise) {
this.emit('error', err);
}
process.exit(1);
}
});
});

const req = this.#handle.internalRequire();
this.#loader = req('internal/process/esm_loader').esmLoader;
} catch (err) {
this.#handle.stop();
throw err;
}
}

/**
* @returns {Promise<void>}
*/
async stop() {
// TODO(@mcollina): add support for AbortController, we want to abort this,
// or add a timeout.
return this.#stoppedPromise ??= new Promise((resolve) => {
const tryClosing = () => {
const closed = this.#handle.tryCloseAllHandles();
debug('closed %d handles', closed);
if (closed > 0) {
// This is an active wait for the handles to close.
// We might want to change this in the future to use a callback,
// but at this point it seems like a premature optimization.
// We cannot unref() this because we need to shut this down properly.
// TODO(@mcollina): refactor to use a close callback
setTimeout(tryClosing, 100)
} else {

mcollina marked this conversation as resolved.
Show resolved Hide resolved
this.#handle.stop();
resolve();
}
};

// We use setTimeout instead of setImmediate because it runs in a different
// phase of the event loop. This is important because the immediate queue
// would crash if the environment it refers to has been already closed.
// We cannot unref() this because we need to shut this down properly.
setTimeout(tryClosing, 100)
});
}

get process() {
return this.#process;
}

get globalThis() {
return this.#global;
}

/**
* @param {string|URL} parentURL
*/
createImport(parentURL) {
if (typeof parentURL === 'string') {
if (parentURL.indexOf('file://') === 0) {
parentURL = new URL(parentURL);
} else {
parentURL = pathToFileURL(parentURL);
}
} else if (!(parentURL instanceof URL)) {
throw new Error('createImport() must be called with a string or URL');
}

return (specifiers, importAssertions) => {
return this.#loader.import(specifiers, parentURL, importAssertions || {});
};
}
}

module.exports = NodeRealm;
2 changes: 2 additions & 0 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,5 @@ module.exports = {
// The vm module is patched to include vm.Module, vm.SourceTextModule
// and vm.SyntheticModule in the pre-execution phase when
// --experimental-vm-modules is on.
// The vm module is also patched to include vm.NodeRealm in the
// pre-execution phase when --experimental-noderealm is on.
36 changes: 28 additions & 8 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1019,21 +1019,41 @@ void Environment::CleanupHandles() {

RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);

for (ReqWrapBase* request : req_wrap_queue_)
CleanupHandlesNoUvRun();

while (handle_cleanup_waiting_ != 0 ||
request_waiting_ != 0 ||
!handle_wrap_queue_.IsEmpty()) {
uv_run(event_loop(), UV_RUN_ONCE);
}
}

int Environment::CleanupHandlesNoUvRun() {
{
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
task_queues_async_initialized_ = false;
}

auto count = 0;
mcollina marked this conversation as resolved.
Show resolved Hide resolved

for (ReqWrapBase* request : req_wrap_queue_) {
count++;
request->Cancel();
}

for (HandleWrap* handle : handle_wrap_queue_)
for (HandleWrap* handle : handle_wrap_queue_) {
count++;
handle->Close();
}

for (HandleCleanup& hc : handle_cleanup_queue_)
for (HandleCleanup& hc : handle_cleanup_queue_) {
count++;
hc.cb_(this, hc.handle_, hc.arg_);
}

handle_cleanup_queue_.clear();

while (handle_cleanup_waiting_ != 0 ||
request_waiting_ != 0 ||
!handle_wrap_queue_.IsEmpty()) {
uv_run(event_loop(), UV_RUN_ONCE);
}
return count;
}

void Environment::StartProfilerIdleNotifier() {
Expand Down
4 changes: 4 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ class Environment : public MemoryRetainer {

void RegisterHandleCleanups();
void CleanupHandles();
int CleanupHandlesNoUvRun();
void Exit(ExitCode code);
void ExitEnv(StopFlags::Flags flags);

Expand Down Expand Up @@ -857,6 +858,9 @@ class Environment : public MemoryRetainer {

inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
inline int handle_cleanup_waiting() const {
return handle_cleanup_waiting_;
}

// https://w3c.github.io/hr-time/#dfn-time-origin
inline uint64_t time_origin() {
Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
V(socketaddress_constructor_template, v8::FunctionTemplate) \
V(streambaseentry_ctor_template, v8::FunctionTemplate) \
V(streambaseoutputstream_constructor_template, v8::ObjectTemplate) \
V(noderealm_constructor_template, v8::FunctionTemplate) \
V(streamentry_ctor_template, v8::FunctionTemplate) \
V(streamentry_opaque_ctor_template, v8::FunctionTemplate) \
V(qlogoutputstream_constructor_template, v8::ObjectTemplate) \
Expand Down
Loading