0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-26 00:59:28 -05:00

Add ObjectTemplate::set_accessor_property (#908)

This commit is contained in:
Divy Srivastava 2022-03-02 15:40:54 +05:30 committed by GitHub
parent 0ce68a675f
commit 90f2f79c42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 0 deletions

View file

@ -1047,6 +1047,12 @@ void v8__ObjectTemplate__SetAccessorWithSetter(
ptr_to_local(&self)->SetAccessor(ptr_to_local(&key), getter, setter);
}
void v8__ObjectTemplate__SetAccessorProperty(const v8::ObjectTemplate& self,
const v8::Name& key,
v8::FunctionTemplate& getter, v8::FunctionTemplate& setter, v8::PropertyAttribute attr) {
ptr_to_local(&self)->SetAccessorProperty(ptr_to_local(&key), ptr_to_local(&getter), ptr_to_local(&setter), attr);
}
const v8::Object* v8__Object__New(v8::Isolate* isolate) {
return local_to_ptr(v8::Object::New(isolate));
}

View file

@ -90,6 +90,13 @@ extern "C" {
getter: AccessorNameGetterCallback,
setter: AccessorNameSetterCallback,
);
fn v8__ObjectTemplate__SetAccessorProperty(
this: *const ObjectTemplate,
key: *const Name,
getter: *const FunctionTemplate,
setter: *const FunctionTemplate,
attr: PropertyAttribute,
);
}
impl Template {
@ -303,4 +310,28 @@ impl ObjectTemplate {
)
}
}
/// Sets an [accessor property](https://tc39.es/ecma262/#sec-property-attributes)
/// on the object template.
///
/// # Panics
///
/// Panics if both `getter` and `setter` are `None`.
pub fn set_accessor_property(
&self,
key: Local<Name>,
getter: Option<Local<FunctionTemplate>>,
setter: Option<Local<FunctionTemplate>>,
attr: PropertyAttribute,
) {
assert!(!getter.is_none() || !setter.is_none());
unsafe {
let getter = getter.map_or_else(std::ptr::null, |v| &*v);
let setter = setter.map_or_else(std::ptr::null, |v| &*v);
v8__ObjectTemplate__SetAccessorProperty(
self, &*key, &*getter, &*setter, attr,
)
}
}
}

View file

@ -1546,6 +1546,54 @@ fn object_template_set_accessor() {
.strict_equals(new_int.into()));
// Falls back on standard setter
assert!(eval(scope, "obj.key2 = null; obj.key2").unwrap().is_null());
// Accessor property
let getter = v8::FunctionTemplate::new(scope, fortytwo_callback);
fn property_setter(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
_: v8::ReturnValue,
) {
let this = args.this();
let ret = v8::Integer::new(scope, 69);
assert!(this.set_internal_field(0, ret.into()));
}
let setter = v8::FunctionTemplate::new(scope, property_setter);
let templ = v8::ObjectTemplate::new(scope);
templ.set_internal_field_count(1);
// Getter
let key = v8::String::new(scope, "key1").unwrap();
templ.set_accessor_property(
key.into(),
Some(getter),
None,
v8::PropertyAttribute::default(),
);
// Setter
let key = v8::String::new(scope, "key2").unwrap();
templ.set_accessor_property(
key.into(),
None,
Some(setter),
v8::PropertyAttribute::default(),
);
let obj = templ.new_instance(scope).unwrap();
let int = v8::Integer::new(scope, 42);
obj.set_internal_field(0, int.into());
scope.get_current_context().global(scope).set(
scope,
name.into(),
obj.into(),
);
assert!(eval(scope, "obj.key1").unwrap().strict_equals(int.into()));
assert!(eval(scope, "obj.key2 = 123; obj.key2")
.unwrap()
.is_undefined());
}
}