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 {