0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-24 15:19:31 -05:00

feat: add FunctionTemplate::instance_template binding (#952)

This commit is contained in:
Spencer 2022-05-09 10:43:33 -07:00 committed by GitHub
parent 1ed35fd22e
commit 0cd8ae6f16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 0 deletions

View file

@ -1776,6 +1776,11 @@ const v8::ObjectTemplate* v8__FunctionTemplate__PrototypeTemplate(
return local_to_ptr(ptr_to_local(&self)->PrototypeTemplate());
}
const v8::ObjectTemplate* v8__FunctionTemplate__InstanceTemplate(
const v8::FunctionTemplate& self) {
return local_to_ptr(ptr_to_local(&self)->InstanceTemplate());
}
v8::Isolate* v8__FunctionCallbackInfo__GetIsolate(
const v8::FunctionCallbackInfo<v8::Value>& self) {
return self.GetIsolate();

View file

@ -54,6 +54,9 @@ extern "C" {
fn v8__FunctionTemplate__PrototypeTemplate(
this: *const FunctionTemplate,
) -> *const ObjectTemplate;
fn v8__FunctionTemplate__InstanceTemplate(
this: *const FunctionTemplate,
) -> *const ObjectTemplate;
fn v8__FunctionTemplate__SetClassName(
this: *const FunctionTemplate,
name: *const String,
@ -226,6 +229,18 @@ impl FunctionTemplate {
.unwrap()
}
/// Returns the object template that is used for instances created when this function
/// template is called as a constructor.
pub fn instance_template<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, ObjectTemplate> {
unsafe {
scope.cast_local(|_sd| v8__FunctionTemplate__InstanceTemplate(self))
}
.unwrap()
}
/// Causes the function template to inherit from a parent function template.
/// This means the function's prototype.__proto__ is set to the parent function's prototype.
pub fn inherit(&self, parent: Local<FunctionTemplate>) {

View file

@ -1566,6 +1566,42 @@ fn function_template_prototype() {
}
}
#[test]
fn instance_template_with_internal_field() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
pub fn constructor_callback(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut retval: v8::ReturnValue,
) {
let this = args.this();
assert!(this.set_internal_field(0, v8::Integer::new(scope, 42).into()));
retval.set(this.into())
}
let function_templ = v8::FunctionTemplate::new(scope, constructor_callback);
let instance_templ = function_templ.instance_template(scope);
instance_templ.set_internal_field_count(1);
let name = v8::String::new(scope, "WithInternalField").unwrap();
let val = function_templ.get_function(scope).unwrap();
context.global(scope).set(scope, name.into(), val.into());
let new_instance = eval(scope, "new WithInternalField()").unwrap();
let internal_field = new_instance
.to_object(scope)
.unwrap()
.get_internal_field(scope, 0)
.unwrap();
assert_eq!(internal_field.integer_value(scope).unwrap(), 42);
}
#[test]
fn object_template_set_accessor() {
let _setup_guard = setup();