Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/node/ops/handle_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl HandleWrap {

if let Some(cb) = cb {
let recv = v8::undefined(scope);
cb.open(scope).call(scope, recv.into(), &[]);
v8::Local::new(scope, &cb).call(scope, recv.into(), &[]);
}
};

Expand Down
88 changes: 46 additions & 42 deletions ext/node/ops/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ impl ContextifyScript {
}

pub struct ContextifyContext {
microtask_queue: *mut v8::MicrotaskQueue,
/// Owns the queue for contexts created with `microtaskMode: "afterEvaluate"`,
/// and is `None` otherwise (the context then uses the isolate's default
/// queue). Dropping the handle releases a root into the isolate's heap; the
/// associated context keeps the queue itself alive for as long as it needs
/// it, so there is nothing else to tear down by hand.
microtask_queue: Option<v8::OwnedMicrotaskQueue>,
context: v8::TracedReference<v8::Context>,
sandbox: v8::TracedReference<v8::Object>,
allow_code_gen_wasm: bool,
Expand All @@ -371,17 +376,6 @@ unsafe impl deno_core::GarbageCollected for ContextifyContext {
}
}

impl Drop for ContextifyContext {
fn drop(&mut self) {
if !self.microtask_queue.is_null() {
// SAFETY: If this isn't null, it is a valid MicrotaskQueue.
unsafe {
std::ptr::drop_in_place(self.microtask_queue);
}
}
}
}

// A unique tag pointer used as a marker in embedder slot
// `CONTEXTIFY_TAG_SLOT` to identify v8::Context instances created by
// `ContextifyContext::attach` / `attach_vanilla`. The wasm code generation
Expand Down Expand Up @@ -470,17 +464,14 @@ impl ContextifyContext {

let tmp = init_global_template(scope, ContextInitMode::UseSnapshot);

let microtask_queue = if own_microtask_queue {
v8::MicrotaskQueue::new(scope, v8::MicrotasksPolicy::Explicit).into_raw()
} else {
std::ptr::null_mut()
};
let microtask_queue = own_microtask_queue
.then(|| v8::MicrotaskQueue::new(scope, v8::MicrotasksPolicy::Explicit));

let context = create_v8_context(
scope,
tmp,
ContextInitMode::UseSnapshot,
microtask_queue,
microtask_queue_ptr(&microtask_queue),
);

let context_state = main_context.get_aligned_pointer_from_embedder_data(
Expand Down Expand Up @@ -566,20 +557,18 @@ impl ContextifyContext {
) -> v8::Local<'s, v8::Object> {
let main_context = scope.get_current_context();

let microtask_queue = if own_microtask_queue {
v8::MicrotaskQueue::new(scope, v8::MicrotasksPolicy::Explicit).into_raw()
} else {
std::ptr::null_mut()
};
let microtask_queue = own_microtask_queue
.then(|| v8::MicrotaskQueue::new(scope, v8::MicrotasksPolicy::Explicit));

// Create a vanilla V8 context without global template (no interceptors)
let context = {
let queue_ptr = microtask_queue_ptr(&microtask_queue);
let esc_scope = std::pin::pin!(v8::EscapableHandleScope::new(scope));
let esc_scope = &mut esc_scope.init();
let ctx = v8::Context::new(
esc_scope,
v8::ContextOptions {
microtask_queue: Some(microtask_queue),
microtask_queue: Some(queue_ptr),
..Default::default()
},
);
Expand Down Expand Up @@ -714,12 +703,14 @@ impl ContextifyContext {
}

fn microtask_queue(&self) -> Option<&v8::MicrotaskQueue> {
if self.microtask_queue.is_null() {
None
} else {
// SAFETY: If this isn't null, it is a valid MicrotaskQueue.
Some(unsafe { &*self.microtask_queue })
}
self.microtask_queue.as_deref()
}

/// A non-owning alias of the queue, for the callers that still need to hand
/// it to V8 as a raw pointer. Null when this context uses the isolate's
/// default queue.
fn microtask_queue_ptr(&self) -> *mut v8::MicrotaskQueue {
microtask_queue_ptr(&self.microtask_queue)
}

fn get<'a, 'c>(
Expand Down Expand Up @@ -748,6 +739,21 @@ pub enum ContextInitMode {
UseSnapshot,
}

/// A non-owning alias of `handle`'s queue, for passing to
/// `v8::ContextOptions::microtask_queue`, which still takes a raw pointer.
/// Null when there is no handle, which tells V8 to use the isolate's default
/// queue.
///
/// The queue is only borrowed here: whoever holds the [`v8::OwnedMicrotaskQueue`]
/// keeps it alive, and must outlive every context created with the pointer.
fn microtask_queue_ptr(
handle: &Option<v8::OwnedMicrotaskQueue>,
) -> *mut v8::MicrotaskQueue {
handle.as_ref().map_or(std::ptr::null_mut(), |queue| {
&**queue as *const v8::MicrotaskQueue as *mut v8::MicrotaskQueue
})
}

pub fn create_v8_context<'a>(
scope: &mut v8::PinScope<'a, '_, ()>,
object_template: v8::Local<v8::ObjectTemplate>,
Expand Down Expand Up @@ -998,7 +1004,7 @@ fn property_setter<'s>(
key: v8::Local<'s, v8::Name>,
value: v8::Local<'s, v8::Value>,
args: v8::PropertyCallbackArguments<'s>,
_rv: v8::ReturnValue<()>,
_rv: v8::PropertyInterceptorReturnValue<'_>,
) -> v8::Intercepted {
let Some(ctx) = ContextifyContext::get(scope, args.holder()) else {
return v8::Intercepted::kNo;
Expand Down Expand Up @@ -1114,7 +1120,7 @@ fn property_definer<'s>(
key: v8::Local<'s, v8::Name>,
desc: &v8::PropertyDescriptor,
args: v8::PropertyCallbackArguments<'s>,
_: v8::ReturnValue<()>,
_: v8::PropertyInterceptorReturnValue<'_>,
) -> v8::Intercepted {
let Some(ctx) = ContextifyContext::get(scope, args.holder()) else {
return v8::Intercepted::kNo;
Expand Down Expand Up @@ -1323,7 +1329,7 @@ fn indexed_property_setter<'s>(
index: u32,
value: v8::Local<'s, v8::Value>,
args: v8::PropertyCallbackArguments<'s>,
rv: v8::ReturnValue<()>,
rv: v8::PropertyInterceptorReturnValue<'_>,
) -> v8::Intercepted {
let key = uint32_to_name(scope, index);
property_setter(scope, key, value, args, rv)
Expand All @@ -1344,7 +1350,7 @@ fn indexed_property_definer<'s>(
index: u32,
descriptor: &v8::PropertyDescriptor,
args: v8::PropertyCallbackArguments<'s>,
rv: v8::ReturnValue<()>,
rv: v8::PropertyInterceptorReturnValue<'_>,
) -> v8::Intercepted {
let key = uint32_to_name(scope, index);
property_definer(scope, key, descriptor, args, rv)
Expand Down Expand Up @@ -1635,13 +1641,11 @@ pub fn op_vm_script_create_cached_data<'s>(
pub struct ContextifyModule {
module: v8::TracedReference<v8::Module>,
context: v8::TracedReference<v8::Context>,
// SAFETY invariant: `microtask_queue` aliases the `MicrotaskQueue` owned
// by the `ContextifyContext` of `context`. The queue is a separate C++
// allocation, but `ContextifyContext` keeps it alive for as long as the
// context is reachable, and the `TracedReference<v8::Context>` above
// keeps the context reachable for this module's lifetime. Dereferencing
// is therefore safe as long as no code path drops the `ContextifyContext`
// while a `ContextifyModule` is still live.
// SAFETY invariant: `microtask_queue` aliases the queue whose handle is held
// by the `ContextifyContext` of `context`. The `TracedReference<v8::Context>`
// above keeps that context reachable for this module's lifetime, and a
// context keeps its queue alive independently of the handle, so the alias
// stays valid even if the owning `ContextifyContext` is collected first.
microtask_queue: *mut v8::MicrotaskQueue,
identifier: String,
/// Map of import specifier -> resolved ContextifyModule wrapper object.
Expand Down Expand Up @@ -1845,7 +1849,7 @@ fn resolve_module_context<'s>(
if let Some(context_object) = context_object {
let contextify =
ContextifyContext::from_sandbox_obj(scope, context_object)?;
Some((contextify.context(scope), contextify.microtask_queue))
Some((contextify.context(scope), contextify.microtask_queue_ptr()))
} else {
Some((scope.get_current_context(), std::ptr::null_mut()))
}
Expand Down
4 changes: 2 additions & 2 deletions ext/webgpu/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Drop for GPUBuffer {
impl GPUBuffer {
fn detach_mapped_js_buffers(&self, scope: &mut v8::PinScope<'_, '_>) {
for mapped in self.mapped_js_buffers.replace(vec![]) {
let ab = mapped.buffer.open(scope);
let ab = v8::Local::new(scope, &mapped.buffer);
ab.detach(None);
}
}
Expand All @@ -90,7 +90,7 @@ impl GPUBuffer {
) -> Result<(), BufferError> {
let mut first_error = None;
for mapped in self.mapped_js_buffers.replace(vec![]) {
let ab = mapped.buffer.open(scope);
let ab = v8::Local::new(scope, &mapped.buffer);
// A zero `byte_length` means the range is already detached, which can
// only happen if the caller detached it themselves (e.g. by transferring
// it to a worker). There is nothing left to read from, so the range is
Expand Down
2 changes: 1 addition & 1 deletion ext/webgpu/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl DeviceErrorHandler {
.unwrap();

let recv = v8::Local::new(scope, device);
func.open(scope).call(scope, recv, &[event.into()]);
v8::Local::new(scope, &func).call(scope, recv, &[event.into()]);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions libs/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub fn to_v8_error<'s, 'i>(
.borrow()
.clone()
.expect("Custom error builder must be set");
let cb = cb.open(tc_scope);
let cb = v8::Local::new(tc_scope, &cb);
let this = v8::undefined(tc_scope).into();
let class = v8::String::new(tc_scope, &error.get_class()).unwrap();
let message = v8::String::new(tc_scope, &error.get_message()).unwrap();
Expand Down Expand Up @@ -1040,7 +1040,7 @@ impl JsError {
let js_format_exception_cb =
exception_state.js_format_exception_cb.borrow().clone();
if let Some(format_exception_cb) = js_format_exception_cb {
let format_exception_cb = format_exception_cb.open(scope);
let format_exception_cb = v8::Local::new(scope, &format_exception_cb);
let this = v8::undefined(scope).into();
let formatted = format_exception_cb.call(scope, this, &[exception]);
if let Some(formatted) = formatted
Expand Down
28 changes: 14 additions & 14 deletions libs/core/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ impl ModuleMap {
}

// No pending TLA, safe to resolve immediately
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
let module_namespace = module.get_module_namespace();
resolver.resolve(scope, module_namespace).unwrap();

Expand All @@ -1835,15 +1835,15 @@ impl ModuleMap {
{
match self.lazy_load_esm_module(scope, module_specifier.as_str()) {
Ok(module_ns) => {
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
let module_ns_local = v8::Local::new(scope, module_ns);
resolver.resolve(scope, module_ns_local).unwrap();
return false;
}
Err(e) => {
let exception = e.to_v8_error(scope);
let exception_local = v8::Local::new(scope, exception);
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
resolver.reject(scope, exception_local).unwrap();
return false;
}
Expand All @@ -1866,15 +1866,15 @@ impl ModuleMap {
.lazy_load_synthetic_esm_module(scope, module_specifier.as_str())
{
Ok(module_ns) => {
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
let module_ns_local = v8::Local::new(scope, module_ns);
resolver.resolve(scope, module_ns_local).unwrap();
return false;
}
Err(e) => {
let exception = e.to_v8_error(scope);
let exception_local = v8::Local::new(scope, exception);
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
resolver.reject(scope, exception_local).unwrap();
return false;
}
Expand Down Expand Up @@ -2219,7 +2219,7 @@ impl ModuleMap {
let module_handle = self.get_handle(id).expect("ModuleInfo not found");

let status = {
let module = module_handle.open(scope);
let module = v8::Local::new(scope, &module_handle);
module.get_status()
};

Expand Down Expand Up @@ -2315,7 +2315,7 @@ impl ModuleMap {
let mut resolved_any = false;
let mut still_pending = vec![];
for eval in pending {
let promise = eval.promise.open(scope);
let promise = v8::Local::new(scope, &eval.promise);
match promise.state() {
v8::PromiseState::Pending => {
still_pending.push(eval);
Expand Down Expand Up @@ -2350,7 +2350,7 @@ impl ModuleMap {
let module_namespace = module.get_module_namespace();

for resolver_handle in waiters {
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
resolver.resolve(scope, module_namespace).unwrap();
}
if !JsRealm::state_from_scope(scope).has_tick_scheduled() {
Expand All @@ -2370,7 +2370,7 @@ impl ModuleMap {
if let Some(waiters) = waiters {
let exception = v8::Local::new(scope, exception);
for resolver_handle in waiters {
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);
resolver.reject(scope, exception).unwrap();
}
if !JsRealm::state_from_scope(scope).has_tick_scheduled() {
Expand All @@ -2391,7 +2391,7 @@ impl ModuleMap {
.remove(&id)
.expect("Invalid dynamic import id")
.resolver;
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);

let exception = v8::Local::new(scope, exception);
resolver.reject(scope, exception).unwrap();
Expand All @@ -2412,7 +2412,7 @@ impl ModuleMap {
.remove(&id)
.expect("Invalid dynamic import id")
.resolver;
let resolver = resolver_handle.open(scope);
let resolver = v8::Local::new(scope, &resolver_handle);

let module = self
.data
Expand Down Expand Up @@ -2636,7 +2636,7 @@ impl ModuleMap {
.remove(&dyn_import_id)
.expect("Invalid dynamic import id")
.resolver;
let resolver = resolver_handle.open(tc_scope);
let resolver = v8::Local::new(tc_scope, &resolver_handle);
resolver.resolve(tc_scope, module_namespace).unwrap();
tc_scope.perform_microtask_checkpoint();
}
Expand Down Expand Up @@ -2684,7 +2684,7 @@ impl ModuleMap {
let source = data.sources.get(&key).expect("Source had to have been inserted successfully, or recursion would error.");
v8::Local::new(scope, source).into()
};
let resolver = state.resolver.open(scope);
let resolver = v8::Local::new(scope, &state.resolver);
resolver.resolve(scope, source).unwrap();
}
}
Expand Down Expand Up @@ -2733,7 +2733,7 @@ impl ModuleMap {
.get_handle(module_id)
.expect("ModuleInfo not found");

let module = module_handle.open(scope);
let module = v8::Local::new(scope, &module_handle);

if module.get_status() == v8::ModuleStatus::Errored {
let exception = module.get_exception();
Expand Down
4 changes: 1 addition & 3 deletions libs/core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,9 +1407,7 @@ pub fn wasm_streaming_callback<'a>(

let undefined = v8::undefined(scope);
let rid = serde_v8::to_v8(scope, streaming_rid).unwrap();
cb_handle
.open(scope)
.call(scope, undefined.into(), &[arg, rid]);
v8::Local::new(scope, &cb_handle).call(scope, undefined.into(), &[arg, rid]);
}

// This op is re-entrant as it makes a v8 call. It also cannot be fast because
Expand Down
2 changes: 1 addition & 1 deletion libs/core/runtime/jsrealm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl JsRealmInner {
std::mem::take(&mut *state.js_wasm_streaming_cb.borrow_mut());

{
let ctx = self.context().open(scope);
let ctx = v8::Local::new(scope, self.context());
// SAFETY: Clear all embedder data
unsafe {
let ctx_state =
Expand Down
Loading
Loading