diff --git a/src/binding.cpp b/src/binding.cpp index f03e134..2338fa5 100644 --- a/src/binding.cpp +++ b/src/binding.cpp @@ -590,6 +590,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, @@ -602,6 +607,25 @@ const v8::Module* v8__ScriptCompiler__CompileModule( size_t v8__ScriptCompiler__CompilationDetails__SIZEOF() { return sizeof(v8::ScriptCompiler::CompilationDetails); } + +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 af86650..e3efd14 100644 --- a/src/binding.h +++ b/src/binding.h @@ -15,11 +15,13 @@ typedef struct Module Module; typedef struct FunctionTemplate FunctionTemplate; typedef struct Message Message; 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. typedef Data Value; -typedef Value Object; +typedef Value Object;a typedef Value String; typedef Value Function; typedef Value Number; @@ -978,6 +980,13 @@ typedef struct CompilationDetails { typedef bool (*CompileHintCallback)(int, void*); +// 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; @@ -1032,11 +1041,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 91e17d7..a5669cb 100644 --- a/src/v8.zig +++ b/src/v8.zig @@ -1855,6 +1855,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 { @@ -1881,6 +1899,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();