v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
v8::FunctionTemplate Class Reference

#include <v8-template.h>

Inheritance diagram for v8::FunctionTemplate:
Collaboration diagram for v8::FunctionTemplate:

Public Member Functions

V8_WARN_UNUSED_RESULT MaybeLocal< FunctionGetFunction (Local< Context > context)
 
V8_WARN_UNUSED_RESULT MaybeLocal< ObjectNewRemoteInstance ()
 
void SetCallHandler (FunctionCallback callback, Local< Value > data=Local< Value >(), SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const MemorySpan< const CFunction > &c_function_overloads={})
 
void SetLength (int length)
 
Local< ObjectTemplateInstanceTemplate ()
 
void Inherit (Local< FunctionTemplate > parent)
 
Local< ObjectTemplatePrototypeTemplate ()
 
void SetPrototypeProviderTemplate (Local< FunctionTemplate > prototype_provider)
 
void SetClassName (Local< String > name)
 
void SetInterfaceName (Local< String > name)
 
void SetExceptionContext (ExceptionContext context)
 
void SetAcceptAnyReceiver (bool value)
 
void ReadOnlyPrototype ()
 
void RemovePrototype ()
 
bool HasInstance (Local< Value > object)
 
bool IsLeafTemplateForApiObject (v8::Local< v8::Value > value) const
 
void SealAndPrepareForPromotionToReadOnly ()
 
- Public Member Functions inherited from v8::Template
void Set (Local< Name > name, Local< Data > value, PropertyAttribute attributes=None)
 
void SetPrivate (Local< Private > name, Local< Data > value, PropertyAttribute attributes=None)
 
V8_INLINE void Set (Isolate *isolate, const char *name, Local< Data > value, PropertyAttribute attributes=None)
 
void SetAccessorProperty (Local< Name > name, Local< FunctionTemplate > getter=Local< FunctionTemplate >(), Local< FunctionTemplate > setter=Local< FunctionTemplate >(), PropertyAttribute attribute=None)
 
void SetNativeDataProperty (Local< Name > name, AccessorNameGetterCallback getter, AccessorNameSetterCallback setter=nullptr, Local< Value > data=Local< Value >(), PropertyAttribute attribute=None, SideEffectType getter_side_effect_type=SideEffectType::kHasSideEffect, SideEffectType setter_side_effect_type=SideEffectType::kHasSideEffect)
 
void SetLazyDataProperty (Local< Name > name, AccessorNameGetterCallback getter, Local< Value > data=Local< Value >(), PropertyAttribute attribute=None, SideEffectType getter_side_effect_type=SideEffectType::kHasSideEffect, SideEffectType setter_side_effect_type=SideEffectType::kHasSideEffect)
 
void SetIntrinsicDataProperty (Local< Name > name, Intrinsic intrinsic, PropertyAttribute attribute=None)
 
- Public Member Functions inherited from v8::Data
bool IsValue () const
 
bool IsModule () const
 
bool IsModuleRequest () const
 
bool IsFixedArray () const
 
bool IsPrivate () const
 
bool IsObjectTemplate () const
 
bool IsFunctionTemplate () const
 
bool IsContext () const
 

Static Public Member Functions

static Local< FunctionTemplateNew (Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const CFunction *c_function=nullptr, uint16_t instance_type=0, uint16_t allowed_receiver_instance_type_range_start=0, uint16_t allowed_receiver_instance_type_range_end=0)
 
static Local< FunctionTemplateNewWithCFunctionOverloads (Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const MemorySpan< const CFunction > &c_function_overloads={})
 
static Local< FunctionTemplateNewWithCache (Isolate *isolate, FunctionCallback callback, Local< Private > cache_property, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, SideEffectType side_effect_type=SideEffectType::kHasSideEffect)
 
static V8_INLINE FunctionTemplateCast (Data *data)
 

Private Member Functions

 FunctionTemplate ()
 

Static Private Member Functions

static void CheckCast (Data *that)
 

Friends

class Context
 
class ObjectTemplate
 

Detailed 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:

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->SetNativeDataProperty(
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();
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const CFunction *c_function=nullptr, uint16_t instance_type=0, uint16_t allowed_receiver_instance_type_range_start=0, uint16_t allowed_receiver_instance_type_range_end=0)
Definition api.cc:1101
static Local< Number > New(Isolate *isolate, double value)
Definition api.cc:9557
static V8_WARN_UNUSED_RESULT Local< String > NewFromUtf8Literal(Isolate *isolate, const char(&literal)[N], NewStringType type=NewStringType::kNormal)

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 -> { }
void Inherit(Local< FunctionTemplate > parent)
Definition api.cc:1089

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:

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.

Definition at line 584 of file v8-template.h.

Constructor & Destructor Documentation

◆ FunctionTemplate()

v8::FunctionTemplate::FunctionTemplate ( )
private

Member Function Documentation

◆ Cast()

FunctionTemplate * v8::FunctionTemplate::Cast ( Data * data)
static

Definition at line 1129 of file v8-template.h.

Here is the call graph for this function:

◆ CheckCast()

void v8::FunctionTemplate::CheckCast ( Data * that)
staticprivate

Definition at line 7399 of file api.cc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFunction()

MaybeLocal< v8::Function > v8::FunctionTemplate::GetFunction ( Local< Context > context)

Returns the unique function instance in the current execution context.

Definition at line 7411 of file api.cc.

Here is the call graph for this function:

◆ HasInstance()

bool v8::FunctionTemplate::HasInstance ( Local< Value > object)

Returns true if the given object is an instance of this function template.

Definition at line 7449 of file api.cc.

Here is the call graph for this function:

◆ Inherit()

void v8::FunctionTemplate::Inherit ( v8::Local< FunctionTemplate > value)

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.

Definition at line 1089 of file api.cc.

Here is the call graph for this function:

◆ InstanceTemplate()

Local< ObjectTemplate > v8::FunctionTemplate::InstanceTemplate ( )

Get the InstanceTemplate.

Definition at line 1297 of file api.cc.

Here is the call graph for this function:

◆ IsLeafTemplateForApiObject()

bool v8::FunctionTemplate::IsLeafTemplateForApiObject ( v8::Local< v8::Value > value) const

Returns true if the given value is an API object that was constructed by an instance of this function template (without checking for inheriting function templates).

This is an experimental feature and may still change significantly.

Definition at line 7468 of file api.cc.

Here is the call graph for this function:

◆ New()

Local< FunctionTemplate > v8::FunctionTemplate::New ( Isolate * isolate,
FunctionCallback callback = nullptr,
v8::Local< Value > data = Local<Value>(),
v8::Local< Signature > signature = Local<Signature>(),
int length = 0,
ConstructorBehavior behavior = ConstructorBehavior::kAllow,
SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
const CFunction * c_function = nullptr,
uint16_t instance_type = 0,
uint16_t allowed_receiver_instance_type_range_start = 0,
uint16_t allowed_receiver_instance_type_range_end = 0 )
static

Creates a function template.

Definition at line 1101 of file api.cc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NewRemoteInstance()

MaybeLocal< v8::Object > v8::FunctionTemplate::NewRemoteInstance ( )

Similar to Context::NewRemoteContext, this creates an instance that isn't backed by an actual object.

The InstanceTemplate of this FunctionTemplate must have access checks with handlers installed.

Definition at line 7423 of file api.cc.

Here is the call graph for this function:

◆ NewWithCache()

Local< FunctionTemplate > v8::FunctionTemplate::NewWithCache ( Isolate * isolate,
FunctionCallback callback,
Local< Private > cache_property,
Local< Value > data = Local<Value>(),
Local< Signature > signature = Local<Signature>(),
int length = 0,
SideEffectType side_effect_type = SideEffectType::kHasSideEffect )
static

Creates a function template backed/cached by a private property.

Definition at line 1191 of file api.cc.

◆ NewWithCFunctionOverloads()

Local< FunctionTemplate > v8::FunctionTemplate::NewWithCFunctionOverloads ( Isolate * isolate,
FunctionCallback callback = nullptr,
v8::Local< Value > data = Local<Value>(),
v8::Local< Signature > signature = Local<Signature>(),
int length = 0,
ConstructorBehavior behavior = ConstructorBehavior::kAllow,
SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
const MemorySpan< const CFunction > & c_function_overloads = {} )
static

Creates a function template for multiple overloaded fast API calls.

Definition at line 1158 of file api.cc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PrototypeTemplate()

Local< ObjectTemplate > v8::FunctionTemplate::PrototypeTemplate ( )

A PrototypeTemplate is the template used to create the prototype object of the function created by this template.

Definition at line 1014 of file api.cc.

Here is the call graph for this function:

◆ ReadOnlyPrototype()

void v8::FunctionTemplate::ReadOnlyPrototype ( )

Sets the ReadOnly flag in the attributes of the 'prototype' property of functions created from this FunctionTemplate to true.

Definition at line 1358 of file api.cc.

Here is the call graph for this function:

◆ RemovePrototype()

void v8::FunctionTemplate::RemovePrototype ( )

Removes the prototype property from functions created from this FunctionTemplate.

Definition at line 1366 of file api.cc.

Here is the call graph for this function:

◆ SealAndPrepareForPromotionToReadOnly()

void v8::FunctionTemplate::SealAndPrepareForPromotionToReadOnly ( )

Checks if the object can be promoted to read only space, seals it and prepares for promotion.

This is an experimental feature and may still change significantly.

Definition at line 7476 of file api.cc.

Here is the call graph for this function:

◆ SetAcceptAnyReceiver()

void v8::FunctionTemplate::SetAcceptAnyReceiver ( bool value)

When set to true, no access check will be performed on the receiver of a function call. Currently defaults to true, but this is subject to change.

Definition at line 1350 of file api.cc.

Here is the call graph for this function:

◆ SetCallHandler()

void v8::FunctionTemplate::SetCallHandler ( FunctionCallback callback,
v8::Local< Value > data = Local<Value>(),
SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
const MemorySpan< const CFunction > & c_function_overloads = {} )

Set the call-handler callback for a FunctionTemplate. This callback is called whenever the function created from this FunctionTemplate is called. The 'c_function' represents a fast API call, see the comment above the class declaration.

Definition at line 1216 of file api.cc.

Here is the call graph for this function:

◆ SetClassName()

void v8::FunctionTemplate::SetClassName ( Local< String > name)

Set the class name of the FunctionTemplate. This is used for printing objects created with the function created from the FunctionTemplate as its constructor.

Definition at line 1326 of file api.cc.

Here is the call graph for this function:

◆ SetExceptionContext()

void v8::FunctionTemplate::SetExceptionContext ( ExceptionContext context)

Provides information on the type of FunctionTemplate for embedder exception handling.

Definition at line 1342 of file api.cc.

Here is the call graph for this function:

◆ SetInterfaceName()

void v8::FunctionTemplate::SetInterfaceName ( Local< String > name)

Set the interface name of the FunctionTemplate. This is provided as contextual information in an ExceptionPropagationMessage to the embedder.

Definition at line 1334 of file api.cc.

Here is the call graph for this function:

◆ SetLength()

void v8::FunctionTemplate::SetLength ( int length)

Set the predefined length property for the FunctionTemplate.

Definition at line 1318 of file api.cc.

Here is the call graph for this function:

◆ SetPrototypeProviderTemplate()

void v8::FunctionTemplate::SetPrototypeProviderTemplate ( Local< FunctionTemplate > prototype_provider)

A PrototypeProviderTemplate is another function template whose prototype property is used for this template. This is mutually exclusive with setting a prototype template indirectly by calling PrototypeTemplate() or using Inherit().

Definition at line 1032 of file api.cc.

Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ Context

friend class Context
friend

Definition at line 732 of file v8-template.h.

◆ ObjectTemplate

friend class ObjectTemplate
friend

Definition at line 733 of file v8-template.h.


The documentation for this class was generated from the following files: