V8: v8::FunctionTemplate Class Reference (original) (raw)

#include <[v8-template.h](v8-template%5F8h%5Fsource.html)>

Public Member Functions
MaybeLocal< Function > GetFunction (Local< Context > context)
MaybeLocal< Object > NewRemoteInstance ()
void SetCallHandler (FunctionCallback callback, Local< Data > data={}, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const MemorySpan< const CFunction > &c_function_overloads={})
void SetLength (int length)
Local< ObjectTemplate > InstanceTemplate ()
void Inherit (Local< FunctionTemplate > parent)
Local< ObjectTemplate > PrototypeTemplate ()
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)
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 IsDictionaryTemplate () const
bool IsContext () const
bool IsCppHeapExternal () const
Static Public Member Functions
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)
static Local< FunctionTemplate > NewWithCFunctionOverloads (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< 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 FunctionTemplate * Cast (Data *data)

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));

proto_t->Set(isolate,

"proto_method",

proto_t->Set(isolate, "proto_const", v8::Number::New(isolate, 2));

instance_t->SetNativeDataProperty(

InstanceAccessorCallback);

instance_t->SetHandler(

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: v8-local-handle.h:366

static Local< Number > New(Isolate *isolate, double value)

static Local< String > NewFromUtf8Literal(Isolate *isolate, const char(&literal)[N], NewStringType type=NewStringType::kNormal)

Definition: v8-primitive.h:474

Definition: v8-template.h:692

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:

^ ^

| Inherit(Parent) | .__proto__

| |

Definition: v8-template.h:502

void Inherit(Local< FunctionTemplate > parent)

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 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.

Examples

process.cc.

Cast()

GetFunction()

Returns the unique function instance in the current execution context.

HasInstance()

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

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

Inherit()

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.

InstanceTemplate()

Get the InstanceTemplate.

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.

New()

static Local< FunctionTemplate > v8::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 ) static

Creates a function template.

Examples

shell.cc.

NewRemoteInstance()

NewWithCache()

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

NewWithCFunctionOverloads()

Creates a function template for multiple overloaded fast API calls.

PrototypeTemplate()

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

ReadOnlyPrototype()

void v8::FunctionTemplate::ReadOnlyPrototype ( )

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

RemovePrototype()

void v8::FunctionTemplate::RemovePrototype ( )

Removes the prototype property from functions created from this FunctionTemplate.

SealAndPrepareForPromotionToReadOnly()

void v8::FunctionTemplate::SealAndPrepareForPromotionToReadOnly ( )

Seal the object and mark it for promotion to read only space during context snapshot creation.

This is an experimental feature and may still change significantly.

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.

SetCallHandler()

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.

SetClassName()

SetExceptionContext()

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

SetInterfaceName()

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

SetLength()

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

SetPrototypeProviderTemplate()

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().

Context

ObjectTemplate


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