0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-02-02 12:49:57 -05:00
denoland-rusty-v8/src/primitive_array.rs
2020-06-26 01:42:00 +02:00

76 lines
1.5 KiB
Rust

// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
use crate::support::int;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Primitive;
use crate::PrimitiveArray;
extern "C" {
fn v8__PrimitiveArray__New(
isolate: *mut Isolate,
length: int,
) -> *const PrimitiveArray;
fn v8__PrimitiveArray__Length(this: *const PrimitiveArray) -> int;
fn v8__PrimitiveArray__Set(
this: *const PrimitiveArray,
isolate: *mut Isolate,
index: int,
item: *const Primitive,
);
fn v8__PrimitiveArray__Get(
this: *const PrimitiveArray,
isolate: *mut Isolate,
index: int,
) -> *const Primitive;
}
impl PrimitiveArray {
pub fn new<'s>(
scope: &mut HandleScope<'s>,
length: usize,
) -> Local<'s, PrimitiveArray> {
unsafe {
scope.cast_local(|sd| {
v8__PrimitiveArray__New(sd.get_isolate_ptr(), length as int)
})
}
.unwrap()
}
pub fn length(&self) -> usize {
unsafe { v8__PrimitiveArray__Length(self) as usize }
}
pub fn set<'s>(
&self,
scope: &mut HandleScope<'s>,
index: usize,
item: Local<'_, Primitive>,
) {
unsafe {
v8__PrimitiveArray__Set(
self,
scope.get_isolate_ptr(),
index as int,
&*item,
)
}
}
pub fn get<'s>(
&self,
scope: &mut HandleScope<'s>,
index: usize,
) -> Local<'s, Primitive> {
unsafe {
scope.cast_local(|sd| {
v8__PrimitiveArray__Get(self, sd.get_isolate_ptr(), index as int)
})
}
.unwrap()
}
}