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.pull/11992/head
parent
6479d90ca5
commit
0c38e8be60
|
|
@ -138,12 +138,11 @@
|
||||||
wasmMemory = wasmInstance.exports.memory;
|
wasmMemory = wasmInstance.exports.memory;
|
||||||
|
|
||||||
// Load the type layout JSON from the library
|
// Load the type layout JSON from the library
|
||||||
const lenPtr = wasmInstance.exports.ghostty_wasm_alloc_usize();
|
const jsonPtr = wasmInstance.exports.ghostty_type_json();
|
||||||
const jsonPtr = wasmInstance.exports.ghostty_type_json(lenPtr);
|
const jsonStr = new TextDecoder().decode(
|
||||||
const jsonLen = new DataView(wasmMemory.buffer).getUint32(lenPtr, true);
|
new Uint8Array(wasmMemory.buffer, jsonPtr, wasmMemory.buffer.byteLength - jsonPtr)
|
||||||
wasmInstance.exports.ghostty_wasm_free_usize(lenPtr);
|
).split('\0')[0];
|
||||||
const jsonBytes = new Uint8Array(wasmMemory.buffer, jsonPtr, jsonLen);
|
typeLayout = JSON.parse(jsonStr);
|
||||||
typeLayout = JSON.parse(new TextDecoder().decode(jsonBytes));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -84,8 +84,8 @@ typedef struct {
|
||||||
((type){ .size = sizeof(type) })
|
((type){ .size = sizeof(type) })
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a pointer to a JSON string describing the layout of every
|
* Return a pointer to a null-terminated JSON string describing the
|
||||||
* C API struct for the current target.
|
* layout of every C API struct for the current target.
|
||||||
*
|
*
|
||||||
* This is primarily useful for language bindings that can't easily
|
* 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,
|
* set C struct fields and need to do so via byte offsets. For example,
|
||||||
|
|
@ -113,12 +113,9 @@ typedef struct {
|
||||||
* @endcode
|
* @endcode
|
||||||
*
|
*
|
||||||
* The returned pointer is valid for the lifetime of the process.
|
* 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 null-terminated JSON string.
|
||||||
* @return Pointer to the JSON string.
|
|
||||||
*/
|
*/
|
||||||
const char *ghostty_type_json(size_t *len);
|
const char *ghostty_type_json(void);
|
||||||
|
|
||||||
#endif /* GHOSTTY_VT_TYPES_H */
|
#endif /* GHOSTTY_VT_TYPES_H */
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
//! extern structs for the current target.
|
//! extern structs for the current target.
|
||||||
//!
|
//!
|
||||||
//! This is embedded in the binary as a const string and exposed via
|
//! 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.
|
//! build structs without hardcoding byte offsets.
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lib = @import("../lib.zig");
|
const lib = @import("../lib.zig");
|
||||||
|
|
@ -28,23 +28,22 @@ pub const structs: std.StaticStringMap(StructInfo) = .initComptime(.{
|
||||||
});
|
});
|
||||||
|
|
||||||
/// The comptime-generated JSON string of all structs.
|
/// The comptime-generated JSON string of all structs.
|
||||||
pub const json: []const u8 = json: {
|
pub const json: [:0]const u8 = json: {
|
||||||
@setEvalBranchQuota(50000);
|
@setEvalBranchQuota(50000);
|
||||||
var counter: std.Io.Writer.Discarding = .init(&.{});
|
var counter: std.Io.Writer.Discarding = .init(&.{});
|
||||||
jsonWriteAll(&counter.writer) catch unreachable;
|
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);
|
var writer: std.Io.Writer = .fixed(&buf);
|
||||||
jsonWriteAll(&writer) catch unreachable;
|
jsonWriteAll(&writer) catch unreachable;
|
||||||
const final = buf;
|
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
|
/// Returns a pointer to the comptime-generated JSON string describing
|
||||||
/// the layout of all C API extern structs, and writes its length to `len`.
|
/// the layout of all C API extern structs, and writes its length to `len`.
|
||||||
/// Exported as `ghostty_type_json` for FFI consumers.
|
/// Exported as `ghostty_type_json` for FFI consumers.
|
||||||
pub fn get_json(len: *usize) callconv(lib.calling_conv) [*]const u8 {
|
pub fn get_json() callconv(lib.calling_conv) [*:0]const u8 {
|
||||||
len.* = json.len;
|
|
||||||
return json.ptr;
|
return json.ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue