mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-24 15:19:31 -05:00
Add PropertyDescriptor::new_from_value_writable and new_from_value (#1187)
This commit is contained in:
parent
cb5216ba64
commit
ffb875d5ae
3 changed files with 93 additions and 0 deletions
|
@ -3261,6 +3261,17 @@ void v8__PropertyDescriptor__CONSTRUCT(uninit_t<v8::PropertyDescriptor>* buf) {
|
|||
construct_in_place<v8::PropertyDescriptor>(buf);
|
||||
}
|
||||
|
||||
void v8__PropertyDescriptor__CONSTRUCT__Value_Writable(
|
||||
uninit_t<v8::PropertyDescriptor>* buf, v8::Local<v8::Value> value,
|
||||
bool writable) {
|
||||
construct_in_place<v8::PropertyDescriptor>(buf, value, writable);
|
||||
}
|
||||
|
||||
void v8__PropertyDescriptor__CONSTRUCT__Value(
|
||||
uninit_t<v8::PropertyDescriptor>* buf, v8::Local<v8::Value> value) {
|
||||
construct_in_place<v8::PropertyDescriptor>(buf, value);
|
||||
}
|
||||
|
||||
void v8__PropertyDescriptor__CONSTRUCT__Get_Set(
|
||||
uninit_t<v8::PropertyDescriptor>* buf, v8::Local<v8::Value> get,
|
||||
v8::Local<v8::Value> set) {
|
||||
|
|
|
@ -6,6 +6,15 @@ use crate::Value;
|
|||
|
||||
extern "C" {
|
||||
fn v8__PropertyDescriptor__CONSTRUCT(out: *mut PropertyDescriptor);
|
||||
fn v8__PropertyDescriptor__CONSTRUCT__Value(
|
||||
this: *const PropertyDescriptor,
|
||||
value: *const Value,
|
||||
);
|
||||
fn v8__PropertyDescriptor__CONSTRUCT__Value_Writable(
|
||||
this: *const PropertyDescriptor,
|
||||
value: *const Value,
|
||||
writable: bool,
|
||||
);
|
||||
fn v8__PropertyDescriptor__CONSTRUCT__Get_Set(
|
||||
this: *const PropertyDescriptor,
|
||||
get: *const Value,
|
||||
|
@ -47,6 +56,26 @@ impl PropertyDescriptor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new_from_value(value: Local<Value>) -> Self {
|
||||
let mut this = MaybeUninit::<Self>::uninit();
|
||||
unsafe {
|
||||
v8__PropertyDescriptor__CONSTRUCT__Value(this.as_mut_ptr(), &*value);
|
||||
this.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_value_writable(value: Local<Value>, writable: bool) -> Self {
|
||||
let mut this = MaybeUninit::<Self>::uninit();
|
||||
unsafe {
|
||||
v8__PropertyDescriptor__CONSTRUCT__Value_Writable(
|
||||
this.as_mut_ptr(),
|
||||
&*value,
|
||||
writable,
|
||||
);
|
||||
this.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_get_set(get: Local<Value>, set: Local<Value>) -> Self {
|
||||
let mut this = MaybeUninit::<Self>::uninit();
|
||||
unsafe {
|
||||
|
|
|
@ -9066,4 +9066,57 @@ fn object_define_property() {
|
|||
let expected = v8::String::new(scope, "true,false,false").unwrap();
|
||||
assert!(expected.strict_equals(actual));
|
||||
}
|
||||
|
||||
{
|
||||
let scope = &mut v8::HandleScope::new(isolate);
|
||||
let context = v8::Context::new(scope);
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
|
||||
let mut desc = v8::PropertyDescriptor::new_from_value_writable(
|
||||
v8::Integer::new(scope, 42).into(),
|
||||
true,
|
||||
);
|
||||
desc.set_configurable(true);
|
||||
desc.set_enumerable(false);
|
||||
|
||||
let name = v8::String::new(scope, "g").unwrap();
|
||||
context
|
||||
.global(scope)
|
||||
.define_property(scope, name.into(), &desc);
|
||||
let source = r#"
|
||||
{
|
||||
const d = Object.getOwnPropertyDescriptor(globalThis, "g");
|
||||
[d.configurable, d.enumerable, d.writable].toString()
|
||||
}
|
||||
"#;
|
||||
let actual = eval(scope, source).unwrap();
|
||||
let expected = v8::String::new(scope, "true,false,true").unwrap();
|
||||
assert!(expected.strict_equals(actual));
|
||||
}
|
||||
|
||||
{
|
||||
let scope = &mut v8::HandleScope::new(isolate);
|
||||
let context = v8::Context::new(scope);
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
|
||||
let mut desc = v8::PropertyDescriptor::new_from_value(
|
||||
v8::Integer::new(scope, 42).into(),
|
||||
);
|
||||
desc.set_configurable(true);
|
||||
desc.set_enumerable(false);
|
||||
|
||||
let name = v8::String::new(scope, "g").unwrap();
|
||||
context
|
||||
.global(scope)
|
||||
.define_property(scope, name.into(), &desc);
|
||||
let source = r#"
|
||||
{
|
||||
const d = Object.getOwnPropertyDescriptor(globalThis, "g");
|
||||
[d.configurable, d.enumerable, d.writable].toString()
|
||||
}
|
||||
"#;
|
||||
let actual = eval(scope, source).unwrap();
|
||||
let expected = v8::String::new(scope, "true,false,false").unwrap();
|
||||
assert!(expected.strict_equals(actual));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue