Skip to Content
V8-wasm
API Reference

V8 Templates & Callbacks

V8 Template, ObjectTemplate, FunctionTemplate, Signature, and property/interceptor callback configurations

Generated TypeScript bindings for defining host-backed object shapes, prototypes, function templates, accessors, and interceptors in V8.

Contains 13 classes and 34 bound methods. Inherited WasmHandle base methods (ptr, isNull(), as()) are documented on Core Runtime & Realms.

DictionaryTemplate

class DictionaryTemplate extends Data

A template to create dictionary objects at runtime.

Upstream C++: v8::DictionaryTemplate (v8-template.h)

Static Methods

  • static fromPtr(ptr: number): DictionaryTemplate

    Wraps a raw V8-wasm handle pointer as a DictionaryTemplate.

FunctionTemplate

class FunctionTemplate extends Template

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

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.

Upstream C++: v8::FunctionTemplate (v8-template.h)

Static Methods

  • static fromPtr(ptr: number): FunctionTemplate

    Wraps a raw V8-wasm handle pointer as a FunctionTemplate.

Methods

  • getFunction(context: Context): MaybeLocal<V8Function>

    Returns the unique function instance in the current execution context.

    Upstream C++: v8::FunctionTemplate::GetFunction

  • hasInstance(object: Value): boolean

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

    Upstream C++: v8::FunctionTemplate::HasInstance

  • inherit(parent: FunctionTemplate): void

    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.

    Upstream C++: v8::FunctionTemplate::Inherit

  • instanceTemplate(): ObjectTemplate

    Get the InstanceTemplate.

    Upstream C++: v8::FunctionTemplate::InstanceTemplate

  • isLeafTemplateForApiObject(value: Value): boolean

    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.

    Upstream C++: v8::FunctionTemplate::IsLeafTemplateForApiObject

  • newRemoteInstance(): MaybeLocal<V8Object>

    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.

    Upstream C++: v8::FunctionTemplate::NewRemoteInstance

  • prototypeTemplate(): ObjectTemplate

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

    Upstream C++: v8::FunctionTemplate::PrototypeTemplate

  • readOnlyPrototype(): void

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

    Upstream C++: v8::FunctionTemplate::ReadOnlyPrototype

  • removePrototype(): void

    Removes the prototype property from functions created from this FunctionTemplate.

    Upstream C++: v8::FunctionTemplate::RemovePrototype

  • sealAndPrepareForPromotionToReadOnly(): void

    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.

    Upstream C++: v8::FunctionTemplate::SealAndPrepareForPromotionToReadOnly

  • setAcceptAnyReceiver(value: boolean): void

    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.

    Upstream C++: v8::FunctionTemplate::SetAcceptAnyReceiver

  • setClassName(name: V8String): void

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

    Upstream C++: v8::FunctionTemplate::SetClassName

  • setExceptionContext(context: ExceptionContext): void

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

    Upstream C++: v8::FunctionTemplate::SetExceptionContext

  • setInterfaceName(name: V8String): void

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

    Upstream C++: v8::FunctionTemplate::SetInterfaceName

  • setLength(length: number): void

    Set the predefined length property for the FunctionTemplate.

    Upstream C++: v8::FunctionTemplate::SetLength

  • setPrototypeProviderTemplate(prototype_provider: FunctionTemplate): void

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

    Upstream C++: v8::FunctionTemplate::SetPrototypeProviderTemplate

IndexedPropertyHandlerConfiguration

class IndexedPropertyHandlerConfiguration extends WasmHandle

Upstream V8 C++ struct v8::IndexedPropertyHandlerConfiguration (v8-template.h).

JitCodeEvent

class JitCodeEvent extends WasmHandle

A JIT code event is issued each time code is added, moved or removed.

Note: removal events are not currently issued.

Upstream C++: v8::JitCodeEvent (v8-callbacks.h)

JitCodeEventline_info_t

class JitCodeEventline_info_t extends WasmHandle

Upstream V8 C++ struct v8::JitCodeEvent::line_info_t (v8-callbacks.h).

JitCodeEventname_t

class JitCodeEventname_t extends WasmHandle

Upstream V8 C++ struct v8::JitCodeEvent::name_t (v8-callbacks.h).

JitCodeEventwasm_source_info_t

class JitCodeEventwasm_source_info_t extends WasmHandle

Upstream V8 C++ struct v8::JitCodeEvent::wasm_source_info_t (v8-callbacks.h).

ModifyCodeGenerationFromStringsResult

class ModifyCodeGenerationFromStringsResult extends WasmHandle

--- Modify Code Generation From Strings Callback ---

Upstream C++: v8::ModifyCodeGenerationFromStringsResult (v8-callbacks.h)

NamedPropertyHandlerConfiguration

class NamedPropertyHandlerConfiguration extends WasmHandle

Upstream V8 C++ struct v8::NamedPropertyHandlerConfiguration (v8-template.h).

ObjectTemplate

class ObjectTemplate extends Template

An ObjectTemplate is used to create objects at runtime.

Properties added to an ObjectTemplate are added to each object created from the ObjectTemplate.

Upstream C++: v8::ObjectTemplate (v8-template.h)

Static Methods

  • static fromPtr(ptr: number): ObjectTemplate

    Wraps a raw V8-wasm handle pointer as a ObjectTemplate.

  • static new(isolate: Isolate, constructor?: FunctionTemplate): ObjectTemplate

    Creates an ObjectTemplate.

    Upstream C++: v8::ObjectTemplate::New

Methods

  • internalFieldCount(): number

    Gets the number of internal fields for objects generated from this template.

    Upstream C++: v8::ObjectTemplate::InternalFieldCount

  • isCodeLike(): boolean

    Binds upstream V8 C++ v8::ObjectTemplate::IsCodeLike.

  • isImmutableProto(): boolean

    Returns true if the object will be an immutable prototype exotic object.

    Upstream C++: v8::ObjectTemplate::IsImmutableProto

  • markAsUndetectable(): void

    Mark object instances of the template as undetectable.

    In many ways, undetectable objects behave as though they are not there. They behave like 'undefined' in conditionals and when printed. However, properties can be accessed and called as on normal objects.

    Upstream C++: v8::ObjectTemplate::MarkAsUndetectable

  • newInstance(context: Context): MaybeLocal<V8Object>

    Creates a new instance of this template.

    • context: The context in which the instance is created.

    Upstream C++: v8::ObjectTemplate::NewInstance

  • sealAndPrepareForPromotionToReadOnly(): void

    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.

    Upstream C++: v8::ObjectTemplate::SealAndPrepareForPromotionToReadOnly

  • setCodeLike(): void

    Support for TC39 "dynamic code brand checks" proposal.

    This API allows to mark (& query) objects as "code like", which causes them to be treated like Strings in the context of eval and function constructor.

    Reference: https://github.com/tc39/proposal-dynamic-code-brand-checks

    Upstream C++: v8::ObjectTemplate::SetCodeLike

  • setHandler(configuration: NamedPropertyHandlerConfiguration): void

    Sets a named property handler on the object template.

    Whenever a property whose name is a string or a symbol is accessed on objects created from this object template, the provided callback is invoked instead of accessing the property directly on the JavaScript object.

    • configuration: The NamedPropertyHandlerConfiguration that defines the callbacks to invoke when accessing a property.

    Upstream C++: v8::ObjectTemplate::SetHandler

  • setHandler2(configuration: IndexedPropertyHandlerConfiguration): void

    Sets an indexed property handler on the object template.

    Whenever an indexed property is accessed on objects created from this object template, the provided callback is invoked instead of accessing the property directly on the JavaScript object.

    • configuration: The IndexedPropertyHandlerConfiguration that defines the callbacks to invoke when accessing a property.

    Upstream C++: v8::ObjectTemplate::SetHandler

  • setImmutableProto(): void

    Makes the ObjectTemplate for an immutable prototype exotic object, with an immutable proto.

    Upstream C++: v8::ObjectTemplate::SetImmutableProto

  • setInternalFieldCount(value: number): void

    Sets the number of internal fields for objects generated from this template.

    Upstream C++: v8::ObjectTemplate::SetInternalFieldCount

OOMDetails

class OOMDetails extends WasmHandle

Upstream V8 C++ struct v8::OOMDetails (v8-callbacks.h).

Signature

class Signature extends Data

A Signature specifies which receiver is valid for a function.

A receiver matches a given signature if the receiver (or any of its hidden prototypes) was created from the signature's FunctionTemplate, or from a FunctionTemplate that inherits directly or indirectly from the signature's FunctionTemplate.

Upstream C++: v8::Signature (v8-template.h)

Static Methods

  • static fromPtr(ptr: number): Signature

    Wraps a raw V8-wasm handle pointer as a Signature.

  • static new(isolate: Isolate, receiver?: FunctionTemplate): Signature

    Binds upstream V8 C++ v8::Signature::New.

Template

class Template extends Data

The superclass of object and function templates.

Upstream C++: v8::Template (v8-template.h)

Static Methods

  • static fromPtr(ptr: number): Template

    Wraps a raw V8-wasm handle pointer as a Template.

Methods

  • set(name: Name, value: Data, attributes: PropertyAttribute): void

    Adds a property to each instance created by this template.

    The property must be defined either as a primitive value, or a template.

    Upstream C++: v8::Template::Set

  • set2(isolate: Isolate, name: string | null, value: Data, attributes: PropertyAttribute): void

    Adds a property to each instance created by this template.

    The property must be defined either as a primitive value, or a template.

    Upstream C++: v8::Template::Set

  • setAccessorProperty(name: Name, getter: FunctionTemplate, setter: FunctionTemplate, attribute: PropertyAttribute): void

    Sets an "accessor property" on the object template, see https://tc39.es/ecma262/#sec-object-type.

    Whenever the property with the given name is accessed on objects created from this ObjectTemplate the getter and setter functions are called.

    • name: The name of the property for which an accessor is added.
    • getter: The callback to invoke when getting the property.
    • setter: The callback to invoke when setting the property.
    • attribute: The attributes of the property for which an accessor is added.

    Upstream C++: v8::Template::SetAccessorProperty

  • setIntrinsicDataProperty(name: Name, intrinsic: Intrinsic, attribute: PropertyAttribute): void

    During template instantiation, sets the value with the intrinsic property from the correct context.

    Upstream C++: v8::Template::SetIntrinsicDataProperty

  • setPrivate(name: Private, value: Data, attributes: PropertyAttribute): void

    Binds upstream V8 C++ v8::Template::SetPrivate.

On this page