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
use crate::support::Delete;
use crate::support::Opaque;
use crate::support::UniqueRef;

extern "C" {
  fn v8__ArrayBuffer__Allocator__NewDefaultAllocator() -> *mut Allocator;
  fn v8__ArrayBuffer__Allocator__DELETE(this: &'static mut Allocator);
}

// TODO: allow the user to implement their own Allocator.
#[repr(C)]
pub struct Allocator(Opaque);

impl Allocator {
  pub fn new_default_allocator() -> UniqueRef<Allocator> {
    unsafe {
      UniqueRef::from_raw(v8__ArrayBuffer__Allocator__NewDefaultAllocator())
    }
  }
}

impl Delete for Allocator {
  fn delete(&'static mut self) {
    unsafe { v8__ArrayBuffer__Allocator__DELETE(self) };
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_default_allocator() {
    Allocator::new_default_allocator();
  }
}