mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-17 00:38:59 +00:00
Support binding JS strings to [:0]const u8
Some APIs need a null-terminated string. Currently, they have to ask for a `[]const u8` and then convert it to a `[:0]const u8`. This is 2 allocations: 1 for jsruntime to get the `[]const u8` from v8, and then one to get the [:0]. By supporting `[:0]const u8` directly, this is now a single allocation.
This commit is contained in:
@@ -1862,7 +1862,13 @@ fn Caller(comptime E: type) type {
|
||||
},
|
||||
.slice => {
|
||||
if (ptr.child == u8) {
|
||||
return valueToString(self.call_allocator, js_value, self.isolate, self.context);
|
||||
if (ptr.sentinel()) |s| {
|
||||
if (comptime s == 0) {
|
||||
return valueToStringZ(self.call_allocator, js_value, self.isolate, self.context);
|
||||
}
|
||||
} else {
|
||||
return valueToString(self.call_allocator, js_value, self.isolate, self.context);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: TypedArray
|
||||
@@ -2241,6 +2247,16 @@ fn valueToString(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, con
|
||||
return buf;
|
||||
}
|
||||
|
||||
fn valueToStringZ(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, context: v8.Context) ![:0]u8 {
|
||||
const str = try value.toString(context);
|
||||
const len = str.lenUtf8(isolate);
|
||||
const buf = try allocator.alloc(u8, len + 1);
|
||||
const n = str.writeUtf8(isolate, buf[0..len]);
|
||||
std.debug.assert(n == len);
|
||||
buf[len] = 0;
|
||||
return buf[0..len :0];
|
||||
}
|
||||
|
||||
const NoopInspector = struct {
|
||||
pub fn onInspectorResponse(_: *anyopaque, _: u32, _: []const u8) void {}
|
||||
pub fn onInspectorEvent(_: *anyopaque, _: []const u8) void {}
|
||||
|
||||
Reference in New Issue
Block a user