use crate::isolate::Isolate;
use crate::support::int;
use crate::support::MapFnTo;
use crate::support::MaybeBool;
use crate::AccessorNameGetterCallback;
use crate::Array;
use crate::Context;
use crate::Local;
use crate::Map;
use crate::Name;
use crate::Object;
use crate::PropertyAttribute;
use crate::ToLocal;
use crate::Value;
extern "C" {
fn v8__Object__New(isolate: *mut Isolate) -> *const Object;
fn v8__Object__New__with_prototype_and_properties(
isolate: *mut Isolate,
prototype_or_null: *const Value,
names: *const *const Name,
values: *const *const Value,
length: usize,
) -> *const Object;
fn v8__Object__SetAccessor(
this: *const Object,
context: *const Context,
key: *const Name,
getter: AccessorNameGetterCallback,
) -> MaybeBool;
fn v8__Object__Get(
this: *const Object,
context: *const Context,
key: *const Value,
) -> *const Value;
fn v8__Object__GetIndex(
this: *const Object,
context: *const Context,
index: u32,
) -> *const Value;
fn v8__Object__GetPrototype(this: *const Object) -> *const Value;
fn v8__Object__Set(
this: *const Object,
context: *const Context,
key: *const Value,
value: *const Value,
) -> MaybeBool;
fn v8__Object__SetIndex(
this: *const Object,
context: *const Context,
index: u32,
value: *const Value,
) -> MaybeBool;
fn v8__Object__SetPrototype(
this: *const Object,
context: *const Context,
prototype: *const Value,
) -> MaybeBool;
fn v8__Object__CreateDataProperty(
this: *const Object,
context: *const Context,
key: *const Name,
value: *const Value,
) -> MaybeBool;
fn v8__Object__DefineOwnProperty(
this: *const Object,
context: *const Context,
key: *const Name,
value: *const Value,
attr: PropertyAttribute,
) -> MaybeBool;
fn v8__Object__GetIdentityHash(this: *const Object) -> int;
fn v8__Object__CreationContext(this: *const Object) -> *const Context;
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *const Array;
fn v8__Array__New_with_elements(
isolate: *mut Isolate,
elements: *const *const Value,
length: usize,
) -> *const Array;
fn v8__Array__Length(array: *const Array) -> u32;
fn v8__Map__Size(map: *const Map) -> usize;
fn v8__Map__As__Array(this: *const Map) -> *const Array;
}
impl Object {
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Object> {
let ptr = unsafe { v8__Object__New(scope.isolate()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn with_prototype_and_properties<'sc>(
scope: &mut impl ToLocal<'sc>,
prototype_or_null: Local<'sc, Value>,
names: &[Local<Name>],
values: &[Local<Value>],
) -> Local<'sc, Object> {
assert_eq!(names.len(), values.len());
let names = Local::slice_into_raw(names);
let values = Local::slice_into_raw(values);
unsafe {
let object = v8__Object__New__with_prototype_and_properties(
scope.isolate(),
&*prototype_or_null,
names.as_ptr(),
values.as_ptr(),
names.len(),
);
scope.to_local(object).unwrap()
}
}
pub fn set(
&self,
context: Local<Context>,
key: Local<Value>,
value: Local<Value>,
) -> Option<bool> {
unsafe { v8__Object__Set(self, &*context, &*key, &*value) }.into()
}
pub fn set_index(
&self,
context: Local<Context>,
index: u32,
value: Local<Value>,
) -> Option<bool> {
unsafe { v8__Object__SetIndex(self, &*context, index, &*value) }.into()
}
pub fn set_prototype(
&self,
context: Local<Context>,
prototype: Local<Value>,
) -> Option<bool> {
unsafe { v8__Object__SetPrototype(self, &*context, &*prototype) }.into()
}
pub fn create_data_property(
&self,
context: Local<Context>,
key: Local<Name>,
value: Local<Value>,
) -> Option<bool> {
unsafe { v8__Object__CreateDataProperty(self, &*context, &*key, &*value) }
.into()
}
pub fn define_own_property(
&self,
context: Local<Context>,
key: Local<Name>,
value: Local<Value>,
attr: PropertyAttribute,
) -> Option<bool> {
unsafe {
v8__Object__DefineOwnProperty(self, &*context, &*key, &*value, attr)
}
.into()
}
pub fn get<'a>(
&self,
scope: &mut impl ToLocal<'a>,
context: Local<Context>,
key: Local<Value>,
) -> Option<Local<'a, Value>> {
unsafe {
let ptr = v8__Object__Get(self, &*context, &*key);
scope.to_local(ptr)
}
}
pub fn get_index<'a>(
&self,
scope: &mut impl ToLocal<'a>,
context: Local<Context>,
index: u32,
) -> Option<Local<'a, Value>> {
unsafe {
let ptr = v8__Object__GetIndex(self, &*context, index);
scope.to_local(ptr)
}
}
pub fn get_prototype<'a>(
&self,
scope: &mut impl ToLocal<'a>,
) -> Option<Local<'a, Value>> {
unsafe {
let ptr = v8__Object__GetPrototype(self);
scope.to_local(ptr)
}
}
pub fn set_accessor(
&mut self,
context: Local<Context>,
name: Local<Name>,
getter: impl for<'s> MapFnTo<AccessorNameGetterCallback<'s>>,
) -> Option<bool> {
unsafe {
v8__Object__SetAccessor(self, &*context, &*name, getter.map_fn_to())
}
.into()
}
pub fn get_identity_hash(&self) -> int {
unsafe { v8__Object__GetIdentityHash(self) }
}
pub fn creation_context<'a>(
&self,
scope: &mut impl ToLocal<'a>,
) -> Local<'a, Context> {
unsafe {
let ptr = v8__Object__CreationContext(self);
scope.to_local(ptr).unwrap()
}
}
}
impl Array {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
length: i32,
) -> Local<'sc, Array> {
let ptr = unsafe { v8__Array__New(scope.isolate(), length) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_with_elements<'sc>(
scope: &mut impl ToLocal<'sc>,
elements: &[Local<Value>],
) -> Local<'sc, Array> {
if elements.is_empty() {
return Self::new(scope, 0);
}
let elements = Local::slice_into_raw(elements);
let ptr = unsafe {
v8__Array__New_with_elements(
scope.isolate(),
elements.as_ptr(),
elements.len(),
)
};
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn length(&self) -> u32 {
unsafe { v8__Array__Length(self) }
}
}
impl Map {
pub fn size(&self) -> usize {
unsafe { v8__Map__Size(self) }
}
pub fn as_array<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Array> {
let ptr = unsafe { v8__Map__As__Array(self) };
unsafe { scope.to_local(ptr) }.unwrap()
}
}