rusty_v8/
script.rs

1use std::marker::PhantomData;
2use std::mem::MaybeUninit;
3use std::ptr::null;
4
5use crate::Context;
6use crate::HandleScope;
7use crate::Isolate;
8use crate::Local;
9use crate::Script;
10use crate::String;
11use crate::UnboundScript;
12use crate::Value;
13
14/// The origin, within a file, of a script.
15#[repr(C)]
16#[derive(Debug)]
17pub struct ScriptOrigin<'s>([usize; 8], PhantomData<&'s ()>);
18
19extern "C" {
20  fn v8__Script__Compile(
21    context: *const Context,
22    source: *const String,
23    origin: *const ScriptOrigin,
24  ) -> *const Script;
25  fn v8__Script__GetUnboundScript(
26    script: *const Script,
27  ) -> *const UnboundScript;
28  fn v8__Script__Run(
29    script: *const Script,
30    context: *const Context,
31  ) -> *const Value;
32
33  fn v8__ScriptOrigin__CONSTRUCT(
34    isolate: *mut Isolate,
35    buf: *mut MaybeUninit<ScriptOrigin>,
36    resource_name: *const Value,
37    resource_line_offset: i32,
38    resource_column_offset: i32,
39    resource_is_shared_cross_origin: bool,
40    script_id: i32,
41    source_map_url: *const Value,
42    resource_is_opaque: bool,
43    is_wasm: bool,
44    is_module: bool,
45  );
46}
47
48impl Script {
49  /// A shorthand for ScriptCompiler::Compile().
50  pub fn compile<'s>(
51    scope: &mut HandleScope<'s>,
52    source: Local<String>,
53    origin: Option<&ScriptOrigin>,
54  ) -> Option<Local<'s, Script>> {
55    unsafe {
56      scope.cast_local(|sd| {
57        v8__Script__Compile(
58          sd.get_current_context(),
59          &*source,
60          origin.map(|r| r as *const _).unwrap_or_else(null),
61        )
62      })
63    }
64  }
65
66  /// Returns the corresponding context-unbound script.
67  pub fn get_unbound_script<'s>(
68    &self,
69    scope: &mut HandleScope<'s>,
70  ) -> Local<'s, UnboundScript> {
71    unsafe {
72      scope
73        .cast_local(|_| v8__Script__GetUnboundScript(self))
74        .unwrap()
75    }
76  }
77
78  /// Runs the script returning the resulting value. It will be run in the
79  /// context in which it was created (ScriptCompiler::CompileBound or
80  /// UnboundScript::BindToCurrentContext()).
81  pub fn run<'s>(
82    &self,
83    scope: &mut HandleScope<'s>,
84  ) -> Option<Local<'s, Value>> {
85    unsafe {
86      scope.cast_local(|sd| v8__Script__Run(self, sd.get_current_context()))
87    }
88  }
89}
90
91/// The origin, within a file, of a script.
92impl<'s> ScriptOrigin<'s> {
93  #[allow(clippy::too_many_arguments)]
94  pub fn new(
95    scope: &mut HandleScope<'s, ()>,
96    resource_name: Local<'s, Value>,
97    resource_line_offset: i32,
98    resource_column_offset: i32,
99    resource_is_shared_cross_origin: bool,
100    script_id: i32,
101    source_map_url: Local<'s, Value>,
102    resource_is_opaque: bool,
103    is_wasm: bool,
104    is_module: bool,
105  ) -> Self {
106    unsafe {
107      let mut buf = std::mem::MaybeUninit::<ScriptOrigin>::uninit();
108      v8__ScriptOrigin__CONSTRUCT(
109        scope.get_isolate_ptr(),
110        &mut buf,
111        &*resource_name,
112        resource_line_offset,
113        resource_column_offset,
114        resource_is_shared_cross_origin,
115        script_id,
116        &*source_map_url,
117        resource_is_opaque,
118        is_wasm,
119        is_module,
120      );
121      buf.assume_init()
122    }
123  }
124}