1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Private;
use crate::String;
use crate::Value;

extern "C" {
  fn v8__Private__New(
    isolate: *mut Isolate,
    name: *const String,
  ) -> *const Private;
  fn v8__Private__ForApi(
    isolate: *mut Isolate,
    name: *const String,
  ) -> *const Private;
  fn v8__Private__Name(this: *const Private) -> *const Value;
}

impl Private {
  /// Create a private symbol. If name is not empty, it will be the description.
  #[inline(always)]
  pub fn new<'s>(
    scope: &mut HandleScope<'s, ()>,
    name: Option<Local<String>>,
  ) -> Local<'s, Private> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Private__New(
          sd.get_isolate_ptr(),
          name.map_or_else(std::ptr::null, |v| &*v),
        )
      })
    }
    .unwrap()
  }

  /// Retrieve a global private symbol. If a symbol with this name has not
  /// been retrieved in the same isolate before, it is created.
  /// Note that private symbols created this way are never collected, so
  /// they should only be used for statically fixed properties.
  /// Also, there is only one global name space for the names used as keys.
  /// To minimize the potential for clashes, use qualified names as keys,
  /// e.g., "Class#property".
  #[inline(always)]
  pub fn for_api<'s>(
    scope: &mut HandleScope<'s, ()>,
    name: Option<Local<String>>,
  ) -> Local<'s, Private> {
    unsafe {
      scope.cast_local(|sd| {
        v8__Private__ForApi(
          sd.get_isolate_ptr(),
          name.map_or_else(std::ptr::null, |v| &*v),
        )
      })
    }
    .unwrap()
  }

  /// Returns the print name string of the private symbol, or undefined if none.
  #[inline(always)]
  pub fn name<'s>(&self, scope: &mut HandleScope<'s, ()>) -> Local<'s, Value> {
    unsafe { scope.cast_local(|_| v8__Private__Name(&*self)) }.unwrap()
  }
}