Skip to content

Commit fd10baf

Browse files
committed
Cleanup interface with CZigString
1 parent 5a8b116 commit fd10baf

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

src/binding.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,11 @@ 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+
};
1564+
15601565
/// Header for Zig
15611566
/// Allocates `bytes` bytes of memory using the allocator.
15621567
/// @param allocator: A Zig std.mem.Allocator
@@ -1607,37 +1612,34 @@ const char* allocStringWith0(const v8_inspector::String16& str, const void* allo
16071612
/// The strings pointer and length should therefore be returned together
16081613
/// @param str: The string contents to allocate
16091614
/// @param allocator: A Zig std.mem.Allocator
1610-
/// @param out_str: Points to the now allocated string on the heap (without sentinel \0),null if view was null or allocation failed
1611-
/// @param out_len: The corresponding length of the string
1615+
/// @param out: Points to the now allocated string on the heap (without sentinel \0), NULL if view was null, invalid if allocation failed
16121616
/// @returns false if allocation errored
1613-
bool allocString(const v8_inspector::StringView& str, const void* allocator, const char*& out_str, size_t& out_len) {
1614-
if (str.characters8() == nullptr) {
1615-
out_str = nullptr;
1616-
out_len = 0;
1617+
bool allocString(const v8_inspector::StringView& input, const void* allocator, CZigString& output) {
1618+
if (input.characters8() == nullptr) {
1619+
output.ptr = nullptr;
1620+
output.len = 0;
16171621
return true;
16181622
}
16191623

16201624
std::string utf8_str; // Harmless if not used by 8bit string
1621-
if (str.is8Bit()) {
1622-
out_len = str.length();
1625+
if (input.is8Bit()) {
1626+
output.len = input.length();
16231627
} else {
1624-
utf8_str = v8_inspector::UTF16ToUTF8(str.characters16(), str.length());
1625-
out_len = utf8_str.length();
1628+
utf8_str = v8_inspector::UTF16ToUTF8(input.characters16(), input.length());
1629+
output.len = utf8_str.length();
16261630
}
16271631

1628-
char* heap_str = zigAlloc(allocator, out_len);
1632+
char* heap_str = zigAlloc(allocator, output.len);
16291633
if (heap_str == nullptr) {
1630-
out_str = nullptr;
1631-
out_len = 0;
16321634
return false;
16331635
}
16341636

1635-
if (str.is8Bit()) {
1636-
memcpy(heap_str, str.characters8(), out_len);
1637+
if (input.is8Bit()) {
1638+
memcpy(heap_str, input.characters8(), output.len);
16371639
} else {
1638-
memcpy(heap_str, utf8_str.c_str(), out_len);
1640+
memcpy(heap_str, utf8_str.c_str(), output.len);
16391641
}
1640-
out_str = heap_str;
1642+
output.ptr = heap_str;
16411643
return true;
16421644
}
16431645

@@ -1714,16 +1716,13 @@ v8_inspector::protocol::Runtime::RemoteObject* v8_inspector__Session__wrapObject
17141716
bool v8_inspector__Session__unwrapObject(
17151717
v8_inspector::V8InspectorSession *session,
17161718
const void *allocator,
1717-
const char *&out_error,
1718-
uint64_t &out_error_len,
1719-
const char *in_objectId,
1720-
int in_objectId_len,
1719+
CZigString &out_error,
1720+
CZigString in_objectId,
17211721
v8::Local<v8::Value> &out_value,
17221722
v8::Local<v8::Context> &out_context,
1723-
const char *&out_objectGroup,
1724-
uint64_t &out_objectGroup_len
1723+
CZigString &out_objectGroup
17251724
) {
1726-
auto objectId = toStringView(in_objectId, in_objectId_len);
1725+
auto objectId = toStringView(in_objectId.ptr, in_objectId.len);
17271726
auto error = v8_inspector::StringBuffer::create({});
17281727
auto objectGroup = v8_inspector::StringBuffer::create({});
17291728

@@ -1734,10 +1733,10 @@ bool v8_inspector__Session__unwrapObject(
17341733
// [out optional ] std::unique_ptr<StringBuffer>* objectGroup
17351734
bool result = session->unwrapObject(&error, objectId, &out_value, &out_context, &objectGroup);
17361735
if (!result) {
1737-
allocString(error->string(), allocator, out_error, out_error_len);
1736+
allocString(error->string(), allocator, out_error);
17381737
return false;
17391738
}
1740-
return allocString(objectGroup->string(), allocator, out_objectGroup, out_objectGroup_len);
1739+
return allocString(objectGroup->string(), allocator, out_objectGroup);
17411740
}
17421741

17431742
// RemoteObject

src/binding.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,13 @@ const String* v8__JSON__Stringify(
958958
// Misc.
959959
void v8__base__SetDcheckFunction(void (*func)(const char*, int, const char*));
960960

961+
// Utils
962+
963+
typedef struct {
964+
const char *ptr;
965+
uint64_t len;
966+
} CZigString;
967+
961968
// Inspector
962969
// ---------
963970

@@ -1015,7 +1022,6 @@ char* v8_inspector__Client__IMPL__valueSubtype(
10151022
char* v8_inspector__Client__IMPL__descriptionForValueSubtype(
10161023
InspectorClientImpl* self, Context context, Value value);
10171024

1018-
10191025
// RemoteObject
10201026
typedef struct RemoteObject RemoteObject;
10211027
typedef struct WebDriverValue WebDriverValue;
@@ -1035,14 +1041,11 @@ RemoteObject* v8_inspector__Session__wrapObject(
10351041
bool v8_inspector__Session__unwrapObject(
10361042
InspectorSession *session,
10371043
const void* allocator,
1038-
const char** out_error,
1039-
uint64_t* out_error_len,
1040-
const char *in_objectId,
1041-
uint64_t in_objectId_len,
1044+
CZigString* out_error,
1045+
CZigString in_objectId,
10421046
Value** out_value,
10431047
Context** out_context,
1044-
const char** out_objectGroup,
1045-
uint64_t* out_objectGroup_len
1048+
CZigString* out_objectGroup
10461049
);
10471050

10481051
// Inspector

src/v8.zig

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,41 +2721,42 @@ pub const InspectorSession = struct {
27212721
}
27222722

27232723
pub fn unwrapObject(self: InspectorSession, allocator: std.mem.Allocator, objectId: []const u8) !UnwrapResult {
2724-
var out_error: [*c]const u8 = null;
2725-
var out_error_len: u64 = 0;
2726-
2724+
const in_objectId = c.CZigString{
2725+
.ptr = objectId.ptr,
2726+
.len = objectId.len,
2727+
};
2728+
var out_error: c.CZigString = .{ .ptr = null, .len = 0 };
27272729
var out_value_handle: ?*c.Value = null;
27282730
var out_context_handle: ?*c.Context = null;
2729-
2730-
var out_objectGroup: [*c]const u8 = null;
2731-
var out_objectGroup_len: u64 = 0;
2731+
var out_objectGroup: c.CZigString = .{ .ptr = null, .len = 0 };
27322732

27332733
const result = c.v8_inspector__Session__unwrapObject(
27342734
self.handle,
27352735
&allocator,
27362736
&out_error,
2737-
&out_error_len,
2738-
objectId.ptr,
2739-
objectId.len,
2737+
in_objectId,
27402738
&out_value_handle,
27412739
&out_context_handle,
27422740
&out_objectGroup,
2743-
&out_objectGroup_len,
27442741
);
27452742
if (!result) {
2746-
return .{ .err = if (out_error != null) out_error[0..out_error_len] else null };
2743+
return .{ .err = CZigStringToString(out_error) };
27472744
}
27482745

27492746
return .{
27502747
.ok = .{
27512748
.value = Value{ .handle = out_value_handle.? },
27522749
.context = Context{ .handle = out_context_handle.? },
2753-
.objectGroup = if (out_objectGroup != null) out_objectGroup[0..out_objectGroup_len] else null,
2750+
.objectGroup = CZigStringToString(out_objectGroup),
27542751
},
27552752
};
27562753
}
27572754
};
27582755

2756+
pub fn CZigStringToString(slice: c.CZigString) ?[]const u8 {
2757+
return if (slice.ptr == null) null else slice.ptr[0..slice.len];
2758+
}
2759+
27592760
pub const UnwrappedObject = struct {
27602761
value: Value,
27612762
context: Context,

0 commit comments

Comments
 (0)