Function templates versus Object templates in V8

589 views
Skip to first unread message

Prahalad Deshpande

unread,
Aug 1, 2010, 6:52:34 AM8/1/10
to v8-users
Hi,

I was reading the Embedders guide on the Wiki and came across the
concept of FunctionTemplates and ObjectTemplates.

Could someone provide an explanation about them annotated with
examples? Somehow I felt that the documentation in the Wiki did not
make the distinction clear.

Basically, when will we want to go for a FunctionTemplate and when do
we use an ObjectTemplate?

Thanks
-Prahalad

Matthias Ernst

unread,
Aug 1, 2010, 7:33:52 AM8/1/10
to v8-u...@googlegroups.com

christy

unread,
Aug 2, 2010, 6:29:55 PM8/2/10
to v8-users
I have a question can FunctionTemplates be used like an
ObjectTemplate?

In other words it is possible for a FunctionTemplate to do something
like this...

V8FunctionTemplateHandle functionTemplateHandle =
V8FunctionTemplate::New(InternalConstructor, external);
V8ObjectTemplateHandle instanceTemplate = new functionTemplateHandle-
>InstanceTemplate();
V8ObjectHandle instance = instanceTemplate->NewInstance();
instance->SetInternalField(0, external);
global->Set(V8Converter::ToV8String(Name), v8Instance);

Essentially what I want to do is register a single instance of the
Function Template and "hide" the constructor by not registering it in
the context.

Since I'm registering a single instance to the context it seems to me
the instance should behave as if I used an ObjectTemplate.

If this behavior is supported than that would make coding for what I'm
doing easier since I can internally treat all objects being created
from FunctionTemplate.

Thanks.

Matthias Ernst

unread,
Aug 3, 2010, 1:56:40 AM8/3/10
to v8-u...@googlegroups.com
On Tue, Aug 3, 2010 at 12:29 AM, christy <christy...@gmail.com> wrote:
> I have a question can FunctionTemplates be used like an
> ObjectTemplate?
>
> In other words it is possible for a FunctionTemplate to do something
> like this...
>
> V8FunctionTemplateHandle functionTemplateHandle =
> V8FunctionTemplate::New(InternalConstructor, external);
> V8ObjectTemplateHandle instanceTemplate = new functionTemplateHandle-
>>InstanceTemplate();
> V8ObjectHandle instance = instanceTemplate->NewInstance();
> instance->SetInternalField(0, external);
> global->Set(V8Converter::ToV8String(Name), v8Instance);
>
> Essentially what I want to do is register a single instance of the
> Function Template and "hide" the constructor by not registering it in
> the context.

Absolutely. FunctionTemplate#getFunction#newInstance and
FunctionTemplate#getInstanceTemplate#newInstance are equivalent.

But note that the "constructor" property of the object will still be
available to JS code to call.

>
> Since I'm registering a single instance to the context it seems to me
> the instance should behave as if I used an ObjectTemplate.
>
> If this behavior is supported than that would make coding for what I'm
> doing easier since I can internally treat all objects being created
> from FunctionTemplate.
>
> Thanks.
>

christy

unread,
Aug 3, 2010, 9:53:19 PM8/3/10
to v8-users
Matthias,

Thanks for your response. I can live with the constructor property
on the object since if the constructor property is called it will only
return a reference of the same object so there is no harm.. Using
only the FunctionTemplate for what I'm doing makes the code cleaner.

Thanks again.

Reply all
Reply to author
Forward
0 new messages