Deno.UnsafeCallback - Deno documentation
class Deno.UnsafeCallback
Unstable

An unsafe function pointer for passing JavaScript functions as C function pointers to foreign function calls.

The function pointer remains valid until the close() method is called.

All UnsafeCallback are always thread safe in that they can be called from foreign threads without crashing. However, they do not wake up the Deno event loop by default.

If a callback is to be called from foreign threads, use the threadSafe() static constructor or explicitly call ref() to have the callback wake up the Deno event loop when called from foreign threads. This also stops Deno's process from exiting while the callback still exists and is not unref'ed.

Use deref() to then allow Deno's process to exit. Calling deref() on a ref'ed callback does not stop it from waking up the Deno event loop when called from foreign threads.

Constructors

new
UnsafeCallback(
definition: Definition,
callback: UnsafeCallbackFunction<Definition["parameters"], Definition["result"]>,
)

Type Parameters

Properties

readonly
callback: UnsafeCallbackFunction<Definition["parameters"], Definition["result"]>

The callback function.

The definition of the unsafe callback.

The pointer to the unsafe callback.

Methods

close(): void

Removes the C function pointer associated with this instance.

Continuing to use the instance or the C function pointer after closing the UnsafeCallback will lead to errors and crashes.

Calling this method sets the callback's reference counting to zero, stops the callback from waking up the Deno event loop when called from foreign threads and no longer keeps Deno's process from exiting.

ref(): number

Increments the callback's reference counting and returns the new reference count.

After ref() has been called, the callback always wakes up the Deno event loop when called from foreign threads.

If the callback's reference count is non-zero, it keeps Deno's process from exiting.

unref(): number

Decrements the callback's reference counting and returns the new reference count.

Calling unref() does not stop a callback from waking up the Deno event loop when called from foreign threads.

If the callback's reference counter is zero, it no longer keeps Deno's process from exiting.

Static Methods

Creates an UnsafeCallback and calls ref() once to allow it to wake up the Deno event loop when called from foreign threads.

This also stops Deno's process from exiting while the callback still exists and is not unref'ed.