Skip to content

expose UnboundScript funcs #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: fork
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<v8::Script> 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<v8::UnboundScript> 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) {
Expand Down
24 changes: 19 additions & 5 deletions src/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions src/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

Expand Down