Skip to content

Commit 2aafa05

Browse files
committed
Remote object as plain struct
1 parent 4672b16 commit 2aafa05

File tree

1 file changed

+58
-85
lines changed

1 file changed

+58
-85
lines changed

src/v8.zig

Lines changed: 58 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,7 +2670,7 @@ pub const InspectorSession = struct {
26702670
}
26712671

26722672
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(
2673+
const remote_object = c.v8_inspector__Session__wrapObject(
26742674
self.handle,
26752675
isolate.handle,
26762676
ctx.handle,
@@ -2679,125 +2679,98 @@ pub const InspectorSession = struct {
26792679
grpname.len,
26802680
generatepreview,
26812681
).?;
2682-
return RemoteObject{ .handle = remote_obj };
2682+
return RemoteObject.init(remote_object);
26832683
}
26842684
};
26852685

2686-
/// RemoteObject is implemented as an interface to its members as opposed to a POD struct, since V8 is inconsistent with
2687-
/// returning owning and non-owning data. This is partly due to v8 using UTF16 strings.
2688-
/// NOTE: Some getters return owned memory (strings), while others return memory owned by V8 (objects).
2689-
/// The latter are not freed by the caller, but the former must be freed.
2690-
/// The getter methods short-circuit if the dafavalues is not available as converting the defaults to V8 causes unnecessary overhead.
2686+
/// Currently RemoteObject is implemented as a plain struct as opposed to an interface to its members.
2687+
/// This likely needs to change if we need to call setters in the future, or as more uscases with remoteObject appear.
2688+
/// An interface like implementation is not prefered as V8 is inconsistend with handing out owning an non-owning memory. This is partly due to v8 using UTF16 strings.
2689+
/// For an interface implementation: Some getters return owned memory (strings), while others return memory owned by V8 (objects).
2690+
/// Now instead, we do not have to exposed that to our users, which only have to make a single deinit call.
2691+
/// The getters short-circuit if the default values is not available as converting the defaults to V8 causes unnecessary overhead.
2692+
///
2693+
/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject
26912694
pub const RemoteObject = struct {
26922695
handle: *c.RemoteObject,
2696+
type: []const u8,
2697+
subtype: ?[]const u8,
2698+
class_name: ?[]const u8,
2699+
// NotYetImplemented: value,
2700+
// NotYetImplemented: unserializable_value,
2701+
description: ?[]const u8,
2702+
// NotYetImplemented: web_driver_value,
2703+
object_id: ?[]const u8,
2704+
// NotYetImplemented: preview,
2705+
// NotYetImplemented: custom_preview,
2706+
2707+
pub fn init(handle: *c.RemoteObject) RemoteObject {
2708+
return .{
2709+
.handle = handle,
2710+
.type = RemoteObject.getType(handle),
2711+
.subtype = RemoteObject.getSubtype(handle),
2712+
.class_name = RemoteObject.getClassName(handle),
2713+
.description = RemoteObject.getDescription(handle),
2714+
.object_id = RemoteObject.getObjectId(handle),
2715+
};
2716+
}
26932717

26942718
pub fn deinit(self: RemoteObject) void {
26952719
c.v8_inspector__RemoteObject__DELETE(self.handle);
2720+
std.c.free(self.type);
2721+
if (self.subtype) |subtype| {
2722+
std.c.free(subtype);
2723+
}
2724+
if (self.class_name) |class_name| {
2725+
std.c.free(class_name);
2726+
}
2727+
if (self.description) |description| {
2728+
std.c.free(description);
2729+
}
2730+
if (self.object_id) |object_id| {
2731+
std.c.free(object_id);
2732+
}
26962733
}
26972734

2698-
// Type
2699-
pub fn getType(self: RemoteObject) []const u8 {
2700-
const type_ = c.v8_inspector__RemoteObject__getType(self.handle);
2735+
fn getType(handle: *c.RemoteObject) []const u8 {
2736+
const type_ = c.v8_inspector__RemoteObject__getType(handle);
27012737
const idx = std.mem.indexOfSentinel(u8, 0, type_);
27022738
return type_[0..idx];
27032739
}
2704-
pub fn setType(self: RemoteObject, type_: []const u8) void {
2705-
c.v8_inspector__RemoteObject__setType(self.handle, type_.ptr, type_.len);
2706-
}
2707-
2708-
// Subtype
2709-
pub fn hasSubtype(self: RemoteObject) bool {
2710-
return c.v8_inspector__RemoteObject__hasSubtype(self.handle);
2711-
}
2712-
pub fn getSubtype(self: RemoteObject) ?[]const u8 {
2713-
if (!c.v8_inspector__RemoteObject__hasSubtype(self.handle)) {
2740+
fn getSubtype(handle: *c.RemoteObject) ?[]const u8 {
2741+
if (!c.v8_inspector__RemoteObject__hasSubtype(handle)) {
27142742
return null;
27152743
}
27162744

2717-
const subtype = c.v8_inspector__RemoteObject__getSubtype(self.handle);
2745+
const subtype = c.v8_inspector__RemoteObject__getSubtype(handle);
27182746
const idx = std.mem.indexOfSentinel(u8, 0, subtype);
27192747
return subtype[0..idx];
27202748
}
2721-
pub fn setSubtype(self: RemoteObject, subtype: []const u8) void {
2722-
c.v8_inspector__RemoteObject__setSubtype(self.handle, subtype.ptr, subtype.len);
2723-
}
2724-
2725-
// ClassName
2726-
pub fn hasClassName(self: RemoteObject) bool {
2727-
return c.v8_inspector__RemoteObject__hasClassName(self.handle);
2728-
}
2729-
pub fn getClassName(self: RemoteObject) ?[]const u8 {
2730-
if (!c.v8_inspector__RemoteObject__hasClassName(self.handle)) {
2749+
fn getClassName(handle: *c.RemoteObject) ?[]const u8 {
2750+
if (!c.v8_inspector__RemoteObject__hasClassName(handle)) {
27312751
return null;
27322752
}
27332753

2734-
const class_name = c.v8_inspector__RemoteObject__getClassName(self.handle);
2754+
const class_name = c.v8_inspector__RemoteObject__getClassName(handle);
27352755
const idx = std.mem.indexOfSentinel(u8, 0, class_name);
27362756
return class_name[0..idx];
27372757
}
2738-
2739-
// Value
2740-
pub fn hasValue(self: RemoteObject) bool {
2741-
return c.v8_inspector__RemoteObject__hasValue(self.handle);
2742-
}
2743-
// TODO GET / SET
2744-
2745-
// UnserializableValue
2746-
pub fn hasUnserializableValue(self: RemoteObject) bool {
2747-
return c.v8_inspector__RemoteObject__hasUnserializableValue(self.handle);
2748-
}
2749-
// TODO GET / SET
2750-
2751-
// Description
2752-
pub fn hasDescription(self: RemoteObject) bool {
2753-
return c.v8_inspector__RemoteObject__hasDescription(self.handle);
2754-
}
2755-
pub fn getDescription(self: RemoteObject) ?[]const u8 {
2756-
if (!c.v8_inspector__RemoteObject__hasDescription(self.handle)) {
2758+
fn getDescription(handle: *c.RemoteObject) ?[]const u8 {
2759+
if (!c.v8_inspector__RemoteObject__hasDescription(handle)) {
27572760
return null;
27582761
}
27592762

2760-
const description = c.v8_inspector__RemoteObject__getDescription(self.handle);
2763+
const description = c.v8_inspector__RemoteObject__getDescription(handle);
27612764
const idx = std.mem.indexOfSentinel(u8, 0, description);
27622765
return description[0..idx];
27632766
}
2764-
pub fn setDescription(self: RemoteObject, description: []const u8) void {
2765-
c.v8_inspector__RemoteObject__setDescription(self.handle, description.ptr, description.len);
2766-
}
2767-
2768-
// WebDriverValue
2769-
pub fn hasWebDriverValue(self: RemoteObject) bool {
2770-
return c.v8_inspector__RemoteObject__hasWebDriverValue(self.handle);
2771-
}
2772-
// TODO GET / SET
2773-
2774-
// ObjectId
2775-
pub fn hasObjectId(self: RemoteObject) bool {
2776-
return c.v8_inspector__RemoteObject__hasObjectId(self.handle);
2777-
}
2778-
pub fn getObjectId(self: RemoteObject) ?[]const u8 {
2779-
if (!c.v8_inspector__RemoteObject__hasObjectId(self.handle)) {
2767+
fn getObjectId(handle: *c.RemoteObject) ?[]const u8 {
2768+
if (!c.v8_inspector__RemoteObject__hasObjectId(handle)) {
27802769
return null;
27812770
}
27822771

2783-
const object_id = c.v8_inspector__RemoteObject__getObjectId(self.handle);
2772+
const object_id = c.v8_inspector__RemoteObject__getObjectId(handle);
27842773
const idx = std.mem.indexOfSentinel(u8, 0, object_id);
27852774
return object_id[0..idx];
27862775
}
2787-
2788-
pub fn setObjectId(self: RemoteObject, object_id: []const u8) void {
2789-
c.v8_inspector__RemoteObject__setObjectId(self.handle, object_id.ptr, object_id.len);
2790-
}
2791-
2792-
// Preview
2793-
pub fn hasPreview(self: RemoteObject) bool {
2794-
return c.v8_inspector__RemoteObject__hasPreview(self.handle);
2795-
}
2796-
// TODO GET / SET
2797-
2798-
// CustomPreview
2799-
pub fn hasCustomPreview(self: RemoteObject) bool {
2800-
return c.v8_inspector__RemoteObject__hasCustomPreview(self.handle);
2801-
}
2802-
// TODO GET / SET
28032776
};

0 commit comments

Comments
 (0)