mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
39 lines
956 B
Zig
39 lines
956 B
Zig
//! This is a slower wrapper around a function pointer.
|
|
//! Prefer adding a task type directly to `Task` instead of using this.
|
|
|
|
const AnyTask = @This();
|
|
|
|
ctx: ?*anyopaque,
|
|
callback: *const (fn (*anyopaque) bun.JSError!void),
|
|
|
|
pub fn task(this: *AnyTask) Task {
|
|
return Task.init(this);
|
|
}
|
|
|
|
pub fn run(this: *AnyTask) bun.JSError!void {
|
|
@setRuntimeSafety(false);
|
|
const callback = this.callback;
|
|
const ctx = this.ctx;
|
|
try callback(ctx.?);
|
|
}
|
|
|
|
pub fn New(comptime Type: type, comptime Callback: anytype) type {
|
|
return struct {
|
|
pub fn init(ctx: *Type) AnyTask {
|
|
return AnyTask{
|
|
.callback = wrap,
|
|
.ctx = ctx,
|
|
};
|
|
}
|
|
|
|
pub fn wrap(this: ?*anyopaque) bun.JSError!void {
|
|
return @call(bun.callmod_inline, Callback, .{@as(*Type, @ptrCast(@alignCast(this.?)))});
|
|
}
|
|
};
|
|
}
|
|
|
|
const bun = @import("bun");
|
|
|
|
const jsc = bun.jsc;
|
|
const Task = jsc.Task;
|