summaryrefslogtreecommitdiff
path: root/ops/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ops/lib.rs')
-rw-r--r--ops/lib.rs51
1 files changed, 18 insertions, 33 deletions
diff --git a/ops/lib.rs b/ops/lib.rs
index 54f4212bc..3dc7665e2 100644
--- a/ops/lib.rs
+++ b/ops/lib.rs
@@ -138,17 +138,19 @@ fn codegen_v8_async(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {
quote! {}
};
let rust_i0 = if uses_opstate { 1 } else { 0 };
- let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 2);
+ let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
let type_params = &f.sig.generics.params;
quote! {
use #core::futures::FutureExt;
- // SAFETY: Called from Deno.core.opAsync. Which retrieves the index using opId table.
- let op_id = unsafe {
- #core::v8::Local::<#core::v8::Integer>::cast(args.get(0))
- }.value() as usize;
+ // SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime
+ let ctx = unsafe {
+ &*(#core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked()).value()
+ as *const #core::_ops::OpCtx)
+ };
+ let op_id = ctx.id;
- let promise_id = args.get(1);
+ let promise_id = args.get(0);
let promise_id = #core::v8::Local::<#core::v8::Integer>::try_from(promise_id)
.map(|l| l.value() as #core::PromiseId)
.map_err(#core::anyhow::Error::from);
@@ -163,18 +165,8 @@ fn codegen_v8_async(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {
#arg_decls
- // SAFETY: Unchecked cast to external since #core guarantees args.data() is a v8 External.
- let state_refcell_raw = unsafe {
- #core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked())
- }.value();
-
- // SAFETY: The Rc<RefCell<OpState>> is functionally pinned and is tied to the isolate's lifetime
- let state = unsafe {
- let ptr = state_refcell_raw as *const std::cell::RefCell<#core::OpState>;
- // Increment so it will later be decremented/dropped by the underlaying func it is moved to
- std::rc::Rc::increment_strong_count(ptr);
- std::rc::Rc::from_raw(ptr)
- };
+ let state = ctx.state.clone();
+
// Track async call & get copy of get_error_class_fn
let get_class = {
let state = state.borrow();
@@ -199,30 +191,23 @@ fn codegen_v8_sync(core: &TokenStream2, f: &syn::ItemFn) -> TokenStream2 {
quote! {}
};
let rust_i0 = if uses_opstate { 1 } else { 0 };
- let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
+ let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 0);
let ret = codegen_sync_ret(core, &f.sig.output);
let type_params = &f.sig.generics.params;
quote! {
- // SAFETY: Called from Deno.core.opSync. Which retrieves the index using opId table.
- let op_id = unsafe {
- #core::v8::Local::<#core::v8::Integer>::cast(args.get(0)).value()
- } as usize;
+ // SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime
+ let ctx = unsafe {
+ &*(#core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked()).value()
+ as *const #core::_ops::OpCtx)
+ };
#arg_decls
- // SAFETY: Unchecked cast to external since #core guarantees args.data() is a v8 External.
- let state_refcell_raw = unsafe {
- #core::v8::Local::<#core::v8::External>::cast(args.data().unwrap_unchecked())
- }.value();
-
- // SAFETY: The Rc<RefCell<OpState>> is functionally pinned and is tied to the isolate's lifetime
- let state = unsafe { &*(state_refcell_raw as *const std::cell::RefCell<#core::OpState>) };
-
- let op_state = &mut state.borrow_mut();
+ let op_state = &mut ctx.state.borrow_mut();
let result = Self::call::<#type_params>(#args_head #args_tail);
- op_state.tracker.track_sync(op_id);
+ op_state.tracker.track_sync(ctx.id);
#ret
}