mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
Merge pull request #546 from lightpanda-io/jsruntime_js_to_null_terminated_string
Support binding JS strings to [:0]const u8
This commit is contained in:
@@ -1925,7 +1925,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
|
||||
@@ -2319,6 +2325,15 @@ 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.allocSentinel(u8, len, 0);
|
||||
const n = str.writeUtf8(isolate, buf);
|
||||
std.debug.assert(n == len);
|
||||
return buf;
|
||||
}
|
||||
|
||||
const NoopInspector = struct {
|
||||
pub fn onInspectorResponse(_: *anyopaque, _: u32, _: []const u8) void {}
|
||||
pub fn onInspectorEvent(_: *anyopaque, _: []const u8) void {}
|
||||
|
||||
@@ -96,6 +96,14 @@ const Primitives = struct {
|
||||
pub fn _checkOptionalReturnString(_: *const Primitives) ?[]const u8 {
|
||||
return "ok";
|
||||
}
|
||||
|
||||
pub fn _echoString(_: *const Primitives, a: []const u8) []const u8 {
|
||||
return a;
|
||||
}
|
||||
|
||||
pub fn _echoStringZ(_: *const Primitives, a: [:0]const u8) []const u8 {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
const testing = @import("testing.zig");
|
||||
@@ -172,5 +180,9 @@ test "JS: primitive types" {
|
||||
.{ "p.checkOptionalReturn() === true;", "true" },
|
||||
.{ "p.checkOptionalReturnNull() === null;", "true" },
|
||||
.{ "p.checkOptionalReturnString() === 'ok';", "true" },
|
||||
|
||||
// strings
|
||||
.{ "p.echoString('over 9000!');", "over 9000!" },
|
||||
.{ "p.echoStringZ('Teg');", "Teg" },
|
||||
}, .{});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user