2019-12-19 19:15:52 -05:00
|
|
|
use crate::isolate::Isolate;
|
2020-01-02 10:41:40 -05:00
|
|
|
use crate::support::int;
|
2019-12-24 16:10:40 -05:00
|
|
|
use crate::support::MaybeBool;
|
2020-01-02 10:41:40 -05:00
|
|
|
use crate::Array;
|
2019-12-24 16:10:40 -05:00
|
|
|
use crate::Context;
|
2019-12-09 19:14:07 -05:00
|
|
|
use crate::Local;
|
|
|
|
use crate::Name;
|
2019-12-30 09:28:39 -05:00
|
|
|
use crate::Object;
|
2020-01-02 12:01:36 -05:00
|
|
|
use crate::PropertyCallbackInfo;
|
2019-12-24 18:31:36 -05:00
|
|
|
use crate::ToLocal;
|
2019-12-09 19:14:07 -05:00
|
|
|
use crate::Value;
|
|
|
|
|
2020-01-02 12:01:36 -05:00
|
|
|
pub type AccessorNameGetterCallback =
|
|
|
|
extern "C" fn(Local<Name>, &PropertyCallbackInfo);
|
|
|
|
|
2019-12-09 19:14:07 -05:00
|
|
|
extern "C" {
|
2019-12-30 12:14:06 -05:00
|
|
|
fn v8__Object__New(isolate: *mut Isolate) -> *mut Object;
|
|
|
|
fn v8__Object__New2(
|
2019-12-20 10:01:45 -05:00
|
|
|
isolate: *mut Isolate,
|
2019-12-09 19:14:07 -05:00
|
|
|
prototype_or_null: *mut Value,
|
|
|
|
names: *mut *mut Name,
|
|
|
|
values: *mut *mut Value,
|
|
|
|
length: usize,
|
|
|
|
) -> *mut Object;
|
2020-01-02 12:01:36 -05:00
|
|
|
fn v8__Object__SetAccessor(
|
|
|
|
self_: &Object,
|
|
|
|
context: *const Context,
|
|
|
|
name: *const Name,
|
|
|
|
getter: AccessorNameGetterCallback,
|
|
|
|
) -> MaybeBool;
|
2019-12-19 19:15:52 -05:00
|
|
|
fn v8__Object__GetIsolate(object: &Object) -> &mut Isolate;
|
2019-12-24 16:10:40 -05:00
|
|
|
|
|
|
|
fn v8__Object__Get(
|
|
|
|
object: &Object,
|
|
|
|
context: *const Context,
|
|
|
|
key: *const Value,
|
|
|
|
) -> *mut Value;
|
2019-12-26 14:38:16 -05:00
|
|
|
fn v8__Object__Set(
|
|
|
|
object: &Object,
|
|
|
|
context: *const Context,
|
|
|
|
key: *const Value,
|
|
|
|
value: *const Value,
|
|
|
|
) -> MaybeBool;
|
2019-12-24 16:10:40 -05:00
|
|
|
fn v8__Object__CreateDataProperty(
|
|
|
|
object: &Object,
|
|
|
|
context: *const Context,
|
|
|
|
key: *const Name,
|
|
|
|
value: *const Value,
|
|
|
|
) -> MaybeBool;
|
2020-01-02 13:56:28 -05:00
|
|
|
fn v8__Object__GetIdentityHash(object: &Object) -> int;
|
2020-01-02 10:41:40 -05:00
|
|
|
|
|
|
|
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *mut Array;
|
2019-12-09 19:14:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Object {
|
2019-12-30 12:14:06 -05:00
|
|
|
/// Creates an empty 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()
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:14:07 -05:00
|
|
|
/// Creates a JavaScript object with the given properties, and
|
|
|
|
/// a the given prototype_or_null (which can be any JavaScript
|
|
|
|
/// value, and if it's null, the newly created object won't have
|
|
|
|
/// a prototype at all). This is similar to Object.create().
|
|
|
|
/// All properties will be created as enumerable, configurable
|
|
|
|
/// and writable properties.
|
2019-12-30 12:14:06 -05:00
|
|
|
pub fn new2<'sc>(
|
2019-12-24 18:31:36 -05:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
2019-12-20 10:01:45 -05:00
|
|
|
mut prototype_or_null: Local<'sc, Value>,
|
|
|
|
names: Vec<Local<'sc, Name>>,
|
|
|
|
values: Vec<Local<'sc, Value>>,
|
|
|
|
) -> Local<'sc, Object> {
|
2019-12-30 12:14:06 -05:00
|
|
|
let length = names.len();
|
|
|
|
assert_eq!(length, values.len());
|
2019-12-09 19:14:07 -05:00
|
|
|
let mut names_: Vec<*mut Name> = vec![];
|
|
|
|
for mut name in names {
|
|
|
|
let n = &mut *name;
|
|
|
|
names_.push(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut values_: Vec<*mut Value> = vec![];
|
|
|
|
for mut value in values {
|
|
|
|
let n = &mut *value;
|
|
|
|
values_.push(n);
|
|
|
|
}
|
2019-12-24 18:31:36 -05:00
|
|
|
let ptr = unsafe {
|
2019-12-30 12:14:06 -05:00
|
|
|
v8__Object__New2(
|
2019-12-24 18:31:36 -05:00
|
|
|
scope.isolate(),
|
2019-12-09 19:14:07 -05:00
|
|
|
&mut *prototype_or_null,
|
|
|
|
names_.as_mut_ptr(),
|
|
|
|
values_.as_mut_ptr(),
|
|
|
|
length,
|
2019-12-24 18:31:36 -05:00
|
|
|
)
|
|
|
|
};
|
|
|
|
unsafe { scope.to_local(ptr) }.unwrap()
|
2019-12-09 19:14:07 -05:00
|
|
|
}
|
2019-12-19 08:13:33 -05:00
|
|
|
|
2019-12-26 14:38:16 -05:00
|
|
|
/// Set only return Just(true) or Empty(), so if it should never fail, use
|
|
|
|
/// result.Check().
|
|
|
|
pub fn set(
|
|
|
|
&self,
|
|
|
|
context: Local<Context>,
|
|
|
|
key: Local<Value>,
|
|
|
|
value: Local<Value>,
|
|
|
|
) -> MaybeBool {
|
|
|
|
unsafe { v8__Object__Set(self, &*context, &*key, &*value) }
|
|
|
|
}
|
|
|
|
|
2019-12-24 16:10:40 -05:00
|
|
|
/// Implements CreateDataProperty (ECMA-262, 7.3.4).
|
|
|
|
///
|
|
|
|
/// Defines a configurable, writable, enumerable property with the given value
|
|
|
|
/// on the object unless the property already exists and is not configurable
|
|
|
|
/// or the object is not extensible.
|
|
|
|
///
|
|
|
|
/// Returns true on success.
|
|
|
|
pub fn create_data_property(
|
|
|
|
&self,
|
|
|
|
context: Local<Context>,
|
|
|
|
key: Local<Name>,
|
|
|
|
value: Local<Value>,
|
|
|
|
) -> MaybeBool {
|
|
|
|
unsafe { v8__Object__CreateDataProperty(self, &*context, &*key, &*value) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'a>(
|
|
|
|
&self,
|
2019-12-24 18:31:36 -05:00
|
|
|
scope: &mut impl ToLocal<'a>,
|
2019-12-24 16:10:40 -05:00
|
|
|
context: Local<Context>,
|
|
|
|
key: Local<Value>,
|
|
|
|
) -> Option<Local<'a, Value>> {
|
|
|
|
unsafe {
|
|
|
|
let ptr = v8__Object__Get(self, &*context, &*key);
|
2019-12-24 18:31:36 -05:00
|
|
|
scope.to_local(ptr)
|
2019-12-24 16:10:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 12:01:36 -05:00
|
|
|
/// Note: SideEffectType affects the getter only, not the setter.
|
|
|
|
pub fn set_accessor(
|
|
|
|
&mut self,
|
|
|
|
context: Local<Context>,
|
|
|
|
name: Local<Name>,
|
|
|
|
getter: AccessorNameGetterCallback,
|
|
|
|
) -> MaybeBool {
|
|
|
|
unsafe { v8__Object__SetAccessor(self, &*context, &*name, getter) }
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:13:33 -05:00
|
|
|
/// Return the isolate to which the Object belongs to.
|
2019-12-24 18:31:36 -05:00
|
|
|
pub fn get_isolate(&mut self) -> &Isolate {
|
2019-12-19 08:13:33 -05:00
|
|
|
unsafe { v8__Object__GetIsolate(self) }
|
|
|
|
}
|
2020-01-02 13:56:28 -05:00
|
|
|
|
|
|
|
/// Returns the identity hash for this object. The current implementation
|
|
|
|
/// uses a hidden property on the object to store the identity hash.
|
|
|
|
///
|
|
|
|
/// The return value will never be 0. Also, it is not guaranteed to be
|
|
|
|
/// unique.
|
|
|
|
pub fn get_identity_hash(&self) -> int {
|
|
|
|
unsafe { v8__Object__GetIdentityHash(self) }
|
|
|
|
}
|
2019-12-09 19:14:07 -05:00
|
|
|
}
|
2020-01-02 10:41:40 -05:00
|
|
|
|
|
|
|
impl Array {
|
|
|
|
/// Creates a JavaScript array with the given length. If the length
|
|
|
|
/// is negative the returned array will have length 0.
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|