mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-05-05 15:33:00 +00:00
hook up logging to wasm, example uses new zig-js package
This commit is contained in:
parent
437f1772f1
commit
c48d8cd151
4
example/.gitignore
vendored
Normal file
4
example/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.parcel-cache/
|
||||
dist/
|
||||
node_modules/
|
||||
example.wasm*
|
49
example/app.ts
Normal file
49
example/app.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { ZigJS } from 'zig-js';
|
||||
|
||||
const zjs = new ZigJS();
|
||||
const importObject = {
|
||||
module: {},
|
||||
env: {
|
||||
log: (ptr: number, len: number) => {
|
||||
const view = new DataView(zjs.memory.buffer, ptr, Number(len));
|
||||
const str = new TextDecoder('utf-8').decode(view);
|
||||
console.log(str);
|
||||
},
|
||||
},
|
||||
|
||||
...zjs.importObject(),
|
||||
};
|
||||
|
||||
const url = new URL('ghostty-wasm.wasm', import.meta.url);
|
||||
fetch(url.href).then(response =>
|
||||
response.arrayBuffer()
|
||||
).then(bytes =>
|
||||
WebAssembly.instantiate(bytes, importObject)
|
||||
).then(results => {
|
||||
const {
|
||||
memory,
|
||||
malloc,
|
||||
free,
|
||||
face_new,
|
||||
face_free,
|
||||
} = results.instance.exports;
|
||||
// Give us access to the zjs value for debugging.
|
||||
globalThis.zjs = zjs;
|
||||
console.log(zjs);
|
||||
|
||||
// Initialize our zig-js memory
|
||||
zjs.memory = memory;
|
||||
|
||||
// Create some memory for our string
|
||||
const font = new TextEncoder().encode("monospace");
|
||||
const font_ptr = malloc(font.byteLength);
|
||||
try {
|
||||
new Uint8Array(memory.buffer, font_ptr).set(font);
|
||||
|
||||
// Call whatever example you want:
|
||||
const face = face_new(font_ptr, font.byteLength);
|
||||
face_free(face);
|
||||
} finally {
|
||||
free(font_ptr);
|
||||
}
|
||||
});
|
@ -1,36 +1,11 @@
|
||||
<!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-wasm.wasm').then(response =>
|
||||
response.arrayBuffer()
|
||||
).then(bytes =>
|
||||
WebAssembly.instantiate(bytes, importObject)
|
||||
).then(results => {
|
||||
const {
|
||||
atlas_new,
|
||||
atlas_free,
|
||||
atlas_reserve,
|
||||
free,
|
||||
memory,
|
||||
} = results.instance.exports;
|
||||
|
||||
const atlas = atlas_new(512, 0);
|
||||
const reg = atlas_reserve(atlas, 10, 10);
|
||||
free(reg);
|
||||
atlas_free(atlas);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Ghostty Example</title>
|
||||
<script type="module" src="app.ts"></script>
|
||||
</head>
|
||||
<body>
|
||||
Open your console, we are just debugging here.
|
||||
</body>
|
||||
</html>
|
||||
|
4436
example/package-lock.json
generated
Normal file
4436
example/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
22
example/package.json
Normal file
22
example/package.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "ghostty example",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing ghostty and wasm.",
|
||||
"source": "index.html",
|
||||
"browserslist": "> 0.5%, last 2 versions, not dead",
|
||||
"scripts": {
|
||||
"start": "parcel",
|
||||
"build": "parcel build",
|
||||
"check": "tsc --noEmit"
|
||||
},
|
||||
"author": "Mitchell Hashimoto",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@parcel/transformer-inline-string": "^2.8.0",
|
||||
"parcel": "^2.8.0",
|
||||
"typescript": "^4.9.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"zig-js": "file:../vendor/zig-js/js"
|
||||
}
|
||||
}
|
@ -49,6 +49,8 @@ pub const Face = struct {
|
||||
const canvas = try doc.call(js.Object, "createElement", .{js.string("canvas")});
|
||||
errdefer canvas.deinit();
|
||||
|
||||
log.debug("face initialized: {s}", .{raw});
|
||||
|
||||
return Face{
|
||||
.alloc = alloc,
|
||||
.font_str = font_str,
|
||||
|
@ -1,6 +1,17 @@
|
||||
// This is the main file for the WASM module. The WASM module has to
|
||||
// export a C ABI compatible API.
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub usingnamespace @import("os/wasm.zig");
|
||||
pub usingnamespace @import("os/wasm/log.zig");
|
||||
pub usingnamespace @import("font/main.zig");
|
||||
|
||||
// Set our log level. We try to get as much logging as possible but in
|
||||
// ReleaseSmall mode where we're optimizing for space, we elevate the
|
||||
// log level.
|
||||
pub const log_level: std.log.Level = switch (builtin.mode) {
|
||||
.Debug => .debug,
|
||||
.ReleaseSmall => .warn,
|
||||
else => .info,
|
||||
};
|
||||
|
@ -37,6 +37,18 @@ pub export fn malloc(len: usize) ?[*]u8 {
|
||||
return alloc_(len) catch return null;
|
||||
}
|
||||
|
||||
fn alloc_(len: usize) ![*]u8 {
|
||||
// Create the allocation
|
||||
const slice = try alloc.alloc(u8, len);
|
||||
errdefer alloc.free(slice);
|
||||
|
||||
// Store the size so we can deallocate later
|
||||
try allocs.putNoClobber(alloc, slice.ptr, slice.len);
|
||||
errdefer _ = allocs.remove(slice.ptr);
|
||||
|
||||
return slice.ptr;
|
||||
}
|
||||
|
||||
/// Free an allocation from malloc.
|
||||
pub export fn free(ptr: ?[*]u8) void {
|
||||
if (ptr) |v| {
|
||||
@ -79,18 +91,6 @@ pub fn toModuleOwned(ptr: anytype) void {
|
||||
_ = allocs.remove(casted);
|
||||
}
|
||||
|
||||
fn alloc_(len: usize) ![*]u8 {
|
||||
// Create the allocation
|
||||
const slice = try alloc.alloc(u8, len);
|
||||
errdefer alloc.free(slice);
|
||||
|
||||
// Store the size so we can deallocate later
|
||||
try allocs.putNoClobber(alloc, slice.ptr, slice.len);
|
||||
errdefer _ = allocs.remove(slice.ptr);
|
||||
|
||||
return slice.ptr;
|
||||
}
|
||||
|
||||
test "basics" {
|
||||
const testing = std.testing;
|
||||
var buf = malloc(32).?;
|
||||
|
2
vendor/zig-js
vendored
2
vendor/zig-js
vendored
@ -1 +1 @@
|
||||
Subproject commit a6d0b5a0c421925409f1bce542f64b9e5f6b96df
|
||||
Subproject commit faa353da8aca78269bc7d2afaf481b755007e849
|
Loading…
x
Reference in New Issue
Block a user