Skip to main content

v8/
private.rs

1use crate::Local;
2use crate::Private;
3use crate::String;
4use crate::Value;
5use crate::isolate::RealIsolate;
6use crate::scope::PinScope;
7
8unsafe extern "C" {
9  fn v8__Private__New(
10    isolate: *mut RealIsolate,
11    name: *const String,
12  ) -> *const Private;
13  fn v8__Private__ForApi(
14    isolate: *mut RealIsolate,
15    name: *const String,
16  ) -> *const Private;
17  fn v8__Private__Name(this: *const Private) -> *const Value;
18}
19
20impl Private {
21  /// Create a private symbol. If name is not empty, it will be the description.
22  #[inline(always)]
23  pub fn new<'s>(
24    scope: &PinScope<'s, '_, ()>,
25    name: Option<Local<String>>,
26  ) -> Local<'s, Private> {
27    unsafe {
28      scope.cast_local(|sd| {
29        v8__Private__New(
30          sd.get_isolate_ptr(),
31          name.map_or_else(std::ptr::null, |v| &*v),
32        )
33      })
34    }
35    .unwrap()
36  }
37
38  /// Retrieve a global private symbol. If a symbol with this name has not
39  /// been retrieved in the same isolate before, it is created.
40  /// Note that private symbols created this way are never collected, so
41  /// they should only be used for statically fixed properties.
42  /// Also, there is only one global name space for the names used as keys.
43  /// To minimize the potential for clashes, use qualified names as keys,
44  /// e.g., "Class#property".
45  #[inline(always)]
46  pub fn for_api<'s>(
47    scope: &PinScope<'s, '_, ()>,
48    name: Option<Local<String>>,
49  ) -> Local<'s, Private> {
50    unsafe {
51      scope.cast_local(|sd| {
52        v8__Private__ForApi(
53          sd.get_isolate_ptr(),
54          name.map_or_else(std::ptr::null, |v| &*v),
55        )
56      })
57    }
58    .unwrap()
59  }
60
61  /// Returns the print name string of the private symbol, or undefined if none.
62  #[inline(always)]
63  pub fn name<'s>(&self, scope: &PinScope<'s, '_, ()>) -> Local<'s, Value> {
64    unsafe { scope.cast_local(|_| v8__Private__Name(self)) }.unwrap()
65  }
66}