Skip to content

Commit 0460c98

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

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ typedef struct CreateParams {
303303
usize v8__Isolate__CreateParams__SIZEOF();
304304
void v8__Isolate__CreateParams__CONSTRUCT(CreateParams* buf);
305305

306+
//
307+
306308
// FixedArray
307309
int v8__FixedArray__Length(const FixedArray* self);
308310
const Data* v8__FixedArray__Get(
@@ -1002,9 +1004,9 @@ typedef struct ScriptCompilerSource {
10021004

10031005
CompileHintCallback compile_hint_callback;
10041006
void* compile_hint_callback_data;
1005-
CompilationDetails compilation_details
1006-
1007+
CompilationDetails compilation_details;
10071008
} ScriptCompilerSource;
1009+
10081010
typedef enum BufferPolicy {
10091011
BufferNotOwned,
10101012
BufferOwned
@@ -1263,3 +1265,15 @@ void v8_inspector__RemoteObject__setPreview(RemoteObject* self, ObjectPreview* p
12631265
bool v8_inspector__RemoteObject__hasCustomPreview(RemoteObject* self);
12641266
const CustomPreview* v8_inspector__RemoteObject__getCustomPreview(RemoteObject* self);
12651267
void v8_inspector__RemoteObject__setCustomPreview(RemoteObject* self, CustomPreview* customPreview);
1268+
1269+
// SnapshotCreator
1270+
typedef struct SnapshotCreator SnapshotCreator;
1271+
1272+
SnapshotCreator* v8__SnapshotCreator__CREATE(const CreateParams* params);
1273+
Isolate* v8__SnapshotCreator__getIsolate(SnapshotCreator* self);
1274+
StartupData v8__internal__SnapshotCreator__CreateSnapshotDataBlobInternal(
1275+
SnapshotCreator* self,
1276+
const char* source,
1277+
int source_len
1278+
);
1279+
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)