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
56 changes: 42 additions & 14 deletions src/browser/js/Env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const js = @import("js.zig");
const v8 = js.v8;

const log = @import("../../log.zig");
const polyfill = @import("../polyfill/polyfill.zig");

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

isolate_snaphost: v8.StartupData,

// Given a type, we can lookup its index in TYPE_LOOKUP and then have
// access to its TunctionTemplate (the thing we need to create an instance
// of it)
Expand All @@ -54,6 +57,21 @@ context_id: usize,
const Opts = struct {};

pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
const env = try allocator.create(Env);
errdefer allocator.destroy(env);

env.* = .{
.context_id = 0,
.isolate = undefined,
.isolate_params = undefined,
.isolate_snaphost = undefined,
.platform = platform,
.templates = undefined,
.allocator = allocator,
.meta_lookup = undefined,
.prototype_lookup = undefined,
};

// var params = v8.initCreateParams();
var params = try allocator.create(v8.CreateParams);
errdefer allocator.destroy(params);
Expand All @@ -63,9 +81,15 @@ pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
params.array_buffer_allocator = v8.createDefaultArrayBufferAllocator();
errdefer v8.destroyArrayBufferAllocator(params.array_buffer_allocator.?);

env.isolate_snaphost = generateIsolateSnaphot(params);
params.snapshot_blob = &env.isolate_snaphost;

var isolate = v8.Isolate.init(params);
errdefer isolate.deinit();

env.isolate = isolate;
env.isolate_params = params;

// This is the callback that runs whenever a module is dynamically imported.
isolate.setHostImportModuleDynamicallyCallback(Context.dynamicModuleCallback);
isolate.setPromiseRejectCallback(promiseRejectCallback);
Expand All @@ -80,20 +104,6 @@ pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Env {
v8.HandleScope.init(&temp_scope, isolate);
defer temp_scope.deinit();

const env = try allocator.create(Env);
errdefer allocator.destroy(env);

env.* = .{
.context_id = 0,
.platform = platform,
.isolate = isolate,
.templates = undefined,
.allocator = allocator,
.isolate_params = params,
.meta_lookup = undefined,
.prototype_lookup = undefined,
};

// Populate our templates lookup. generateClass creates the
// v8.FunctionTemplate, which we store in our env.templates.
// The ordering doesn't matter. What matters is that, given a type
Expand Down Expand Up @@ -537,3 +547,21 @@ fn generateUndetectable(comptime Struct: type, template: v8.ObjectTemplate) void
template.markAsUndetectable();
}
}

fn generateIsolateSnaphot(params: *v8.CreateParams) v8.StartupData {
var snapshot_creator = v8.SnapshotCreator{};
snapshot_creator.init(params);
defer snapshot_creator.deinit();

const isolate = snapshot_creator.getIsolate();

var temp_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&temp_scope, isolate);
defer temp_scope.deinit();

const context = v8.Context.init(isolate, null, null);
context.enter();
defer context.exit();

return snapshot_creator.createSnapshotDataBlob("function LightpandaSnapshotPoC() { return 73; }");
}
2 changes: 2 additions & 0 deletions src/tests/polyfill/webcomponents.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
}
}

testing.expectEqual(73, LightpandaSnapshotPoC());

window.customElements.define("lightpanda-test", LightPanda);
const main = document.getElementById('main');
main.appendChild(document.createElement('lightpanda-test'));
Expand Down
Loading