ObjectTemplate vs FunctionTemplate

1,301 views
Skip to first unread message

christy

unread,
Jul 7, 2010, 3:52:13 PM7/7/10
to v8-users, chr...@coretech.co.nz
I'm confused how to use these two template. I've looked at two
examples where there seems to be contradictory information. I want to
create to expose an object from C++ to Javascript but the user should
be able in Javascript to construct the object and to extend the object
in Javascript.

In one example the guidance is to use two FunctionTemplates: one to
represent the object and the other to represent the constructor. In
another example the ObjectTemplate::SetCallAsFunctionHandler method is
used to handle object construction. However the PrototypeTemplate is
a member of the FuncitonTemplate. It would seem to me that
ObjectTemplate should be a derive class of FunctionTemplate but I'm
really confused as which one should be used to expose C++ object to
Javascript.

Can someone provide some guidance and provide an example. Also if you
use a code snippet please explain the object construction. I've seen
snippet where variable are introduced without any prior reference and
that severely confused me since I couldn't see how the variable was
constructed.

Thanks in advance
christy

Matthias Ernst

unread,
Jul 8, 2010, 3:57:41 AM7/8/10
to v8-u...@googlegroups.com, chr...@coretech.co.nz
In short:

ObjectTemplate gives you JS objects without a dedicated constructor
function and prototype. They use Object[.prototype]. Since you can
still attach property handlers, they can be useful. FunctionTemplates
give you the additional benefit that they provide a constructor
function for use from JS code and JS code can add methods to the
prototype.

To study a working example that shows what you need to do for your
case, including JS->native reference management, check out

http://github.com/ry/node/blob/master/src/node_object_wrap.h
http://github.com/ry/node/blob/master/src/node_buffer.{h,cc}

Matthias

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

christy

unread,
Jul 12, 2010, 7:11:25 PM7/12/10
to v8-users
Matthias,

Thanks for your response. I was able to figure it out by cobbling
together other samples that I found. It seems where you want to use a
constructor you use a FunctionTemplate to define the constructor.
That in turns has a prototypeTemplate which is an ObjectTemplate.
From there I was able to set up the bridge between the C++ class and
Javascript.

On Jul 8, 7:57 pm, Matthias Ernst <matth...@mernst.org> wrote:
> In short:
>
> ObjectTemplate gives you JS objects without a dedicated constructor
> function and prototype. They use Object[.prototype]. Since you can
> still attach property handlers, they can be useful. FunctionTemplates
> give you the additional benefit that they provide a constructor
> function for use from JS code and JS code can add methods to the
> prototype.
>
> To study a working example that shows what you need to do for your
> case, including JS->native reference management, check out
>
> http://github.com/ry/node/blob/master/src/node_object_wrap.hhttp://github.com/ry/node/blob/master/src/node_buffer.{h,cc}
>
> Matthias
Reply all
Reply to author
Forward
0 new messages