Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/deno/cli/tsc/dts/lib.deno.ns.d.ts>Deno.UnsafeCallback

A modern runtime for JavaScript and TypeScript.
Latest
class Deno.UnsafeCallback
import { Deno } from "https://deno.land/x/deno@v2.0.4/cli/tsc/dts/lib.deno.ns.d.ts";
const { UnsafeCallback } = Deno;

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"]>)

Properties

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

The callback function.

readonly
definition: Definition

The definition of the unsafe callback.

readonly
pointer: PointerObject<Definition>

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.