2020-01-18 18:38:41 -05:00
|
|
|
use crate::data::Data;
|
|
|
|
use crate::data::FunctionTemplate;
|
|
|
|
use crate::data::Name;
|
|
|
|
use crate::data::ObjectTemplate;
|
|
|
|
use crate::data::Template;
|
|
|
|
use crate::isolate::Isolate;
|
2020-09-29 19:04:59 -04:00
|
|
|
use crate::support::int;
|
2020-01-18 18:38:41 -05:00
|
|
|
use crate::support::MapFnTo;
|
2021-05-11 10:40:53 -04:00
|
|
|
use crate::AccessorNameGetterCallback;
|
|
|
|
use crate::AccessorNameSetterCallback;
|
2020-12-07 17:03:46 -05:00
|
|
|
use crate::CFunction;
|
|
|
|
use crate::ConstructorBehavior;
|
2020-01-18 18:38:41 -05:00
|
|
|
use crate::Context;
|
|
|
|
use crate::Function;
|
2020-12-07 17:03:46 -05:00
|
|
|
use crate::FunctionBuilder;
|
2020-01-18 18:38:41 -05:00
|
|
|
use crate::FunctionCallback;
|
2020-06-03 01:38:34 -04:00
|
|
|
use crate::HandleScope;
|
2020-01-18 18:38:41 -05:00
|
|
|
use crate::Local;
|
|
|
|
use crate::Object;
|
2020-01-20 11:16:24 -05:00
|
|
|
use crate::PropertyAttribute;
|
2020-12-07 17:03:46 -05:00
|
|
|
use crate::SideEffectType;
|
|
|
|
use crate::Signature;
|
2020-01-18 18:38:41 -05:00
|
|
|
use crate::String;
|
2020-12-07 17:03:46 -05:00
|
|
|
use crate::Value;
|
2020-01-20 11:16:24 -05:00
|
|
|
use crate::NONE;
|
2020-09-29 19:04:59 -04:00
|
|
|
use std::convert::TryFrom;
|
2020-12-07 17:03:46 -05:00
|
|
|
use std::ptr::null;
|
2020-01-18 18:38:41 -05:00
|
|
|
|
|
|
|
extern "C" {
|
2020-01-20 11:16:24 -05:00
|
|
|
fn v8__Template__Set(
|
2020-04-13 08:43:56 -04:00
|
|
|
this: *const Template,
|
2020-01-20 11:16:24 -05:00
|
|
|
key: *const Name,
|
|
|
|
value: *const Data,
|
|
|
|
attr: PropertyAttribute,
|
|
|
|
);
|
2021-04-20 06:48:20 -04:00
|
|
|
fn v8__Signature__New(
|
|
|
|
isolate: *mut Isolate,
|
|
|
|
templ: *const FunctionTemplate,
|
|
|
|
) -> *const Signature;
|
2020-01-18 18:38:41 -05:00
|
|
|
fn v8__FunctionTemplate__New(
|
2020-04-13 08:43:56 -04:00
|
|
|
isolate: *mut Isolate,
|
2020-01-18 18:38:41 -05:00
|
|
|
callback: FunctionCallback,
|
2020-12-07 17:03:46 -05:00
|
|
|
data_or_null: *const Value,
|
|
|
|
signature_or_null: *const Signature,
|
|
|
|
length: i32,
|
|
|
|
constructor_behavior: ConstructorBehavior,
|
|
|
|
side_effect_type: SideEffectType,
|
|
|
|
c_function_or_null: *const CFunction,
|
2020-04-13 08:43:56 -04:00
|
|
|
) -> *const FunctionTemplate;
|
2020-01-18 18:38:41 -05:00
|
|
|
fn v8__FunctionTemplate__GetFunction(
|
2020-04-13 08:43:56 -04:00
|
|
|
this: *const FunctionTemplate,
|
|
|
|
context: *const Context,
|
|
|
|
) -> *const Function;
|
2021-06-30 10:55:14 -04:00
|
|
|
fn v8__FunctionTemplate__PrototypeTemplate(
|
|
|
|
this: *const FunctionTemplate,
|
|
|
|
) -> *const ObjectTemplate;
|
2020-01-18 18:38:41 -05:00
|
|
|
fn v8__FunctionTemplate__SetClassName(
|
2020-04-13 08:43:56 -04:00
|
|
|
this: *const FunctionTemplate,
|
|
|
|
name: *const String,
|
|
|
|
);
|
2021-06-30 10:55:14 -04:00
|
|
|
fn v8__FunctionTemplate__Inherit(
|
|
|
|
this: *const FunctionTemplate,
|
|
|
|
parent: *const FunctionTemplate,
|
|
|
|
);
|
|
|
|
fn v8__FunctionTemplate__ReadOnlyPrototype(this: *const FunctionTemplate);
|
|
|
|
fn v8__FunctionTemplate__RemovePrototype(this: *const FunctionTemplate);
|
2020-01-18 18:38:41 -05:00
|
|
|
|
|
|
|
fn v8__ObjectTemplate__New(
|
|
|
|
isolate: *mut Isolate,
|
|
|
|
templ: *const FunctionTemplate,
|
2020-04-13 08:43:56 -04:00
|
|
|
) -> *const ObjectTemplate;
|
2020-01-18 18:38:41 -05:00
|
|
|
fn v8__ObjectTemplate__NewInstance(
|
2020-04-13 08:43:56 -04:00
|
|
|
this: *const ObjectTemplate,
|
|
|
|
context: *const Context,
|
|
|
|
) -> *const Object;
|
2020-09-29 19:04:59 -04:00
|
|
|
fn v8__ObjectTemplate__InternalFieldCount(this: *const ObjectTemplate)
|
|
|
|
-> int;
|
|
|
|
fn v8__ObjectTemplate__SetInternalFieldCount(
|
|
|
|
this: *const ObjectTemplate,
|
|
|
|
value: int,
|
|
|
|
);
|
2021-05-11 10:40:53 -04:00
|
|
|
fn v8__ObjectTemplate__SetAccessor(
|
|
|
|
this: *const ObjectTemplate,
|
|
|
|
key: *const Name,
|
|
|
|
getter: AccessorNameGetterCallback,
|
|
|
|
);
|
|
|
|
fn v8__ObjectTemplate__SetAccessorWithSetter(
|
|
|
|
this: *const ObjectTemplate,
|
|
|
|
key: *const Name,
|
|
|
|
getter: AccessorNameGetterCallback,
|
|
|
|
setter: AccessorNameSetterCallback,
|
|
|
|
);
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Template {
|
|
|
|
/// Adds a property to each instance created by this template.
|
|
|
|
pub fn set(&self, key: Local<Name>, value: Local<Data>) {
|
2020-01-20 11:16:24 -05:00
|
|
|
self.set_with_attr(key, value, NONE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a property to each instance created by this template with
|
|
|
|
/// the specified property attributes.
|
|
|
|
pub fn set_with_attr(
|
|
|
|
&self,
|
|
|
|
key: Local<Name>,
|
|
|
|
value: Local<Data>,
|
|
|
|
attr: PropertyAttribute,
|
|
|
|
) {
|
|
|
|
unsafe { v8__Template__Set(self, &*key, &*value, attr) }
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:03:46 -05:00
|
|
|
impl<'s> FunctionBuilder<'s, FunctionTemplate> {
|
|
|
|
/// Set the function call signature. The default is no signature.
|
|
|
|
pub fn signature(mut self, signature: Local<'s, Signature>) -> Self {
|
|
|
|
self.signature = Some(signature);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the function template.
|
|
|
|
pub fn build(
|
|
|
|
self,
|
2020-06-03 01:38:34 -04:00
|
|
|
scope: &mut HandleScope<'s, ()>,
|
|
|
|
) -> Local<'s, FunctionTemplate> {
|
2020-05-31 09:00:23 -04:00
|
|
|
unsafe {
|
2020-06-03 01:38:34 -04:00
|
|
|
scope.cast_local(|sd| {
|
2020-12-07 17:03:46 -05:00
|
|
|
v8__FunctionTemplate__New(
|
|
|
|
sd.get_isolate_ptr(),
|
|
|
|
self.callback,
|
|
|
|
self.data.map_or_else(null, |p| &*p),
|
|
|
|
self.signature.map_or_else(null, |p| &*p),
|
|
|
|
self.length,
|
|
|
|
self.constructor_behavior,
|
|
|
|
self.side_effect_type,
|
|
|
|
null(),
|
|
|
|
)
|
2020-05-31 09:00:23 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
.unwrap()
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
2020-12-07 17:03:46 -05:00
|
|
|
}
|
|
|
|
|
2021-04-20 06:48:20 -04:00
|
|
|
/// A Signature specifies which receiver is valid for a function.
|
|
|
|
///
|
|
|
|
/// A receiver matches a given signature if the receiver (or any of its
|
|
|
|
/// hidden prototypes) was created from the signature's FunctionTemplate, or
|
|
|
|
/// from a FunctionTemplate that inherits directly or indirectly from the
|
|
|
|
/// signature's FunctionTemplate.
|
|
|
|
impl Signature {
|
|
|
|
pub fn new<'s>(
|
|
|
|
scope: &mut HandleScope<'s, ()>,
|
|
|
|
templ: Local<FunctionTemplate>,
|
|
|
|
) -> Local<'s, Self> {
|
|
|
|
unsafe {
|
|
|
|
scope.cast_local(|sd| v8__Signature__New(sd.get_isolate_ptr(), &*templ))
|
|
|
|
}
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:03:46 -05:00
|
|
|
impl FunctionTemplate {
|
|
|
|
/// Create a FunctionBuilder to configure a FunctionTemplate.
|
|
|
|
/// This is the same as FunctionBuilder::<FunctionTemplate>::new().
|
|
|
|
pub fn builder<'s>(
|
|
|
|
callback: impl MapFnTo<FunctionCallback>,
|
|
|
|
) -> FunctionBuilder<'s, Self> {
|
|
|
|
FunctionBuilder::new(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a function template.
|
|
|
|
pub fn new<'s>(
|
|
|
|
scope: &mut HandleScope<'s, ()>,
|
|
|
|
callback: impl MapFnTo<FunctionCallback>,
|
|
|
|
) -> Local<'s, FunctionTemplate> {
|
|
|
|
Self::builder(callback).build(scope)
|
|
|
|
}
|
2020-01-18 18:38:41 -05:00
|
|
|
|
|
|
|
/// Returns the unique function instance in the current execution context.
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn get_function<'s>(
|
2020-06-26 08:47:13 -04:00
|
|
|
&self,
|
2020-06-25 20:56:24 -04:00
|
|
|
scope: &mut HandleScope<'s>,
|
2020-06-03 01:38:34 -04:00
|
|
|
) -> Option<Local<'s, Function>> {
|
2020-01-18 18:38:41 -05:00
|
|
|
unsafe {
|
2020-06-25 20:56:24 -04:00
|
|
|
scope.cast_local(|sd| {
|
2020-06-26 08:47:13 -04:00
|
|
|
v8__FunctionTemplate__GetFunction(self, sd.get_current_context())
|
2020-06-25 20:56:24 -04:00
|
|
|
})
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 18:38:41 -05:00
|
|
|
|
|
|
|
/// Set the class name of the FunctionTemplate. This is used for
|
|
|
|
/// printing objects created with the function created from the
|
|
|
|
/// FunctionTemplate as its constructor.
|
2020-06-26 08:47:13 -04:00
|
|
|
pub fn set_class_name(&self, name: Local<String>) {
|
|
|
|
unsafe { v8__FunctionTemplate__SetClassName(self, &*name) };
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
2021-06-30 10:55:14 -04:00
|
|
|
|
|
|
|
/// Returns the ObjectTemplate that is used by this
|
|
|
|
/// FunctionTemplate as a PrototypeTemplate
|
|
|
|
pub fn prototype_template<'s>(
|
|
|
|
&self,
|
|
|
|
scope: &mut HandleScope<'s, ()>,
|
|
|
|
) -> Local<'s, ObjectTemplate> {
|
|
|
|
unsafe {
|
|
|
|
scope.cast_local(|_sd| v8__FunctionTemplate__PrototypeTemplate(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>) {
|
|
|
|
unsafe { v8__FunctionTemplate__Inherit(self, &*parent) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the ReadOnly flag in the attributes of the 'prototype' property
|
|
|
|
/// of functions created from this FunctionTemplate to true.
|
|
|
|
pub fn read_only_prototype(&self) {
|
|
|
|
unsafe { v8__FunctionTemplate__ReadOnlyPrototype(self) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the prototype property from functions created from this FunctionTemplate.
|
|
|
|
pub fn remove_prototype(&self) {
|
|
|
|
unsafe { v8__FunctionTemplate__RemovePrototype(self) };
|
|
|
|
}
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectTemplate {
|
|
|
|
/// Creates an object template.
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn new<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, ObjectTemplate> {
|
2020-05-31 09:00:23 -04:00
|
|
|
unsafe {
|
2020-06-03 01:38:34 -04:00
|
|
|
scope.cast_local(|sd| {
|
|
|
|
v8__ObjectTemplate__New(sd.get_isolate_ptr(), std::ptr::null())
|
2020-05-31 09:00:23 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
.unwrap()
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an object template from a function template.
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn new_from_template<'s>(
|
|
|
|
scope: &mut HandleScope<'s, ()>,
|
2020-01-18 18:38:41 -05:00
|
|
|
templ: Local<FunctionTemplate>,
|
2020-06-03 01:38:34 -04:00
|
|
|
) -> Local<'s, ObjectTemplate> {
|
2020-05-31 09:00:23 -04:00
|
|
|
unsafe {
|
|
|
|
scope
|
2020-06-03 01:38:34 -04:00
|
|
|
.cast_local(|sd| v8__ObjectTemplate__New(sd.get_isolate_ptr(), &*templ))
|
2020-05-31 09:00:23 -04:00
|
|
|
}
|
|
|
|
.unwrap()
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new instance of this object template.
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn new_instance<'s>(
|
2020-01-18 18:38:41 -05:00
|
|
|
&self,
|
2020-06-03 01:38:34 -04:00
|
|
|
scope: &mut HandleScope<'s>,
|
|
|
|
) -> Option<Local<'s, Object>> {
|
2020-05-31 09:00:23 -04:00
|
|
|
unsafe {
|
2020-06-25 20:56:24 -04:00
|
|
|
scope.cast_local(|sd| {
|
|
|
|
v8__ObjectTemplate__NewInstance(self, sd.get_current_context())
|
|
|
|
})
|
2020-05-31 09:00:23 -04:00
|
|
|
}
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|
2020-09-29 19:04:59 -04:00
|
|
|
|
|
|
|
/// Gets the number of internal fields for objects generated from
|
|
|
|
/// this template.
|
|
|
|
pub fn internal_field_count(&self) -> usize {
|
|
|
|
let count = unsafe { v8__ObjectTemplate__InternalFieldCount(self) };
|
|
|
|
usize::try_from(count).expect("bad internal field count") // Can't happen.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the number of internal fields for objects generated from
|
|
|
|
/// this template.
|
|
|
|
pub fn set_internal_field_count(&self, value: usize) -> bool {
|
|
|
|
// The C++ API takes an i32 but trying to set a value < 0
|
|
|
|
// results in unpredictable behavior, hence we disallow it.
|
|
|
|
match int::try_from(value) {
|
|
|
|
Err(_) => false,
|
|
|
|
Ok(value) => {
|
|
|
|
unsafe { v8__ObjectTemplate__SetInternalFieldCount(self, value) };
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 10:40:53 -04:00
|
|
|
|
|
|
|
pub fn set_accessor(
|
|
|
|
&self,
|
|
|
|
key: Local<Name>,
|
|
|
|
getter: impl for<'s> MapFnTo<AccessorNameGetterCallback<'s>>,
|
|
|
|
) {
|
|
|
|
unsafe { v8__ObjectTemplate__SetAccessor(self, &*key, getter.map_fn_to()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_accessor_with_setter(
|
|
|
|
&self,
|
|
|
|
key: Local<Name>,
|
|
|
|
getter: impl for<'s> MapFnTo<AccessorNameGetterCallback<'s>>,
|
|
|
|
setter: impl for<'s> MapFnTo<AccessorNameSetterCallback<'s>>,
|
|
|
|
) {
|
|
|
|
unsafe {
|
|
|
|
v8__ObjectTemplate__SetAccessorWithSetter(
|
|
|
|
self,
|
|
|
|
&*key,
|
|
|
|
getter.map_fn_to(),
|
|
|
|
setter.map_fn_to(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 18:38:41 -05:00
|
|
|
}
|