v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FunctionTemplate Class Reference

#include <v8.h>

Inheritance diagram for FunctionTemplate:
Collaboration diagram for FunctionTemplate:

Public Member Functions

Local< FunctionGetFunction ()
 
void SetCallHandler (InvocationCallback callback, Handle< Value > data=Handle< Value >())
 
Local< ObjectTemplateInstanceTemplate ()
 
void Inherit (Handle< FunctionTemplate > parent)
 
Local< ObjectTemplatePrototypeTemplate ()
 
void SetClassName (Handle< String > name)
 
void SetHiddenPrototype (bool value)
 
void ReadOnlyPrototype ()
 
bool HasInstance (Handle< Value > object)
 
- Public Member Functions inherited from Template
void Set (Handle< String > name, Handle< Data > value, PropertyAttribute attributes=None)
 
void Set (const char *name, Handle< Data > value)
 

Static Public Member Functions

static Local< FunctionTemplateNew (InvocationCallback callback=0, Handle< Value > data=Handle< Value >(), Handle< Signature > signature=Handle< Signature >())
 

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.

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("func_property", v8::Number::New(1));
proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
proto_t->Set("proto_const", v8::Number::New(2));
instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
instance_t->Set("instance_property", Number::New(3));
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;

Definition at line 2252 of file v8.h.

Member Function Documentation

Local< v8::Function > GetFunction ( )

Returns the unique function instance in the current execution context.

Definition at line 4675 of file api.cc.

References ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, Execution::InstantiateFunction(), LOG_API, ON_BAILOUT, and Utils::ToLocal().

Referenced by CreateFramePointerGrabberConstructor(), TEST(), and THREADED_TEST().

bool HasInstance ( Handle< Value object)

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

Definition at line 4690 of file api.cc.

References Object::IsInstanceOf(), and ON_BAILOUT.

void Inherit ( v8::Handle< FunctionTemplate value)

Causes the function template to inherit from a parent function template.

Definition at line 943 of file api.cc.

References ENTER_V8.

Referenced by THREADED_TEST().

Local< ObjectTemplate > InstanceTemplate ( )

Get the InstanceTemplate.

Definition at line 1122 of file api.cc.

References ObjectTemplateInfo::cast(), ENTER_V8, v8::internal::instance_template, ObjectTemplate::New(), and Utils::ToLocal().

Referenced by TEST(), and THREADED_TEST().

Local< ObjectTemplate > PrototypeTemplate ( )

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

Definition at line 928 of file api.cc.

References ENTER_V8, and ObjectTemplate::New().

Referenced by TEST(), and THREADED_TEST().

void ReadOnlyPrototype ( )

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

Definition at line 1157 of file api.cc.

References ENTER_V8.

void SetCallHandler ( InvocationCallback  callback,
v8::Handle< Value data = Handle<Value>() 
)

Set the call-handler callback for a FunctionTemplate. This callback is called whenever the function created from this FunctionTemplate is called.

Definition at line 1051 of file api.cc.

References v8::internal::CALL_HANDLER_INFO_TYPE, Handle< T >::cast(), ENTER_V8, Isolate::factory(), Handle< T >::IsEmpty(), Factory::NewStruct(), SET_FIELD_WRAPPED, and v8::Undefined().

void SetClassName ( Handle< 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 1139 of file api.cc.

References ENTER_V8.

Referenced by CreateFramePointerGrabberConstructor(), TEST(), and THREADED_TEST().

void SetHiddenPrototype ( bool  value)

Determines whether the proto accessor ignores instances of the function template. If instances of the function template are ignored, proto skips all instances and instead returns the next object in the prototype chain.

Call with a value of true to make the proto accessor ignore instances of the function template. Call with a value of false to make the proto accessor not ignore instances of the function template. By default, instances of a function template are not ignored.

Definition at line 1147 of file api.cc.

References ENTER_V8.

Friends And Related Function Documentation

friend class Context
friend

Definition at line 2340 of file v8.h.

friend class ObjectTemplate
friend

Definition at line 2341 of file v8.h.


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