@@ -1550,6 +1550,13 @@ void v8__base__SetDcheckFunction(void (*func)(const char*, int, const char*)) {
15501550
15511551// Utils
15521552
1553+ // / Header for Zig
1554+ // / Allocates `bytes` bytes of memory using the allocator.
1555+ // / @param allocator: A Zig std.mem.Allocator
1556+ // / @param bytes: The number of bytes to allocate
1557+ // / @returns A pointer to the allocated memory, null if allocation failed
1558+ char * zigAlloc (void * allocator, uint64_t bytes);
1559+
15531560static inline v8_inspector::StringView toStringView (const char *str, size_t length) {
15541561 auto * stringView = reinterpret_cast <const uint8_t *>(str);
15551562 return { stringView, length };
@@ -1569,12 +1576,20 @@ static inline std::string fromStringView(v8::Isolate* isolate, const v8_inspecto
15691576 return *result;
15701577}
15711578
1572- const char * toHeapCharPtr (const v8_inspector::String16& str) {
1573- std::string utf8_str = str.utf8 (); // Note that this may hold the string on the stack as an SSO
1574- char * heap_str = new char [utf8_str.length () + 1 ];
1579+ // / Allocates a string as utf8 on the heap, such that it can be returned to Zig.
1580+ // / @param str: The string to return
1581+ // / @param allocator: A Zig std.mem.Allocator
1582+ // / @returns string pointer with null terminator, null if allocation failed
1583+ const char * toHeapCharPtr (const v8_inspector::String16& str, void * allocator) {
1584+ 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.
1585+ char * heap_str = zigAlloc (allocator, utf8_str.length () + 1 ); // +1 for null terminator, needed to communicate the length to Zig
1586+ if (heap_str == nullptr ) {
1587+ return nullptr ;
1588+ }
15751589 strcpy (heap_str, utf8_str.c_str ());
1576- return heap_str;
1577- }
1590+ return heap_str;
1591+ }
1592+
15781593
15791594// Inspector
15801595
@@ -1642,15 +1657,12 @@ v8_inspector::protocol::Runtime::RemoteObject* v8_inspector__Session__wrapObject
16421657 const char *grpname, int grpname_len, bool generatepreview) {
16431658 auto sv_grpname = toStringView (grpname, grpname_len);
16441659 auto remote_object = session->wrapObject (ptr_to_local (ctx), ptr_to_local (val), sv_grpname, generatepreview);
1645- assert (remote_object != nullptr );
1646- auto * not_api = static_cast <v8_inspector::protocol::Runtime::RemoteObject*>(remote_object.release ());
1647- assert (not_api != nullptr );
1648- return not_api;
1660+ return static_cast <v8_inspector::protocol::Runtime::RemoteObject*>(remote_object.release ());
16491661}
16501662
16511663// RemoteObject
16521664
1653- // To prevent extra allocations on every call a single default value value is reused everytime.
1665+ // To prevent extra allocations on every call a single default value is reused everytime.
16541666// It is expected that the precense of a value is checked before calling get* methods.
16551667const v8_inspector::String16 DEFAULT_STRING = {" default" };
16561668
@@ -1659,9 +1671,9 @@ void v8_inspector__RemoteObject__DELETE(v8_inspector::protocol::Runtime::RemoteO
16591671}
16601672
16611673// RemoteObject - Type
1662- const char * v8_inspector__RemoteObject__getType (v8_inspector::protocol::Runtime::RemoteObject* self) {
1674+ const char * v8_inspector__RemoteObject__getType (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator ) {
16631675 auto str = self->getType ();
1664- return toHeapCharPtr (str);
1676+ return toHeapCharPtr (str, allocator );
16651677}
16661678void v8_inspector__RemoteObject__setType (v8_inspector::protocol::Runtime::RemoteObject* self, const char * type, int type_len) {
16671679 self->setType (v8_inspector::String16::fromUTF8 (type, type_len));
@@ -1671,9 +1683,9 @@ void v8_inspector__RemoteObject__setType(v8_inspector::protocol::Runtime::Remote
16711683bool v8_inspector__RemoteObject__hasSubtype (v8_inspector::protocol::Runtime::RemoteObject* self) {
16721684 return self->hasSubtype ();
16731685}
1674- const char * v8_inspector__RemoteObject__getSubtype (v8_inspector::protocol::Runtime::RemoteObject* self) {
1686+ const char * v8_inspector__RemoteObject__getSubtype (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator ) {
16751687 auto str = self->getSubtype (DEFAULT_STRING);
1676- return toHeapCharPtr (str);
1688+ return toHeapCharPtr (str, allocator );
16771689}
16781690void v8_inspector__RemoteObject__setSubtype (v8_inspector::protocol::Runtime::RemoteObject* self, const char * subtype, int subtype_len) {
16791691 self->setSubtype (v8_inspector::String16::fromUTF8 (subtype, subtype_len));
@@ -1683,9 +1695,9 @@ void v8_inspector__RemoteObject__setSubtype(v8_inspector::protocol::Runtime::Rem
16831695bool v8_inspector__RemoteObject__hasClassName (v8_inspector::protocol::Runtime::RemoteObject* self) {
16841696 return self->hasClassName ();
16851697}
1686- const char * v8_inspector__RemoteObject__getClassName (v8_inspector::protocol::Runtime::RemoteObject* self) {
1698+ const char * v8_inspector__RemoteObject__getClassName (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator ) {
16871699 auto str = self->getClassName (DEFAULT_STRING);
1688- return toHeapCharPtr (str);
1700+ return toHeapCharPtr (str, allocator );
16891701}
16901702void v8_inspector__RemoteObject__setClassName (v8_inspector::protocol::Runtime::RemoteObject* self, const char * className, int className_len) {
16911703 self->setClassName (v8_inspector::String16::fromUTF8 (className, className_len));
@@ -1706,9 +1718,9 @@ void v8_inspector__RemoteObject__setValue(v8_inspector::protocol::Runtime::Remot
17061718bool v8_inspector__RemoteObject__hasUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
17071719 return self->hasUnserializableValue ();
17081720}
1709- const char * v8_inspector__RemoteObject__getUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1721+ const char * v8_inspector__RemoteObject__getUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator ) {
17101722 auto str = self->getUnserializableValue (DEFAULT_STRING);
1711- return toHeapCharPtr (str);
1723+ return toHeapCharPtr (str, allocator );
17121724}
17131725void v8_inspector__RemoteObject__setUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self, const char * unserializableValue, int unserializableValue_len) {
17141726 self->setUnserializableValue (v8_inspector::String16::fromUTF8 (unserializableValue, unserializableValue_len));
@@ -1718,9 +1730,9 @@ void v8_inspector__RemoteObject__setUnserializableValue(v8_inspector::protocol::
17181730bool v8_inspector__RemoteObject__hasDescription (v8_inspector::protocol::Runtime::RemoteObject* self) {
17191731 return self->hasDescription ();
17201732}
1721- const char * v8_inspector__RemoteObject__getDescription (v8_inspector::protocol::Runtime::RemoteObject* self) {
1733+ const char * v8_inspector__RemoteObject__getDescription (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator ) {
17221734 auto str = self->getDescription (DEFAULT_STRING);
1723- return toHeapCharPtr (str);
1735+ return toHeapCharPtr (str, allocator );
17241736}
17251737void v8_inspector__RemoteObject__setDescription (v8_inspector::protocol::Runtime::RemoteObject* self, const char * description, int description_len) {
17261738 self->setDescription (v8_inspector::String16::fromUTF8 (description, description_len));
@@ -1739,15 +1751,16 @@ void v8_inspector__RemoteObject__setWebDriverValue(v8_inspector::protocol::Runti
17391751
17401752// RemoteObject - ObjectId
17411753bool v8_inspector__RemoteObject__hasObjectId (v8_inspector::protocol::Runtime::RemoteObject* self) {
1742- return self->hasObjectId ();
1743- }
1744- const char * v8_inspector__RemoteObject__getObjectId (v8_inspector::protocol::Runtime::RemoteObject* self) {
1745- auto str = self->getObjectId (DEFAULT_STRING);
1746- return toHeapCharPtr (str);
1747- }
1754+ return self->hasObjectId ();
1755+ }
1756+
1757+ const char * v8_inspector__RemoteObject__getObjectId (v8_inspector::protocol::Runtime::RemoteObject* self, void * allocator) {
1758+ auto str = self->getObjectId (DEFAULT_STRING);
1759+ return toHeapCharPtr (str, allocator);
1760+ }
17481761 void v8_inspector__RemoteObject__setObjectId (v8_inspector::protocol::Runtime::RemoteObject* self, const char * objectId, int objectId_len) {
1749- self->setObjectId (v8_inspector::String16::fromUTF8 (objectId, objectId_len));
1750- }
1762+ self->setObjectId (v8_inspector::String16::fromUTF8 (objectId, objectId_len));
1763+ }
17511764
17521765// RemoteObject - Preview
17531766bool v8_inspector__RemoteObject__hasPreview (v8_inspector::protocol::Runtime::RemoteObject* self) {
0 commit comments