[][src]Struct rusty_v8_m::FunctionTemplate

#[repr(C)]pub struct FunctionTemplate(_);

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:

This example is not tested
   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->SetAccessor(
       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:

This example is not tested
  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:

This example is not tested
  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:

This example is not tested
  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:

This example is not tested
  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.

Methods

impl FunctionTemplate[src]

pub fn new<'sc>(
    scope: &mut impl ToLocal<'sc>,
    callback: impl MapFnTo<FunctionCallback>
) -> Local<'sc, FunctionTemplate>
[src]

Creates a function template.

pub fn get_function<'sc>(
    &mut self,
    scope: &mut impl ToLocal<'sc>,
    context: Local<Context>
) -> Option<Local<'sc, Function>>
[src]

Returns the unique function instance in the current execution context.

pub fn set_class_name(&mut self, name: Local<String>)[src]

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

Methods from Deref<Target = Template>

pub fn set(&self, key: Local<Name>, value: Local<Data>)[src]

Adds a property to each instance created by this template.

pub fn set_with_attr(
    &self,
    key: Local<Name>,
    value: Local<Data>,
    attr: PropertyAttribute
)
[src]

Adds a property to each instance created by this template with the specified property attributes.

Trait Implementations

impl Deref for FunctionTemplate[src]

type Target = Template

The resulting type after dereferencing.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.