From d7fc6e0e3ba5943a2081c8ffb64fe462120db0e5 Mon Sep 17 00:00:00 2001 From: cirospaciari Date: Fri, 26 Jan 2024 14:54:43 -0300 Subject: [PATCH] more wrappers --- src/bun.js/ipc.zig | 19 +++++++++---------- src/deps/libuv.zig | 32 +++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/bun.js/ipc.zig b/src/bun.js/ipc.zig index 7228004893ae32..b789fb24b5d38d 100644 --- a/src/bun.js/ipc.zig +++ b/src/bun.js/ipc.zig @@ -322,7 +322,7 @@ const NamedPipeIPCData = struct { return .{ .err = err }; } ipc_pipe.data = @ptrCast(instance); - if (ipc_pipe.listenNamedPipe(named_pipe, 0, NewNamedPipeIPCHandler(Context).onNewClientConnect).asErr()) |err| { + if (ipc_pipe.listenNamedPipe(named_pipe, 0, instance, NewNamedPipeIPCHandler(Context).onNewClientConnect).asErr()) |err| { return .{ .err = err }; } @@ -340,7 +340,7 @@ const NamedPipeIPCData = struct { try ipc_pipe.init(uv.Loop.get(), true).unwrap(); ipc_pipe.data = @ptrCast(instance); this.connect_req.data = @ptrCast(instance); - try ipc_pipe.connect(&this.connect_req, named_pipe, NewNamedPipeIPCHandler(Context).onConnect).unwrap(); + try ipc_pipe.connect(&this.connect_req, named_pipe, instance, NewNamedPipeIPCHandler(Context).onConnect).unwrap(); this.writeVersionPacket(); } @@ -569,13 +569,12 @@ fn NewNamedPipeIPCHandler(comptime Context: type) type { } } - pub fn onNewClientConnect(req: *uv.uv_stream_t, status: c_int) callconv(.C) void { - log("onNewClientConnect {d}", .{status}); - if (status < 0) { + pub fn onNewClientConnect(this: *Context, status: uv.ReturnCode) void { + log("onNewClientConnect {d}", .{status.int()}); + if (status.errEnum()) |_| { Output.printErrorln("Failed to connect IPC pipe", .{}); return; } - const this = bun.cast(*Context, req.data); const client = &this.ipc.pipe; const server = &this.ipc.server; client.init(uv.Loop.get(), true).unwrap() catch { @@ -600,13 +599,13 @@ fn NewNamedPipeIPCHandler(comptime Context: type) type { }, } } - pub fn onConnect(req: *uv.uv_connect_t, status: c_int) callconv(.C) void { - log("onConnect {d}", .{status}); - if (status < 0) { + + pub fn onConnect(this: *Context, status: uv.ReturnCode) void { + log("onConnect {d}", .{status.int()}); + if (status.errEnum()) |_| { Output.printErrorln("Failed to connect IPC pipe", .{}); return; } - const this = bun.cast(*Context, req.data); this.ipc.pipe.readStart(this, onReadAlloc, onReadError, onRead).unwrap() catch { this.ipc.close(Context); Output.printErrorln("Failed to connect IPC pipe", .{}); diff --git a/src/deps/libuv.zig b/src/deps/libuv.zig index 883ecddbd2626d..d0cf16690eb050 100644 --- a/src/deps/libuv.zig +++ b/src/deps/libuv.zig @@ -479,15 +479,18 @@ fn ReqMixin(comptime Type: type) type { // https://docs.libuv.org/en/v1.x/stream.html fn StreamMixin(comptime Type: type) type { return struct { - - // pub extern fn uv_write(req: *uv_write_t, handle: *uv_stream_t, bufs: [*]const uv_buf_t, nbufs: c_uint, cb: uv_write_cb) ReturnCode; - // pub extern fn uv_write2(req: *uv_write_t, handle: *uv_stream_t, bufs: [*]const uv_buf_t, nbufs: c_uint, send_handle: *uv_stream_t, cb: uv_write_cb) ReturnCode; pub fn getWriteQueueSize(this: *Type) usize { return uv_stream_get_write_queue_size(@ptrCast(this)); } - pub fn listen(this: *Type, backlog: i32, cb: uv_connection_cb) Maybe(void) { - const rc = uv_listen(@ptrCast(this), backlog, cb); + pub fn listen(this: *Type, backlog: i32, context: anytype, comptime onConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) { + this.data = @ptrCast(context); + const Wrapper = struct { + pub fn uvConnectCb(handle: *uv_stream_t, status: ReturnCode) callconv(.C) void { + onConnect(@ptrCast(@alignCast(handle.data)), status); + } + }; + const rc = uv_listen(@ptrCast(this), backlog, &Wrapper.uvConnectCb); if (rc.errno()) |errno| { return .{ .err = .{ .errno = errno, .syscall = .listen, .from_libuv = true } }; } @@ -1094,7 +1097,7 @@ const struct_unnamed_385 = extern struct { write_reqs_pending: c_uint, shutdown_req: [*c]uv_shutdown_t, }; -pub const uv_connection_cb = ?*const fn (*uv_stream_t, c_int) callconv(.C) void; +pub const uv_connection_cb = ?*const fn (*uv_stream_t, ReturnCode) callconv(.C) void; const struct_unnamed_389 = extern struct { connection_cb: uv_connection_cb, }; @@ -1402,11 +1405,11 @@ pub const struct_uv_pipe_s = extern struct { return .{ .result = {} }; } - pub fn listenNamedPipe(this: *@This(), named_pipe: []const u8, backlog: i32, onClientConnect: uv_connection_cb) Maybe(void) { + pub fn listenNamedPipe(this: *@This(), named_pipe: []const u8, backlog: i32, context: anytype, comptime onClientConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) { if (this.bind(named_pipe, 0).asErr()) |err| { return .{ .err = err }; } - return this.listen(backlog, onClientConnect); + return this.listen(backlog, context, onClientConnect); } pub fn bind(this: *@This(), named_pipe: []const u8, flags: i32) Maybe(void) { @@ -1417,8 +1420,15 @@ pub const struct_uv_pipe_s = extern struct { return .{ .result = {} }; } - pub fn connect(this: *@This(), req: *uv_connect_t, name: []const u8, cb: uv_connect_cb) Maybe(void) { - const rc = uv_pipe_connect2(req, this, @ptrCast(name.ptr), name.len, 0, cb); + pub fn connect(this: *@This(), req: *uv_connect_t, name: []const u8, context: anytype, comptime onConnect: *const (fn (@TypeOf(context), ReturnCode) void)) Maybe(void) { + this.data = @ptrCast(context); + const Wrapper = struct { + pub fn uvConnectCb(handle: *uv_connect_t, status: ReturnCode) callconv(.C) void { + onConnect(@ptrCast(@alignCast(handle.data)), status); + } + }; + + const rc = uv_pipe_connect2(req, this, @ptrCast(name.ptr), name.len, 0, &Wrapper.uvConnectCb); if (rc.errno()) |errno| { return .{ .err = .{ .errno = errno, .syscall = .connect2, .from_libuv = true } }; } @@ -1740,7 +1750,7 @@ const union_unnamed_441 = extern union { connect: struct_unnamed_443, }; pub const uv_connect_t = struct_uv_connect_s; -pub const uv_connect_cb = ?*const fn (*uv_connect_t, c_int) callconv(.C) void; +pub const uv_connect_cb = ?*const fn (*uv_connect_t, ReturnCode) callconv(.C) void; pub const struct_uv_connect_s = extern struct { data: ?*anyopaque, type: uv_req_type,