Struct rustjs::deno_core::v8::FunctionTemplate
source · #[repr(C)]pub struct FunctionTemplate(/* private fields */);
Expand description
A FunctionTemplate is used to create functions at runtime. There can only be one function created from a FunctionTemplate in a context. The lifetime of the created function is equal to the lifetime of the context. So in case the embedder needs to create temporary functions that can be collected using Scripts is preferred.
Any modification of a FunctionTemplate after first instantiation will trigger a crash.
A FunctionTemplate can have properties, these properties are added to the function object when it is created.
A FunctionTemplate has a corresponding instance template which is used to create object instances when the function is used as a constructor. Properties added to the instance template are added to each object instance.
A FunctionTemplate can have a prototype template. The prototype template is used to create the prototype object of the function.
The following example shows how to use a FunctionTemplate:
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
t->Set(isolate, "func_property", v8::Number::New(isolate, 1));
v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
proto_t->Set(isolate,
"proto_method",
v8::FunctionTemplate::New(isolate, InvokeCallback));
proto_t->Set(isolate, "proto_const", v8::Number::New(isolate, 2));
v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
instance_t->SetAccessor(
String::NewFromUtf8Literal(isolate, "instance_accessor"),
InstanceAccessorCallback);
instance_t->SetHandler(
NamedPropertyHandlerConfiguration(PropertyHandlerCallback));
instance_t->Set(String::NewFromUtf8Literal(isolate, "instance_property"),
Number::New(isolate, 3));
v8::Local<v8::Function> function = t->GetFunction();
v8::Local<v8::Object> instance = function->NewInstance();
Let’s use “function” as the JS variable name of the function object and “instance” for the instance object created above. The function and the instance will have the following properties:
func_property in function == true;
function.func_property == 1;
function.prototype.proto_method() invokes 'InvokeCallback'
function.prototype.proto_const == 2;
instance instanceof function == true;
instance.instance_accessor calls 'InstanceAccessorCallback'
instance.instance_property == 3;
A FunctionTemplate can inherit from another one by calling the FunctionTemplate::Inherit method. The following graph illustrates the semantics of inheritance:
FunctionTemplate Parent -> Parent() . prototype -> { }
^ ^
| Inherit(Parent) | .__proto__
| |
FunctionTemplate Child -> Child() . prototype -> { }
A FunctionTemplate ‘Child’ inherits from ‘Parent’, the prototype object of the Child() function has proto pointing to the Parent() function’s prototype object. An instance of the Child function has all properties on Parent’s instance templates.
Let Parent be the FunctionTemplate initialized in the previous section and create a Child FunctionTemplate by:
Local<FunctionTemplate> parent = t;
Local<FunctionTemplate> child = FunctionTemplate::New();
child->Inherit(parent);
Local<Function> child_function = child->GetFunction();
Local<Object> child_instance = child_function->NewInstance();
The Child function and Child instance will have the following properties:
child_func.prototype.__proto__ == function.prototype;
child_instance.instance_accessor calls 'InstanceAccessorCallback'
child_instance.instance_property == 3;
The additional ‘c_function’ parameter refers to a fast API call, which must not trigger GC or JavaScript execution, or call into V8 in other ways. For more information how to define them, see include/v8-fast-api-calls.h. Please note that this feature is still experimental.
Implementations§
source§impl FunctionTemplate
impl FunctionTemplate
sourcepub fn builder<'s>(
callback: impl MapFnTo<extern "C" fn(_: *const FunctionCallbackInfo)>,
) -> FunctionBuilder<'s, FunctionTemplate>
pub fn builder<'s>( callback: impl MapFnTo<extern "C" fn(_: *const FunctionCallbackInfo)>, ) -> FunctionBuilder<'s, FunctionTemplate>
Create a FunctionBuilder to configure a FunctionTemplate.
This is the same as FunctionBuilder::
pub fn builder_raw<'s>( callback: extern "C" fn(_: *const FunctionCallbackInfo), ) -> FunctionBuilder<'s, FunctionTemplate>
sourcepub fn new<'s>(
scope: &mut HandleScope<'s, ()>,
callback: impl MapFnTo<extern "C" fn(_: *const FunctionCallbackInfo)>,
) -> Local<'s, FunctionTemplate>
pub fn new<'s>( scope: &mut HandleScope<'s, ()>, callback: impl MapFnTo<extern "C" fn(_: *const FunctionCallbackInfo)>, ) -> Local<'s, FunctionTemplate>
Creates a function template.
pub fn new_raw<'s>( scope: &mut HandleScope<'s, ()>, callback: extern "C" fn(_: *const FunctionCallbackInfo), ) -> Local<'s, FunctionTemplate>
sourcepub fn get_function<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Option<Local<'s, Function>>
pub fn get_function<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Function>>
Returns the unique function instance in the current execution context.
sourcepub fn set_class_name(&self, name: Local<'_, String>)
pub fn set_class_name(&self, name: Local<'_, String>)
Set the class name of the FunctionTemplate. This is used for printing objects created with the function created from the FunctionTemplate as its constructor.
sourcepub fn prototype_template<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, ObjectTemplate>
pub fn prototype_template<'s>( &self, scope: &mut HandleScope<'s, ()>, ) -> Local<'s, ObjectTemplate>
Returns the ObjectTemplate that is used by this FunctionTemplate as a PrototypeTemplate
sourcepub fn instance_template<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, ObjectTemplate>
pub fn instance_template<'s>( &self, scope: &mut HandleScope<'s, ()>, ) -> Local<'s, ObjectTemplate>
Returns the object template that is used for instances created when this function template is called as a constructor.
sourcepub fn inherit(&self, parent: Local<'_, FunctionTemplate>)
pub fn inherit(&self, parent: Local<'_, FunctionTemplate>)
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.
sourcepub fn read_only_prototype(&self)
pub fn read_only_prototype(&self)
Sets the ReadOnly flag in the attributes of the ‘prototype’ property of functions created from this FunctionTemplate to true.
sourcepub fn remove_prototype(&self)
pub fn remove_prototype(&self)
Removes the prototype property from functions created from this FunctionTemplate.
Methods from Deref<Target = Template>§
sourcepub fn set(&self, key: Local<'_, Name>, value: Local<'_, Data>)
pub fn set(&self, key: Local<'_, Name>, value: Local<'_, Data>)
Adds a property to each instance created by this template.
sourcepub fn set_with_attr(
&self,
key: Local<'_, Name>,
value: Local<'_, Data>,
attr: PropertyAttribute,
)
pub fn set_with_attr( &self, key: Local<'_, Name>, value: Local<'_, Data>, attr: PropertyAttribute, )
Adds a property to each instance created by this template with the specified property attributes.
Methods from Deref<Target = Data>§
sourcepub fn is_big_int(&self) -> bool
pub fn is_big_int(&self) -> bool
Returns true if this data is a BigInt
.
sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if this data is a Boolean
.
sourcepub fn is_context(&self) -> bool
pub fn is_context(&self) -> bool
Returns true if this data is a Context
.
sourcepub fn is_fixed_array(&self) -> bool
pub fn is_fixed_array(&self) -> bool
Returns true if this data is a FixedArray
.
sourcepub fn is_function_template(&self) -> bool
pub fn is_function_template(&self) -> bool
Returns true if this data is a FunctionTemplate
.
sourcepub fn is_module_request(&self) -> bool
pub fn is_module_request(&self) -> bool
Returns true if this data is a ModuleRequest
.
sourcepub fn is_object_template(&self) -> bool
pub fn is_object_template(&self) -> bool
Returns true if this data is a ObjectTemplate
.
sourcepub fn is_primitive(&self) -> bool
pub fn is_primitive(&self) -> bool
Returns true if this data is a Primitive
.
sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true if this data is a Private
.
Trait Implementations§
source§impl Debug for FunctionTemplate
impl Debug for FunctionTemplate
source§impl Deref for FunctionTemplate
impl Deref for FunctionTemplate
source§impl<'s> PartialEq<Data> for FunctionTemplate
impl<'s> PartialEq<Data> for FunctionTemplate
source§impl<'s> PartialEq<FunctionTemplate> for Data
impl<'s> PartialEq<FunctionTemplate> for Data
source§fn eq(&self, other: &FunctionTemplate) -> bool
fn eq(&self, other: &FunctionTemplate) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'s> PartialEq<FunctionTemplate> for Template
impl<'s> PartialEq<FunctionTemplate> for Template
source§fn eq(&self, other: &FunctionTemplate) -> bool
fn eq(&self, other: &FunctionTemplate) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'s> PartialEq<Template> for FunctionTemplate
impl<'s> PartialEq<Template> for FunctionTemplate
source§impl<'s> PartialEq for FunctionTemplate
impl<'s> PartialEq for FunctionTemplate
source§fn eq(&self, other: &FunctionTemplate) -> bool
fn eq(&self, other: &FunctionTemplate) -> bool
self
and other
values to be equal, and is used
by ==
.impl Eq for FunctionTemplate
Auto Trait Implementations§
impl Freeze for FunctionTemplate
impl RefUnwindSafe for FunctionTemplate
impl Send for FunctionTemplate
impl Sync for FunctionTemplate
impl Unpin for FunctionTemplate
impl UnwindSafe for FunctionTemplate
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.