2019-12-21 06:38:26 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::support::int;
|
|
|
|
use crate::support::Opaque;
|
|
|
|
use crate::Isolate;
|
|
|
|
use crate::Local;
|
|
|
|
use crate::Primitive;
|
2019-12-25 00:31:36 +01:00
|
|
|
use crate::ToLocal;
|
2019-12-21 06:38:26 -05:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn v8__PrimitiveArray__New(
|
|
|
|
isolate: *mut Isolate,
|
|
|
|
length: int,
|
|
|
|
) -> *mut PrimitiveArray;
|
|
|
|
|
|
|
|
fn v8__PrimitiveArray__Length(this: &PrimitiveArray) -> int;
|
|
|
|
|
|
|
|
fn v8__PrimitiveArray__Set(
|
|
|
|
this: &PrimitiveArray,
|
|
|
|
isolate: *mut Isolate,
|
|
|
|
index: int,
|
|
|
|
item: &Primitive,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn v8__PrimitiveArray__Get(
|
|
|
|
this: &PrimitiveArray,
|
|
|
|
isolate: *mut Isolate,
|
|
|
|
index: int,
|
|
|
|
) -> *mut Primitive;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An array to hold Primitive values. This is used by the embedder to pass host
|
|
|
|
/// defined options to the ScriptOptions during compilation.
|
|
|
|
///
|
|
|
|
/// This is passed back to the embedder as part of
|
|
|
|
/// HostImportModuleDynamicallyCallback for module loading.
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PrimitiveArray(Opaque);
|
|
|
|
|
|
|
|
impl PrimitiveArray {
|
|
|
|
pub fn new<'sc>(
|
2019-12-25 00:31:36 +01:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-21 06:38:26 -05:00
|
|
|
length: usize,
|
|
|
|
) -> Local<'sc, PrimitiveArray> {
|
2019-12-25 00:31:36 +01:00
|
|
|
let ptr =
|
|
|
|
unsafe { v8__PrimitiveArray__New(scope.isolate(), length as int) };
|
|
|
|
unsafe { scope.to_local(ptr) }.unwrap()
|
2019-12-21 06:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn length(&self) -> usize {
|
|
|
|
unsafe { v8__PrimitiveArray__Length(self) as usize }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set<'sc>(
|
|
|
|
&self,
|
2019-12-25 00:31:36 +01:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-21 06:38:26 -05:00
|
|
|
index: usize,
|
|
|
|
item: Local<'_, Primitive>,
|
|
|
|
) {
|
|
|
|
unsafe {
|
2019-12-25 00:31:36 +01:00
|
|
|
v8__PrimitiveArray__Set(self, scope.isolate(), index as int, &item)
|
2019-12-21 06:38:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'sc>(
|
|
|
|
&self,
|
2019-12-25 00:31:36 +01:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-21 06:38:26 -05:00
|
|
|
index: usize,
|
|
|
|
) -> Local<'sc, Primitive> {
|
2019-12-25 00:31:36 +01:00
|
|
|
let ptr =
|
|
|
|
unsafe { v8__PrimitiveArray__Get(self, scope.isolate(), index as int) };
|
|
|
|
unsafe { scope.to_local(ptr) }.unwrap()
|
2019-12-21 06:38:26 -05:00
|
|
|
}
|
|
|
|
}
|