mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
hook up logging to wasm, example uses new zig-js package
This commit is contained in:
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>
|
<!doctype html>
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8"/>
|
||||||
<title>WASM Example</title>
|
<title>Ghostty Example</title>
|
||||||
</head>
|
<script type="module" src="app.ts"></script>
|
||||||
<body>
|
</head>
|
||||||
<script>
|
<body>
|
||||||
const importObject = {
|
Open your console, we are just debugging here.
|
||||||
module: {},
|
</body>
|
||||||
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>
|
|
||||||
</html>
|
</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")});
|
const canvas = try doc.call(js.Object, "createElement", .{js.string("canvas")});
|
||||||
errdefer canvas.deinit();
|
errdefer canvas.deinit();
|
||||||
|
|
||||||
|
log.debug("face initialized: {s}", .{raw});
|
||||||
|
|
||||||
return Face{
|
return Face{
|
||||||
.alloc = alloc,
|
.alloc = alloc,
|
||||||
.font_str = font_str,
|
.font_str = font_str,
|
||||||
|
@ -1,6 +1,17 @@
|
|||||||
// This is the main file for the WASM module. The WASM module has to
|
// This is the main file for the WASM module. The WASM module has to
|
||||||
// export a C ABI compatible API.
|
// 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.zig");
|
||||||
pub usingnamespace @import("os/wasm/log.zig");
|
pub usingnamespace @import("os/wasm/log.zig");
|
||||||
pub usingnamespace @import("font/main.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;
|
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.
|
/// Free an allocation from malloc.
|
||||||
pub export fn free(ptr: ?[*]u8) void {
|
pub export fn free(ptr: ?[*]u8) void {
|
||||||
if (ptr) |v| {
|
if (ptr) |v| {
|
||||||
@ -79,18 +91,6 @@ pub fn toModuleOwned(ptr: anytype) void {
|
|||||||
_ = allocs.remove(casted);
|
_ = 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" {
|
test "basics" {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
var buf = malloc(32).?;
|
var buf = malloc(32).?;
|
||||||
|
2
vendor/zig-js
vendored
2
vendor/zig-js
vendored
@ -1 +1 @@
|
|||||||
Subproject commit a6d0b5a0c421925409f1bce542f64b9e5f6b96df
|
Subproject commit faa353da8aca78269bc7d2afaf481b755007e849
|
Reference in New Issue
Block a user