4bd29601f0
Currently realms are supported on `deno_core`, but there was no support for async ops anywhere other than the main realm. The main issue is that the `js_recv_cb` callback, which resolves promises corresponding to async ops, was only set for the main realm, so async ops in other realms would never resolve. Furthermore, promise ID's are specific to each realm, which meant that async ops from other realms would result in a wrong promise from the main realm being resolved. This change takes the `ContextState` struct added in #17050, and adds to it a `js_recv_cb` callback for each realm. Combined with the fact that that same PR also added a list of known realms to `JsRuntimeState`, and that #17174 made `OpCtx` instances realm-specific and had them include an index into that list of known realms, this makes it possible to know the current realm in the `queue_async_op` and `queue_fast_async_op` methods, and therefore to send the results of promises for each realm to that realm, and prevent the ID's from getting mixed up. Additionally, since promise ID's are no longer unique to the isolate, having a single set of unrefed ops doesn't work. This change therefore also moves `unrefed_ops` from `JsRuntimeState` to `ContextState`, and adds the lengths of the unrefed op sets for all known realms to get the total number of unrefed ops to compare in the event loop. This PR is a reland of #14734 after it was reverted in #16366, except that `ContextState` and `JsRuntimeState::known_realms` were previously relanded in #17050. Another significant difference with the original PR is passing around an index into `JsRuntimeState::known_realms` instead of a `v8::Global<v8::Context>` to identify the realm, because async op queuing in fast calls cannot call into V8, and therefore cannot have access to V8 globals. This also simplified the implementation of `resolve_async_ops`. Co-authored-by: Luis Malheiro <luismalheiro@gmail.com> |
||
---|---|---|
.. | ||
examples | ||
00_primordials.js | ||
01_core.js | ||
02_error.js | ||
async_cancel.rs | ||
async_cell.rs | ||
bindings.rs | ||
Cargo.toml | ||
encode_decode_test.js | ||
error.rs | ||
error_builder_test.js | ||
error_codes.rs | ||
extensions.rs | ||
flags.rs | ||
gotham_state.rs | ||
icudtl.dat | ||
inspector.rs | ||
internal.d.ts | ||
io.rs | ||
lib.deno_core.d.ts | ||
lib.rs | ||
module_specifier.rs | ||
modules.rs | ||
normalize_path.rs | ||
ops.rs | ||
ops_builtin.rs | ||
ops_builtin_v8.rs | ||
ops_metrics.rs | ||
README.md | ||
resources.rs | ||
runtime.rs | ||
serialize_deserialize_test.js | ||
snapshot_util.rs | ||
source_map.rs |
Deno Core Crate
The main dependency of this crate is rusty_v8, which provides the V8-Rust bindings.
This Rust crate contains the essential V8 bindings for Deno's command-line interface (Deno CLI). The main abstraction here is the JsRuntime which provides a way to execute JavaScript.
The JsRuntime implements an event loop abstraction for the executed code that
keeps track of all pending tasks (async ops, dynamic module loads). It is user's
responsibility to drive that loop by using JsRuntime::run_event_loop
method -
it must be executed in the context of Rust's future executor (eg. tokio, smol).
Rust functions can be registered in JavaScript using deno_core::Extension
. Use
the Deno.core.ops.op_name()
and Deno.core.opAsync("op_name", ...)
functions
to trigger the op function callback. A conventional way to write ops is using
the deno_ops
crate.
Documentation for this crate is thin at the moment. Please see hello_world.rs and http_bench_json_ops.rs as examples of usage.
TypeScript support and lots of other functionality are not available at this layer. See the CLI for that.