Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "src/inspector/protocol/Runtime.h"
#include "src/inspector/v8-string-conversions.h"
#include "src/debug/debug-interface.h"
#include "src/snapshot/snapshot.h"

#include "inspector.h"

Expand Down Expand Up @@ -2029,6 +2030,48 @@ void v8_inspector__Channel__IMPL__sendNotification(
void v8_inspector__Channel__IMPL__flushProtocolNotifications(
v8_inspector__Channel__IMPL* self, void *data);

// SnapshotCreator

// Initialize and enter an isolate, and set it up for serialization. The
// isolate is either created from scratch or from an existing snapshot. The
// caller keeps ownership of the argument snapshot.

void v8__SnapshotCreator__CONSTRUCT(v8::SnapshotCreator* ptr, const v8::Isolate::CreateParams& params) {
new (ptr) v8::SnapshotCreator(params.external_references, nullptr);
}

v8::Isolate* v8__SnapshotCreator__getIsolate(v8::SnapshotCreator& self) {
return self.GetIsolate();
}

void v8__SnapshotCreator__setDefaultContext(v8::SnapshotCreator& self, const v8::Context& ctx) {
self.SetDefaultContext(ptr_to_local(&ctx));
}

// Add additional context to be included in the snapshot blob. The snapshot
// will include the global proxy.
// Returns the index of the context in the snapshot blob.
size_t v8__SnapshotCreator__addContext(v8::SnapshotCreator& self, const v8::Context& ctx) {
return self.AddContext(ptr_to_local(&ctx));
}

void v8__SnapshotCreator__setDefaultContextWithCallbacks(
v8::SnapshotCreator& self,
const v8::Context& ctx,
v8::SerializeInternalFieldsCallback internal_fields,
v8::SerializeContextDataCallback context_data,
v8::SerializeAPIWrapperCallback api_wrapper) {

self.SetDefaultContext(ptr_to_local(&ctx), internal_fields, context_data, api_wrapper);
}


v8::StartupData v8__SnapshotCreator__createBlob(v8::SnapshotCreator& self) {
return self.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kKeep);
}

void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator* self) { self->~SnapshotCreator(); }

// c++ implementation (just wrappers around the C/zig functions)
} // extern "C"
void v8_inspector__Channel__IMPL::sendResponse(
Expand Down
36 changes: 36 additions & 0 deletions src/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,25 @@ typedef struct StartupData {
int raw_size;
} StartupData;

// Callback types for snapshot serialization
typedef StartupData (*SerializeInternalFieldsCallbackFunction)(Object* holder, int index, void* data);
typedef struct SerializeInternalFieldsCallback {
SerializeInternalFieldsCallbackFunction callback;
void* data;
} SerializeInternalFieldsCallback;

typedef StartupData (*SerializeContextDataCallbackFunction)(Context* context, int index, void* data);
typedef struct SerializeContextDataCallback {
SerializeContextDataCallbackFunction callback;
void* data;
} SerializeContextDataCallback;

typedef StartupData (*SerializeAPIWrapperCallbackFunction)(Object* holder, void* cpp_heap_pointer, void* data);
typedef struct SerializeAPIWrapperCallback {
SerializeAPIWrapperCallbackFunction callback;
void* data;
} SerializeAPIWrapperCallback;

typedef struct ResourceConstraints {
usize code_range_size_;
usize max_old_generation_size_;
Expand Down Expand Up @@ -1238,3 +1257,20 @@ void v8_inspector__RemoteObject__setPreview(RemoteObject* self, ObjectPreview* p
bool v8_inspector__RemoteObject__hasCustomPreview(RemoteObject* self);
const CustomPreview* v8_inspector__RemoteObject__getCustomPreview(RemoteObject* self);
void v8_inspector__RemoteObject__setCustomPreview(RemoteObject* self, CustomPreview* customPreview);

// SnapshotCreator
typedef struct SnapshotCreator {
void* impl_;
} SnapshotCreator;
void v8__SnapshotCreator__CONSTRUCT(SnapshotCreator* self, const CreateParams* params);
void v8__SnapshotCreator__setDefaultContext(SnapshotCreator* self, const Context* ctx);
void v8__SnapshotCreator__setDefaultContextWithCallbacks(
SnapshotCreator* self,
const Context* ctx,
SerializeInternalFieldsCallback internal_fields,
SerializeContextDataCallback context_data,
SerializeAPIWrapperCallback api_wrapper);
Isolate* v8__SnapshotCreator__getIsolate(SnapshotCreator* self);
size_t v8__SnapshotCreator__addContext(SnapshotCreator* self, const Context* ctx);
StartupData v8__SnapshotCreator__createBlob(SnapshotCreator* self);
void v8__SnapshotCreator__DESTRUCT(SnapshotCreator* self);
58 changes: 57 additions & 1 deletion src/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,6 @@ pub const Value = struct {
return c.v8__Value__IsString(self.handle);
}


pub fn isSymbol(self: Self) bool {
return c.v8__Value__IsSymbol(self.handle);
}
Expand Down Expand Up @@ -3118,3 +3117,60 @@ pub export fn zigAlloc(self: *anyopaque, bytes: usize) callconv(.C) ?[*]u8 {
const allocated_bytes = allocator.alloc(u8, bytes) catch return null;
return allocated_bytes.ptr;
}
pub const StartupData = c.StartupData;

pub const C_SerializeInternalFieldsCallback = c.SerializeInternalFieldsCallback;
pub const C_SerializeContextDataCallback = c.SerializeContextDataCallback;
pub const C_SerializeAPIWrapperCallback = c.SerializeAPIWrapperCallback;

pub const SnapshotCreator = struct {
const Self = @This();

inner: c.SnapshotCreator,

pub fn init(params: *const c.CreateParams) Self {
var inner: c.SnapshotCreator = undefined;
c.v8__SnapshotCreator__CONSTRUCT(&inner, params);
return .{
.inner = inner,
};
}

pub fn getIsolate(self: *Self) Isolate {
return .{
.handle = c.v8__SnapshotCreator__getIsolate(&self.inner).?,
};
}

pub fn setDefaultContext(self: *Self, ctx: Context) void {
c.v8__SnapshotCreator__setDefaultContext(&self.inner, ctx.handle);
}

pub fn setDefaultContextWithCallbacks(
self: *Self,
ctx: Context,
si: C_SerializeInternalFieldsCallback,
scd: C_SerializeContextDataCallback,
sapiw: C_SerializeAPIWrapperCallback,
) void {
c.v8__SnapshotCreator__setDefaultContextWithCallbacks(
&self.inner,
ctx.handle,
si,
scd,
sapiw,
);
}

pub fn addContext(self: *Self, ctx: Context) usize {
return c.v8__SnapshotCreator__addContext(&self.inner, ctx.handle);
}

pub fn createBlob(self: *Self) StartupData {
return c.v8__SnapshotCreator__createBlob(&self.inner);
}

pub fn deinit(self: *Self) void {
c.v8__SnapshotCreator__DESTRUCT(&self.inner);
}
};