adapt stringToPersistedFunction to compileFunction

This is just a thin wrapper around it now.
This commit is contained in:
Halil Durak
2026-03-10 19:15:53 +03:00
parent e610506df4
commit 9d2ba52160
2 changed files with 26 additions and 25 deletions

View File

@@ -336,28 +336,18 @@ pub fn toLocal(self: *Context, global: anytype) js.Local.ToLocalReturnType(@Type
return l.toLocal(global); return l.toLocal(global);
} }
// This isn't expected to be called often. It's for converting attributes into pub fn stringToPersistedFunction(
// function calls, e.g. <body onload="doSomething"> will turn that "doSomething" self: *Context,
// string into a js.Function which looks like: function(e) { doSomething(e) } function_body: []const u8,
// There might be more efficient ways to do this, but doing it this way means comptime parameter_names: []const []const u8,
// our code only has to worry about js.Funtion, not some union of a js.Function extensions: []const v8.Object,
// or a string. ) !js.Function.Global {
pub fn stringToPersistedFunction(self: *Context, str: []const u8) !js.Function.Global {
var ls: js.Local.Scope = undefined; var ls: js.Local.Scope = undefined;
self.localScope(&ls); self.localScope(&ls);
defer ls.deinit(); defer ls.deinit();
var extra: []const u8 = ""; const js_function = try ls.local.compileFunction(function_body, parameter_names, extensions);
const normalized = std.mem.trim(u8, str, &std.ascii.whitespace); return js_function.persist();
if (normalized.len > 0 and normalized[normalized.len - 1] != ')') {
extra = "(e)";
}
const full = try std.fmt.allocPrintSentinel(self.call_arena, "(function(e) {{ {s}{s} }})", .{ normalized, extra }, 0);
const js_val = try ls.local.compileAndRun(full, null);
if (!js_val.isFunction()) {
return error.StringFunctionError;
}
return try (js.Function{ .local = &ls.local, .handle = @ptrCast(js_val.handle) }).persist();
} }
pub fn module(self: *Context, comptime want_result: bool, local: *const js.Local, src: []const u8, url: []const u8, cacheable: bool) !(if (want_result) ModuleEntry else void) { pub fn module(self: *Context, comptime want_result: bool, local: *const js.Local, src: []const u8, url: []const u8, cacheable: bool) !(if (want_result) ModuleEntry else void) {

View File

@@ -118,16 +118,27 @@ pub fn exec(self: *const Local, src: []const u8, name: ?[]const u8) !js.Value {
/// Compiles a function body as function. /// Compiles a function body as function.
/// ///
/// https://v8.github.io/api/head/classv8_1_1ScriptCompiler.html#a3a15bb5a7dfc3f998e6ac789e6b4646a /// https://v8.github.io/api/head/classv8_1_1ScriptCompiler.html#a3a15bb5a7dfc3f998e6ac789e6b4646a
pub fn compileFunction(self: *const Local, function_body: []const u8) !js.Function { pub fn compileFunction(
self: *const Local,
function_body: []const u8,
/// We tend to know how many params we'll pass; can remove the comptime if necessary.
comptime parameter_names: []const []const u8,
extensions: []const v8.Object,
) !js.Function {
// TODO: Make configurable. // TODO: Make configurable.
const script_name = self.isolate.initStringHandle("anonymous"); const script_name = self.isolate.initStringHandle("anonymous");
const script_source = self.isolate.initStringHandle(function_body); const script_source = self.isolate.initStringHandle(function_body);
// Create ScriptOrigin. var parameter_list: [parameter_names.len]*const v8.String = undefined;
inline for (0..parameter_names.len) |i| {
parameter_list[i] = self.isolate.initStringHandle(parameter_names[i]);
}
// Create `ScriptOrigin`.
var origin: v8.ScriptOrigin = undefined; var origin: v8.ScriptOrigin = undefined;
v8.v8__ScriptOrigin__CONSTRUCT(&origin, script_name); v8.v8__ScriptOrigin__CONSTRUCT(&origin, script_name);
// Create ScriptCompilerSource. // Create `ScriptCompilerSource`.
var script_compiler_source: v8.ScriptCompilerSource = undefined; var script_compiler_source: v8.ScriptCompilerSource = undefined;
v8.v8__ScriptCompiler__Source__CONSTRUCT2(script_source, &origin, null, &script_compiler_source); v8.v8__ScriptCompiler__Source__CONSTRUCT2(script_source, &origin, null, &script_compiler_source);
defer v8.v8__ScriptCompiler__Source__DESTRUCT(&script_compiler_source); defer v8.v8__ScriptCompiler__Source__DESTRUCT(&script_compiler_source);
@@ -136,10 +147,10 @@ pub fn compileFunction(self: *const Local, function_body: []const u8) !js.Functi
const result = v8.v8__ScriptCompiler__CompileFunction( const result = v8.v8__ScriptCompiler__CompileFunction(
self.handle, self.handle,
&script_compiler_source, &script_compiler_source,
0, parameter_list.len,
null, &parameter_list,
0, extensions.len,
null, @ptrCast(&extensions),
v8.kNoCompileOptions, v8.kNoCompileOptions,
v8.kNoCacheNoReason, v8.kNoCacheNoReason,
) orelse return error.CompilationError; ) orelse return error.CompilationError;