mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-02-02 04:37:35 -05:00
Add Object::get_private() and friends (#498)
This commit is contained in:
parent
836557e84f
commit
1c38b66093
3 changed files with 140 additions and 0 deletions
|
@ -944,6 +944,40 @@ void v8__Object__SetInternalField(const v8::Object& self, int index,
|
|||
ptr_to_local(&self)->SetInternalField(index, ptr_to_local(&value));
|
||||
}
|
||||
|
||||
const v8::Value* v8__Object__GetPrivate(const v8::Object& self,
|
||||
const v8::Context& context,
|
||||
const v8::Private& key) {
|
||||
return maybe_local_to_ptr(
|
||||
ptr_to_local(&self)->GetPrivate(ptr_to_local(&context),
|
||||
ptr_to_local(&key)));
|
||||
}
|
||||
|
||||
MaybeBool v8__Object__SetPrivate(const v8::Object& self,
|
||||
const v8::Context& context,
|
||||
const v8::Private& key,
|
||||
const v8::Value& value) {
|
||||
return maybe_to_maybe_bool(
|
||||
ptr_to_local(&self)->SetPrivate(ptr_to_local(&context),
|
||||
ptr_to_local(&key),
|
||||
ptr_to_local(&value)));
|
||||
}
|
||||
|
||||
MaybeBool v8__Object__DeletePrivate(const v8::Object& self,
|
||||
const v8::Context& context,
|
||||
const v8::Private& key) {
|
||||
return maybe_to_maybe_bool(
|
||||
ptr_to_local(&self)->DeletePrivate(ptr_to_local(&context),
|
||||
ptr_to_local(&key)));
|
||||
}
|
||||
|
||||
MaybeBool v8__Object__HasPrivate(const v8::Object& self,
|
||||
const v8::Context& context,
|
||||
const v8::Private& key) {
|
||||
return maybe_to_maybe_bool(
|
||||
ptr_to_local(&self)->HasPrivate(ptr_to_local(&context),
|
||||
ptr_to_local(&key)));
|
||||
}
|
||||
|
||||
const v8::Array* v8__Array__New(v8::Isolate* isolate, int length) {
|
||||
return local_to_ptr(v8::Array::New(isolate, length));
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ use crate::Local;
|
|||
use crate::Map;
|
||||
use crate::Name;
|
||||
use crate::Object;
|
||||
use crate::Private;
|
||||
use crate::PropertyAttribute;
|
||||
use crate::Value;
|
||||
use std::convert::TryFrom;
|
||||
|
@ -118,6 +119,27 @@ extern "C" {
|
|||
index: int,
|
||||
value: *const Value,
|
||||
);
|
||||
fn v8__Object__GetPrivate(
|
||||
this: *const Object,
|
||||
context: *const Context,
|
||||
key: *const Private,
|
||||
) -> *const Value;
|
||||
fn v8__Object__SetPrivate(
|
||||
this: *const Object,
|
||||
context: *const Context,
|
||||
key: *const Private,
|
||||
value: *const Value,
|
||||
) -> MaybeBool;
|
||||
fn v8__Object__DeletePrivate(
|
||||
this: *const Object,
|
||||
context: *const Context,
|
||||
key: *const Private,
|
||||
) -> MaybeBool;
|
||||
fn v8__Object__HasPrivate(
|
||||
this: *const Object,
|
||||
context: *const Context,
|
||||
key: *const Private,
|
||||
) -> MaybeBool;
|
||||
|
||||
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *const Array;
|
||||
fn v8__Array__New_with_elements(
|
||||
|
@ -457,6 +479,73 @@ impl Object {
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Functionality for private properties.
|
||||
/// This is an experimental feature, use at your own risk.
|
||||
/// Note: Private properties are not inherited. Do not rely on this, since it
|
||||
/// may change.
|
||||
pub fn get_private<'s>(
|
||||
&self,
|
||||
scope: &mut HandleScope<'s>,
|
||||
key: Local<Private>,
|
||||
) -> Option<Local<'s, Value>> {
|
||||
unsafe {
|
||||
scope.cast_local(|sd| {
|
||||
v8__Object__GetPrivate(self, sd.get_current_context(), &*key)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Functionality for private properties.
|
||||
/// This is an experimental feature, use at your own risk.
|
||||
/// Note: Private properties are not inherited. Do not rely on this, since it
|
||||
/// may change.
|
||||
pub fn set_private<'s>(
|
||||
&self,
|
||||
scope: &mut HandleScope<'s>,
|
||||
key: Local<Private>,
|
||||
value: Local<Value>,
|
||||
) -> Option<bool> {
|
||||
unsafe {
|
||||
v8__Object__SetPrivate(
|
||||
self,
|
||||
&*scope.get_current_context(),
|
||||
&*key,
|
||||
&*value,
|
||||
)
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Functionality for private properties.
|
||||
/// This is an experimental feature, use at your own risk.
|
||||
/// Note: Private properties are not inherited. Do not rely on this, since it
|
||||
/// may change.
|
||||
pub fn delete_private<'s>(
|
||||
&self,
|
||||
scope: &mut HandleScope<'s>,
|
||||
key: Local<Private>,
|
||||
) -> Option<bool> {
|
||||
unsafe {
|
||||
v8__Object__DeletePrivate(self, &*scope.get_current_context(), &*key)
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Functionality for private properties.
|
||||
/// This is an experimental feature, use at your own risk.
|
||||
/// Note: Private properties are not inherited. Do not rely on this, since it
|
||||
/// may change.
|
||||
pub fn has_private<'s>(
|
||||
&self,
|
||||
scope: &mut HandleScope<'s>,
|
||||
key: Local<Private>,
|
||||
) -> Option<bool> {
|
||||
unsafe {
|
||||
v8__Object__HasPrivate(self, &*scope.get_current_context(), &*key)
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Array {
|
||||
|
|
|
@ -3832,6 +3832,23 @@ fn private() {
|
|||
let p_api2 = v8::Private::for_api(scope, Some(name));
|
||||
assert!(p_api2 != p);
|
||||
assert!(p_api == p_api2);
|
||||
|
||||
let object = v8::Object::new(scope);
|
||||
let sentinel = v8::Object::new(scope).into();
|
||||
assert!(!object.has_private(scope, p).unwrap());
|
||||
assert!(object.get_private(scope, p).unwrap().is_undefined());
|
||||
// True indicates that the operation didn't throw an
|
||||
// exception, not that it found and deleted a key.
|
||||
assert!(object.delete_private(scope, p).unwrap());
|
||||
assert!(object.set_private(scope, p, sentinel).unwrap());
|
||||
assert!(object.has_private(scope, p).unwrap());
|
||||
assert!(object
|
||||
.get_private(scope, p)
|
||||
.unwrap()
|
||||
.strict_equals(sentinel));
|
||||
assert!(object.delete_private(scope, p).unwrap());
|
||||
assert!(!object.has_private(scope, p).unwrap());
|
||||
assert!(object.get_private(scope, p).unwrap().is_undefined());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Reference in a new issue