From 22856a39a4ef4f2b6be422ab0651acee71907676 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 29 Nov 2024 17:36:07 +0100 Subject: [PATCH] give a way to compile scripts --- src/engines/v8/v8.zig | 31 +++++++++++++++++++++++++++++++ vendor/zig-v8 | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index 2901d3a..18438af 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -302,6 +302,33 @@ pub const Env = struct { } } + // compile a JS script + pub fn compile( + self: Env, + script: []const u8, + name: []const u8, + ) anyerror!JSScript { + + // compile + const scr_name = v8.String.initUtf8(self.isolate, name); + const script_source = v8.String.initUtf8(self.isolate, script); + + const origin = v8.ScriptOrigin.initDefault(self.isolate, scr_name.toValue()); + + var script_comp_source: v8.ScriptCompilerSource = undefined; + script_comp_source.init(script_source, origin, null); + defer script_comp_source.deinit(); + + const value = v8.ScriptCompiler.CompileUnboundScript( + self.isolate, + &script_comp_source, + .kNoCompileOptions, + .kNoCacheNoReason, + ) catch return error.JSCompile; + + return .{ .value = value }; + } + // compile and run a JS script // It doesn't wait for callbacks execution pub fn exec( @@ -431,6 +458,10 @@ pub const JSObject = struct { } }; +pub const JSScript = struct { + value: v8.UnboundScript, +}; + pub const JSValue = struct { value: v8.Value, diff --git a/vendor/zig-v8 b/vendor/zig-v8 index 821da4f..4fc2dc5 160000 --- a/vendor/zig-v8 +++ b/vendor/zig-v8 @@ -1 +1 @@ -Subproject commit 821da4f11073dfdc17e296f2113208844b20a8fa +Subproject commit 4fc2dc51eecbc5ec4006f3be1f7caac0671f1d91