diff --git a/src/binding.cpp b/src/binding.cpp index f03e134..e87809b 100644 --- a/src/binding.cpp +++ b/src/binding.cpp @@ -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" @@ -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( diff --git a/src/binding.h b/src/binding.h index af86650..917372d 100644 --- a/src/binding.h +++ b/src/binding.h @@ -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_; @@ -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); diff --git a/src/v8.zig b/src/v8.zig index 91e17d7..210e84c 100644 --- a/src/v8.zig +++ b/src/v8.zig @@ -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); } @@ -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); + } +};