@@ -1556,6 +1556,13 @@ void v8__base__SetDcheckFunction(void (*func)(const char*, int, const char*)) {
15561556
15571557// Utils
15581558
1559+ // / Header for Zig
1560+ // / Allocates `bytes` bytes of memory using the allocator.
1561+ // / @param allocator: A Zig std.mem.Allocator
1562+ // / @param bytes: The number of bytes to allocate
1563+ // / @returns A pointer to the allocated memory, null if allocation failed
1564+ char * zigAlloc (const void * allocator, uint64_t bytes);
1565+
15591566static inline v8_inspector::StringView toStringView (const char *str, size_t length) {
15601567 auto * stringView = reinterpret_cast <const uint8_t *>(str);
15611568 return { stringView, length };
@@ -1575,6 +1582,21 @@ static inline std::string fromStringView(v8::Isolate* isolate, const v8_inspecto
15751582 return *result;
15761583}
15771584
1585+ // / Allocates a string as utf8 on the heap, such that it can be returned to Zig.
1586+ // / @param str: The string to return
1587+ // / @param allocator: A Zig std.mem.Allocator
1588+ // / @returns string pointer with null terminator, null if allocation failed
1589+ const char * toHeapCharPtr (const v8_inspector::String16& str, const void * allocator) {
1590+ 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.
1591+ char * heap_str = zigAlloc (allocator, utf8_str.length () + 1 ); // +1 for null terminator, needed to communicate the length to Zig
1592+ if (heap_str == nullptr ) {
1593+ return nullptr ;
1594+ }
1595+ strcpy (heap_str, utf8_str.c_str ());
1596+ return heap_str;
1597+ }
1598+
1599+
15781600// Inspector
15791601
15801602v8_inspector::V8Inspector *v8_inspector__Inspector__Create (
@@ -1635,21 +1657,139 @@ void v8_inspector__Session__dispatchProtocolMessage(
16351657 session->dispatchProtocolMessage (str_view);
16361658}
16371659
1638- v8_inspector::protocol::Runtime::API:: RemoteObject* v8_inspector__Session__wrapObject (
1660+ v8_inspector::protocol::Runtime::RemoteObject* v8_inspector__Session__wrapObject (
16391661 v8_inspector::V8InspectorSession *session, v8::Isolate *isolate,
16401662 const v8::Context* ctx, const v8::Value* val,
16411663 const char *grpname, int grpname_len, bool generatepreview) {
16421664 auto sv_grpname = toStringView (grpname, grpname_len);
16431665 auto remote_object = session->wrapObject (ptr_to_local (ctx), ptr_to_local (val), sv_grpname, generatepreview);
1644- return remote_object.release ();
1666+ return static_cast <v8_inspector::protocol::Runtime::RemoteObject*>( remote_object.release () );
16451667}
16461668
16471669// RemoteObject
16481670
1649- void v8_inspector__RemoteObject__DELETE (v8_inspector::protocol::Runtime::API::RemoteObject* self) {
1671+ // To prevent extra allocations on every call a single default value is reused everytime.
1672+ // It is expected that the precense of a value is checked before calling get* methods.
1673+ const v8_inspector::String16 DEFAULT_STRING = {" default" };
1674+
1675+ void v8_inspector__RemoteObject__DELETE (v8_inspector::protocol::Runtime::RemoteObject* self) {
16501676 delete self;
16511677}
16521678
1679+ // RemoteObject - Type
1680+ const char * v8_inspector__RemoteObject__getType (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1681+ auto str = self->getType ();
1682+ return toHeapCharPtr (str, allocator);
1683+ }
1684+ void v8_inspector__RemoteObject__setType (v8_inspector::protocol::Runtime::RemoteObject* self, const char * type, int type_len) {
1685+ self->setType (v8_inspector::String16::fromUTF8 (type, type_len));
1686+ }
1687+
1688+ // RemoteObject - Subtype
1689+ bool v8_inspector__RemoteObject__hasSubtype (v8_inspector::protocol::Runtime::RemoteObject* self) {
1690+ return self->hasSubtype ();
1691+ }
1692+ const char * v8_inspector__RemoteObject__getSubtype (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1693+ auto str = self->getSubtype (DEFAULT_STRING);
1694+ return toHeapCharPtr (str, allocator);
1695+ }
1696+ void v8_inspector__RemoteObject__setSubtype (v8_inspector::protocol::Runtime::RemoteObject* self, const char * subtype, int subtype_len) {
1697+ self->setSubtype (v8_inspector::String16::fromUTF8 (subtype, subtype_len));
1698+ }
1699+
1700+ // RemoteObject - ClassName
1701+ bool v8_inspector__RemoteObject__hasClassName (v8_inspector::protocol::Runtime::RemoteObject* self) {
1702+ return self->hasClassName ();
1703+ }
1704+ const char * v8_inspector__RemoteObject__getClassName (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1705+ auto str = self->getClassName (DEFAULT_STRING);
1706+ return toHeapCharPtr (str, allocator);
1707+ }
1708+ void v8_inspector__RemoteObject__setClassName (v8_inspector::protocol::Runtime::RemoteObject* self, const char * className, int className_len) {
1709+ self->setClassName (v8_inspector::String16::fromUTF8 (className, className_len));
1710+ }
1711+
1712+ // RemoteObject - Value
1713+ bool v8_inspector__RemoteObject__hasValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1714+ return self->hasValue ();
1715+ }
1716+ v8_inspector::protocol::Value* v8_inspector__RemoteObject__getValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1717+ return self->getValue (nullptr );
1718+ }
1719+ void v8_inspector__RemoteObject__setValue (v8_inspector::protocol::Runtime::RemoteObject* self, v8_inspector::protocol::Value* value) {
1720+ self->setValue (std::unique_ptr<v8_inspector::protocol::Value>(value));
1721+ }
1722+
1723+ // RemoteObject - UnserializableValue
1724+ bool v8_inspector__RemoteObject__hasUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1725+ return self->hasUnserializableValue ();
1726+ }
1727+ const char * v8_inspector__RemoteObject__getUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1728+ auto str = self->getUnserializableValue (DEFAULT_STRING);
1729+ return toHeapCharPtr (str, allocator);
1730+ }
1731+ void v8_inspector__RemoteObject__setUnserializableValue (v8_inspector::protocol::Runtime::RemoteObject* self, const char * unserializableValue, int unserializableValue_len) {
1732+ self->setUnserializableValue (v8_inspector::String16::fromUTF8 (unserializableValue, unserializableValue_len));
1733+ }
1734+
1735+ // RemoteObject - Description
1736+ bool v8_inspector__RemoteObject__hasDescription (v8_inspector::protocol::Runtime::RemoteObject* self) {
1737+ return self->hasDescription ();
1738+ }
1739+ const char * v8_inspector__RemoteObject__getDescription (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1740+ auto str = self->getDescription (DEFAULT_STRING);
1741+ return toHeapCharPtr (str, allocator);
1742+ }
1743+ void v8_inspector__RemoteObject__setDescription (v8_inspector::protocol::Runtime::RemoteObject* self, const char * description, int description_len) {
1744+ self->setDescription (v8_inspector::String16::fromUTF8 (description, description_len));
1745+ }
1746+
1747+ // RemoteObject - WebDriverValue
1748+ bool v8_inspector__RemoteObject__hasWebDriverValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1749+ return self->hasWebDriverValue ();
1750+ }
1751+ v8_inspector::protocol::Runtime::WebDriverValue* v8_inspector__RemoteObject__getWebDriverValue (v8_inspector::protocol::Runtime::RemoteObject* self) {
1752+ return self->getWebDriverValue (nullptr );
1753+ }
1754+ void v8_inspector__RemoteObject__setWebDriverValue (v8_inspector::protocol::Runtime::RemoteObject* self, v8_inspector::protocol::Runtime::WebDriverValue* webDriverValue) {
1755+ self->setWebDriverValue (std::unique_ptr<v8_inspector::protocol::Runtime::WebDriverValue>(webDriverValue));
1756+ }
1757+
1758+ // RemoteObject - ObjectId
1759+ bool v8_inspector__RemoteObject__hasObjectId (v8_inspector::protocol::Runtime::RemoteObject* self) {
1760+ return self->hasObjectId ();
1761+ }
1762+
1763+ const char * v8_inspector__RemoteObject__getObjectId (v8_inspector::protocol::Runtime::RemoteObject* self, const void * allocator) {
1764+ auto str = self->getObjectId (DEFAULT_STRING);
1765+ return toHeapCharPtr (str, allocator);
1766+ }
1767+ void v8_inspector__RemoteObject__setObjectId (v8_inspector::protocol::Runtime::RemoteObject* self, const char * objectId, int objectId_len) {
1768+ self->setObjectId (v8_inspector::String16::fromUTF8 (objectId, objectId_len));
1769+ }
1770+
1771+ // RemoteObject - Preview
1772+ bool v8_inspector__RemoteObject__hasPreview (v8_inspector::protocol::Runtime::RemoteObject* self) {
1773+ return self->hasPreview ();
1774+ }
1775+ const v8_inspector::protocol::Runtime::ObjectPreview* v8_inspector__RemoteObject__getPreview (v8_inspector::protocol::Runtime::RemoteObject* self) {
1776+ return self->getPreview (nullptr );
1777+ }
1778+ void v8_inspector__RemoteObject__setPreview (v8_inspector::protocol::Runtime::RemoteObject* self, v8_inspector::protocol::Runtime::ObjectPreview* preview) {
1779+ self->setPreview (std::unique_ptr<v8_inspector::protocol::Runtime::ObjectPreview>(preview));
1780+ }
1781+
1782+ // RemoteObject - CustomPreview
1783+ bool v8_inspector__RemoteObject__hasCustomPreview (v8_inspector::protocol::Runtime::RemoteObject* self) {
1784+ return self->hasCustomPreview ();
1785+ }
1786+ const v8_inspector::protocol::Runtime::CustomPreview* v8_inspector__RemoteObject__getCustomPreview (v8_inspector::protocol::Runtime::RemoteObject* self) {
1787+ return self->getCustomPreview (nullptr );
1788+ }
1789+ void v8_inspector__RemoteObject__setCustomPreview (v8_inspector::protocol::Runtime::RemoteObject* self, v8_inspector::protocol::Runtime::CustomPreview* customPreview) {
1790+ self->setCustomPreview (std::unique_ptr<v8_inspector::protocol::Runtime::CustomPreview>(customPreview));
1791+ }
1792+
16531793// InspectorChannel
16541794
16551795v8_inspector__Channel__IMPL * v8_inspector__Channel__IMPL__CREATE (v8::Isolate *isolate) {
0 commit comments