0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-28 16:21:04 -05:00

Add PropertyDescriptor::new_from_value_writable and new_from_value (#1187)

This commit is contained in:
Divy Srivastava 2023-02-11 22:51:51 +05:30 committed by GitHub
parent cb5216ba64
commit ffb875d5ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 0 deletions

View file

@ -3261,6 +3261,17 @@ void v8__PropertyDescriptor__CONSTRUCT(uninit_t<v8::PropertyDescriptor>* buf) {
construct_in_place<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( void v8__PropertyDescriptor__CONSTRUCT__Get_Set(
uninit_t<v8::PropertyDescriptor>* buf, v8::Local<v8::Value> get, uninit_t<v8::PropertyDescriptor>* buf, v8::Local<v8::Value> get,
v8::Local<v8::Value> set) { v8::Local<v8::Value> set) {

View file

@ -6,6 +6,15 @@ use crate::Value;
extern "C" { extern "C" {
fn v8__PropertyDescriptor__CONSTRUCT(out: *mut PropertyDescriptor); 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( fn v8__PropertyDescriptor__CONSTRUCT__Get_Set(
this: *const PropertyDescriptor, this: *const PropertyDescriptor,
get: *const Value, 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 { pub fn new_from_get_set(get: Local<Value>, set: Local<Value>) -> Self {
let mut this = MaybeUninit::<Self>::uninit(); let mut this = MaybeUninit::<Self>::uninit();
unsafe { unsafe {

View file

@ -9066,4 +9066,57 @@ fn object_define_property() {
let expected = v8::String::new(scope, "true,false,false").unwrap(); let expected = v8::String::new(scope, "true,false,false").unwrap();
assert!(expected.strict_equals(actual)); 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));
}
} }