V8 Project
v8::FunctionTemplate Class Reference

A FunctionTemplate is used to create functions at runtime. More...

#include <v8.h>

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

Public Member Functions

Local< FunctionGetFunction ()
 Returns the unique function instance in the current execution context. More...
 
void SetCallHandler (FunctionCallback callback, Handle< Value > data=Handle< Value >())
 Set the call-handler callback for a FunctionTemplate. More...
 
void SetLength (int length)
 Set the predefined length property for the FunctionTemplate. More...
 
Local< ObjectTemplateInstanceTemplate ()
 Get the InstanceTemplate. More...
 
void Inherit (Handle< FunctionTemplate > parent)
 Causes the function template to inherit from a parent function template. More...
 
Local< ObjectTemplatePrototypeTemplate ()
 A PrototypeTemplate is the template used to create the prototype object of the function created by this template. More...
 
void SetClassName (Handle< String > name)
 Set the class name of the FunctionTemplate. More...
 
void SetHiddenPrototype (bool value)
 Determines whether the proto accessor ignores instances of the function template. More...
 
void ReadOnlyPrototype ()
 Sets the ReadOnly flag in the attributes of the 'prototype' property of functions created from this FunctionTemplate to true. More...
 
void RemovePrototype ()
 Removes the prototype property from functions created from this FunctionTemplate. More...
 
bool HasInstance (Handle< Value > object)
 Returns true if the given object is an instance of this function template. More...
 
- Public Member Functions inherited from v8::Template
void Set (Handle< Name > name, Handle< Data > value, PropertyAttribute attributes=None)
 Adds a property to each instance created by this template. More...
 
void Set (Isolate *isolate, const char *name, Handle< Data > value)
 
void SetAccessorProperty (Local< Name > name, Local< FunctionTemplate > getter=Local< FunctionTemplate >(), Local< FunctionTemplate > setter=Local< FunctionTemplate >(), PropertyAttribute attribute=None, AccessControl settings=DEFAULT)
 
void SetNativeDataProperty (Local< String > name, AccessorGetterCallback getter, AccessorSetterCallback setter=0, Handle< Value > data=Handle< Value >(), PropertyAttribute attribute=None, Local< AccessorSignature > signature=Local< AccessorSignature >(), AccessControl settings=DEFAULT)
 Whenever the property with the given name is accessed on objects created from this Template the getter and setter callbacks are called instead of getting and setting the property directly on the JavaScript object. More...
 
void SetNativeDataProperty (Local< Name > name, AccessorNameGetterCallback getter, AccessorNameSetterCallback setter=0, Handle< Value > data=Handle< Value >(), PropertyAttribute attribute=None, Local< AccessorSignature > signature=Local< AccessorSignature >(), AccessControl settings=DEFAULT)
 
bool SetDeclaredAccessor (Local< Name > name, Local< DeclaredAccessorDescriptor > descriptor, PropertyAttribute attribute=None, Local< AccessorSignature > signature=Local< AccessorSignature >(), AccessControl settings=DEFAULT)
 

Static Public Member Functions

static Local< FunctionTemplateNew (Isolate *isolate, FunctionCallback callback=0, Handle< Value > data=Handle< Value >(), Handle< Signature > signature=Handle< Signature >(), int length=0)
 Creates a function template. More...
 

Private Member Functions

 FunctionTemplate ()
 

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));
v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
proto_t->Set("proto_const", v8::Number::New(2));
v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
instance_t->Set("instance_property", Number::New(3));
v8::Local<v8::Function> function = t->GetFunction();
v8::Local<v8::Object> instance = function->NewInstance();
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=0, Handle< Value > data=Handle< Value >(), Handle< Signature > signature=Handle< Signature >(), int length=0)
Creates a function template.
Definition: api.cc:904
A light-weight stack-allocated object handle.
Definition: v8.h:334
static Local< Number > New(Isolate *isolate, double value)
Definition: api.cc:6268

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(Handle< FunctionTemplate > parent)
Causes the function template to inherit from a parent function template.
Definition: api.cc:864

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 3640 of file v8.h.

Constructor & Destructor Documentation

◆ FunctionTemplate()

v8::FunctionTemplate::FunctionTemplate ( )
private

Member Function Documentation

◆ GetFunction()

Local< v8::Function > v8::FunctionTemplate::GetFunction ( )

Returns the unique function instance in the current execution context.

Definition at line 5327 of file api.cc.

5327  {
5328  i::Isolate* isolate = i::Isolate::Current();
5329  ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()",
5330  return Local<v8::Function>());
5331  LOG_API(isolate, "FunctionTemplate::GetFunction");
5332  ENTER_V8(isolate);
5333  EXCEPTION_PREAMBLE(isolate);
5335  has_pending_exception = !i::Execution::InstantiateFunction(
5336  Utils::OpenHandle(this)).ToHandle(&obj);
5337  EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>());
5339 }
#define ON_BAILOUT(isolate, location, code)
Definition: api.cc:60
#define LOG_API(isolate, expr)
Definition: api.cc:52
#define EXCEPTION_BAILOUT_CHECK(isolate, value)
Definition: api.cc:93
#define ENTER_V8(isolate)
Definition: api.cc:54
#define EXCEPTION_PREAMBLE(isolate)
Definition: api.cc:67
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:288

References ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, LOG_API, ON_BAILOUT, v8::Utils::OpenHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ HasInstance()

bool v8::FunctionTemplate::HasInstance ( Handle< Value object)

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

Definition at line 5342 of file api.cc.

5342  {
5343  ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()",
5344  return false);
5345  i::Object* obj = *Utils::OpenHandle(*value);
5346  return Utils::OpenHandle(this)->IsTemplateFor(obj);
5347 }

References ON_BAILOUT, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ Inherit()

void v8::FunctionTemplate::Inherit ( v8::Handle< FunctionTemplate value)

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

Definition at line 864 of file api.cc.

864  {
865  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
866  ENTER_V8(isolate);
867  Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
868 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ InstanceTemplate()

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

Get the InstanceTemplate.

Definition at line 1181 of file api.cc.

1181  {
1183  if (!Utils::ApiCheck(!handle.is_null(),
1184  "v8::FunctionTemplate::InstanceTemplate()",
1185  "Reading from empty handle")) {
1186  return Local<ObjectTemplate>();
1187  }
1188  i::Isolate* isolate = handle->GetIsolate();
1189  ENTER_V8(isolate);
1190  if (handle->instance_template()->IsUndefined()) {
1191  Local<ObjectTemplate> templ =
1192  ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1193  handle->set_instance_template(*Utils::OpenHandle(*templ));
1194  }
1196  i::ObjectTemplateInfo::cast(handle->instance_template()));
1197  return Utils::ToLocal(result);
1198 }
static Local< ObjectTemplate > New()
Definition: api.cc:1244
static bool ApiCheck(bool condition, const char *location, const char *message)
Definition: api.h:177
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:146

References v8::Utils::ApiCheck(), ENTER_V8, v8::internal::handle(), v8::ObjectTemplate::New(), v8::Utils::OpenHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ New()

Local< FunctionTemplate > v8::FunctionTemplate::New ( Isolate isolate,
FunctionCallback  callback = 0,
v8::Handle< Value data = Handle<Value>(),
v8::Handle< Signature signature = Handle<Signature>(),
int  length = 0 
)
static

Creates a function template.

Definition at line 904 of file api.cc.

909  {
910  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
911  LOG_API(i_isolate, "FunctionTemplate::New");
912  ENTER_V8(i_isolate);
913  return FunctionTemplateNew(
914  i_isolate, callback, data, signature, length, false);
915 }
static Local< FunctionTemplate > FunctionTemplateNew(i::Isolate *isolate, FunctionCallback callback, v8::Handle< Value > data, v8::Handle< Signature > signature, int length, bool do_not_cache)
Definition: api.cc:871

References ENTER_V8, v8::FunctionTemplateNew(), and LOG_API.

Referenced by v8::Shell::AddOSMethods(), v8::Shell::CreateGlobalTemplate(), v8::EnsureConstructor(), v8::internal::ExternalizeStringExtension::GetNativeFunctionTemplate(), v8::internal::FreeBufferExtension::GetNativeFunctionTemplate(), v8::internal::GCExtension::GetNativeFunctionTemplate(), v8::internal::StatisticsExtension::GetNativeFunctionTemplate(), and v8::internal::TriggerFailureExtension::GetNativeFunctionTemplate().

+ 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 850 of file api.cc.

850  {
851  i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
852  ENTER_V8(i_isolate);
854  i_isolate);
855  if (result->IsUndefined()) {
856  v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(i_isolate);
857  result = Utils::OpenHandle(*ObjectTemplate::New(isolate));
858  Utils::OpenHandle(this)->set_prototype_template(*result);
859  }
860  return ToApiHandle<ObjectTemplate>(result);
861 }
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
kSerializedDataOffset prototype_template
Definition: objects-inl.h:5322

References ENTER_V8, v8::ObjectTemplate::New(), v8::Utils::OpenHandle(), and v8::internal::prototype_template.

+ 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 1222 of file api.cc.

1222  {
1223  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1224  ENTER_V8(isolate);
1225  Utils::OpenHandle(this)->set_read_only_prototype(true);
1226 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ 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 1229 of file api.cc.

1229  {
1230  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1231  ENTER_V8(isolate);
1232  Utils::OpenHandle(this)->set_remove_prototype(true);
1233 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ SetCallHandler()

void v8::FunctionTemplate::SetCallHandler ( FunctionCallback  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 1107 of file api.cc.

1108  {
1109  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1110  ENTER_V8(isolate);
1111  i::HandleScope scope(isolate);
1112  i::Handle<i::Struct> struct_obj =
1113  isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1116  SET_FIELD_WRAPPED(obj, set_callback, callback);
1117  if (data.IsEmpty()) {
1118  data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1119  }
1120  obj->set_data(*Utils::OpenHandle(*data));
1121  Utils::OpenHandle(this)->set_call_code(*obj);
1122 }
#define SET_FIELD_WRAPPED(obj, setter, cdata)
Definition: api.cc:1101
bool IsEmpty() const
Returns true if the handle is empty.
Definition: v8.h:228
static Handle< T > cast(Handle< S > that)
Definition: handles.h:116
Factory * factory()
Definition: isolate.h:982
@ CALL_HANDLER_INFO_TYPE
Definition: objects.h:702
Handle< Primitive > Undefined(Isolate *isolate)
Definition: v8.h:6836

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

+ Here is the call graph for this function:

◆ SetClassName()

void v8::FunctionTemplate::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 1208 of file api.cc.

1208  {
1209  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1210  ENTER_V8(isolate);
1211  Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
1212 }
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name

References ENTER_V8, name, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ SetHiddenPrototype()

void v8::FunctionTemplate::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 1215 of file api.cc.

1215  {
1216  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1217  ENTER_V8(isolate);
1218  Utils::OpenHandle(this)->set_hidden_prototype(value);
1219 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ 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 1201 of file api.cc.

1201  {
1202  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1203  ENTER_V8(isolate);
1204  Utils::OpenHandle(this)->set_length(length);
1205 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ Context

friend class Context
friend

Definition at line 3717 of file v8.h.

◆ ObjectTemplate

friend class ObjectTemplate
friend

Definition at line 3718 of file v8.h.


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