Skip to content

Commit b479fef

Browse files
committed
expose UnboundScript funcs
1 parent 821da4f commit b479fef

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/binding.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ void v8__ScriptCompiler__CachedData__DELETE(v8::ScriptCompiler::CachedData* self
458458
delete self;
459459
}
460460

461+
// UnboundScript
462+
v8::Script* v8__UnboundScript__BindToCurrentContext(const v8::UnboundScript* unboundedScript) {
463+
return local_to_ptr(ptr_to_local(unboundedScript)->BindToCurrentContext());
464+
}
465+
461466
const v8::Module* v8__ScriptCompiler__CompileModule(
462467
v8::Isolate* isolate,
463468
v8::ScriptCompiler::Source* source,
@@ -467,6 +472,24 @@ const v8::Module* v8__ScriptCompiler__CompileModule(
467472
return maybe_local_to_ptr(maybe_local);
468473
}
469474

475+
const v8::Script* v8__ScriptCompiler__Compile (
476+
const v8::Context& context,
477+
v8::ScriptCompiler::Source* source,
478+
v8::ScriptCompiler::CompileOptions options,
479+
v8::ScriptCompiler::NoCacheReason reason) {
480+
v8::MaybeLocal<v8::Script> maybe_local = v8::ScriptCompiler::Compile(ptr_to_local(&context), source, options, reason);
481+
return maybe_local_to_ptr(maybe_local);
482+
}
483+
484+
const v8::UnboundScript* v8__ScriptCompiler__CompileUnboundScript (
485+
v8::Isolate* isolate,
486+
v8::ScriptCompiler::Source* source,
487+
v8::ScriptCompiler::CompileOptions options,
488+
v8::ScriptCompiler::NoCacheReason reason) {
489+
v8::MaybeLocal<v8::UnboundScript> maybe_local = v8::ScriptCompiler::CompileUnboundScript(isolate, source, options, reason);
490+
return maybe_local_to_ptr(maybe_local);
491+
}
492+
470493
// Module
471494

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

src/binding.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ typedef struct FunctionTemplate FunctionTemplate;
1616
typedef struct Message Message;
1717
typedef struct Name Name;
1818
typedef struct Context Context;
19+
typedef struct UnboundScript UnboundScript;
20+
typedef struct Script Script;
1921
// Internally, all Value types have a base InternalAddress struct.
2022
typedef uintptr_t InternalAddress;
2123
// Super type.
@@ -800,6 +802,13 @@ void v8__ScriptOrigin__CONSTRUCT2(
800802
const Data* host_defined_options
801803
);
802804

805+
// Script
806+
Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin);
807+
Value* v8__Script__Run(const Script* script, const Context* context);
808+
809+
// UnboundScript
810+
Script* v8__UnboundScript__BindToCurrentContext(const UnboundScript* unboundedScript);
811+
803812
// ScriptCompiler
804813
typedef struct ScriptCompilerSource {
805814
String* source_string;
@@ -849,11 +858,16 @@ const Module* v8__ScriptCompiler__CompileModule(
849858
ScriptCompilerSource* source,
850859
CompileOptions options,
851860
NoCacheReason reason);
852-
853-
// Script
854-
typedef struct Script Script;
855-
Script* v8__Script__Compile(const Context* context, const String* src, const ScriptOrigin* origin);
856-
Value* v8__Script__Run(const Script* script, const Context* context);
861+
const Script* v8__ScriptCompiler__Compile(
862+
const Context* context,
863+
ScriptCompilerSource* source,
864+
CompileOptions options,
865+
NoCacheReason reason);
866+
const UnboundScript* v8__ScriptCompiler__CompileUnboundScript(
867+
Isolate* isolate,
868+
ScriptCompilerSource* source,
869+
CompileOptions options,
870+
NoCacheReason reason);
857871

858872
// Module
859873
typedef enum ModuleStatus {

src/v8.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,24 @@ pub const ScriptCompiler = struct {
16911691
};
16921692
} else return error.JsException;
16931693
}
1694+
1695+
/// [v8]
1696+
/// Compiles the specified script (context-independent). Cached data as
1697+
/// part of the source object can be optionally produced to be consumed
1698+
/// later to speed up compilation of identical source scripts.
1699+
pub fn CompileUnboundScript(iso: Isolate, src: *ScriptCompilerSource, options: ScriptCompiler.CompileOptions, reason: ScriptCompiler.NoCacheReason) !UnboundScript {
1700+
const mb_res = c.v8__ScriptCompiler__CompileUnboundScript(
1701+
iso.handle,
1702+
&src.inner,
1703+
@intFromEnum(options),
1704+
@intFromEnum(reason),
1705+
);
1706+
if (mb_res) |res| {
1707+
return UnboundScript{
1708+
.handle = res,
1709+
};
1710+
} else return error.JsException;
1711+
}
16941712
};
16951713

16961714
pub const Script = struct {
@@ -1717,6 +1735,20 @@ pub const Script = struct {
17171735
}
17181736
};
17191737

1738+
pub const UnboundScript = struct {
1739+
const Self = @This();
1740+
1741+
handle: *const c.UnboundScript,
1742+
1743+
pub fn bindToCurrentContext(self: Self) !Script {
1744+
if (c.v8__UnboundScript__BindToCurrentContext(self.handle)) |script| {
1745+
return Script{
1746+
.handle = script,
1747+
};
1748+
} else return error.JsException;
1749+
}
1750+
};
1751+
17201752
pub const Module = struct {
17211753
const Self = @This();
17221754

0 commit comments

Comments
 (0)