v8/
primitive_array.rs

1// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
2
3use crate::Local;
4use crate::Primitive;
5use crate::PrimitiveArray;
6use crate::isolate::RealIsolate;
7use crate::scope::GetIsolate;
8use crate::scope::PinScope;
9use crate::support::int;
10
11unsafe extern "C" {
12  fn v8__PrimitiveArray__New(
13    isolate: *mut RealIsolate,
14    length: int,
15  ) -> *const PrimitiveArray;
16
17  fn v8__PrimitiveArray__Length(this: *const PrimitiveArray) -> int;
18
19  fn v8__PrimitiveArray__Set(
20    this: *const PrimitiveArray,
21    isolate: *mut RealIsolate,
22    index: int,
23    item: *const Primitive,
24  );
25
26  fn v8__PrimitiveArray__Get(
27    this: *const PrimitiveArray,
28    isolate: *mut RealIsolate,
29    index: int,
30  ) -> *const Primitive;
31}
32
33impl PrimitiveArray {
34  #[inline(always)]
35  pub fn new<'s>(
36    scope: &PinScope<'s, '_>,
37    length: usize,
38  ) -> Local<'s, PrimitiveArray> {
39    unsafe {
40      scope.cast_local(|sd| {
41        v8__PrimitiveArray__New(sd.get_isolate_ptr(), length as int)
42      })
43    }
44    .unwrap()
45  }
46
47  #[inline(always)]
48  pub fn length(&self) -> usize {
49    unsafe { v8__PrimitiveArray__Length(self) as usize }
50  }
51
52  #[inline(always)]
53  pub fn set(
54    &self,
55    scope: &PinScope<'_, '_>,
56    index: usize,
57    item: Local<'_, Primitive>,
58  ) {
59    unsafe {
60      v8__PrimitiveArray__Set(
61        self,
62        scope.get_isolate_ptr(),
63        index as int,
64        &*item,
65      );
66    }
67  }
68
69  #[inline(always)]
70  pub fn get<'s>(
71    &self,
72    scope: &PinScope<'s, '_>,
73    index: usize,
74  ) -> Local<'s, Primitive> {
75    unsafe {
76      scope.cast_local(|sd| {
77        v8__PrimitiveArray__Get(self, sd.get_isolate_ptr(), index as int)
78      })
79    }
80    .unwrap()
81  }
82}