diff options
| author | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-08-17 13:57:21 -0700 |
|---|---|---|
| committer | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-08-17 13:57:21 -0700 |
| commit | 2457454b07f0b24e6bbb149cb1498fb4671b465f (patch) | |
| tree | b50160310741bae25483de0512af2871d1036a84 | |
| parent | ead6e5a4359f3c8122299565c5f163b7b35d079b (diff) | |
test wasm of the term package
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | build.zig | 9 | ||||
| -rw-r--r-- | example/index.html | 34 | ||||
| -rw-r--r-- | nix/devshell.nix | 2 | ||||
| -rw-r--r-- | src/terminal/Terminal.zig | 2 | ||||
| -rw-r--r-- | src/terminal/c_api.zig | 15 |
6 files changed, 59 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore index 0135036b0..83d1031dc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ zig-cache/ zig-out/ /result* +example/*.wasm test/ghostty test/cases/**/*.actual.png @@ -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" }, diff --git a/example/index.html b/example/index.html new file mode 100644 index 000000000..f93da37d7 --- /dev/null +++ b/example/index.html @@ -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> diff --git a/nix/devshell.nix b/nix/devshell.nix index ebc44c3d9..b587e1915 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -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 diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index ac934263b..d86e5c2da 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -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"); diff --git a/src/terminal/c_api.zig b/src/terminal/c_api.zig index fbebd0815..65bc54208 100644 --- a/src/terminal/c_api.zig +++ b/src/terminal/c_api.zig @@ -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; + } +} |
