mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-19 14:00:29 +00:00
vt: simplify ghostty_type_json to return null-terminated string
The function previously took a size_t* out-parameter for the string length. Since the JSON blob is now null-terminated, the len parameter is unnecessary. Remove it from the Zig implementation, C header, and the WASM example consumer which no longer needs to allocate and free a usize just to read the length.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user