Skip to main content

v8/
unbound_script.rs

1use crate::CachedData;
2use crate::Local;
3use crate::PinScope;
4use crate::Script;
5use crate::UnboundScript;
6use crate::UniqueRef;
7use crate::Value;
8
9unsafe extern "C" {
10  fn v8__UnboundScript__BindToCurrentContext(
11    script: *const UnboundScript,
12  ) -> *const Script;
13  fn v8__UnboundScript__CreateCodeCache(
14    script: *const UnboundScript,
15  ) -> *mut CachedData<'static>;
16
17  fn v8__UnboundScript__GetSourceMappingURL(
18    script: *const UnboundScript,
19  ) -> *const Value;
20
21  fn v8__UnboundScript__GetSourceURL(
22    script: *const UnboundScript,
23  ) -> *const Value;
24}
25
26impl UnboundScript {
27  /// Binds the script to the currently entered context.
28  #[inline(always)]
29  pub fn bind_to_current_context<'s>(
30    &self,
31    scope: &PinScope<'s, '_>,
32  ) -> Local<'s, Script> {
33    unsafe {
34      scope.cast_local(|_| v8__UnboundScript__BindToCurrentContext(self))
35    }
36    .unwrap()
37  }
38
39  /// Creates and returns code cache for the specified unbound_script.
40  /// This will return nullptr if the script cannot be serialized. The
41  /// CachedData returned by this function should be owned by the caller.
42  #[inline(always)]
43  pub fn create_code_cache(&self) -> Option<UniqueRef<CachedData<'static>>> {
44    let code_cache = unsafe {
45      UniqueRef::try_from_raw(v8__UnboundScript__CreateCodeCache(self))
46    };
47    if let Some(code_cache) = &code_cache {
48      debug_assert_eq!(
49        code_cache.buffer_policy(),
50        crate::script_compiler::BufferPolicy::BufferOwned
51      );
52    }
53    code_cache
54  }
55
56  #[inline(always)]
57  pub fn get_source_mapping_url<'s>(
58    &self,
59    scope: &PinScope<'s, '_>,
60  ) -> Local<'s, Value> {
61    unsafe {
62      scope
63        .cast_local(|_| v8__UnboundScript__GetSourceMappingURL(self))
64        .unwrap()
65    }
66  }
67
68  #[inline(always)]
69  pub fn get_source_url<'s>(
70    &self,
71    scope: &PinScope<'s, '_>,
72  ) -> Local<'s, Value> {
73    unsafe {
74      scope
75        .cast_local(|_| v8__UnboundScript__GetSourceURL(self))
76        .unwrap()
77    }
78  }
79}