Skip to content
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
24 changes: 24 additions & 0 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<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
26 changes: 20 additions & 6 deletions src/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions src/v8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

Expand Down