0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-12 02:27:50 -05:00
denoland-rusty-v8/src/script.rs

101 lines
2.8 KiB
Rust
Raw Normal View History

2019-12-18 05:46:36 -05:00
use std::mem::MaybeUninit;
2019-12-04 08:12:27 -05:00
use std::ptr::null;
use crate::support::Opaque;
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;
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([usize; 7]);
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
}
2019-12-19 08:14:19 -05:00
/// A compiled JavaScript script, tied to a Context which was active when the
/// script was compiled.
2019-12-04 08:12:27 -05:00
#[repr(C)]
pub struct Script(Opaque);
impl Script {
2019-12-19 08:14:19 -05:00
/// A shorthand for ScriptCompiler::Compile().
pub fn compile(
mut context: Local<Context>,
mut source: Local<String>,
2019-12-04 08:12:27 -05:00
origin: Option<&'_ ScriptOrigin>,
) -> Option<Local<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.
unsafe {
Local::from_raw(v8__Script__Compile(
&mut *context,
&mut *source,
origin.map(|r| r as *const _).unwrap_or(null()),
))
}
}
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(&mut self, mut context: Local<Context>) -> Option<Local<Value>> {
2019-12-04 08:12:27 -05:00
unsafe { Local::from_raw(v8__Script__Run(self, &mut *context)) }
}
}
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-18 05:46:36 -05:00
impl ScriptOrigin {
#[allow(clippy::too_many_arguments)]
2019-12-18 05:46:36 -05:00
pub fn new(
mut resource_name: Local<Value>,
mut resource_line_offset: Local<Integer>,
mut resource_column_offset: Local<Integer>,
mut resource_is_shared_cross_origin: Local<Boolean>,
mut script_id: Local<Integer>,
mut source_map_url: Local<Value>,
mut resource_is_opaque: Local<Boolean>,
mut is_wasm: Local<Boolean>,
mut is_module: Local<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()
}
}
}