mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-05-18 03:00:23 +00:00
hook up logging to wasm, example uses new zig-js package
This commit is contained in:
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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user