Skip to content

Commit a12f7d9

Browse files
committed
expose UnboundScript funcs
1 parent dd08777 commit a12f7d9

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

src/binding.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ void v8__ScriptCompiler__CachedData__DELETE(v8::ScriptCompiler::CachedData* self
590590
delete self;
591591
}
592592

593+
// UnboundScript
594+
v8::Script* v8__UnboundScript__BindToCurrentContext(const v8::UnboundScript* unboundedScript) {
595+
return local_to_ptr(ptr_to_local(unboundedScript)->BindToCurrentContext());
596+
}
597+
593598
const v8::Module* v8__ScriptCompiler__CompileModule(
594599
v8::Isolate* isolate,
595600
v8::ScriptCompiler::Source* source,
@@ -602,6 +607,25 @@ const v8::Module* v8__ScriptCompiler__CompileModule(
602607
size_t v8__ScriptCompiler__CompilationDetails__SIZEOF() {
603608
return sizeof(v8::ScriptCompiler::CompilationDetails);
604609
}
610+
611+
const v8::Script* v8__ScriptCompiler__Compile (
612+
const v8::Context& context,
613+
v8::ScriptCompiler::Source* source,
614+
v8::ScriptCompiler::CompileOptions options,
615+
v8::ScriptCompiler::NoCacheReason reason) {
616+
v8::MaybeLocal<v8::Script> maybe_local = v8::ScriptCompiler::Compile(ptr_to_local(&context), source, options, reason);
617+
return maybe_local_to_ptr(maybe_local);
618+
}
619+
620+
const v8::UnboundScript* v8__ScriptCompiler__CompileUnboundScript (
621+
v8::Isolate* isolate,
622+
v8::ScriptCompiler::Source* source,
623+
v8::ScriptCompiler::CompileOptions options,
624+
v8::ScriptCompiler::NoCacheReason reason) {
625+
v8::MaybeLocal<v8::UnboundScript> maybe_local = v8::ScriptCompiler::CompileUnboundScript(isolate, source, options, reason);
626+
return maybe_local_to_ptr(maybe_local);
627+
}
628+
605629
// Module
606630

607631
v8::Module::Status v8__Module__GetStatus(const v8::Module& self) {

src/binding.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ typedef struct Module Module;
1515
typedef struct FunctionTemplate FunctionTemplate;
1616
typedef struct Message Message;
1717
typedef struct Context Context;
18+
typedef struct UnboundScript UnboundScript;
19+
typedef struct Script Script;
1820
// Internally, all Value types have a base InternalAddress struct.
1921
typedef uintptr_t InternalAddress;
2022
// Super type.
2123
typedef Data Value;
22-
typedef Value Object;
24+
typedef Value Object;a
2325
typedef Value String;
2426
typedef Value Function;
2527
typedef Value Number;
@@ -978,6 +980,13 @@ typedef struct CompilationDetails {
978980

979981
typedef bool (*CompileHintCallback)(int, void*);
980982

983+
// Script
984+
Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin);
985+
Value* v8__Script__Run(const Script* script, const Context* context);
986+
987+
// UnboundScript
988+
Script* v8__UnboundScript__BindToCurrentContext(const UnboundScript* unboundedScript);
989+
981990
// ScriptCompiler
982991
typedef struct ScriptCompilerSource {
983992
String* source_string;
@@ -1032,11 +1041,16 @@ const Module* v8__ScriptCompiler__CompileModule(
10321041
ScriptCompilerSource* source,
10331042
CompileOptions options,
10341043
NoCacheReason reason);
1035-
1036-
// Script
1037-
typedef struct Script Script;
1038-
Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin);
1039-
Value* v8__Script__Run(const Script* script, const Context* context);
1044+
const Script* v8__ScriptCompiler__Compile(
1045+
const Context* context,
1046+
ScriptCompilerSource* source,
1047+
CompileOptions options,
1048+
NoCacheReason reason);
1049+
const UnboundScript* v8__ScriptCompiler__CompileUnboundScript(
1050+
Isolate* isolate,
1051+
ScriptCompilerSource* source,
1052+
CompileOptions options,
1053+
NoCacheReason reason);
10401054

10411055
// Module
10421056
typedef enum ModuleStatus {

src/v8.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,24 @@ pub const ScriptCompiler = struct {
18551855
};
18561856
} else return error.JsException;
18571857
}
1858+
1859+
/// [v8]
1860+
/// Compiles the specified script (context-independent). Cached data as
1861+
/// part of the source object can be optionally produced to be consumed
1862+
/// later to speed up compilation of identical source scripts.
1863+
pub fn CompileUnboundScript(iso: Isolate, src: *ScriptCompilerSource, options: ScriptCompiler.CompileOptions, reason: ScriptCompiler.NoCacheReason) !UnboundScript {
1864+
const mb_res = c.v8__ScriptCompiler__CompileUnboundScript(
1865+
iso.handle,
1866+
&src.inner,
1867+
@intFromEnum(options),
1868+
@intFromEnum(reason),
1869+
);
1870+
if (mb_res) |res| {
1871+
return UnboundScript{
1872+
.handle = res,
1873+
};
1874+
} else return error.JsException;
1875+
}
18581876
};
18591877

18601878
pub const Script = struct {
@@ -1881,6 +1899,20 @@ pub const Script = struct {
18811899
}
18821900
};
18831901

1902+
pub const UnboundScript = struct {
1903+
const Self = @This();
1904+
1905+
handle: *const c.UnboundScript,
1906+
1907+
pub fn bindToCurrentContext(self: Self) !Script {
1908+
if (c.v8__UnboundScript__BindToCurrentContext(self.handle)) |script| {
1909+
return Script{
1910+
.handle = script,
1911+
};
1912+
} else return error.JsException;
1913+
}
1914+
};
1915+
18841916
pub const Module = struct {
18851917
const Self = @This();
18861918

0 commit comments

Comments
 (0)