terminal: shuffle some C APIs to make it more long term maintainable

pull/8941/head
Mitchell Hashimoto 2025-09-27 13:20:50 -07:00
parent e4e8a61e0c
commit 8a1dc5bd97
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
4 changed files with 28 additions and 20 deletions

12
src/terminal/c/main.zig Normal file
View File

@ -0,0 +1,12 @@
pub const osc = @import("osc.zig");
// The full C API, unexported.
pub const osc_new = osc.new;
pub const osc_free = osc.free;
test {
_ = osc;
// We want to make sure we run the tests for the C allocator interface.
_ = @import("../../lib/allocator.zig");
}

View File

@ -1,22 +1,17 @@
const std = @import("std"); const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const builtin = @import("builtin"); const builtin = @import("builtin");
const lib_alloc = @import("../lib/allocator.zig"); const lib_alloc = @import("../../lib/allocator.zig");
const CAllocator = lib_alloc.Allocator; const CAllocator = lib_alloc.Allocator;
const osc = @import("osc.zig"); const osc = @import("../osc.zig");
const Result = @import("result.zig").Result;
/// C: GhosttyOscParser /// C: GhosttyOscParser
pub const OscParser = ?*osc.Parser; pub const Parser = ?*osc.Parser;
/// C: GhosttyResult pub fn new(
pub const Result = enum(c_int) {
success = 0,
out_of_memory = -1,
};
pub fn osc_new(
alloc_: ?*const CAllocator, alloc_: ?*const CAllocator,
result: *OscParser, result: *Parser,
) callconv(.c) Result { ) callconv(.c) Result {
const alloc = lib_alloc.default(alloc_); const alloc = lib_alloc.default(alloc_);
const ptr = alloc.create(osc.Parser) catch const ptr = alloc.create(osc.Parser) catch
@ -26,7 +21,7 @@ pub fn osc_new(
return .success; return .success;
} }
pub fn osc_free(parser_: OscParser) callconv(.c) void { pub fn free(parser_: Parser) callconv(.c) void {
// C-built parsers always have an associated allocator. // C-built parsers always have an associated allocator.
const parser = parser_ orelse return; const parser = parser_ orelse return;
const alloc = parser.alloc.?; const alloc = parser.alloc.?;
@ -34,16 +29,12 @@ pub fn osc_free(parser_: OscParser) callconv(.c) void {
alloc.destroy(parser); alloc.destroy(parser);
} }
test {
_ = lib_alloc;
}
test "osc" { test "osc" {
const testing = std.testing; const testing = std.testing;
var p: OscParser = undefined; var p: Parser = undefined;
try testing.expectEqual(Result.success, osc_new( try testing.expectEqual(Result.success, new(
&lib_alloc.test_allocator, &lib_alloc.test_allocator,
&p, &p,
)); ));
osc_free(p); free(p);
} }

View File

@ -0,0 +1,5 @@
/// C: GhosttyResult
pub const Result = enum(c_int) {
success = 0,
out_of_memory = -1,
};

View File

@ -64,7 +64,7 @@ pub const isSafePaste = sanitize.isSafePaste;
/// This is set to true when we're building the C library. /// This is set to true when we're building the C library.
pub const is_c_lib = @import("root") == @import("../lib_vt.zig"); pub const is_c_lib = @import("root") == @import("../lib_vt.zig");
pub const c_api = @import("c_api.zig"); pub const c_api = @import("c/main.zig");
test { test {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());