Skip to content

Commit 3cb2368

Browse files
Merge pull request #36 from lightpanda-io/objectunwrap
Expose inspector session wrapObject
2 parents 6e7f0da + 03bf8b9 commit 3cb2368

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

src/binding.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "include/v8-inspector.h"
66
#include "include/v8.h"
77
#include "src/api/api.h"
8+
#include "src/inspector/protocol/Runtime.h"
89

910
#include "inspector.h"
1011

@@ -1549,9 +1550,12 @@ void v8__base__SetDcheckFunction(void (*func)(const char*, int, const char*)) {
15491550

15501551
// Utils
15511552

1553+
static inline v8_inspector::StringView toStringView(const char *str, size_t length) {
1554+
auto* stringView = reinterpret_cast<const uint8_t*>(str);
1555+
return { stringView, length };
1556+
}
15521557
static inline v8_inspector::StringView toStringView(const std::string &str) {
1553-
auto* stringView = reinterpret_cast<const uint8_t*>(str.c_str());
1554-
return { stringView, str.length() };
1558+
return toStringView(str.c_str(), str.length());
15551559
}
15561560

15571561
static inline std::string fromStringView(v8::Isolate* isolate, const v8_inspector::StringView stringView) {
@@ -1621,12 +1625,25 @@ void v8_inspector__Session__DELETE(v8_inspector::V8InspectorSession* self) {
16211625
void v8_inspector__Session__dispatchProtocolMessage(
16221626
v8_inspector::V8InspectorSession *session, v8::Isolate *isolate,
16231627
const char *msg, int msg_len) {
1624-
std::string message;
1625-
message.assign(msg, msg_len);
1626-
auto str_view = toStringView(message);
1628+
auto str_view = toStringView(msg, msg_len);
16271629
session->dispatchProtocolMessage(str_view);
16281630
}
16291631

1632+
v8_inspector::protocol::Runtime::API::RemoteObject* v8_inspector__Session__wrapObject(
1633+
v8_inspector::V8InspectorSession *session, v8::Isolate *isolate,
1634+
const v8::Context* ctx, const v8::Value* val,
1635+
const char *grpname, int grpname_len, bool generatepreview) {
1636+
auto sv_grpname = toStringView(grpname, grpname_len);
1637+
auto remote_object = session->wrapObject(ptr_to_local(ctx), ptr_to_local(val), sv_grpname, generatepreview);
1638+
return remote_object.release();
1639+
}
1640+
1641+
// RemoteObject
1642+
1643+
void v8_inspector__RemoteObject__DELETE(v8_inspector::protocol::Runtime::API::RemoteObject* self) {
1644+
delete self;
1645+
}
1646+
16301647
// InspectorChannel
16311648

16321649
v8_inspector__Channel__IMPL * v8_inspector__Channel__IMPL__CREATE(v8::Isolate *isolate) {

src/binding.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,11 +993,19 @@ const Context* v8_inspector__Client__IMPL__valueSubtype(
993993
const Context* v8_inspector__Client__IMPL__descriptionForValueSubtype(
994994
InspectorClientImpl* self, Context context, Value value);
995995

996+
997+
// RemoteObject
998+
typedef struct RemoteObject RemoteObject;
999+
9961000
// InspectorSession
9971001

9981002
typedef struct InspectorSession InspectorSession;
999-
void v8_inspector__Session__DELETE(Inspector *self);
1003+
void v8_inspector__Session__DELETE(InspectorSession *self);
10001004
void v8_inspector__Session__dispatchProtocolMessage(InspectorSession *session, Isolate *isolate, const char* msg, usize msg_len);
1005+
RemoteObject* v8_inspector__Session__wrapObject(
1006+
InspectorSession *session, Isolate *isolate,
1007+
const Context* ctx, const Value* val,
1008+
const char *grpname, int grpname_len, bool generatepreview);
10011009

10021010
// Inspector
10031011
typedef struct Inspector Inspector;

src/v8.zig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,6 @@ pub export fn v8_inspector__Client__IMPL__ensureDefaultContextInGroup(
25512551
return inspector.ctx_handle;
25522552
}
25532553

2554-
25552554
usingnamespace if (@import("default_exports").inspector_subtype) struct {
25562555
pub export fn v8_inspector__Client__IMPL__valueSubtype(
25572556
_: *c.InspectorClientImpl,
@@ -2669,4 +2668,27 @@ pub const InspectorSession = struct {
26692668
msg.len,
26702669
);
26712670
}
2671+
2672+
pub fn wrapObject(self: InspectorSession, isolate: Isolate, ctx: Context, val: Value, grpname: []const u8, generatepreview: bool) !RemoteObject {
2673+
const remote_obj = c.v8_inspector__Session__wrapObject(
2674+
self.handle,
2675+
isolate.handle,
2676+
ctx.handle,
2677+
val.handle,
2678+
grpname.ptr,
2679+
grpname.len,
2680+
generatepreview,
2681+
);
2682+
if (remote_obj) {
2683+
return RemoteObject{ .handle = remote_obj };
2684+
} else return error.JsException;
2685+
}
2686+
};
2687+
2688+
pub const RemoteObject = struct {
2689+
handle: *c.RemoteObject,
2690+
2691+
pub fn deinit(self: *RemoteObject) void {
2692+
c.v8_inspector__RemoteObject__DELETE(self.handle);
2693+
}
26722694
};

0 commit comments

Comments
 (0)