Skip to content

Commit

Permalink
expose JSScript as unbound script
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Dec 2, 2024
1 parent 22856a3 commit 3620e2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 16 additions & 3 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3620e2b

Please sign in to comment.