From 22856a39a4ef4f2b6be422ab0651acee71907676 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 29 Nov 2024 17:36:07 +0100 Subject: [PATCH 1/2] 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 From 3620e2b1f1ea7bff2ee806e057a1b74c11c2a549 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 2 Dec 2024 14:57:20 +0100 Subject: [PATCH 2/2] expose JSScript as unbound script --- src/api.zig | 1 + src/engines/v8/v8.zig | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/api.zig b/src/api.zig index efc42a5..4179f97 100644 --- a/src/api.zig +++ b/src/api.zig @@ -73,6 +73,7 @@ const Engine = @import("private_api.zig").Engine; pub const JSValue = Engine.JSValue; pub const JSObject = Engine.JSObject; pub const JSObjectID = Engine.JSObjectID; +pub const JSScript = Engine.JSScript; pub const Callback = Engine.Callback; pub const CallbackSync = Engine.CallbackSync; diff --git a/src/engines/v8/v8.zig b/src/engines/v8/v8.zig index 18438af..847d9b1 100644 --- a/src/engines/v8/v8.zig +++ b/src/engines/v8/v8.zig @@ -319,14 +319,14 @@ pub const Env = struct { script_comp_source.init(script_source, origin, null); defer script_comp_source.deinit(); - const value = v8.ScriptCompiler.CompileUnboundScript( + const uboundedscript = v8.ScriptCompiler.CompileUnboundScript( self.isolate, &script_comp_source, .kNoCompileOptions, .kNoCacheNoReason, ) catch return error.JSCompile; - return .{ .value = value }; + return .{ .inner = uboundedscript }; } // compile and run a JS script @@ -459,7 +459,20 @@ pub const JSObject = struct { }; pub const JSScript = struct { - value: v8.UnboundScript, + inner: v8.UnboundScript, + + // Bind the unbounded script to the current context and run it. + pub fn run(self: JSScript, env: Env) anyerror!JSValue { + if (env.js_ctx == null) { + return error.EnvNotStarted; + } + + const scr = self.inner.bindToCurrentContext() catch return error.JSExec; + + // run + const value = scr.run(env.js_ctx.?) catch return error.JSExec; + return .{ .value = value }; + } }; pub const JSValue = struct {