Skip to content

Commit ba935ad

Browse files
committed
Add API for v8 snapshots
1 parent 305bb37 commit ba935ad

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/binding.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "src/inspector/protocol/Runtime.h"
1010
#include "src/inspector/v8-string-conversions.h"
1111
#include "src/debug/debug-interface.h"
12+
#include "src/snapshot/snapshot.h"
1213

1314
#include "inspector.h"
1415

@@ -2247,3 +2248,28 @@ void v8_inspector__Client__consoleAPIMessage(
22472248
}
22482249

22492250
} // extern "C"
2251+
2252+
// SnapshotCreator
2253+
extern "C" {
2254+
2255+
v8::SnapshotCreator* v8__SnapshotCreator__CREATE(const v8::Isolate::CreateParams& params) {
2256+
return new v8::SnapshotCreator(params);
2257+
}
2258+
2259+
v8::Isolate* v8__SnapshotCreator__getIsolate(v8::SnapshotCreator* self) {
2260+
return self->GetIsolate();
2261+
}
2262+
2263+
v8::StartupData v8__internal__SnapshotCreator__CreateSnapshotDataBlobInternal(
2264+
v8::SnapshotCreator *self, const char *source, int source_len) {
2265+
std::string source_string(source, source_len);
2266+
return v8::internal::CreateSnapshotDataBlobInternal(
2267+
v8::SnapshotCreator::FunctionCodeHandling::kClear, source_string.data(),
2268+
*self);
2269+
}
2270+
2271+
void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator* self) {
2272+
delete self;
2273+
}
2274+
2275+
} // extern "C"

src/binding.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,9 +1002,9 @@ typedef struct ScriptCompilerSource {
10021002

10031003
CompileHintCallback compile_hint_callback;
10041004
void* compile_hint_callback_data;
1005-
CompilationDetails compilation_details
1006-
1005+
CompilationDetails compilation_details;
10071006
} ScriptCompilerSource;
1007+
10081008
typedef enum BufferPolicy {
10091009
BufferNotOwned,
10101010
BufferOwned
@@ -1263,3 +1263,15 @@ void v8_inspector__RemoteObject__setPreview(RemoteObject* self, ObjectPreview* p
12631263
bool v8_inspector__RemoteObject__hasCustomPreview(RemoteObject* self);
12641264
const CustomPreview* v8_inspector__RemoteObject__getCustomPreview(RemoteObject* self);
12651265
void v8_inspector__RemoteObject__setCustomPreview(RemoteObject* self, CustomPreview* customPreview);
1266+
1267+
// SnapshotCreator
1268+
typedef struct SnapshotCreator SnapshotCreator;
1269+
1270+
SnapshotCreator* v8__SnapshotCreator__CREATE(const CreateParams* params);
1271+
Isolate* v8__SnapshotCreator__getIsolate(SnapshotCreator* self);
1272+
StartupData v8__internal__SnapshotCreator__CreateSnapshotDataBlobInternal(
1273+
SnapshotCreator* self,
1274+
const char* source,
1275+
int source_len
1276+
);
1277+
void v8__SnapshotCreator__DESTRUCT(SnapshotCreator* self);

src/v8.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,3 +3203,31 @@ pub export fn zigAlloc(self: *anyopaque, bytes: usize) callconv(.c) ?[*]u8 {
32033203
const allocated_bytes = allocator.alloc(u8, bytes) catch return null;
32043204
return allocated_bytes.ptr;
32053205
}
3206+
3207+
pub const StartupData = c.StartupData;
3208+
3209+
pub const SnapshotCreator = struct {
3210+
handle: *c.SnapshotCreator = undefined,
3211+
3212+
pub fn init(self: *SnapshotCreator, params: *const c.CreateParams) void {
3213+
self.handle = c.v8__SnapshotCreator__CREATE(params).?;
3214+
}
3215+
3216+
pub fn getIsolate(self: *SnapshotCreator) Isolate {
3217+
return .{
3218+
.handle = c.v8__SnapshotCreator__getIsolate(self.handle).?,
3219+
};
3220+
}
3221+
3222+
pub fn createSnapshotDataBlob(self: *SnapshotCreator, source: []const u8) StartupData {
3223+
return c.v8__internal__SnapshotCreator__CreateSnapshotDataBlobInternal(
3224+
self.handle,
3225+
source.ptr,
3226+
@intCast(source.len),
3227+
);
3228+
}
3229+
3230+
pub fn deinit(self: *SnapshotCreator) void {
3231+
c.v8__SnapshotCreator__DESTRUCT(self.handle);
3232+
}
3233+
};

0 commit comments

Comments
 (0)