Skip to content

Commit

Permalink
give a way to compile scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Nov 29, 2024
1 parent a244e75 commit 22856a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/engines/v8/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -431,6 +458,10 @@ pub const JSObject = struct {
}
};

pub const JSScript = struct {
value: v8.UnboundScript,
};

pub const JSValue = struct {
value: v8.Value,

Expand Down
2 changes: 1 addition & 1 deletion vendor/zig-v8

0 comments on commit 22856a3

Please sign in to comment.