diff options
author | Aapo Alasuutari <aapo.alasuutari@gmail.com> | 2023-02-22 21:09:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-22 19:09:59 +0000 |
commit | 0f9daaeacb402a7199e58b14ad01ec0091ac2c8d (patch) | |
tree | 0c5269bb8b7b4905e4cb1e4ade61149767479abf /ext | |
parent | 9b8992d4b4acb3a54ca7d988d181a266841013d9 (diff) |
fix(ext/ffi): Fix re-ref'ing UnsafeCallback (#17704)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/ffi/00_ffi.js | 24 | ||||
-rw-r--r-- | ext/ffi/callback.rs | 13 | ||||
-rw-r--r-- | ext/ffi/lib.rs | 2 |
3 files changed, 19 insertions, 20 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index 24a0dc913..d107e89fa 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -25,8 +25,11 @@ const { MathCeil, SafeMap, SafeArrayIterator, + SymbolFor, } = primordials; +const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId"); + const U32_BUFFER = new Uint32Array(2); const U64_BUFFER = new BigUint64Array(U32_BUFFER.buffer); const I64_BUFFER = new BigInt64Array(U32_BUFFER.buffer); @@ -360,12 +363,23 @@ class UnsafeCallback { this.callback = callback; } + static threadSafe(definition, callback) { + const unsafeCallback = new UnsafeCallback(definition, callback); + unsafeCallback.ref(); + return unsafeCallback; + } + ref() { if (this.#refcount++ === 0) { - this.#refpromise = core.opAsync( - "op_ffi_unsafe_callback_ref", - this.#rid, - ); + if (this.#refpromise) { + // Re-refing + core.refOp(this.#refpromise[promiseIdSymbol]); + } else { + this.#refpromise = core.opAsync( + "op_ffi_unsafe_callback_ref", + this.#rid, + ); + } } return this.#refcount; } @@ -374,7 +388,7 @@ class UnsafeCallback { // Only decrement refcount if it is positive, and only // unref the callback if refcount reaches zero. if (this.#refcount > 0 && --this.#refcount === 0) { - ops.op_ffi_unsafe_callback_unref(this.#rid); + core.unrefOp(this.#refpromise[promiseIdSymbol]); } return this.#refcount; } diff --git a/ext/ffi/callback.rs b/ext/ffi/callback.rs index d608c5432..ae2780391 100644 --- a/ext/ffi/callback.rs +++ b/ext/ffi/callback.rs @@ -532,19 +532,6 @@ pub fn op_ffi_unsafe_callback_ref( }) } -#[op(fast)] -pub fn op_ffi_unsafe_callback_unref( - state: &mut deno_core::OpState, - rid: u32, -) -> Result<(), AnyError> { - state - .resource_table - .get::<UnsafeCallbackResource>(rid)? - .cancel - .cancel(); - Ok(()) -} - #[derive(Deserialize)] pub struct RegisterCallbackArgs { parameters: Vec<NativeType>, diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index d49b66274..b8e3ac503 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -29,7 +29,6 @@ use call::op_ffi_call_ptr; use call::op_ffi_call_ptr_nonblocking; use callback::op_ffi_unsafe_callback_create; use callback::op_ffi_unsafe_callback_ref; -use callback::op_ffi_unsafe_callback_unref; use dlfcn::op_ffi_load; use dlfcn::ForeignFunction; use r#static::op_ffi_get_static; @@ -113,7 +112,6 @@ pub fn init<P: FfiPermissions + 'static>(unstable: bool) -> Extension { op_ffi_read_ptr::decl::<P>(), op_ffi_unsafe_callback_create::decl::<P>(), op_ffi_unsafe_callback_ref::decl(), - op_ffi_unsafe_callback_unref::decl(), ]) .event_loop_middleware(|op_state_rc, _cx| { // FFI callbacks coming in from other threads will call in and get queued. |