Skip to main content

v8/
script_or_module.rs

1// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
2use crate::Data;
3use crate::Local;
4use crate::ScriptOrModule;
5use crate::Value;
6
7unsafe extern "C" {
8  fn v8__ScriptOrModule__GetResourceName(
9    this: *const ScriptOrModule,
10  ) -> *const Value;
11
12  fn v8__ScriptOrModule__HostDefinedOptions(
13    this: *const ScriptOrModule,
14  ) -> *const Data;
15}
16
17impl ScriptOrModule {
18  /// The name that was passed by the embedder as ResourceName to the
19  /// ScriptOrigin. This can be either a v8::String or v8::Undefined.
20  #[inline(always)]
21  pub fn get_resource_name(&self) -> Local<'_, Value> {
22    // Note: the C++ `v8::ScriptOrModule::GetResourceName()` does not actually
23    // return a local handle, but rather a handle whose lifetime is bound to
24    // the related `ScriptOrModule` object.
25    unsafe {
26      let ptr = v8__ScriptOrModule__GetResourceName(self);
27      Local::from_raw(ptr).unwrap()
28    }
29  }
30
31  /// The options that were passed by the embedder as HostDefinedOptions to the
32  /// ScriptOrigin.
33  #[inline(always)]
34  pub fn host_defined_options(&self) -> Local<'_, Data> {
35    // Note: the C++ `v8::ScriptOrModule::HostDefinedOptions()` does not
36    // actually return a local handle, but rather a handle whose lifetime is
37    // bound to the related `ScriptOrModule` object.
38    unsafe {
39      let ptr = v8__ScriptOrModule__HostDefinedOptions(self);
40      Local::from_raw(ptr).unwrap()
41    }
42  }
43}