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;
|
2019-12-18 05:46:36 -05:00
|
|
|
use crate::Integer;
|
2019-12-04 08:12:27 -05:00
|
|
|
use crate::Local;
|
2020-03-05 20:41:43 -05:00
|
|
|
use crate::Script;
|
2019-12-04 08:12:27 -05:00
|
|
|
use crate::String;
|
2019-12-24 18:31:36 -05:00
|
|
|
use crate::ToLocal;
|
2019-12-04 08:12:27 -05:00
|
|
|
use crate::Value;
|
|
|
|
|
2019-12-18 05:46:36 -05:00
|
|
|
/// The origin, within a file, of a script.
|
|
|
|
#[repr(C)]
|
2019-12-20 10:01:45 -05:00
|
|
|
pub struct ScriptOrigin<'sc>([usize; 7], PhantomData<&'sc ()>);
|
2019-12-18 05:46:36 -05:00
|
|
|
|
2019-12-04 08:12:27 -05:00
|
|
|
extern "C" {
|
|
|
|
fn v8__Script__Compile(
|
|
|
|
context: *mut Context,
|
|
|
|
source: *mut String,
|
|
|
|
origin: *const ScriptOrigin,
|
|
|
|
) -> *mut Script;
|
|
|
|
fn v8__Script__Run(this: &mut Script, context: *mut Context) -> *mut Value;
|
2019-12-18 05:46:36 -05:00
|
|
|
|
|
|
|
fn v8__ScriptOrigin__CONSTRUCT(
|
|
|
|
buf: &mut MaybeUninit<ScriptOrigin>,
|
|
|
|
resource_name: *mut Value,
|
|
|
|
resource_line_offset: *mut Integer,
|
|
|
|
resource_column_offset: *mut Integer,
|
|
|
|
resource_is_shared_cross_origin: *mut Boolean,
|
|
|
|
script_id: *mut Integer,
|
|
|
|
source_map_url: *mut Value,
|
|
|
|
resource_is_opaque: *mut Boolean,
|
|
|
|
is_wasm: *mut Boolean,
|
|
|
|
is_module: *mut Boolean,
|
|
|
|
);
|
2019-12-04 08:12:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Script {
|
2019-12-19 08:14:19 -05:00
|
|
|
/// A shorthand for ScriptCompiler::Compile().
|
2019-12-20 10:01:45 -05:00
|
|
|
pub fn compile<'sc>(
|
2020-02-13 00:23:19 -05:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-19 23:36:29 -05:00
|
|
|
mut context: Local<Context>,
|
|
|
|
mut source: Local<String>,
|
2019-12-20 10:01:45 -05:00
|
|
|
origin: Option<&ScriptOrigin>,
|
|
|
|
) -> Option<Local<'sc, Script>> {
|
2019-12-04 08:12:27 -05:00
|
|
|
// TODO: use the type system to enforce that a Context has been entered.
|
|
|
|
// TODO: `context` and `source` probably shouldn't be mut.
|
2019-12-24 18:31:36 -05:00
|
|
|
let ptr = unsafe {
|
|
|
|
v8__Script__Compile(
|
2019-12-04 08:12:27 -05:00
|
|
|
&mut *context,
|
|
|
|
&mut *source,
|
|
|
|
origin.map(|r| r as *const _).unwrap_or(null()),
|
2019-12-24 18:31:36 -05:00
|
|
|
)
|
|
|
|
};
|
|
|
|
unsafe { scope.to_local(ptr) }
|
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()).
|
2019-12-20 10:01:45 -05:00
|
|
|
pub fn run<'sc>(
|
|
|
|
&mut self,
|
2019-12-24 18:31:36 -05:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-20 10:01:45 -05:00
|
|
|
mut context: Local<Context>,
|
|
|
|
) -> Option<Local<'sc, Value>> {
|
2019-12-24 18:31:36 -05:00
|
|
|
unsafe { scope.to_local(v8__Script__Run(self, &mut *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.
|
2019-12-20 10:01:45 -05:00
|
|
|
impl<'sc> ScriptOrigin<'sc> {
|
2019-12-19 23:36:29 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-12-18 05:46:36 -05:00
|
|
|
pub fn new(
|
2019-12-20 10:01:45 -05:00
|
|
|
mut resource_name: Local<'sc, Value>,
|
|
|
|
mut resource_line_offset: Local<'sc, Integer>,
|
|
|
|
mut resource_column_offset: Local<'sc, Integer>,
|
|
|
|
mut resource_is_shared_cross_origin: Local<'sc, Boolean>,
|
|
|
|
mut script_id: Local<'sc, Integer>,
|
|
|
|
mut source_map_url: Local<'sc, Value>,
|
|
|
|
mut resource_is_opaque: Local<'sc, Boolean>,
|
|
|
|
mut is_wasm: Local<'sc, Boolean>,
|
|
|
|
mut is_module: Local<'sc, Boolean>,
|
2019-12-18 05:46:36 -05:00
|
|
|
) -> Self {
|
|
|
|
unsafe {
|
|
|
|
let mut buf = std::mem::MaybeUninit::<ScriptOrigin>::uninit();
|
|
|
|
v8__ScriptOrigin__CONSTRUCT(
|
|
|
|
&mut buf,
|
|
|
|
&mut *resource_name,
|
|
|
|
&mut *resource_line_offset,
|
|
|
|
&mut *resource_column_offset,
|
|
|
|
&mut *resource_is_shared_cross_origin,
|
|
|
|
&mut *script_id,
|
|
|
|
&mut *source_map_url,
|
|
|
|
&mut *resource_is_opaque,
|
|
|
|
&mut *is_wasm,
|
|
|
|
&mut *is_module,
|
|
|
|
);
|
|
|
|
buf.assume_init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|