Skip to content

Commit 78fd576

Browse files
committed
cbor
1 parent faab449 commit 78fd576

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/binding.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,15 +1724,14 @@ static inline v8_inspector::StringView toStringView(const std::string &str) {
17241724
return toStringView(str.c_str(), str.length());
17251725
}
17261726

1727-
static inline std::string fromStringView(v8::Isolate* isolate, const v8_inspector::StringView stringView) {
1727+
static v8::Local<v8::String> fromStringView(v8::Isolate* isolate, const v8_inspector::StringView stringView) {
17281728
int length = static_cast<int>(stringView.length());
17291729
v8::Local<v8::String> message = (
17301730
stringView.is8Bit()
17311731
? v8::String::NewFromOneByte(isolate, stringView.characters8(), v8::NewStringType::kNormal, length)
17321732
: v8::String::NewFromTwoByte(isolate, stringView.characters16(), v8::NewStringType::kNormal, length)
17331733
).ToLocalChecked();
1734-
v8::String::Utf8Value result(isolate, message);
1735-
return *result;
1734+
return message;
17361735
}
17371736

17381737
/// Allocates a string as utf8 on the allocator without \0 terminator, for use in Zig.
@@ -1997,24 +1996,24 @@ void v8_inspector__Channel__IMPL__SET_DATA(v8_inspector__Channel__IMPL *self, vo
19971996
// NOTE: zig project should provide those implementations with C-ABI functions
19981997
void v8_inspector__Channel__IMPL__sendResponse(
19991998
v8_inspector__Channel__IMPL* self, void* data,
2000-
int callId, const char* message, size_t length);
1999+
int callId, v8::Local<v8::String> resp);
20012000
void v8_inspector__Channel__IMPL__sendNotification(
20022001
v8_inspector__Channel__IMPL* self, void *data,
2003-
const char* msg, size_t length);
2002+
v8::Local<v8::String> notif);
20042003
void v8_inspector__Channel__IMPL__flushProtocolNotifications(
20052004
v8_inspector__Channel__IMPL* self, void *data);
20062005

20072006
// c++ implementation (just wrappers around the C/zig functions)
20082007
} // extern "C"
20092008
void v8_inspector__Channel__IMPL::sendResponse(
20102009
int callId, std::unique_ptr<v8_inspector::StringBuffer> message) {
2011-
const std::string resp = fromStringView(this->isolate, message->string());
2012-
return v8_inspector__Channel__IMPL__sendResponse(this, this->data, callId, resp.c_str(), resp.length());
2010+
const v8::Local<v8::String> resp = fromStringView(this->isolate, message->string());
2011+
return v8_inspector__Channel__IMPL__sendResponse(this, this->data, callId, resp);
20132012
}
20142013
void v8_inspector__Channel__IMPL::sendNotification(
20152014
std::unique_ptr<v8_inspector::StringBuffer> message) {
2016-
const std::string notif = fromStringView(this->isolate, message->string());
2017-
return v8_inspector__Channel__IMPL__sendNotification(this, this->data, notif.c_str(), notif.length());
2015+
const v8::Local<v8::String> notif = fromStringView(this->isolate, message->string());
2016+
return v8_inspector__Channel__IMPL__sendNotification(this, this->data, notif);
20182017
}
20192018
void v8_inspector__Channel__IMPL::flushProtocolNotifications() {
20202019
return v8_inspector__Channel__IMPL__flushProtocolNotifications(this, this->data);

src/binding.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,10 @@ void v8_inspector__Channel__IMPL__SET_DATA(InspectorChannelImpl* self, void *dat
10981098

10991099
void v8_inspector__Channel__IMPL__sendResponse(
11001100
InspectorChannelImpl *self, void *data,
1101-
int callId, char *message, size_t length);
1101+
int callId, String resp);
11021102
void v8_inspector__Channel__IMPL__sendNotification(
11031103
InspectorChannelImpl *self, void *data,
1104-
char *message, size_t length);
1104+
String notif);
11051105
void v8_inspector__Channel__IMPL__flushProtocolNotifications(
11061106
InspectorChannelImpl *self, void *data);
11071107

src/v8.zig

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,26 @@ pub const HandleScope = struct {
501501
}
502502
};
503503

504+
pub const SealHandleScope = struct {
505+
const Self = @This();
506+
507+
inner: c.SealHandleScope,
508+
509+
/// [Notes]
510+
/// This starts a new stack frame to record local objects created.
511+
/// Since deinit depends on the inner pointer being the same, init should construct in place.
512+
pub fn init(self: *Self, isolate: Isolate) void {
513+
c.v8__SealHandleScope__CONSTRUCT(&self.inner, isolate.handle);
514+
}
515+
516+
/// [Notes]
517+
/// This pops the scope frame and allows V8 to mark/free local objects created since SealHandleScope.init.
518+
/// In C++ code, this would happen automatically when the SealHandleScope var leaves the current scope.
519+
pub fn deinit(self: *Self) void {
520+
c.v8__SealHandleScope__DESTRUCT(&self.inner);
521+
}
522+
};
523+
504524
pub const Context = struct {
505525
const Self = @This();
506526

@@ -2886,8 +2906,8 @@ pub const InspectorChannel = struct {
28862906
onNotif: onNotifFn = undefined,
28872907
onResp: onRespFn = undefined,
28882908

2889-
pub const onNotifFn = *const fn (ctx: *anyopaque, msg: []const u8) void;
2890-
pub const onRespFn = *const fn (ctx: *anyopaque, call_id: u32, msg: []const u8) void;
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;
28912911

28922912
pub fn init(
28932913
ctx: *anyopaque,
@@ -2912,11 +2932,11 @@ pub const InspectorChannel = struct {
29122932
c.v8_inspector__Channel__IMPL__SET_DATA(self.handle, inspector);
29132933
}
29142934

2915-
fn resp(self: InspectorChannel, call_id: u32, msg: []const u8) void {
2935+
fn resp(self: InspectorChannel, call_id: u32, msg: *const c.String) void {
29162936
self.onResp(self.ctx, call_id, msg);
29172937
}
29182938

2919-
fn notif(self: InspectorChannel, msg: []const u8) void {
2939+
fn notif(self: InspectorChannel, msg: *const c.String) void {
29202940
self.onNotif(self.ctx, msg);
29212941
}
29222942
};
@@ -2925,21 +2945,19 @@ pub export fn v8_inspector__Channel__IMPL__sendResponse(
29252945
_: *c.InspectorChannelImpl,
29262946
data: *anyopaque,
29272947
call_id: c_int,
2928-
msg: [*c]u8,
2929-
length: usize,
2948+
msg: *const c.String,
29302949
) callconv(.C) void {
29312950
const inspector = Inspector.fromData(data);
2932-
inspector.channel.resp(@as(u32, @intCast(call_id)), msg[0..length]);
2951+
inspector.channel.resp(@as(u32, @intCast(call_id)), msg);
29332952
}
29342953

29352954
pub export fn v8_inspector__Channel__IMPL__sendNotification(
29362955
_: *c.InspectorChannelImpl,
29372956
data: *anyopaque,
2938-
msg: [*c]u8,
2939-
length: usize,
2957+
msg: *const c.String,
29402958
) callconv(.C) void {
29412959
const inspector = Inspector.fromData(data);
2942-
inspector.channel.notif(msg[0..length]);
2960+
inspector.channel.notif(msg);
29432961
}
29442962

29452963
pub export fn v8_inspector__Channel__IMPL__flushProtocolNotifications(

0 commit comments

Comments
 (0)