diff --git a/example/wasm-vt/index.html b/example/wasm-vt/index.html index 6ec4e2408..d720e2375 100644 --- a/example/wasm-vt/index.html +++ b/example/wasm-vt/index.html @@ -138,12 +138,11 @@ wasmMemory = wasmInstance.exports.memory; // Load the type layout JSON from the library - const lenPtr = wasmInstance.exports.ghostty_wasm_alloc_usize(); - const jsonPtr = wasmInstance.exports.ghostty_type_json(lenPtr); - const jsonLen = new DataView(wasmMemory.buffer).getUint32(lenPtr, true); - wasmInstance.exports.ghostty_wasm_free_usize(lenPtr); - const jsonBytes = new Uint8Array(wasmMemory.buffer, jsonPtr, jsonLen); - typeLayout = JSON.parse(new TextDecoder().decode(jsonBytes)); + const jsonPtr = wasmInstance.exports.ghostty_type_json(); + const jsonStr = new TextDecoder().decode( + new Uint8Array(wasmMemory.buffer, jsonPtr, wasmMemory.buffer.byteLength - jsonPtr) + ).split('\0')[0]; + typeLayout = JSON.parse(jsonStr); return true; } catch (e) { diff --git a/include/ghostty/vt/types.h b/include/ghostty/vt/types.h index e06c5184c..987261dee 100644 --- a/include/ghostty/vt/types.h +++ b/include/ghostty/vt/types.h @@ -84,8 +84,8 @@ typedef struct { ((type){ .size = sizeof(type) }) /** - * Return a pointer to a JSON string describing the layout of every - * C API struct for the current target. + * Return a pointer to a null-terminated JSON string describing the + * layout of every C API struct for the current target. * * This is primarily useful for language bindings that can't easily * set C struct fields and need to do so via byte offsets. For example, @@ -113,12 +113,9 @@ typedef struct { * @endcode * * The returned pointer is valid for the lifetime of the process. - * The length of the string (excluding any null terminator) is - * written to @p len. * - * @param[out] len Receives the length of the returned string in bytes. - * @return Pointer to the JSON string. + * @return Pointer to the null-terminated JSON string. */ -const char *ghostty_type_json(size_t *len); +const char *ghostty_type_json(void); #endif /* GHOSTTY_VT_TYPES_H */ diff --git a/src/terminal/c/types.zig b/src/terminal/c/types.zig index 006f06165..1e51d4961 100644 --- a/src/terminal/c/types.zig +++ b/src/terminal/c/types.zig @@ -2,7 +2,7 @@ //! extern structs for the current target. //! //! This is embedded in the binary as a const string and exposed via -//! `ghostty_struct_meta` so that WASM (and other FFI) consumers can +//! `ghostty_type_json` so that WASM (and other FFI) consumers can //! build structs without hardcoding byte offsets. const std = @import("std"); const lib = @import("../lib.zig"); @@ -28,23 +28,22 @@ pub const structs: std.StaticStringMap(StructInfo) = .initComptime(.{ }); /// The comptime-generated JSON string of all structs. -pub const json: []const u8 = json: { +pub const json: [:0]const u8 = json: { @setEvalBranchQuota(50000); var counter: std.Io.Writer.Discarding = .init(&.{}); jsonWriteAll(&counter.writer) catch unreachable; - var buf: [counter.count]u8 = undefined; + var buf: [counter.count:0]u8 = undefined; var writer: std.Io.Writer = .fixed(&buf); jsonWriteAll(&writer) catch unreachable; const final = buf; - break :json final[0..writer.end]; + break :json final[0..writer.end :0]; }; /// Returns a pointer to the comptime-generated JSON string describing /// the layout of all C API extern structs, and writes its length to `len`. /// Exported as `ghostty_type_json` for FFI consumers. -pub fn get_json(len: *usize) callconv(lib.calling_conv) [*]const u8 { - len.* = json.len; +pub fn get_json() callconv(lib.calling_conv) [*:0]const u8 { return json.ptr; }