use super::bindings;
use super::bindings::watch_promise;
use super::exception_state::ExceptionState;
use super::jsrealm::JsRealmInner;
use super::snapshot_util;
use crate::error::exception_to_err_result;
use crate::error::generic_error;
use crate::error::GetErrorClassFn;
use crate::error::JsError;
use crate::extensions::EventLoopMiddlewareFn;
use crate::extensions::GlobalObjectMiddlewareFn;
use crate::extensions::GlobalTemplateMiddlewareFn;
use crate::extensions::OpDecl;
use crate::include_js_files;
use crate::inspector::JsRuntimeInspector;
use crate::module_specifier::ModuleSpecifier;
use crate::modules::default_import_meta_resolve_cb;
use crate::modules::AssertedModuleType;
use crate::modules::ExtModuleLoader;
use crate::modules::ImportMetaResolveCallback;
use crate::modules::ModuleCode;
use crate::modules::ModuleId;
use crate::modules::ModuleLoader;
use crate::modules::ModuleMap;
use crate::modules::ValidateImportAttributesCb;
use crate::ops::*;
use crate::ops_metrics::dispatch_metrics_async;
use crate::ops_metrics::OpMetricsEvent;
use crate::ops_metrics::OpMetricsFactoryFn;
use crate::runtime::ContextState;
use crate::runtime::JsRealm;
use crate::source_map::SourceMapCache;
use crate::source_map::SourceMapGetter;
use crate::Extension;
use crate::ExtensionFileSource;
use crate::FeatureChecker;
use crate::NoopModuleLoader;
use crate::OpMiddlewareFn;
use crate::OpResult;
use crate::OpState;
use crate::V8_WRAPPER_OBJECT_INDEX;
use crate::V8_WRAPPER_TYPE_INDEX;
use anyhow::bail;
use anyhow::Context as AnyhowContext;
use anyhow::Error;
use futures::future::poll_fn;
use futures::task::noop_waker_ref;
use futures::task::AtomicWaker;
use futures::Future;
use futures::FutureExt;
use smallvec::SmallVec;
use std::any::Any;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::ops::DerefMut;
use std::option::Option;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::Once;
use std::task::Context;
use std::task::Poll;
use v8::Isolate;
const STATE_DATA_OFFSET: u32 = 0;
pub enum Snapshot {
Static(&'static [u8]),
JustCreated(v8::StartupData),
Boxed(Box<[u8]>),
}
#[derive(Default)]
pub(crate) struct IsolateAllocations {
pub(crate) near_heap_limit_callback_data:
Option<(Box<RefCell<dyn Any>>, v8::NearHeapLimitCallback)>,
}
pub(crate) struct ManuallyDropRc<T>(ManuallyDrop<Rc<T>>);
impl<T> ManuallyDropRc<T> {
#[allow(unused)]
pub fn clone(&self) -> Rc<T> {
self.0.deref().clone()
}
}
impl<T> Deref for ManuallyDropRc<T> {
type Target = Rc<T>;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<T> DerefMut for ManuallyDropRc<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}
pub(crate) struct InnerIsolateState {
will_snapshot: bool,
main_realm: ManuallyDrop<JsRealm>,
pub(crate) state: ManuallyDropRc<JsRuntimeState>,
v8_isolate: ManuallyDrop<v8::OwnedIsolate>,
}
impl InnerIsolateState {
pub fn prepare_for_cleanup(&mut self) {
let inspector = self.state.inspector.take();
self.state.op_state.borrow_mut().clear();
if let Some(inspector) = inspector {
assert_eq!(
Rc::strong_count(&inspector),
1,
"The inspector must be dropped before the runtime"
);
}
}
pub fn cleanup(&mut self) {
self.prepare_for_cleanup();
let state_ptr = self.v8_isolate.get_data(STATE_DATA_OFFSET);
_ = unsafe { Rc::from_raw(state_ptr as *const JsRuntimeState) };
unsafe {
ManuallyDrop::take(&mut self.main_realm).0.destroy();
}
debug_assert_eq!(Rc::strong_count(&self.state), 1);
}
pub fn prepare_for_snapshot(mut self) -> v8::OwnedIsolate {
self.cleanup();
let (state, isolate) = unsafe {
(
ManuallyDrop::take(&mut self.state.0),
ManuallyDrop::take(&mut self.v8_isolate),
)
};
std::mem::forget(self);
drop(state);
isolate
}
}
impl Drop for InnerIsolateState {
fn drop(&mut self) {
self.cleanup();
unsafe {
ManuallyDrop::drop(&mut self.state.0);
if self.will_snapshot {
eprintln!("WARNING: v8::OwnedIsolate for snapshot was leaked");
} else {
ManuallyDrop::drop(&mut self.v8_isolate);
}
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum InitMode {
New,
FromSnapshot {
skip_op_registration: bool,
},
}
impl InitMode {
fn from_options(options: &RuntimeOptions) -> Self {
match options.startup_snapshot {
None => Self::New,
Some(_) => Self::FromSnapshot {
skip_op_registration: options.skip_op_registration,
},
}
}
#[inline]
pub fn is_from_snapshot(&self) -> bool {
matches!(self, Self::FromSnapshot { .. })
}
}
pub(crate) const BUILTIN_SOURCES: [ExtensionFileSource; 3] = include_js_files!(
core
"00_primordials.js",
"01_core.js",
"02_error.js",
);
pub(crate) const BUILTIN_ES_MODULES: [ExtensionFileSource; 1] =
include_js_files!(core "mod.js",);
pub struct JsRuntime {
pub(crate) inner: InnerIsolateState,
pub(crate) allocations: IsolateAllocations,
extensions: Vec<Extension>,
event_loop_middlewares: Vec<EventLoopMiddlewareFn>,
init_mode: InitMode,
is_main_runtime: bool,
}
pub struct JsRuntimeForSnapshot(JsRuntime);
impl Deref for JsRuntimeForSnapshot {
type Target = JsRuntime;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for JsRuntimeForSnapshot {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub struct CrossIsolateStore<T>(Arc<Mutex<CrossIsolateStoreInner<T>>>);
struct CrossIsolateStoreInner<T> {
map: HashMap<u32, T>,
last_id: u32,
}
impl<T> CrossIsolateStore<T> {
pub(crate) fn insert(&self, value: T) -> u32 {
let mut store = self.0.lock().unwrap();
let last_id = store.last_id;
store.map.insert(last_id, value);
store.last_id += 1;
last_id
}
pub(crate) fn take(&self, id: u32) -> Option<T> {
let mut store = self.0.lock().unwrap();
store.map.remove(&id)
}
}
impl<T> Default for CrossIsolateStore<T> {
fn default() -> Self {
CrossIsolateStore(Arc::new(Mutex::new(CrossIsolateStoreInner {
map: Default::default(),
last_id: 0,
})))
}
}
impl<T> Clone for CrossIsolateStore<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
pub type SharedArrayBufferStore =
CrossIsolateStore<v8::SharedRef<v8::BackingStore>>;
pub type CompiledWasmModuleStore = CrossIsolateStore<v8::CompiledWasmModule>;
pub struct JsRuntimeState {
pub(crate) source_map_getter: Option<Rc<Box<dyn SourceMapGetter>>>,
pub(crate) source_map_cache: Rc<RefCell<SourceMapCache>>,
pub(crate) op_state: Rc<RefCell<OpState>>,
pub(crate) shared_array_buffer_store: Option<SharedArrayBufferStore>,
pub(crate) compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
pub(crate) validate_import_attributes_cb: ValidateImportAttributesCb,
waker: Arc<AtomicWaker>,
inspector: RefCell<Option<Rc<RefCell<JsRuntimeInspector>>>>,
has_inspector: Cell<bool>,
}
fn v8_init(
v8_platform: Option<v8::SharedRef<v8::Platform>>,
predictable: bool,
expose_natives: bool,
) {
#[cfg(feature = "include_icu_data")]
{
#[repr(C, align(16))]
struct IcuData([u8; 10631872]);
static ICU_DATA: IcuData = IcuData(*include_bytes!("icudtl.dat"));
v8::icu::set_common_data_73(&ICU_DATA.0).unwrap();
}
let base_flags = concat!(
" --wasm-test-streaming",
" --harmony-import-assertions",
" --harmony-import-attributes",
" --no-validate-asm",
" --turbo_fast_api_calls",
" --harmony-change-array-by-copy",
" --harmony-array-from_async",
" --harmony-iterator-helpers",
);
let predictable_flags = "--predictable --random-seed=42";
let expose_natives_flags = "--expose_gc --allow_natives_syntax";
#[allow(clippy::useless_format)]
let flags = match (predictable, expose_natives) {
(false, false) => format!("{base_flags}"),
(true, false) => format!("{base_flags} {predictable_flags}"),
(false, true) => format!("{base_flags} {expose_natives_flags}"),
(true, true) => {
format!("{base_flags} {predictable_flags} {expose_natives_flags}")
}
};
v8::V8::set_flags_from_string(&flags);
let v8_platform = v8_platform
.unwrap_or_else(|| v8::new_default_platform(0, false).make_shared());
v8::V8::initialize_platform(v8_platform);
v8::V8::initialize();
}
#[derive(Default)]
pub struct RuntimeOptions {
pub source_map_getter: Option<Box<dyn SourceMapGetter>>,
pub get_error_class_fn: Option<GetErrorClassFn>,
pub module_loader: Option<Rc<dyn ModuleLoader>>,
pub op_metrics_factory_fn: Option<OpMetricsFactoryFn>,
pub extensions: Vec<Extension>,
pub preserve_snapshotted_modules: Option<&'static [&'static str]>,
pub startup_snapshot: Option<Snapshot>,
pub skip_op_registration: bool,
pub create_params: Option<v8::CreateParams>,
pub v8_platform: Option<v8::SharedRef<v8::Platform>>,
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
pub inspector: bool,
pub is_main: bool,
#[cfg(any(test, feature = "unsafe_runtime_options"))]
pub unsafe_expose_natives_and_gc: bool,
pub feature_checker: Option<Arc<FeatureChecker>>,
pub validate_import_attributes_cb: Option<ValidateImportAttributesCb>,
pub import_meta_resolve_callback: Option<ImportMetaResolveCallback>,
}
impl RuntimeOptions {
#[cfg(any(test, feature = "unsafe_runtime_options"))]
fn unsafe_expose_natives_and_gc(&self) -> bool {
self.unsafe_expose_natives_and_gc
}
#[cfg(not(any(test, feature = "unsafe_runtime_options")))]
fn unsafe_expose_natives_and_gc(&self) -> bool {
false
}
}
#[derive(Copy, Clone, Debug)]
pub struct PollEventLoopOptions {
pub wait_for_inspector: bool,
pub pump_v8_message_loop: bool,
}
impl Default for PollEventLoopOptions {
fn default() -> Self {
Self {
wait_for_inspector: true,
pump_v8_message_loop: true,
}
}
}
#[derive(Default)]
pub struct CreateRealmOptions {
pub module_loader: Option<Rc<dyn ModuleLoader>>,
}
impl JsRuntime {
#[cfg(not(any(test, feature = "unsafe_runtime_options")))]
pub fn init_platform(v8_platform: Option<v8::SharedRef<v8::Platform>>) {
JsRuntime::init_v8(v8_platform, cfg!(test), false);
}
#[cfg(any(test, feature = "unsafe_runtime_options"))]
pub fn init_platform(
v8_platform: Option<v8::SharedRef<v8::Platform>>,
expose_natives: bool,
) {
JsRuntime::init_v8(v8_platform, cfg!(test), expose_natives);
}
pub fn new(mut options: RuntimeOptions) -> JsRuntime {
JsRuntime::init_v8(
options.v8_platform.take(),
cfg!(test),
options.unsafe_expose_natives_and_gc(),
);
JsRuntime::new_inner(options, false)
}
pub(crate) fn state_from(isolate: &v8::Isolate) -> Rc<JsRuntimeState> {
let state_ptr = isolate.get_data(STATE_DATA_OFFSET);
let state_rc =
unsafe { Rc::from_raw(state_ptr as *const JsRuntimeState) };
let state = state_rc.clone();
std::mem::forget(state_rc);
state
}
pub fn op_state_from(isolate: &v8::Isolate) -> Rc<RefCell<OpState>> {
let state = Self::state_from(isolate);
state.op_state.clone()
}
pub(crate) fn has_more_work(scope: &mut v8::HandleScope) -> bool {
EventLoopPendingState::new_from_scope(scope).is_pending()
}
fn init_v8(
v8_platform: Option<v8::SharedRef<v8::Platform>>,
predictable: bool,
expose_natives: bool,
) {
static DENO_INIT: Once = Once::new();
static DENO_PREDICTABLE: AtomicBool = AtomicBool::new(false);
static DENO_PREDICTABLE_SET: AtomicBool = AtomicBool::new(false);
if DENO_PREDICTABLE_SET.load(Ordering::SeqCst) {
let current = DENO_PREDICTABLE.load(Ordering::SeqCst);
assert_eq!(current, predictable, "V8 may only be initialized once in either snapshotting or non-snapshotting mode. Either snapshotting or non-snapshotting mode may be used in a single process, not both.");
DENO_PREDICTABLE_SET.store(true, Ordering::SeqCst);
DENO_PREDICTABLE.store(predictable, Ordering::SeqCst);
}
DENO_INIT
.call_once(move || v8_init(v8_platform, predictable, expose_natives));
}
fn new_inner(mut options: RuntimeOptions, will_snapshot: bool) -> JsRuntime {
let init_mode = InitMode::from_options(&options);
let (op_state, ops) = Self::create_opstate(&mut options);
let mut event_loop_middlewares =
Vec::with_capacity(options.extensions.len());
let mut global_template_middlewares =
Vec::with_capacity(options.extensions.len());
let mut global_object_middlewares =
Vec::with_capacity(options.extensions.len());
let mut additional_references =
Vec::with_capacity(options.extensions.len());
for extension in &mut options.extensions {
if let Some(middleware) = extension.get_event_loop_middleware() {
event_loop_middlewares.push(middleware);
}
if let Some(middleware) = extension.get_global_template_middleware() {
global_template_middlewares.push(middleware);
}
if let Some(middleware) = extension.get_global_object_middleware() {
global_object_middlewares.push(middleware);
}
additional_references
.extend_from_slice(extension.get_external_references());
}
let align = std::mem::align_of::<usize>();
let layout = std::alloc::Layout::from_size_align(
std::mem::size_of::<*mut v8::OwnedIsolate>(),
align,
)
.unwrap();
assert!(layout.size() > 0);
let isolate_ptr: *mut v8::OwnedIsolate =
unsafe { std::alloc::alloc(layout) as *mut _ };
let validate_import_attributes_cb = options
.validate_import_attributes_cb
.unwrap_or_else(|| Box::new(crate::modules::validate_import_attributes));
let waker = op_state.waker.clone();
let op_state = Rc::new(RefCell::new(op_state));
let state_rc = Rc::new(JsRuntimeState {
source_map_getter: options.source_map_getter.map(Rc::new),
source_map_cache: Default::default(),
shared_array_buffer_store: options.shared_array_buffer_store,
compiled_wasm_module_store: options.compiled_wasm_module_store,
op_state: op_state.clone(),
inspector: None.into(),
has_inspector: false.into(),
validate_import_attributes_cb,
waker,
});
let weak = Rc::downgrade(&state_rc);
let context_state = Rc::new(RefCell::new(ContextState::default()));
let spawner = context_state
.borrow()
.task_spawner_factory
.clone()
.new_same_thread_spawner();
op_state.borrow_mut().put(spawner);
let spawner = context_state
.borrow()
.task_spawner_factory
.clone()
.new_cross_thread_spawner();
op_state.borrow_mut().put(spawner);
let count = ops.len();
let mut op_ctxs = ops
.into_iter()
.enumerate()
.map(|(id, decl)| {
let metrics_fn = options
.op_metrics_factory_fn
.as_ref()
.and_then(|f| (f)(id as _, count, &decl));
OpCtx::new(
id as _,
std::ptr::null_mut(),
context_state.clone(),
Rc::new(decl),
op_state.clone(),
weak.clone(),
options.get_error_class_fn.unwrap_or(&|_| "Error"),
metrics_fn,
)
})
.collect::<Vec<_>>()
.into_boxed_slice();
context_state.borrow_mut().isolate = Some(isolate_ptr);
let refs = bindings::external_references(&op_ctxs, &additional_references);
let refs: &'static v8::ExternalReferences = Box::leak(Box::new(refs));
let mut isolate = if will_snapshot {
snapshot_util::create_snapshot_creator(
refs,
options.startup_snapshot.take(),
)
} else {
let mut params = options
.create_params
.take()
.unwrap_or_default()
.embedder_wrapper_type_info_offsets(
V8_WRAPPER_TYPE_INDEX,
V8_WRAPPER_OBJECT_INDEX,
)
.external_references(&**refs);
if let Some(snapshot) = options.startup_snapshot.take() {
params = match snapshot {
Snapshot::Static(data) => params.snapshot_blob(data),
Snapshot::JustCreated(data) => params.snapshot_blob(data),
Snapshot::Boxed(data) => params.snapshot_blob(data),
};
}
v8::Isolate::new(params)
};
for op_ctx in op_ctxs.iter_mut() {
op_ctx.isolate = isolate.as_mut() as *mut Isolate;
}
context_state.borrow_mut().op_ctxs = op_ctxs;
isolate.set_capture_stack_trace_for_uncaught_exceptions(true, 10);
isolate.set_promise_reject_callback(bindings::promise_reject_callback);
isolate.set_host_initialize_import_meta_object_callback(
bindings::host_initialize_import_meta_object_callback,
);
isolate.set_host_import_module_dynamically_callback(
bindings::host_import_module_dynamically_callback,
);
isolate.set_wasm_async_resolve_promise_callback(
bindings::wasm_async_resolve_promise_callback,
);
let (main_context, snapshotted_data) = {
let scope = &mut v8::HandleScope::new(&mut isolate);
let context = create_context(
scope,
&global_template_middlewares,
&global_object_middlewares,
);
let snapshotted_data = if init_mode.is_from_snapshot() {
Some(snapshot_util::get_snapshotted_data(scope, context))
} else {
None
};
(v8::Global::new(scope, context), snapshotted_data)
};
isolate = unsafe {
isolate_ptr.write(isolate);
isolate_ptr.read()
};
let mut context_scope: v8::HandleScope =
v8::HandleScope::with_context(&mut isolate, &main_context);
let scope = &mut context_scope;
let context = v8::Local::new(scope, &main_context);
bindings::initialize_context(
scope,
context,
&context_state.borrow().op_ctxs,
init_mode,
);
context.set_slot(scope, context_state.clone());
op_state.borrow_mut().put(isolate_ptr);
let inspector = if options.inspector {
Some(JsRuntimeInspector::new(scope, context, options.is_main))
} else {
None
};
let loader = options
.module_loader
.unwrap_or_else(|| Rc::new(NoopModuleLoader));
let import_meta_resolve_cb = options
.import_meta_resolve_callback
.unwrap_or_else(|| Box::new(default_import_meta_resolve_cb));
let exception_state = context_state.borrow().exception_state.clone();
let module_map = if let Some(snapshotted_data) = snapshotted_data {
Rc::new(ModuleMap::new_from_snapshotted_data(
loader,
exception_state,
import_meta_resolve_cb,
scope,
snapshotted_data,
))
} else {
Rc::new(ModuleMap::new(
loader,
exception_state,
import_meta_resolve_cb,
))
};
context.set_slot(scope, module_map.clone());
let main_realm = {
let main_realm =
JsRealmInner::new(context_state, main_context, module_map.clone());
state_rc.has_inspector.set(inspector.is_some());
*state_rc.inspector.borrow_mut() = inspector;
main_realm
};
let main_realm = JsRealm::new(main_realm);
scope.set_data(
STATE_DATA_OFFSET,
Rc::into_raw(state_rc.clone()) as *mut c_void,
);
drop(context_scope);
let mut js_runtime = JsRuntime {
inner: InnerIsolateState {
will_snapshot,
main_realm: ManuallyDrop::new(main_realm),
state: ManuallyDropRc(ManuallyDrop::new(state_rc)),
v8_isolate: ManuallyDrop::new(isolate),
},
init_mode,
allocations: IsolateAllocations::default(),
event_loop_middlewares,
extensions: options.extensions,
is_main_runtime: options.is_main,
};
js_runtime.init_extension_js().unwrap();
if let Some(preserve_snapshotted_modules) =
options.preserve_snapshotted_modules
{
module_map.clear_module_map(preserve_snapshotted_modules);
}
js_runtime
}
#[cfg(test)]
#[inline]
pub(crate) fn module_map(&mut self) -> Rc<ModuleMap> {
self.inner.main_realm.0.module_map()
}
#[inline]
pub fn main_context(&self) -> v8::Global<v8::Context> {
self.inner.main_realm.0.context().clone()
}
#[cfg(test)]
pub(crate) fn main_realm(&self) -> JsRealm {
JsRealm::clone(&self.inner.main_realm)
}
#[inline]
pub fn v8_isolate(&mut self) -> &mut v8::OwnedIsolate {
&mut self.inner.v8_isolate
}
#[inline]
fn v8_isolate_ptr(&mut self) -> *mut v8::Isolate {
&mut **self.inner.v8_isolate as _
}
#[inline]
pub fn inspector(&mut self) -> Rc<RefCell<JsRuntimeInspector>> {
self.inner.state.inspector()
}
pub fn extensions(&self) -> &Vec<Extension> {
&self.extensions
}
#[inline]
pub fn handle_scope(&mut self) -> v8::HandleScope {
let isolate = &mut self.inner.v8_isolate;
self.inner.main_realm.handle_scope(isolate)
}
#[inline(always)]
fn with_context_scope<'s, T>(
isolate: *mut v8::Isolate,
context: *mut v8::Context,
f: impl FnOnce(&mut v8::HandleScope<'s>) -> T,
) -> T {
let mut isolate_scope =
v8::HandleScope::new(unsafe { isolate.as_mut().unwrap_unchecked() });
let context = unsafe { std::mem::transmute(context) };
let mut scope = v8::ContextScope::new(&mut isolate_scope, context);
f(&mut scope)
}
fn init_extension_js(&mut self) -> Result<(), Error> {
let realm = JsRealm::clone(&self.inner.main_realm);
let module_map = realm.0.module_map();
let extensions = std::mem::take(&mut self.extensions);
let loader = module_map.loader.borrow().clone();
let ext_loader = Rc::new(ExtModuleLoader::new(&extensions));
*module_map.loader.borrow_mut() = ext_loader;
let mut esm_entrypoints = vec![];
futures::executor::block_on(async {
if self.init_mode == InitMode::New {
for file_source in &BUILTIN_SOURCES {
realm.execute_script(
self.v8_isolate(),
file_source.specifier,
file_source.load()?,
)?;
}
for file_source in &BUILTIN_ES_MODULES {
let mut scope = realm.handle_scope(self.v8_isolate());
module_map.lazy_load_es_module_from_code(
&mut scope,
file_source.specifier,
file_source.load()?,
)?;
}
}
self.init_cbs(&realm);
for extension in &extensions {
let maybe_esm_entry_point = extension.get_esm_entry_point();
for file_source in extension.get_esm_sources() {
realm
.load_side_module(
self.v8_isolate(),
&ModuleSpecifier::parse(file_source.specifier)?,
None,
)
.await?;
}
if let Some(entry_point) = maybe_esm_entry_point {
esm_entrypoints.push(entry_point);
}
for file_source in extension.get_js_sources() {
realm.execute_script(
self.v8_isolate(),
file_source.specifier,
file_source.load()?,
)?;
}
}
for specifier in esm_entrypoints {
let mod_id = {
module_map
.get_id(specifier, AssertedModuleType::JavaScriptOrWasm)
.unwrap_or_else(|| {
panic!("{} not present in the module map", specifier)
})
};
let mut receiver = {
let isolate = self.v8_isolate();
let scope = &mut realm.handle_scope(isolate);
module_map.mod_evaluate(scope, mod_id)
};
match receiver.poll_unpin(&mut Context::from_waker(noop_waker_ref())) {
Poll::Ready(result) => {
result
.with_context(|| format!("Couldn't execute '{specifier}'"))?;
}
Poll::Pending => {
let location = {
let scope = &mut realm.handle_scope(self.v8_isolate());
let messages = module_map.find_stalled_top_level_await(scope);
assert!(!messages.is_empty());
let msg = v8::Local::new(scope, &messages[0]);
let js_error = JsError::from_v8_message(scope, msg);
js_error
.frames
.first()
.unwrap()
.maybe_format_location()
.unwrap()
};
panic!("Top-level await is not allowed in extensions ({location})");
}
}
}
#[cfg(debug_assertions)]
{
let mut scope = realm.handle_scope(self.v8_isolate());
module_map.assert_all_modules_evaluated(&mut scope);
}
Ok::<_, anyhow::Error>(())
})?;
self.extensions = extensions;
let module_map = realm.0.module_map();
*module_map.loader.borrow_mut() = loader;
Ok(())
}
fn collect_ops(exts: &mut [Extension]) -> Vec<OpDecl> {
for (ext, previous_exts) in
exts.iter().enumerate().map(|(i, ext)| (ext, &exts[..i]))
{
ext.check_dependencies(previous_exts);
}
let middleware: Vec<Box<OpMiddlewareFn>> = exts
.iter_mut()
.filter_map(|e| e.take_middleware())
.collect();
let macroware = move |d| middleware.iter().fold(d, |d, m| m(d));
let ops: Vec<_> = exts
.iter_mut()
.flat_map(|e| e.init_ops())
.map(|d| OpDecl {
name: d.name,
..macroware(*d)
})
.collect();
#[cfg(debug_assertions)]
{
let mut count_by_name = HashMap::new();
for op in ops.iter() {
count_by_name
.entry(&op.name)
.or_insert(vec![])
.push(op.name.to_string());
}
let mut duplicate_ops = vec![];
for (op_name, _count) in
count_by_name.iter().filter(|(_k, v)| v.len() > 1)
{
duplicate_ops.push(op_name.to_string());
}
if !duplicate_ops.is_empty() {
let mut msg = "Found ops with duplicate names:\n".to_string();
for op_name in duplicate_ops {
msg.push_str(&format!(" - {}\n", op_name));
}
msg.push_str("Op names need to be unique.");
panic!("{}", msg);
}
}
ops
}
fn create_opstate(options: &mut RuntimeOptions) -> (OpState, Vec<OpDecl>) {
options
.extensions
.insert(0, crate::ops_builtin::core::init_ops());
let ops = Self::collect_ops(&mut options.extensions);
let mut op_state = OpState::new(options.feature_checker.take());
for e in &mut options.extensions {
e.take_state(&mut op_state);
}
(op_state, ops)
}
pub fn eval<'s, T>(
scope: &mut v8::HandleScope<'s>,
code: &str,
) -> Option<v8::Local<'s, T>>
where
v8::Local<'s, T>: TryFrom<v8::Local<'s, v8::Value>, Error = v8::DataError>,
{
let scope = &mut v8::EscapableHandleScope::new(scope);
let source = v8::String::new(scope, code).unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
let v = script.run(scope)?;
scope.escape(v).try_into().ok()
}
fn init_cbs(&mut self, realm: &JsRealm) {
let (event_loop_tick_cb, build_custom_error_cb) = {
let scope = &mut realm.handle_scope(self.v8_isolate());
let context = realm.context();
let context_local = v8::Local::new(scope, context);
let global = context_local.global(scope);
let deno_str =
v8::String::new_external_onebyte_static(scope, b"Deno").unwrap();
let core_str =
v8::String::new_external_onebyte_static(scope, b"core").unwrap();
let event_loop_tick_str =
v8::String::new_external_onebyte_static(scope, b"eventLoopTick")
.unwrap();
let build_custom_error_str =
v8::String::new_external_onebyte_static(scope, b"buildCustomError")
.unwrap();
let deno_obj: v8::Local<v8::Object> = global
.get(scope, deno_str.into())
.unwrap()
.try_into()
.unwrap();
let core_obj: v8::Local<v8::Object> = deno_obj
.get(scope, core_str.into())
.unwrap()
.try_into()
.unwrap();
let event_loop_tick_cb: v8::Local<v8::Function> = core_obj
.get(scope, event_loop_tick_str.into())
.unwrap()
.try_into()
.unwrap();
let build_custom_error_cb: v8::Local<v8::Function> = core_obj
.get(scope, build_custom_error_str.into())
.unwrap()
.try_into()
.unwrap();
(
v8::Global::new(scope, event_loop_tick_cb),
v8::Global::new(scope, build_custom_error_cb),
)
};
let state_rc = realm.0.state();
let mut state = state_rc.borrow_mut();
state
.js_event_loop_tick_cb
.replace(Rc::new(event_loop_tick_cb));
state
.exception_state
.js_build_custom_error_cb
.borrow_mut()
.replace(Rc::new(build_custom_error_cb));
}
pub fn op_state(&mut self) -> Rc<RefCell<OpState>> {
self.inner.state.op_state.clone()
}
pub fn op_names(&self) -> Vec<&'static str> {
let main_realm = self.inner.main_realm.clone();
let state_rc = main_realm.0.state();
let state = state_rc.borrow();
state.op_ctxs.iter().map(|o| o.decl.name).collect()
}
pub fn execute_script(
&mut self,
name: &'static str,
source_code: ModuleCode,
) -> Result<v8::Global<v8::Value>, Error> {
let isolate = &mut self.inner.v8_isolate;
self
.inner
.main_realm
.execute_script(isolate, name, source_code)
}
pub fn execute_script_static(
&mut self,
name: &'static str,
source_code: &'static str,
) -> Result<v8::Global<v8::Value>, Error> {
let isolate = &mut self.inner.v8_isolate;
self.inner.main_realm.execute_script(
isolate,
name,
ModuleCode::from_static(source_code),
)
}
pub async fn call_and_await(
&mut self,
function: &v8::Global<v8::Function>,
) -> Result<v8::Global<v8::Value>, Error> {
self.call_with_args_and_await(function, &[]).await
}
pub async fn call_with_args_and_await(
&mut self,
function: &v8::Global<v8::Function>,
args: &[v8::Global<v8::Value>],
) -> Result<v8::Global<v8::Value>, Error> {
let promise = {
let scope = &mut self.handle_scope();
let scope = &mut v8::TryCatch::new(scope);
let cb = function.open(scope);
let this = v8::undefined(scope).into();
let promise = if args.is_empty() {
cb.call(scope, this, &[])
} else {
let mut local_args: SmallVec<[v8::Local<v8::Value>; 8]> =
SmallVec::with_capacity(args.len());
for v in args {
local_args.push(v8::Local::new(scope, v));
}
cb.call(scope, this, &local_args)
};
if promise.is_none() {
if scope.is_execution_terminating() {
let undefined = v8::undefined(scope).into();
return exception_to_err_result(scope, undefined, false, true);
}
let exception = scope.exception().unwrap();
return exception_to_err_result(scope, exception, false, true);
}
let promise = promise.unwrap();
if !promise.is_promise() {
return Ok(v8::Global::new(scope, promise));
}
let promise = v8::Local::<v8::Promise>::try_from(promise).unwrap();
let Some(promise) =
watch_promise(scope, promise, |scope, mut rv, res| {
let array = v8::Array::new(scope, 2);
match res {
Ok(value) => {
let b = v8::Boolean::new(scope, true);
array.set_index(scope, 0, b.into());
array.set_index(scope, 1, value);
}
Err(value) => {
let b = v8::Boolean::new(scope, false);
array.set_index(scope, 0, b.into());
array.set_index(scope, 1, value);
}
}
rv.set(array.into());
})
else {
bail!("Runtime shutdown");
};
v8::Global::new(scope, v8::Local::<v8::Value>::from(promise))
};
let res = self.resolve_value(promise).await?;
let scope = &mut self.handle_scope();
let res = v8::Local::new(scope, res);
if let Ok(array) = v8::Local::<v8::Array>::try_from(res) {
if array.get_index(scope, 0).unwrap().boolean_value(scope) {
let value = array.get_index(scope, 1).unwrap();
Ok(v8::Global::new(scope, value))
} else {
let value = array.get_index(scope, 1).unwrap();
Err(JsError::from_v8_exception(scope, value).into())
}
} else {
bail!("Unexpected V8 type");
}
}
pub fn get_module_namespace(
&mut self,
module_id: ModuleId,
) -> Result<v8::Global<v8::Object>, Error> {
let isolate = &mut self.inner.v8_isolate;
self
.inner
.main_realm
.get_module_namespace(isolate, module_id)
}
pub fn add_near_heap_limit_callback<C>(&mut self, cb: C)
where
C: FnMut(usize, usize) -> usize + 'static,
{
let boxed_cb = Box::new(RefCell::new(cb));
let data = boxed_cb.as_ptr() as *mut c_void;
let prev = self
.allocations
.near_heap_limit_callback_data
.replace((boxed_cb, near_heap_limit_callback::<C>));
if let Some((_, prev_cb)) = prev {
self
.v8_isolate()
.remove_near_heap_limit_callback(prev_cb, 0);
}
self
.v8_isolate()
.add_near_heap_limit_callback(near_heap_limit_callback::<C>, data);
}
pub fn remove_near_heap_limit_callback(&mut self, heap_limit: usize) {
if let Some((_, cb)) = self.allocations.near_heap_limit_callback_data.take()
{
self
.v8_isolate()
.remove_near_heap_limit_callback(cb, heap_limit);
}
}
fn pump_v8_message_loop(
&mut self,
scope: &mut v8::HandleScope,
) -> Result<(), Error> {
while v8::Platform::pump_message_loop(
&v8::V8::get_current_platform(),
scope,
false, ) {
}
let tc_scope = &mut v8::TryCatch::new(scope);
tc_scope.perform_microtask_checkpoint();
match tc_scope.exception() {
None => Ok(()),
Some(exception) => {
exception_to_err_result(tc_scope, exception, false, true)
}
}
}
pub fn maybe_init_inspector(&mut self) {
let inspector = &mut self.inner.state.inspector.borrow_mut();
if inspector.is_some() {
return;
}
let context = self.main_context();
let scope = &mut v8::HandleScope::with_context(
self.inner.v8_isolate.as_mut(),
context.clone(),
);
let context = v8::Local::new(scope, context);
self.inner.state.has_inspector.set(true);
**inspector = Some(JsRuntimeInspector::new(
scope,
context,
self.is_main_runtime,
));
}
pub fn poll_value(
&mut self,
cx: &mut Context,
global: &v8::Global<v8::Value>,
) -> Poll<Result<v8::Global<v8::Value>, Error>> {
Self::with_context_scope(
self.v8_isolate_ptr(),
self.inner.main_realm.context_ptr(),
|scope| self.poll_value_inner(cx, scope, global),
)
}
fn poll_value_inner(
&mut self,
cx: &mut Context,
scope: &mut v8::HandleScope,
global: &v8::Global<v8::Value>,
) -> Poll<Result<v8::Global<v8::Value>, Error>> {
let promise = v8::Local::new(scope, global);
let Ok(promise) = v8::Local::<v8::Promise>::try_from(promise) else {
return Poll::Ready(Ok(global.clone()));
};
match promise.state() {
v8::PromiseState::Pending => {}
v8::PromiseState::Fulfilled => {
let value = promise.result(scope);
let value_handle = v8::Global::new(scope, value);
return Poll::Ready(Ok(value_handle));
}
v8::PromiseState::Rejected => {
let exception = promise.result(scope);
let promise_global = v8::Global::new(scope, promise);
JsRealm::exception_state_from_scope(scope)
.pending_promise_rejections
.borrow_mut()
.retain(|(key, _)| key != &promise_global);
return Poll::Ready(exception_to_err_result(
scope, exception, false, true,
));
}
}
let event_loop_result = self.poll_event_loop_inner(
cx,
scope,
PollEventLoopOptions {
wait_for_inspector: false,
pump_v8_message_loop: true,
},
)?;
match promise.state() {
v8::PromiseState::Pending => match event_loop_result {
Poll::Ready(_) => {
let msg = "Promise resolution is still pending but the event loop has already resolved.";
Poll::Ready(Err(generic_error(msg)))
}
Poll::Pending => Poll::Pending,
},
v8::PromiseState::Fulfilled => {
let value = promise.result(scope);
let value_handle = v8::Global::new(scope, value);
Poll::Ready(Ok(value_handle))
}
v8::PromiseState::Rejected => {
let exception = promise.result(scope);
Poll::Ready(exception_to_err_result(scope, exception, false, true))
}
}
}
pub async fn resolve_value(
&mut self,
global: v8::Global<v8::Value>,
) -> Result<v8::Global<v8::Value>, Error> {
poll_fn(|cx| self.poll_value(cx, &global)).await
}
pub async fn run_event_loop(
&mut self,
wait_for_inspector: bool,
) -> Result<(), Error> {
poll_fn(|cx| self.poll_event_loop(cx, wait_for_inspector)).await
}
pub async fn run_event_loop2(
&mut self,
poll_options: PollEventLoopOptions,
) -> Result<(), Error> {
poll_fn(|cx| self.poll_event_loop2(cx, poll_options)).await
}
pub async fn with_event_loop<'fut, T>(
&mut self,
mut fut: Pin<Box<dyn Future<Output = T> + 'fut>>,
poll_options: PollEventLoopOptions,
) -> T {
loop {
tokio::select! {
biased;
result = &mut fut => {
return result;
},
_ = self.run_event_loop2(poll_options) => {}
}
}
}
pub fn poll_event_loop(
&mut self,
cx: &mut Context,
wait_for_inspector: bool,
) -> Poll<Result<(), Error>> {
Self::with_context_scope(
self.v8_isolate_ptr(),
self.inner.main_realm.context_ptr(),
|scope| {
self.poll_event_loop_inner(
cx,
scope,
PollEventLoopOptions {
wait_for_inspector,
..Default::default()
},
)
},
)
}
pub fn poll_event_loop2(
&mut self,
cx: &mut Context,
poll_options: PollEventLoopOptions,
) -> Poll<Result<(), Error>> {
let isolate = self.v8_isolate_ptr();
Self::with_context_scope(
isolate,
self.inner.main_realm.context_ptr(),
move |scope| self.poll_event_loop_inner(cx, scope, poll_options),
)
}
fn poll_event_loop_inner(
&mut self,
cx: &mut Context,
scope: &mut v8::HandleScope,
poll_options: PollEventLoopOptions,
) -> Poll<Result<(), Error>> {
let has_inspector = self.inner.state.has_inspector.get();
self.inner.state.waker.register(cx.waker());
if has_inspector {
let _ = self.inspector().borrow().poll_sessions(Some(cx)).unwrap();
}
if poll_options.pump_v8_message_loop {
self.pump_v8_message_loop(scope)?;
}
let realm = &self.inner.main_realm;
let modules = &realm.0.module_map;
let exception_state = &modules.exception_state;
modules.poll_progress(cx, scope)?;
let dispatched_ops = Self::do_js_event_loop_tick_realm(
cx,
scope,
&realm.0.context_state,
exception_state,
)?;
exception_state.check_exception_condition(scope)?;
let mut maybe_scheduling = false;
{
let op_state = self.inner.state.op_state.clone();
for f in &self.event_loop_middlewares {
if f(op_state.clone(), cx) {
maybe_scheduling = true;
}
}
}
let pending_state = {
EventLoopPendingState::new(
scope,
&realm.0.context_state.borrow(),
modules,
)
};
if !pending_state.is_pending() && !maybe_scheduling {
if has_inspector {
let inspector = self.inspector();
let has_active_sessions = inspector.borrow().has_active_sessions();
let has_blocking_sessions = inspector.borrow().has_blocking_sessions();
if poll_options.wait_for_inspector && has_active_sessions {
if !has_blocking_sessions {
let context = self.main_context();
inspector.borrow_mut().context_destroyed(scope, context);
println!("Program finished. Waiting for inspector to disconnect to exit the process...");
}
return Poll::Pending;
}
}
return Poll::Ready(Ok(()));
}
#[allow(clippy::suspicious_else_formatting, clippy::if_same_then_else)]
{
if pending_state.has_pending_background_tasks
|| pending_state.has_tick_scheduled
|| pending_state.has_pending_promise_rejections
|| maybe_scheduling
{
self.inner.state.waker.wake();
} else
if (pending_state.has_pending_module_evaluation
|| pending_state.has_pending_dyn_module_evaluation)
&& dispatched_ops
{
self.inner.state.waker.wake();
}
}
if pending_state.has_pending_module_evaluation {
if pending_state.has_pending_refed_ops
|| pending_state.has_pending_dyn_imports
|| pending_state.has_pending_dyn_module_evaluation
|| pending_state.has_pending_background_tasks
|| pending_state.has_tick_scheduled
|| maybe_scheduling
{
} else {
return Poll::Ready(Err(
find_and_report_stalled_level_await_in_any_realm(scope, &realm.0),
));
}
}
if pending_state.has_pending_dyn_module_evaluation {
if pending_state.has_pending_refed_ops
|| pending_state.has_pending_dyn_imports
|| pending_state.has_pending_background_tasks
|| pending_state.has_tick_scheduled
{
} else if realm.modules_idle() {
return Poll::Ready(Err(
find_and_report_stalled_level_await_in_any_realm(scope, &realm.0),
));
} else {
realm.increment_modules_idle();
self.inner.state.waker.wake();
}
}
Poll::Pending
}
}
fn find_and_report_stalled_level_await_in_any_realm(
scope: &mut v8::HandleScope,
inner_realm: &JsRealmInner,
) -> Error {
let module_map = inner_realm.module_map();
let messages = module_map.find_stalled_top_level_await(scope);
if !messages.is_empty() {
let msg = v8::Local::new(scope, &messages[0]);
let js_error = JsError::from_v8_message(scope, msg);
return js_error.into();
}
unreachable!("Expected at least one stalled top-level await");
}
fn create_context<'a>(
scope: &mut v8::HandleScope<'a, ()>,
global_template_middlewares: &[GlobalTemplateMiddlewareFn],
global_object_middlewares: &[GlobalObjectMiddlewareFn],
) -> v8::Local<'a, v8::Context> {
let mut global_object_template = v8::ObjectTemplate::new(scope);
for middleware in global_template_middlewares {
global_object_template = middleware(scope, global_object_template);
}
let context = v8::Context::new_from_template(scope, global_object_template);
let scope = &mut v8::ContextScope::new(scope, context);
let global_wrapper = context.global(scope);
let real_global = global_wrapper
.get_prototype(scope)
.unwrap()
.to_object(scope)
.unwrap();
for middleware in global_object_middlewares {
middleware(scope, real_global);
}
context
}
impl JsRuntimeForSnapshot {
pub fn new(mut options: RuntimeOptions) -> JsRuntimeForSnapshot {
JsRuntime::init_v8(
options.v8_platform.take(),
true,
options.unsafe_expose_natives_and_gc(),
);
JsRuntimeForSnapshot(JsRuntime::new_inner(options, true))
}
pub fn snapshot(mut self) -> v8::StartupData {
self.inner.prepare_for_cleanup();
let realm = JsRealm::clone(&self.inner.main_realm);
{
let mut scope = realm.handle_scope(self.v8_isolate());
let local_context = v8::Local::new(&mut scope, realm.context());
scope.set_default_context(local_context);
}
{
let snapshotted_data = {
let module_map = realm.0.module_map();
module_map.serialize_for_snapshotting(
&mut realm.handle_scope(self.v8_isolate()),
)
};
let mut scope = realm.handle_scope(self.v8_isolate());
snapshot_util::set_snapshotted_data(
&mut scope,
realm.context().clone(),
snapshotted_data,
);
}
drop(realm);
self
.0
.inner
.prepare_for_snapshot()
.create_blob(v8::FunctionCodeHandling::Keep)
.unwrap()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) struct EventLoopPendingState {
has_pending_refed_ops: bool,
has_pending_dyn_imports: bool,
has_pending_dyn_module_evaluation: bool,
has_pending_module_evaluation: bool,
has_pending_background_tasks: bool,
has_tick_scheduled: bool,
has_pending_promise_rejections: bool,
}
impl EventLoopPendingState {
pub fn new(
scope: &mut v8::HandleScope<()>,
state: &ContextState,
modules: &ModuleMap,
) -> Self {
let num_unrefed_ops = state.unrefed_ops.len();
let num_pending_ops = state.pending_ops.len();
let has_pending_tasks = state.task_spawner_factory.has_pending_tasks();
let has_pending_dyn_imports = modules.has_pending_dynamic_imports();
let has_pending_dyn_module_evaluation =
modules.has_pending_dyn_module_evaluation();
let has_pending_module_evaluation = modules.has_pending_module_evaluation();
let has_pending_promise_rejections = !modules
.exception_state
.pending_promise_rejections
.borrow()
.is_empty();
EventLoopPendingState {
has_pending_refed_ops: has_pending_tasks
|| num_pending_ops > num_unrefed_ops,
has_pending_dyn_imports,
has_pending_dyn_module_evaluation,
has_pending_module_evaluation,
has_pending_background_tasks: scope.has_pending_background_tasks(),
has_tick_scheduled: state.has_next_tick_scheduled,
has_pending_promise_rejections,
}
}
pub fn new_from_scope(scope: &mut v8::HandleScope) -> Self {
let module_map = JsRealm::module_map_from(scope);
let context_state = JsRealm::state_from_scope(scope);
let context_state = context_state.borrow();
Self::new(scope, &context_state, &module_map)
}
pub fn is_pending(&self) -> bool {
self.has_pending_refed_ops
|| self.has_pending_dyn_imports
|| self.has_pending_dyn_module_evaluation
|| self.has_pending_module_evaluation
|| self.has_pending_background_tasks
|| self.has_tick_scheduled
|| self.has_pending_promise_rejections
}
}
extern "C" fn near_heap_limit_callback<F>(
data: *mut c_void,
current_heap_limit: usize,
initial_heap_limit: usize,
) -> usize
where
F: FnMut(usize, usize) -> usize,
{
let callback = unsafe { &mut *(data as *mut F) };
callback(current_heap_limit, initial_heap_limit)
}
impl JsRuntimeState {
pub(crate) fn inspector(&self) -> Rc<RefCell<JsRuntimeInspector>> {
self.inspector.borrow().as_ref().unwrap().clone()
}
pub fn notify_new_dynamic_import(&self) {
self.waker.wake();
}
pub(crate) fn with_inspector<T>(
&self,
mut f: impl FnMut(&JsRuntimeInspector) -> T,
) -> Option<T> {
if !self.has_inspector.get() {
return None;
}
self
.inspector
.borrow()
.as_ref()
.map(|inspector| f(&inspector.borrow()))
}
}
impl JsRuntime {
#[cfg(test)]
pub(crate) fn instantiate_module(
&mut self,
id: ModuleId,
) -> Result<(), v8::Global<v8::Value>> {
let isolate = &mut self.inner.v8_isolate;
let realm = JsRealm::clone(&self.inner.main_realm);
let scope = &mut realm.handle_scope(isolate);
realm.instantiate_module(scope, id)
}
pub fn mod_evaluate(
&mut self,
id: ModuleId,
) -> impl Future<Output = Result<(), Error>> {
let isolate = &mut self.inner.v8_isolate;
let realm = &self.inner.main_realm;
let scope = &mut realm.handle_scope(isolate);
self.inner.main_realm.0.module_map.mod_evaluate(scope, id)
}
pub async fn load_main_module(
&mut self,
specifier: &ModuleSpecifier,
code: Option<ModuleCode>,
) -> Result<ModuleId, Error> {
let isolate = &mut self.inner.v8_isolate;
self
.inner
.main_realm
.load_main_module(isolate, specifier, code)
.await
}
pub async fn load_side_module(
&mut self,
specifier: &ModuleSpecifier,
code: Option<ModuleCode>,
) -> Result<ModuleId, Error> {
let isolate = &mut self.inner.v8_isolate;
self
.inner
.main_realm
.load_side_module(isolate, specifier, code)
.await
}
fn do_js_event_loop_tick_realm(
cx: &mut Context,
scope: &mut v8::HandleScope,
context_state: &RefCell<ContextState>,
exception_state: &ExceptionState,
) -> Result<bool, Error> {
let mut dispatched_ops = false;
let tasks = context_state.borrow().task_spawner_factory.poll_inner(cx);
if let Poll::Ready(tasks) = tasks {
dispatched_ops = true;
for task in tasks {
task(scope);
}
}
let mut context_state = context_state.borrow_mut();
let mut args: SmallVec<[v8::Local<v8::Value>; 32]> =
SmallVec::with_capacity(32);
loop {
let Poll::Ready(item) = context_state.pending_ops.poll_join_next(cx)
else {
break;
};
let PendingOp(promise_id, op_id, resp, metrics_event) = item.unwrap();
context_state.unrefed_ops.remove(&promise_id);
dispatched_ops |= true;
args.push(v8::Integer::new(scope, promise_id).into());
let was_error = matches!(resp, OpResult::Err(_));
let res = resp.to_v8(scope);
if metrics_event {
if res.is_ok() && !was_error {
dispatch_metrics_async(
&context_state.op_ctxs[op_id as usize],
OpMetricsEvent::CompletedAsync,
);
} else {
dispatch_metrics_async(
&context_state.op_ctxs[op_id as usize],
OpMetricsEvent::ErrorAsync,
);
}
}
args.push(match res {
Ok(v) => v,
Err(e) => OpResult::Err(OpError::new(&|_| "TypeError", e.into()))
.to_v8(scope)
.unwrap(),
});
}
let undefined: v8::Local<v8::Value> = v8::undefined(scope).into();
let has_tick_scheduled = context_state.has_next_tick_scheduled;
dispatched_ops |= has_tick_scheduled;
let rejections = if !exception_state
.pending_promise_rejections
.borrow_mut()
.is_empty()
{
let mut rejections =
exception_state.pending_promise_rejections.borrow_mut();
let arr = v8::Array::new(scope, (rejections.len() * 2) as i32);
let mut index = 0;
for rejection in rejections.drain(..) {
let value = v8::Local::new(scope, rejection.0);
arr.set_index(scope, index, value.into());
index += 1;
let value = v8::Local::new(scope, rejection.1);
arr.set_index(scope, index, value);
index += 1;
}
drop(rejections);
arr.into()
} else {
undefined
};
args.push(rejections);
let has_tick_scheduled = v8::Boolean::new(scope, has_tick_scheduled);
args.push(has_tick_scheduled.into());
let js_event_loop_tick_cb_handle =
context_state.js_event_loop_tick_cb.clone().unwrap();
let tc_scope = &mut v8::TryCatch::new(scope);
let js_event_loop_tick_cb = js_event_loop_tick_cb_handle.open(tc_scope);
drop(context_state);
js_event_loop_tick_cb.call(tc_scope, undefined, args.as_slice());
if tc_scope.has_terminated() || tc_scope.is_execution_terminating() {
return Ok(false);
}
if let Some(exception) = tc_scope.exception() {
return exception_to_err_result(tc_scope, exception, false, true);
}
Ok(dispatched_ops)
}
}