summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js24
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;
}