src/terminal can build into a minimal wasm library, I think

This commit is contained in:
Mitchell Hashimoto
2022-08-17 12:25:53 -07:00
parent 37b854f77c
commit ead6e5a435
5 changed files with 54 additions and 2 deletions

View File

@ -52,6 +52,21 @@ pub fn build(b: *std.build.Builder) !void {
if (tracy) try tracylib.link(b, exe, target); 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 // Run
{ {
// Build our run step, which runs the main app by default, but will // Build our run step, which runs the main app by default, but will

View File

@ -8,6 +8,8 @@
, tracy , tracy
, vulkan-loader , vulkan-loader
, vttest , vttest
, wabt
, wasmtime
, wraptest , wraptest
, zig , zig
@ -48,6 +50,10 @@ in mkShell rec {
tracy tracy
vttest vttest
wraptest wraptest
# wasm
wabt
wasmtime
]; ];
buildInputs = [ buildInputs = [

27
src/terminal/c_api.zig Normal file
View File

@ -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);
}
}

View File

@ -1,3 +1,5 @@
const builtin = @import("builtin");
const stream = @import("stream.zig"); const stream = @import("stream.zig");
const ansi = @import("ansi.zig"); const ansi = @import("ansi.zig");
const csi = @import("csi.zig"); const csi = @import("csi.zig");
@ -21,8 +23,6 @@ pub const EraseLine = csi.EraseLine;
pub const TabClear = csi.TabClear; pub const TabClear = csi.TabClear;
pub const Attribute = sgr.Attribute; pub const Attribute = sgr.Attribute;
// Not exported because they're just used for tests.
test { test {
_ = ansi; _ = ansi;
_ = color; _ = color;

View File

@ -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");