From b479fef9183ec5bbf757fa29dc909f421997a7f4 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 29 Nov 2024 17:34:47 +0100 Subject: [PATCH] expose UnboundScript funcs --- src/binding.cpp | 23 +++++++++++++++++++++++ src/binding.h | 24 +++++++++++++++++++----- src/v8.zig | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/binding.cpp b/src/binding.cpp index c050bef..bdfd85d 100644 --- a/src/binding.cpp +++ b/src/binding.cpp @@ -458,6 +458,11 @@ void v8__ScriptCompiler__CachedData__DELETE(v8::ScriptCompiler::CachedData* self delete self; } +// UnboundScript +v8::Script* v8__UnboundScript__BindToCurrentContext(const v8::UnboundScript* unboundedScript) { + return local_to_ptr(ptr_to_local(unboundedScript)->BindToCurrentContext()); +} + const v8::Module* v8__ScriptCompiler__CompileModule( v8::Isolate* isolate, v8::ScriptCompiler::Source* source, @@ -467,6 +472,24 @@ const v8::Module* v8__ScriptCompiler__CompileModule( return maybe_local_to_ptr(maybe_local); } +const v8::Script* v8__ScriptCompiler__Compile ( + const v8::Context& context, + v8::ScriptCompiler::Source* source, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason reason) { + v8::MaybeLocal maybe_local = v8::ScriptCompiler::Compile(ptr_to_local(&context), source, options, reason); + return maybe_local_to_ptr(maybe_local); +} + +const v8::UnboundScript* v8__ScriptCompiler__CompileUnboundScript ( + v8::Isolate* isolate, + v8::ScriptCompiler::Source* source, + v8::ScriptCompiler::CompileOptions options, + v8::ScriptCompiler::NoCacheReason reason) { + v8::MaybeLocal maybe_local = v8::ScriptCompiler::CompileUnboundScript(isolate, source, options, reason); + return maybe_local_to_ptr(maybe_local); +} + // Module v8::Module::Status v8__Module__GetStatus(const v8::Module& self) { diff --git a/src/binding.h b/src/binding.h index 4b14047..926b83b 100644 --- a/src/binding.h +++ b/src/binding.h @@ -16,6 +16,8 @@ typedef struct FunctionTemplate FunctionTemplate; typedef struct Message Message; typedef struct Name Name; typedef struct Context Context; +typedef struct UnboundScript UnboundScript; +typedef struct Script Script; // Internally, all Value types have a base InternalAddress struct. typedef uintptr_t InternalAddress; // Super type. @@ -800,6 +802,13 @@ void v8__ScriptOrigin__CONSTRUCT2( const Data* host_defined_options ); +// Script +Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin); +Value* v8__Script__Run(const Script* script, const Context* context); + +// UnboundScript +Script* v8__UnboundScript__BindToCurrentContext(const UnboundScript* unboundedScript); + // ScriptCompiler typedef struct ScriptCompilerSource { String* source_string; @@ -849,11 +858,16 @@ const Module* v8__ScriptCompiler__CompileModule( ScriptCompilerSource* source, CompileOptions options, NoCacheReason reason); - -// Script -typedef struct Script Script; -Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin); -Value* v8__Script__Run(const Script* script, const Context* context); +const Script* v8__ScriptCompiler__Compile( + const Context* context, + ScriptCompilerSource* source, + CompileOptions options, + NoCacheReason reason); +const UnboundScript* v8__ScriptCompiler__CompileUnboundScript( + Isolate* isolate, + ScriptCompilerSource* source, + CompileOptions options, + NoCacheReason reason); // Module typedef enum ModuleStatus { diff --git a/src/v8.zig b/src/v8.zig index cd15445..565ffde 100644 --- a/src/v8.zig +++ b/src/v8.zig @@ -1691,6 +1691,24 @@ pub const ScriptCompiler = struct { }; } else return error.JsException; } + + /// [v8] + /// Compiles the specified script (context-independent). Cached data as + /// part of the source object can be optionally produced to be consumed + /// later to speed up compilation of identical source scripts. + pub fn CompileUnboundScript(iso: Isolate, src: *ScriptCompilerSource, options: ScriptCompiler.CompileOptions, reason: ScriptCompiler.NoCacheReason) !UnboundScript { + const mb_res = c.v8__ScriptCompiler__CompileUnboundScript( + iso.handle, + &src.inner, + @intFromEnum(options), + @intFromEnum(reason), + ); + if (mb_res) |res| { + return UnboundScript{ + .handle = res, + }; + } else return error.JsException; + } }; pub const Script = struct { @@ -1717,6 +1735,20 @@ pub const Script = struct { } }; +pub const UnboundScript = struct { + const Self = @This(); + + handle: *const c.UnboundScript, + + pub fn bindToCurrentContext(self: Self) !Script { + if (c.v8__UnboundScript__BindToCurrentContext(self.handle)) |script| { + return Script{ + .handle = script, + }; + } else return error.JsException; + } +}; + pub const Module = struct { const Self = @This();