0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-14 10:02:09 -05:00
denoland-rusty-v8/src/script.rs

106 lines
2.7 KiB
Rust
Raw Normal View History

2019-12-20 10:01:45 -05:00
use std::marker::PhantomData;
2019-12-18 05:46:36 -05:00
use std::mem::MaybeUninit;
2019-12-04 08:12:27 -05:00
use std::ptr::null;
2019-12-18 05:46:36 -05:00
use crate::Boolean;
2019-12-04 08:12:27 -05:00
use crate::Context;
use crate::HandleScope;
2019-12-18 05:46:36 -05:00
use crate::Integer;
2019-12-04 08:12:27 -05:00
use crate::Local;
use crate::Script;
2019-12-04 08:12:27 -05:00
use crate::String;
use crate::Value;
2019-12-18 05:46:36 -05:00
/// The origin, within a file, of a script.
#[repr(C)]
pub struct ScriptOrigin<'s>([usize; 7], PhantomData<&'s ()>);
2019-12-18 05:46:36 -05:00
2019-12-04 08:12:27 -05:00
extern "C" {
fn v8__Script__Compile(
context: *const Context,
source: *const String,
2019-12-04 08:12:27 -05:00
origin: *const ScriptOrigin,
) -> *const Script;
fn v8__Script__Run(
script: *const Script,
context: *const Context,
) -> *const Value;
2019-12-18 05:46:36 -05:00
fn v8__ScriptOrigin__CONSTRUCT(
buf: *mut MaybeUninit<ScriptOrigin>,
resource_name: *const Value,
resource_line_offset: *const Integer,
resource_column_offset: *const Integer,
resource_is_shared_cross_origin: *const Boolean,
script_id: *const Integer,
source_map_url: *const Value,
resource_is_opaque: *const Boolean,
is_wasm: *const Boolean,
is_module: *const Boolean,
2019-12-18 05:46:36 -05:00
);
2019-12-04 08:12:27 -05:00
}
impl Script {
2019-12-19 08:14:19 -05:00
/// A shorthand for ScriptCompiler::Compile().
pub fn compile<'s>(
scope: &mut HandleScope<'s>,
context: Local<Context>,
source: Local<String>,
2019-12-20 10:01:45 -05:00
origin: Option<&ScriptOrigin>,
) -> Option<Local<'s, Script>> {
unsafe {
scope.cast_local(|_| {
v8__Script__Compile(
&*context,
&*source,
origin.map(|r| r as *const _).unwrap_or_else(null),
)
})
}
2019-12-04 08:12:27 -05:00
}
2019-12-19 08:14:19 -05:00
/// Runs the script returning the resulting value. It will be run in the
/// context in which it was created (ScriptCompiler::CompileBound or
/// UnboundScript::BindToCurrentContext()).
pub fn run<'s>(
2019-12-20 10:01:45 -05:00
&mut self,
scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Value>> {
unsafe { scope.cast_local(|_| v8__Script__Run(self, &*context)) }
2019-12-04 08:12:27 -05:00
}
}
2019-12-18 05:46:36 -05:00
2019-12-19 08:14:19 -05:00
/// The origin, within a file, of a script.
impl<'s> ScriptOrigin<'s> {
#[allow(clippy::too_many_arguments)]
2019-12-18 05:46:36 -05:00
pub fn new(
resource_name: Local<'s, Value>,
resource_line_offset: Local<'s, Integer>,
resource_column_offset: Local<'s, Integer>,
resource_is_shared_cross_origin: Local<'s, Boolean>,
script_id: Local<'s, Integer>,
source_map_url: Local<'s, Value>,
resource_is_opaque: Local<'s, Boolean>,
is_wasm: Local<'s, Boolean>,
is_module: Local<'s, Boolean>,
2019-12-18 05:46:36 -05:00
) -> Self {
unsafe {
let mut buf = std::mem::MaybeUninit::<ScriptOrigin>::uninit();
v8__ScriptOrigin__CONSTRUCT(
&mut buf,
&*resource_name,
&*resource_line_offset,
&*resource_column_offset,
&*resource_is_shared_cross_origin,
&*script_id,
&*source_map_url,
&*resource_is_opaque,
&*is_wasm,
&*is_module,
2019-12-18 05:46:36 -05:00
);
buf.assume_init()
}
}
}