Skip to content

Commit ad4aaa6

Browse files
committed
Provides an PoC of using V8 snapshots
1 parent c9dc4ef commit ad4aaa6

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/browser/js/Env.zig

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const js = @import("js.zig");
33
const v8 = js.v8;
44

55
const log = @import("../../log.zig");
6+
const polyfill = @import("../polyfill/polyfill.zig");
67

78
const types = @import("types.zig");
89
const Types = types.Types;
@@ -34,6 +35,8 @@ isolate: v8.Isolate,
3435
// just kept around because we need to free it on deinit
3536
isolate_params: *v8.CreateParams,
3637

38+
isolate_snaphost: v8.StartupData,
39+
3740
// Given a type, we can lookup its index in TYPE_LOOKUP and then have
3841
// access to its TunctionTemplate (the thing we need to create an instance
3942
// of it)
@@ -54,6 +57,21 @@ context_id: usize,
5457
const Opts = struct {};
5558

5659
pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
60+
const env = try allocator.create(Env);
61+
errdefer allocator.destroy(env);
62+
63+
env.* = .{
64+
.context_id = 0,
65+
.isolate = undefined,
66+
.isolate_params = undefined,
67+
.isolate_snaphost = undefined,
68+
.platform = platform,
69+
.templates = undefined,
70+
.allocator = allocator,
71+
.meta_lookup = undefined,
72+
.prototype_lookup = undefined,
73+
};
74+
5775
// var params = v8.initCreateParams();
5876
var params = try allocator.create(v8.CreateParams);
5977
errdefer allocator.destroy(params);
@@ -63,9 +81,15 @@ pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
6381
params.array_buffer_allocator = v8.createDefaultArrayBufferAllocator();
6482
errdefer v8.destroyArrayBufferAllocator(params.array_buffer_allocator.?);
6583

84+
env.isolate_snaphost = generateIsolateSnaphot(params);
85+
params.snapshot_blob = &env.isolate_snaphost;
86+
6687
var isolate = v8.Isolate.init(params);
6788
errdefer isolate.deinit();
6889

90+
env.isolate = isolate;
91+
env.isolate_params = params;
92+
6993
// This is the callback that runs whenever a module is dynamically imported.
7094
isolate.setHostImportModuleDynamicallyCallback(Context.dynamicModuleCallback);
7195
isolate.setPromiseRejectCallback(promiseRejectCallback);
@@ -80,20 +104,6 @@ pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
80104
v8.HandleScope.init(&temp_scope, isolate);
81105
defer temp_scope.deinit();
82106

83-
const env = try allocator.create(Env);
84-
errdefer allocator.destroy(env);
85-
86-
env.* = .{
87-
.context_id = 0,
88-
.platform = platform,
89-
.isolate = isolate,
90-
.templates = undefined,
91-
.allocator = allocator,
92-
.isolate_params = params,
93-
.meta_lookup = undefined,
94-
.prototype_lookup = undefined,
95-
};
96-
97107
// Populate our templates lookup. generateClass creates the
98108
// v8.FunctionTemplate, which we store in our env.templates.
99109
// The ordering doesn't matter. What matters is that, given a type
@@ -537,3 +547,21 @@ fn generateUndetectable(comptime Struct: type, template: v8.ObjectTemplate) void
537547
template.markAsUndetectable();
538548
}
539549
}
550+
551+
fn generateIsolateSnaphot(params: *v8.CreateParams) v8.StartupData {
552+
var snapshot_creator = v8.SnapshotCreator{};
553+
snapshot_creator.init(params);
554+
defer snapshot_creator.deinit();
555+
556+
const isolate = snapshot_creator.getIsolate();
557+
558+
var temp_scope: v8.HandleScope = undefined;
559+
v8.HandleScope.init(&temp_scope, isolate);
560+
defer temp_scope.deinit();
561+
562+
const context = v8.Context.init(isolate, null, null);
563+
context.enter();
564+
defer context.exit();
565+
566+
return snapshot_creator.createSnapshotDataBlob("function LightpandaSnapshotPoC() { return 73; }");
567+
}

src/tests/polyfill/webcomponents.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
}
1414
}
1515

16+
testing.expectEqual(73, LightpandaSnapshotPoC());
17+
1618
window.customElements.define("lightpanda-test", LightPanda);
1719
const main = document.getElementById('main');
1820
main.appendChild(document.createElement('lightpanda-test'));

0 commit comments

Comments
 (0)