Skip to content

Commit bf7ba69

Browse files
committed
Expose StringView directly from Inspector
Inspector.sendNotification and Inspector.sentResponse now expose the StringView directly, avoid multiple copies of the data.
1 parent 78fd576 commit bf7ba69

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

src/binding.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#include "include/v8.h"
77
#include "src/api/api.h"
88
#include "src/inspector/protocol/Runtime.h"
9+
#include "src/inspector/string-util.h"
910
#include "src/inspector/v8-string-conversions.h"
11+
1012
#include "src/debug/debug-interface.h"
1113

1214
#include "inspector.h"
@@ -1724,15 +1726,15 @@ static inline v8_inspector::StringView toStringView(const std::string &str) {
17241726
return toStringView(str.c_str(), str.length());
17251727
}
17261728

1727-
static v8::Local<v8::String> fromStringView(v8::Isolate* isolate, const v8_inspector::StringView stringView) {
1728-
int length = static_cast<int>(stringView.length());
1729-
v8::Local<v8::String> message = (
1730-
stringView.is8Bit()
1731-
? v8::String::NewFromOneByte(isolate, stringView.characters8(), v8::NewStringType::kNormal, length)
1732-
: v8::String::NewFromTwoByte(isolate, stringView.characters16(), v8::NewStringType::kNormal, length)
1733-
).ToLocalChecked();
1734-
return message;
1735-
}
1729+
// static v8::Local<v8::String> fromStringView(v8::Isolate* isolate, const v8_inspector::StringView stringView) {
1730+
// int length = static_cast<int>(stringView.length());
1731+
// v8::Local<v8::String> message = (
1732+
// stringView.is8Bit()
1733+
// ? v8::String::NewFromOneByte(isolate, stringView.characters8(), v8::NewStringType::kNormal, length)
1734+
// : v8::String::NewFromTwoByte(isolate, stringView.characters16(), v8::NewStringType::kNormal, length)
1735+
// ).ToLocalChecked();
1736+
// return message;
1737+
// }
17361738

17371739
/// Allocates a string as utf8 on the allocator without \0 terminator, for use in Zig.
17381740
/// The strings pointer and length should therefore be returned together
@@ -1996,24 +1998,23 @@ void v8_inspector__Channel__IMPL__SET_DATA(v8_inspector__Channel__IMPL *self, vo
19961998
// NOTE: zig project should provide those implementations with C-ABI functions
19971999
void v8_inspector__Channel__IMPL__sendResponse(
19982000
v8_inspector__Channel__IMPL* self, void* data,
1999-
int callId, v8::Local<v8::String> resp);
2001+
int callId, v8_inspector::StringView resp);
20002002
void v8_inspector__Channel__IMPL__sendNotification(
2001-
v8_inspector__Channel__IMPL* self, void *data,
2002-
v8::Local<v8::String> notif);
2003+
v8_inspector__Channel__IMPL *self, void *data,
2004+
v8_inspector::StringView notif);
20032005
void v8_inspector__Channel__IMPL__flushProtocolNotifications(
20042006
v8_inspector__Channel__IMPL* self, void *data);
20052007

20062008
// c++ implementation (just wrappers around the C/zig functions)
20072009
} // extern "C"
20082010
void v8_inspector__Channel__IMPL::sendResponse(
20092011
int callId, std::unique_ptr<v8_inspector::StringBuffer> message) {
2010-
const v8::Local<v8::String> resp = fromStringView(this->isolate, message->string());
2011-
return v8_inspector__Channel__IMPL__sendResponse(this, this->data, callId, resp);
2012+
return v8_inspector__Channel__IMPL__sendResponse(this, this->data, callId, message->string());
20122013
}
20132014
void v8_inspector__Channel__IMPL::sendNotification(
20142015
std::unique_ptr<v8_inspector::StringBuffer> message) {
2015-
const v8::Local<v8::String> notif = fromStringView(this->isolate, message->string());
2016-
return v8_inspector__Channel__IMPL__sendNotification(this, this->data, notif);
2016+
// const v8::Local<v8::String> msg = v8_inspector::toV8String(this->isolate, message->string());
2017+
return v8_inspector__Channel__IMPL__sendNotification(this, this->data, message->string());
20172018
}
20182019
void v8_inspector__Channel__IMPL::flushProtocolNotifications() {
20192020
return v8_inspector__Channel__IMPL__flushProtocolNotifications(this, this->data);
@@ -2026,14 +2027,15 @@ extern "C" {
20262027
void v8_inspector__Channel__sendResponse(
20272028
v8_inspector::V8Inspector::Channel* self, int callId,
20282029
v8_inspector::StringBuffer* message) {
2030+
20292031
self->sendResponse(
20302032
callId,
20312033
static_cast<std::unique_ptr<v8_inspector::StringBuffer>>(message));
20322034
}
20332035
void v8_inspector__Channel__sendNotification(
20342036
v8_inspector::V8Inspector::Channel* self,
20352037
v8_inspector::StringBuffer* message) {
2036-
self->sendNotification(
2038+
self->sendNotification(
20372039
static_cast<std::unique_ptr<v8_inspector::StringBuffer>>(message));
20382040
}
20392041
void v8_inspector__Channel__flushProtocolNotifications(
@@ -2146,4 +2148,13 @@ void v8_inspector__Client__consoleAPIMessage(
21462148
columnNumber, stackTrace);
21472149
}
21482150

2151+
size_t v8__StringView__Length(const v8_inspector::StringView* self) {
2152+
return self->length();
2153+
}
2154+
2155+
const uint8_t* v8__StringView__Bytes(const v8_inspector::StringView* self) {
2156+
return self->characters8();
2157+
}
2158+
21492159
} // extern "C"
2160+

src/binding.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,7 @@ void v8_inspector__RemoteObject__setPreview(RemoteObject* self, ObjectPreview* p
12271227
bool v8_inspector__RemoteObject__hasCustomPreview(RemoteObject* self);
12281228
const CustomPreview* v8_inspector__RemoteObject__getCustomPreview(RemoteObject* self);
12291229
void v8_inspector__RemoteObject__setCustomPreview(RemoteObject* self, CustomPreview* customPreview);
1230+
1231+
1232+
uint32_t v8__StringView__Length(const StringView* self);
1233+
const uint8_t* v8__StringView__Bytes(const StringView* self);

src/v8.zig

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,18 @@ pub const Primitive = struct {
22452245
}
22462246
};
22472247

2248+
pub const StringView = struct {
2249+
handle: *const c.StringView,
2250+
2251+
pub fn length(self: StringView) usize {
2252+
return c.v8__StringView__Length(self.handle);
2253+
}
2254+
2255+
pub fn bytes(self: StringView) [*c]const u8 {
2256+
return c.v8__StringView__Bytes(self.handle);
2257+
}
2258+
};
2259+
22482260
pub fn initUndefined(isolate: Isolate) Primitive {
22492261
return .{
22502262
.handle = c.v8__Undefined(isolate.handle).?,
@@ -2906,8 +2918,8 @@ pub const InspectorChannel = struct {
29062918
onNotif: onNotifFn = undefined,
29072919
onResp: onRespFn = undefined,
29082920

2909-
pub const onNotifFn = *const fn (ctx: *anyopaque, msg: *const c.String) void;
2910-
pub const onRespFn = *const fn (ctx: *anyopaque, call_id: u32, msg: *const c.String) void;
2921+
pub const onNotifFn = *const fn (ctx: *anyopaque, msg: StringView) void;
2922+
pub const onRespFn = *const fn (ctx: *anyopaque, call_id: u32, msg: StringView) void;
29112923

29122924
pub fn init(
29132925
ctx: *anyopaque,
@@ -2932,20 +2944,20 @@ pub const InspectorChannel = struct {
29322944
c.v8_inspector__Channel__IMPL__SET_DATA(self.handle, inspector);
29332945
}
29342946

2935-
fn resp(self: InspectorChannel, call_id: u32, msg: *const c.String) void {
2936-
self.onResp(self.ctx, call_id, msg);
2947+
fn resp(self: InspectorChannel, call_id: u32, msg: *const c.StringView) void {
2948+
self.onResp(self.ctx, call_id, .{.handle = msg});
29372949
}
29382950

2939-
fn notif(self: InspectorChannel, msg: *const c.String) void {
2940-
self.onNotif(self.ctx, msg);
2951+
fn notif(self: InspectorChannel, msg: *const c.StringView) void {
2952+
self.onNotif(self.ctx, .{.handle = msg});
29412953
}
29422954
};
29432955

29442956
pub export fn v8_inspector__Channel__IMPL__sendResponse(
29452957
_: *c.InspectorChannelImpl,
29462958
data: *anyopaque,
29472959
call_id: c_int,
2948-
msg: *const c.String,
2960+
msg: *const c.StringView,
29492961
) callconv(.C) void {
29502962
const inspector = Inspector.fromData(data);
29512963
inspector.channel.resp(@as(u32, @intCast(call_id)), msg);
@@ -2954,7 +2966,7 @@ pub export fn v8_inspector__Channel__IMPL__sendResponse(
29542966
pub export fn v8_inspector__Channel__IMPL__sendNotification(
29552967
_: *c.InspectorChannelImpl,
29562968
data: *anyopaque,
2957-
msg: *const c.String,
2969+
msg: *const c.StringView,
29582970
) callconv(.C) void {
29592971
const inspector = Inspector.fromData(data);
29602972
inspector.channel.notif(msg);

0 commit comments

Comments
 (0)