FunctionTemplate with accessors

35 views
Skip to first unread message

TB

unread,
Jul 20, 2009, 1:00:14 PM7/20/09
to v8-users
Hi,

I'm trying to make a function-template that apart from having some
default functions or values on new instances and the prototype also
has some default properties (getters, to be more specific) on the
actual function object.
Something like the following Javascript example:

function SillyString(str) {
this.str=str;
return this;
}
SillyString.prototype.silly=function() {
print("Silly"+this.str);
}
//This is the part that I can't find out:
SillyString.__defineGetter__("default",function() { return new
SillyString("String"); });
//Usage example
var y=new SillyString("HelloWorld"); //<-- this should be possible
var y=SillyString.default; //<-- Should return the equivalent of new
SillyString("String");

Would anyone have any idea how to go about doing this ? I've tried
using just an ObjectTemplate with a SetCallAsFunctionHandler, but that
throws a type error when used with "new".

Thanks,
TB

TB

unread,
Jul 20, 2009, 1:41:48 PM7/20/09
to v8-users
I just found a possible solution to this problem. I feel it's a bit
hack-ish, but it works, so I'm not really complaining.
The trick is to create an ObjectTemplate with the appropriate getters
or properties, and then do:
funcTemplate->Set(v8::String::New("__proto__"),objTemplate);

I honestly don't know how I couldn't have thought of that before,
guess I was too caught up in reading all the definitions and
implementation of FunctionTemplate to notice a solution this trivial.

Dean McNamee

unread,
Jul 20, 2009, 1:51:29 PM7/20/09
to v8-u...@googlegroups.com
/**
* A PrototypeTemplate is the template used to create the prototype object
* of the function created by this template.
*/
Local<ObjectTemplate> PrototypeTemplate();

Matthias Ernst

unread,
Jul 20, 2009, 1:52:17 PM7/20/09
to v8-u...@googlegroups.com
On Mon, Jul 20, 2009 at 7:41 PM, TB<theb...@gmail.com> wrote:
>
> I just found a possible solution to this problem. I feel it's a bit
> hack-ish, but it works, so I'm not really complaining.
> The trick is to create an ObjectTemplate with the appropriate getters
> or properties, and then do:
> funcTemplate->Set(v8::String::New("__proto__"),objTemplate);

You should just as well be able to Set these properties on the
funcTemplate instead.
Or even on the funcTemplate->GetFunction() object, since there is only
gonna be one object created from that template anyway.

TB

unread,
Jul 20, 2009, 1:56:09 PM7/20/09
to v8-users
The "prototype" property is the __proto__ property for the objects
created with that function as constructor, not the __proto__ property
of that function itself.
e.g.:
function f() { return this; };
var a=new f();
f.prototype.test="Value";

Would set a.test but not f.test as intended.

TB

unread,
Jul 20, 2009, 1:57:37 PM7/20/09
to v8-users
Yes, Set works for standard properties, but not when you need
accessors.
I needed getters, hence the __proto__ trick. I still don't know how to
do setters though, but luckily I don't need those (for now).

On Jul 20, 7:52 pm, Matthias Ernst <ernst.matth...@gmail.com> wrote:

Christian Plesner Hansen

unread,
Jul 21, 2009, 4:48:31 AM7/21/09
to v8-u...@googlegroups.com
Accessors on functions are indeed not supported except through
workarounds like the __proto__ trick. See
http://groups.google.com/group/v8-users/browse_thread/thread/ab5fefdb4b31b22.

TB

unread,
Jul 21, 2009, 9:27:15 AM7/21/09
to v8-users
Hmm, I haven't tested this from the C++ API, but with the V8 shell
doing this:
var o={};
o.__proto__.__defineSetter("x",function(v) { print("New x value:
"+v); });
o.x=10;
will actually print "New x value: 10".

Seems to me that setters defined on the object proto are also being
called... Weird behaviour though, not sure it should work like that.

Christian Plesner Hansen

unread,
Jul 27, 2009, 4:39:51 AM7/27/09
to v8-u...@googlegroups.com
> Hmm, I haven't tested this from the C++ API, but with the V8 shell
> doing this:
> var o={};
> o.__proto__.__defineSetter("x",function(v) { print("New x value:
> "+v); });
> o.x=10;
> will actually print "New x value: 10".
>
> Seems to me that setters defined on the object proto are also being
> called... Weird behaviour though, not sure it should work like that.

That's the intended behavior. We have to do it that way to be
compatible with other browsers.

Reply all
Reply to author
Forward
0 new messages