Skip to content

zigar.function.release(fn_ptr)

Chung Leong edited this page Mar 15, 2025 · 3 revisions

Release a Zig-to-JavaScript function bridge, freeing memory allocated for the Zig function and allowing the JavaScript function to be garbage-collected.

If the pointer given points to a regular Zig function, nothing happens.

Usage:

const std = @import("std");
const zigar = @import("zigar");

pub const Callback = fn (i32, []const u8) void;

fn none(_: i32, _: []const u8) void {}

var callback: *const Callback = &none;

pub fn setCallback(cb: ?*const Callback) void {
    zigar.function.release(callback);
    callback = cb orelse &none;
}

pub fn runCallback() void {
    callback(123, "Hello world");
}
import { Callback, runCallback, setCallback } from './javascript-fn-example-2.zig';

function hello(number, text) {
    console.log(`number = ${number}, text = ${text.string}`);
}
const callback = new Callback(hello);
setCallback(callback);
runCallback();
setCallback(null);
number = 123, text = Hello world

Arguments:

  • fn_ptr: anytype
    Pointer to a function.

Return value:

void

Clone this wiki locally