Skip to content

Commit 66f5801

Browse files
authored
Merge pull request #52 from lightpanda-io/zigAlloc-always-without-0-sentinel
More string cleanup
2 parents 0a45dea + 4d70430 commit 66f5801

File tree

3 files changed

+81
-118
lines changed

3 files changed

+81
-118
lines changed

src/binding.cpp

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,10 +1557,10 @@ void v8__base__SetDcheckFunction(void (*func)(const char*, int, const char*)) {
15571557

15581558
// Utils
15591559

1560-
struct CZigString {
1561-
const char *ptr = nullptr;
1562-
uint64_t len = 0;
1563-
};
1560+
extern "C" typedef struct {
1561+
const char *ptr;
1562+
uint64_t len;
1563+
} CZigString;
15641564

15651565
/// Header for Zig
15661566
/// Allocates `bytes` bytes of memory using the allocator.
@@ -1594,34 +1594,20 @@ static inline std::string fromStringView(v8::Isolate* isolate, const v8_inspecto
15941594
return *result;
15951595
}
15961596

1597-
/// Allocates a string as utf8 on the heap, such that it can be returned to Zig.
1598-
/// @param str: The string to return
1599-
/// @param allocator: A Zig std.mem.Allocator
1600-
/// @returns string pointer with null terminator, null if allocation failed
1601-
const char* allocStringWith0(const v8_inspector::String16& str, const void* allocator) {
1602-
std::string utf8_str = str.utf8(); // Note the data*'s lifetime is tied to utf8_str and this may hold the string data on the stack as an SSO, so we need to copy it onto the heap.
1603-
char* heap_str = zigAlloc(allocator, utf8_str.length() + 1); // +1 for null terminator, needed to communicate the length to Zig
1604-
if (heap_str == nullptr) {
1605-
return nullptr;
1606-
}
1607-
strcpy(heap_str, utf8_str.c_str());
1608-
return heap_str;
1609-
}
1610-
16111597
/// Allocates a string as utf8 on the allocator without \0 terminator, for use in Zig.
16121598
/// The strings pointer and length should therefore be returned together
1613-
/// @param str: The string contents to allocate
1599+
/// @param input: The string contents to allocate
16141600
/// @param allocator: A Zig std.mem.Allocator
1615-
/// @param out: Points to the now allocated string on the heap (without sentinel \0), NULL if view was null, invalid if allocation failed
1601+
/// @param output: Points to the now allocated string on the heap (without sentinel \0), NULL if view was null, invalid if allocation failed
16161602
/// @returns false if allocation errored
16171603
bool allocString(const v8_inspector::StringView& input, const void* allocator, CZigString& output) {
1604+
output.ptr = nullptr;
1605+
output.len = 0;
16181606
if (input.characters8() == nullptr) {
1619-
output.ptr = nullptr;
1620-
output.len = 0;
16211607
return true;
16221608
}
16231609

1624-
std::string utf8_str; // Harmless if not used by 8bit string
1610+
std::string utf8_str;
16251611
if (input.is8Bit()) {
16261612
output.len = input.length();
16271613
} else {
@@ -1750,36 +1736,36 @@ void v8_inspector__RemoteObject__DELETE(v8_inspector::protocol::Runtime::RemoteO
17501736
}
17511737

17521738
// RemoteObject - Type
1753-
const char* v8_inspector__RemoteObject__getType(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1739+
bool v8_inspector__RemoteObject__getType(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &out_type) {
17541740
auto str = self->getType();
1755-
return allocStringWith0(str, allocator);
1741+
return allocString(toStringView(str), allocator, out_type);
17561742
}
1757-
void v8_inspector__RemoteObject__setType(v8_inspector::protocol::Runtime::RemoteObject* self, const char* type, int type_len) {
1758-
self->setType(v8_inspector::String16::fromUTF8(type, type_len));
1743+
void v8_inspector__RemoteObject__setType(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString type) {
1744+
self->setType(v8_inspector::String16::fromUTF8(type.ptr, type.len));
17591745
}
17601746

17611747
// RemoteObject - Subtype
17621748
bool v8_inspector__RemoteObject__hasSubtype(v8_inspector::protocol::Runtime::RemoteObject* self) {
17631749
return self->hasSubtype();
17641750
}
1765-
const char* v8_inspector__RemoteObject__getSubtype(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1751+
bool v8_inspector__RemoteObject__getSubtype(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &subtype) {
17661752
auto str = self->getSubtype(DEFAULT_STRING);
1767-
return allocStringWith0(str, allocator);
1753+
return allocString(toStringView(str), allocator, subtype);
17681754
}
1769-
void v8_inspector__RemoteObject__setSubtype(v8_inspector::protocol::Runtime::RemoteObject* self, const char* subtype, int subtype_len) {
1770-
self->setSubtype(v8_inspector::String16::fromUTF8(subtype, subtype_len));
1755+
void v8_inspector__RemoteObject__setSubtype(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString subtype) {
1756+
self->setSubtype(v8_inspector::String16::fromUTF8(subtype.ptr, subtype.len));
17711757
}
17721758

17731759
// RemoteObject - ClassName
17741760
bool v8_inspector__RemoteObject__hasClassName(v8_inspector::protocol::Runtime::RemoteObject* self) {
17751761
return self->hasClassName();
17761762
}
1777-
const char* v8_inspector__RemoteObject__getClassName(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1763+
bool v8_inspector__RemoteObject__getClassName(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &className) {
17781764
auto str = self->getClassName(DEFAULT_STRING);
1779-
return allocStringWith0(str, allocator);
1765+
return allocString(toStringView(str), allocator, className);
17801766
}
1781-
void v8_inspector__RemoteObject__setClassName(v8_inspector::protocol::Runtime::RemoteObject* self, const char* className, int className_len) {
1782-
self->setClassName(v8_inspector::String16::fromUTF8(className, className_len));
1767+
void v8_inspector__RemoteObject__setClassName(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString className) {
1768+
self->setClassName(v8_inspector::String16::fromUTF8(className.ptr, className.len));
17831769
}
17841770

17851771
// RemoteObject - Value
@@ -1793,28 +1779,28 @@ void v8_inspector__RemoteObject__setValue(v8_inspector::protocol::Runtime::Remot
17931779
self->setValue(std::unique_ptr<v8_inspector::protocol::Value>(value));
17941780
}
17951781

1796-
//RemoteObject - UnserializableValue
1782+
// RemoteObject - UnserializableValue
17971783
bool v8_inspector__RemoteObject__hasUnserializableValue(v8_inspector::protocol::Runtime::RemoteObject* self) {
17981784
return self->hasUnserializableValue();
17991785
}
1800-
const char* v8_inspector__RemoteObject__getUnserializableValue(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1786+
bool v8_inspector__RemoteObject__getUnserializableValue(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &unserializableValue) {
18011787
auto str = self->getUnserializableValue(DEFAULT_STRING);
1802-
return allocStringWith0(str, allocator);
1788+
return allocString(toStringView(str), allocator, unserializableValue);
18031789
}
1804-
void v8_inspector__RemoteObject__setUnserializableValue(v8_inspector::protocol::Runtime::RemoteObject* self, const char* unserializableValue, int unserializableValue_len) {
1805-
self->setUnserializableValue(v8_inspector::String16::fromUTF8(unserializableValue, unserializableValue_len));
1790+
void v8_inspector__RemoteObject__setUnserializableValue(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString unserializableValue) {
1791+
self->setUnserializableValue(v8_inspector::String16::fromUTF8(unserializableValue.ptr, unserializableValue.len));
18061792
}
18071793

18081794
// RemoteObject - Description
18091795
bool v8_inspector__RemoteObject__hasDescription(v8_inspector::protocol::Runtime::RemoteObject* self) {
18101796
return self->hasDescription();
18111797
}
1812-
const char* v8_inspector__RemoteObject__getDescription(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1798+
bool v8_inspector__RemoteObject__getDescription(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &description) {
18131799
auto str = self->getDescription(DEFAULT_STRING);
1814-
return allocStringWith0(str, allocator);
1800+
return allocString(toStringView(str), allocator, description);
18151801
}
1816-
void v8_inspector__RemoteObject__setDescription(v8_inspector::protocol::Runtime::RemoteObject* self, const char* description, int description_len) {
1817-
self->setDescription(v8_inspector::String16::fromUTF8(description, description_len));
1802+
void v8_inspector__RemoteObject__setDescription(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString description) {
1803+
self->setDescription(v8_inspector::String16::fromUTF8(description.ptr, description.len));
18181804
}
18191805

18201806
// RemoteObject - WebDriverValue
@@ -1833,12 +1819,12 @@ bool v8_inspector__RemoteObject__hasObjectId(v8_inspector::protocol::Runtime::Re
18331819
return self->hasObjectId();
18341820
}
18351821

1836-
const char* v8_inspector__RemoteObject__getObjectId(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator) {
1822+
bool v8_inspector__RemoteObject__getObjectId(v8_inspector::protocol::Runtime::RemoteObject* self, const void* allocator, CZigString &objectId) {
18371823
auto str = self->getObjectId(DEFAULT_STRING);
1838-
return allocStringWith0(str, allocator);
1824+
return allocString(toStringView(str), allocator, objectId);
18391825
}
1840-
void v8_inspector__RemoteObject__setObjectId(v8_inspector::protocol::Runtime::RemoteObject* self, const char* objectId, int objectId_len) {
1841-
self->setObjectId(v8_inspector::String16::fromUTF8(objectId, objectId_len));
1826+
void v8_inspector__RemoteObject__setObjectId(v8_inspector::protocol::Runtime::RemoteObject* self, CZigString objectId) {
1827+
self->setObjectId(v8_inspector::String16::fromUTF8(objectId.ptr, objectId.len));
18421828
}
18431829

18441830
// RemoteObject - Preview

src/binding.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,18 +1068,18 @@ void v8_inspector__Inspector__ContextCreated(Inspector *self, const char *name,
10681068
void v8_inspector__RemoteObject__DELETE(RemoteObject *self);
10691069

10701070
// RemoteObject - Type
1071-
const char* v8_inspector__RemoteObject__getType(RemoteObject* self, const void* allocator);
1072-
void v8_inspector__RemoteObject__setType(RemoteObject* self, const char* type, int type_len);
1071+
bool v8_inspector__RemoteObject__getType(RemoteObject* self, const void* allocator, CZigString* out_type);
1072+
void v8_inspector__RemoteObject__setType(RemoteObject* self, CZigString type);
10731073

10741074
// RemoteObject - Subtype
10751075
bool v8_inspector__RemoteObject__hasSubtype(RemoteObject* self);
1076-
const char* v8_inspector__RemoteObject__getSubtype(RemoteObject* self, const void* allocator);
1077-
void v8_inspector__RemoteObject__setSubtype(RemoteObject* self, const char* subtype, int subtype_len);
1076+
bool v8_inspector__RemoteObject__getSubtype(RemoteObject* self, const void* allocator, CZigString* out_subtype);
1077+
void v8_inspector__RemoteObject__setSubtype(RemoteObject* self, CZigString subtype);
10781078

10791079
// RemoteObject - ClassName
10801080
bool v8_inspector__RemoteObject__hasClassName(RemoteObject* self);
1081-
const char* v8_inspector__RemoteObject__getClassName(RemoteObject* self, const void* allocator);
1082-
void v8_inspector__RemoteObject__setClassName(RemoteObject* self, const char* className, int className_len);
1081+
bool v8_inspector__RemoteObject__getClassName(RemoteObject* self, const void* allocator, CZigString* out_className);
1082+
void v8_inspector__RemoteObject__setClassName(RemoteObject* self, CZigString className);
10831083

10841084
// RemoteObject - Value
10851085
bool v8_inspector__RemoteObject__hasValue(RemoteObject* self);
@@ -1089,13 +1089,13 @@ bool v8_inspector__RemoteObject__hasValue(RemoteObject* self);
10891089

10901090
//RemoteObject - UnserializableValue
10911091
bool v8_inspector__RemoteObject__hasUnserializableValue(RemoteObject* self);
1092-
const char* v8_inspector__RemoteObject__getUnserializableValue(RemoteObject* self, const void* allocator);
1093-
void v8_inspector__RemoteObject__setUnserializableValue(RemoteObject* self, const char* unserializableValue, int unserializableValue_len);
1092+
bool v8_inspector__RemoteObject__getUnserializableValue(RemoteObject* self, const void* allocator, CZigString* out_unserializableValue);
1093+
void v8_inspector__RemoteObject__setUnserializableValue(RemoteObject* self, CZigString unserializableValue);
10941094

10951095
// RemoteObject - Description
10961096
bool v8_inspector__RemoteObject__hasDescription(RemoteObject* self);
1097-
const char* v8_inspector__RemoteObject__getDescription(RemoteObject* self, const void* allocator);
1098-
void v8_inspector__RemoteObject__setDescription(RemoteObject* self, const char* description, int description_len);
1097+
bool v8_inspector__RemoteObject__getDescription(RemoteObject* self, const void* allocator, CZigString* out_description);
1098+
void v8_inspector__RemoteObject__setDescription(RemoteObject* self, CZigString description);
10991099

11001100
// RemoteObject - WebDriverValue
11011101
bool v8_inspector__RemoteObject__hasWebDriverValue(RemoteObject* self);
@@ -1104,8 +1104,8 @@ void v8_inspector__RemoteObject__setWebDriverValue(RemoteObject* self, WebDriver
11041104

11051105
// RemoteObject - ObjectId
11061106
bool v8_inspector__RemoteObject__hasObjectId(RemoteObject* self);
1107-
const char* v8_inspector__RemoteObject__getObjectId(RemoteObject* self, const void* allocator);
1108-
void v8_inspector__RemoteObject__setObjectId(RemoteObject* self, const char* objectId, int objectId_len);
1107+
bool v8_inspector__RemoteObject__getObjectId(RemoteObject* self, const void* allocator, CZigString* out_objectId);
1108+
void v8_inspector__RemoteObject__setObjectId(RemoteObject* self, CZigString objectId);
11091109

11101110
// RemoteObject - Preview
11111111
bool v8_inspector__RemoteObject__hasPreview(RemoteObject* self);

src/v8.zig

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,27 +2720,27 @@ pub const InspectorSession = struct {
27202720
return RemoteObject{ .handle = remote_object };
27212721
}
27222722

2723-
pub fn unwrapObject(self: InspectorSession, allocator: std.mem.Allocator, objectId: []const u8) !UnwrappedObject {
2724-
const in_objectId = c.CZigString{
2725-
.ptr = objectId.ptr,
2726-
.len = objectId.len,
2723+
pub fn unwrapObject(self: InspectorSession, allocator: std.mem.Allocator, object_id: []const u8) !UnwrappedObject {
2724+
const in_object_id = c.CZigString{
2725+
.ptr = object_id.ptr,
2726+
.len = object_id.len,
27272727
};
27282728
var out_error: c.CZigString = .{ .ptr = null, .len = 0 };
27292729
var out_value_handle: ?*c.Value = null;
27302730
var out_context_handle: ?*c.Context = null;
2731-
var out_objectGroup: c.CZigString = .{ .ptr = null, .len = 0 };
2731+
var out_object_group: c.CZigString = .{ .ptr = null, .len = 0 };
27322732

27332733
const result = c.v8_inspector__Session__unwrapObject(
27342734
self.handle,
27352735
&allocator,
27362736
&out_error,
2737-
in_objectId,
2737+
in_object_id,
27382738
&out_value_handle,
27392739
&out_context_handle,
2740-
&out_objectGroup,
2740+
&out_object_group,
27412741
);
27422742
if (!result) {
2743-
if (CZigStringToString(out_error)) |err| {
2743+
if (cZigStringToString(out_error)) |err| {
27442744
if (std.mem.eql(u8, err, "Invalid remote object id")) return error.InvalidRemoteObjectId;
27452745
if (std.mem.eql(u8, err, "Cannot find context with specified id")) return error.CannotFindContextWithSpecifiedId;
27462746
if (std.mem.eql(u8, err, "Could not find object with given id")) return error.CouldNotFindObjectWithGivenId;
@@ -2751,19 +2751,19 @@ pub const InspectorSession = struct {
27512751
return .{
27522752
.value = Value{ .handle = out_value_handle.? },
27532753
.context = Context{ .handle = out_context_handle.? },
2754-
.objectGroup = CZigStringToString(out_objectGroup),
2754+
.object_group = cZigStringToString(out_object_group),
27552755
};
27562756
}
27572757
};
27582758

2759-
pub fn CZigStringToString(slice: c.CZigString) ?[]const u8 {
2759+
pub fn cZigStringToString(slice: c.CZigString) ?[]const u8 {
27602760
return if (slice.ptr == null) null else slice.ptr[0..slice.len];
27612761
}
27622762

27632763
pub const UnwrappedObject = struct {
27642764
value: Value,
27652765
context: Context,
2766-
objectGroup: ?[]const u8,
2766+
object_group: ?[]const u8,
27672767
};
27682768

27692769
/// Note: Some getters return owned memory (strings), while others return memory owned by V8 (objects).
@@ -2777,61 +2777,38 @@ pub const RemoteObject = struct {
27772777
c.v8_inspector__RemoteObject__DELETE(self.handle);
27782778
}
27792779

2780-
pub fn getType(self: RemoteObject, allocator: std.mem.Allocator) ![:0]const u8 {
2781-
const type_ = c.v8_inspector__RemoteObject__getType(self.handle, &allocator);
2782-
if (type_ == null) {
2783-
return error.V8AllocFailed;
2784-
}
2785-
const idx = std.mem.indexOfSentinel(u8, 0, type_);
2786-
return type_[0..idx :0];
2780+
pub fn getType(self: RemoteObject, allocator: std.mem.Allocator) ![]const u8 {
2781+
var ctype_: c.CZigString = .{ .ptr = null, .len = 0 };
2782+
if (!c.v8_inspector__RemoteObject__getType(self.handle, &allocator, &ctype_)) return error.V8AllocFailed;
2783+
return cZigStringToString(ctype_) orelse return error.InvalidType;
27872784
}
2788-
pub fn getSubtype(self: RemoteObject, allocator: std.mem.Allocator) !?[:0]const u8 {
2789-
if (!c.v8_inspector__RemoteObject__hasSubtype(self.handle)) {
2790-
return null;
2791-
}
2785+
pub fn getSubtype(self: RemoteObject, allocator: std.mem.Allocator) !?[]const u8 {
2786+
if (!c.v8_inspector__RemoteObject__hasSubtype(self.handle)) return null;
27922787

2793-
const subtype = c.v8_inspector__RemoteObject__getSubtype(self.handle, &allocator);
2794-
if (subtype == null) {
2795-
return error.V8AllocFailed;
2796-
}
2797-
const idx = std.mem.indexOfSentinel(u8, 0, subtype);
2798-
return subtype[0..idx :0];
2788+
var csubtype: c.CZigString = .{ .ptr = null, .len = 0 };
2789+
if (!c.v8_inspector__RemoteObject__getSubtype(self.handle, &allocator, &csubtype)) return error.V8AllocFailed;
2790+
return cZigStringToString(csubtype);
27992791
}
2800-
pub fn getClassName(self: RemoteObject, allocator: std.mem.Allocator) !?[:0]const u8 {
2801-
if (!c.v8_inspector__RemoteObject__hasClassName(self.handle)) {
2802-
return null;
2803-
}
2792+
pub fn getClassName(self: RemoteObject, allocator: std.mem.Allocator) !?[]const u8 {
2793+
if (!c.v8_inspector__RemoteObject__hasClassName(self.handle)) return null;
28042794

2805-
const class_name = c.v8_inspector__RemoteObject__getClassName(self.handle, &allocator);
2806-
if (class_name == null) {
2807-
return error.V8AllocFailed;
2808-
}
2809-
const idx = std.mem.indexOfSentinel(u8, 0, class_name);
2810-
return class_name[0..idx :0];
2795+
var cclass_name: c.CZigString = .{ .ptr = null, .len = 0 };
2796+
if (!c.v8_inspector__RemoteObject__getClassName(self.handle, &allocator, &cclass_name)) return error.V8AllocFailed;
2797+
return cZigStringToString(cclass_name);
28112798
}
2812-
pub fn getDescription(self: RemoteObject, allocator: std.mem.Allocator) !?[:0]const u8 {
2813-
if (!c.v8_inspector__RemoteObject__hasDescription(self.handle)) {
2814-
return null;
2815-
}
2799+
pub fn getDescription(self: RemoteObject, allocator: std.mem.Allocator) !?[]const u8 {
2800+
if (!c.v8_inspector__RemoteObject__hasDescription(self.handle)) return null;
28162801

2817-
const description = c.v8_inspector__RemoteObject__getDescription(self.handle, &allocator);
2818-
if (description == null) {
2819-
return error.V8AllocFailed;
2820-
}
2821-
const idx = std.mem.indexOfSentinel(u8, 0, description);
2822-
return description[0..idx :0];
2802+
var description: c.CZigString = .{ .ptr = null, .len = 0 };
2803+
if (!c.v8_inspector__RemoteObject__getDescription(self.handle, &allocator, &description)) return error.V8AllocFailed;
2804+
return cZigStringToString(description);
28232805
}
2824-
pub fn getObjectId(self: RemoteObject, allocator: std.mem.Allocator) !?[:0]const u8 {
2825-
if (!c.v8_inspector__RemoteObject__hasObjectId(self.handle)) {
2826-
return null;
2827-
}
2806+
pub fn getObjectId(self: RemoteObject, allocator: std.mem.Allocator) !?[]const u8 {
2807+
if (!c.v8_inspector__RemoteObject__hasObjectId(self.handle)) return null;
28282808

2829-
const object_id = c.v8_inspector__RemoteObject__getObjectId(self.handle, &allocator);
2830-
if (object_id == null) {
2831-
return error.V8AllocFailed;
2832-
}
2833-
const idx = std.mem.indexOfSentinel(u8, 0, object_id);
2834-
return object_id[0..idx :0];
2809+
var cobject_id: c.CZigString = .{ .ptr = null, .len = 0 };
2810+
if (!c.v8_inspector__RemoteObject__getObjectId(self.handle, &allocator, &cobject_id)) return error.V8AllocFailed;
2811+
return cZigStringToString(cobject_id);
28352812
}
28362813
};
28372814

0 commit comments

Comments
 (0)