diff --git a/build.zig b/build.zig index 462a26408..9c6b4b9ce 100644 --- a/build.zig +++ b/build.zig @@ -52,6 +52,21 @@ pub fn build(b: *std.build.Builder) !void { if (tracy) try tracylib.link(b, exe, target); } + // term.wasm + { + const wasm = b.addSharedLibrary( + "ghostty-term", + "src/terminal/main_wasm.zig", + .{ .unversioned = {} }, + ); + wasm.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); + wasm.setBuildMode(mode); + wasm.setOutputDir("zig-out"); + + const step = b.step("term-wasm", "Build the terminal.wasm library"); + step.dependOn(&wasm.step); + } + // Run { // Build our run step, which runs the main app by default, but will diff --git a/nix/devshell.nix b/nix/devshell.nix index 88873141a..ebc44c3d9 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -8,6 +8,8 @@ , tracy , vulkan-loader , vttest +, wabt +, wasmtime , wraptest , zig @@ -48,6 +50,10 @@ in mkShell rec { tracy vttest wraptest + + # wasm + wabt + wasmtime ]; buildInputs = [ diff --git a/src/terminal/c_api.zig b/src/terminal/c_api.zig new file mode 100644 index 000000000..fbebd0815 --- /dev/null +++ b/src/terminal/c_api.zig @@ -0,0 +1,27 @@ +// This is the C-ABI API for the terminal package. This isn't used +// by other Zig programs but by C or WASM interfacing. + +const std = @import("std"); +const builtin = @import("builtin"); +const Allocator = std.mem.Allocator; +const Terminal = @import("main.zig").Terminal; + +// The allocator that we want to use. +const alloc = if (builtin.target.isWasm()) + std.heap.page_allocator +else + std.heap.c_allocator; + +export fn terminal_new(cols: usize, rows: usize) ?*Terminal { + const term = Terminal.init(alloc, cols, rows) catch return null; + const result = alloc.create(Terminal) catch return null; + result.* = term; + return result; +} + +export fn terminal_free(ptr: ?*Terminal) void { + if (ptr) |v| { + v.deinit(alloc); + alloc.destroy(v); + } +} diff --git a/src/terminal/main.zig b/src/terminal/main.zig index 4346b8377..fd75852e4 100644 --- a/src/terminal/main.zig +++ b/src/terminal/main.zig @@ -1,3 +1,5 @@ +const builtin = @import("builtin"); + const stream = @import("stream.zig"); const ansi = @import("ansi.zig"); const csi = @import("csi.zig"); @@ -21,8 +23,6 @@ pub const EraseLine = csi.EraseLine; pub const TabClear = csi.TabClear; pub const Attribute = sgr.Attribute; -// Not exported because they're just used for tests. - test { _ = ansi; _ = color; diff --git a/src/terminal/main_wasm.zig b/src/terminal/main_wasm.zig new file mode 100644 index 000000000..920cb59c1 --- /dev/null +++ b/src/terminal/main_wasm.zig @@ -0,0 +1,4 @@ +// This is the main file for the WASM module. The WASM module just +// imports the C API. + +pub usingnamespace @import("c_api.zig");