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/ffi/00_ffi.js | |
parent | 9b8992d4b4acb3a54ca7d988d181a266841013d9 (diff) |
fix(ext/ffi): Fix re-ref'ing UnsafeCallback (#17704)
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 24 |
1 files changed, 19 insertions, 5 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; } |