Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 7 additions & 34 deletions src/browser/dom/implementation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,20 @@ pub const DOMImplementation = struct {

pub fn _createDocumentType(
_: *DOMImplementation,
qname: []const u8,
publicId: []const u8,
systemId: []const u8,
state: *SessionState,
qname: [:0]const u8,
publicId: [:0]const u8,
systemId: [:0]const u8,
) !*parser.DocumentType {
const allocator = state.arena;
const cqname = try allocator.dupeZ(u8, qname);
defer allocator.free(cqname);

const cpublicId = try allocator.dupeZ(u8, publicId);
defer allocator.free(cpublicId);

const csystemId = try allocator.dupeZ(u8, systemId);
defer allocator.free(csystemId);

return try parser.domImplementationCreateDocumentType(cqname, cpublicId, csystemId);
return try parser.domImplementationCreateDocumentType(qname, publicId, systemId);
}

pub fn _createDocument(
_: *DOMImplementation,
namespace: ?[]const u8,
qname: ?[]const u8,
namespace: ?[:0]const u8,
qname: ?[:0]const u8,
doctype: ?*parser.DocumentType,
state: *SessionState,
) !*parser.Document {
const allocator = state.arena;
var cnamespace: ?[:0]const u8 = null;
if (namespace) |ns| {
cnamespace = try allocator.dupeZ(u8, ns);
}
defer if (cnamespace) |v| allocator.free(v);

var cqname: ?[:0]const u8 = null;
if (qname) |qn| {
cqname = try allocator.dupeZ(u8, qn);
}
defer if (cqname) |v| allocator.free(v);

return try parser.domImplementationCreateDocument(cnamespace, cqname, doctype);
return try parser.domImplementationCreateDocument(namespace, qname, doctype);
}

pub fn _createHTMLDocument(_: *DOMImplementation, title: ?[]const u8) !*parser.DocumentHTML {
Expand All @@ -79,8 +54,6 @@ pub const DOMImplementation = struct {
pub fn _hasFeature(_: *DOMImplementation) bool {
return true;
}

pub fn deinit(_: *DOMImplementation, _: std.mem.Allocator) void {}
};

// Tests
Expand Down
17 changes: 16 additions & 1 deletion src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2241,6 +2247,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 {}
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/test_primitive_types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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" },
}, .{});
}
Loading