test wasm of the term package

This commit is contained in:
Mitchell Hashimoto
2022-08-17 13:57:21 -07:00
parent ead6e5a435
commit 2457454b07
6 changed files with 59 additions and 4 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
zig-cache/
zig-out/
/result*
example/*.wasm
test/ghostty
test/cases/**/*.actual.png

View File

@ -62,6 +62,7 @@ pub fn build(b: *std.build.Builder) !void {
wasm.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
wasm.setBuildMode(mode);
wasm.setOutputDir("zig-out");
wasm.addPackage(pkg_tracy);
const step = b.step("term-wasm", "Build the terminal.wasm library");
step.dependOn(&wasm.step);
@ -158,6 +159,9 @@ fn addDeps(
// Libuv
step.addPackage(libuv.pkg);
try libuv.link(b, step);
// Tracy
step.addPackage(pkg_tracy);
}
fn conformanceSteps(
@ -203,6 +207,11 @@ fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable;
}
pub const pkg_tracy = std.build.Pkg{
.name = "tracy",
.source = .{ .path = root() ++ "/src/tracy/tracy.zig" },
};
/// ANSI escape codes for colored log output
const color_map = std.ComptimeStringMap([]const u8, .{
&.{ "black", "30m" },

34
example/index.html Normal file
View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WASM Example</title>
</head>
<body>
<script>
const importObject = {
module: {},
env: {
// memory: new WebAssembly.Memory({ initial: 256 }),
}
};
fetch('ghostty-term.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results => {
const {
terminal_new,
terminal_free,
terminal_print,
} = results.instance.exports;
const term = terminal_new(80, 80);
console.log(term);
terminal_free(term);
terminal_print('a');
});
</script>
</body>
</html>

View File

@ -4,6 +4,7 @@
, glxinfo
, parallel
, pkg-config
, python
, scdoc
, tracy
, vulkan-loader
@ -47,6 +48,7 @@ in mkShell rec {
# Testing
gdb
parallel
python
tracy
vttest
wraptest

View File

@ -14,7 +14,7 @@ const csi = @import("csi.zig");
const sgr = @import("sgr.zig");
const Selection = @import("Selection.zig");
const Tabstops = @import("Tabstops.zig");
const trace = @import("../tracy/tracy.zig").trace;
const trace = @import("tracy").trace;
const color = @import("color.zig");
const Screen = @import("Screen.zig");

View File

@ -1,5 +1,8 @@
// This is the C-ABI API for the terminal package. This isn't used
// by other Zig programs but by C or WASM interfacing.
//
// NOTE: This is far, far from complete. We did a very minimal amount to
// prove that compilation works, but we haven't completed coverage yet.
const std = @import("std");
const builtin = @import("builtin");
@ -7,10 +10,10 @@ 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
const alloc = if (builtin.link_libc)
std.heap.c_allocator
else
std.heap.c_allocator;
std.heap.page_allocator;
export fn terminal_new(cols: usize, rows: usize) ?*Terminal {
const term = Terminal.init(alloc, cols, rows) catch return null;
@ -25,3 +28,9 @@ export fn terminal_free(ptr: ?*Terminal) void {
alloc.destroy(v);
}
}
export fn terminal_print(ptr: ?*Terminal, char: u32) void {
if (ptr) |t| {
t.print(@intCast(u21, char)) catch return null;
}
}