Class static member variable getter/setter setup.

711 views
Skip to first unread message

vlad

unread,
Nov 15, 2008, 3:26:15 PM11/15/08
to v8-users
Hi,

I've been trying to add static variables getter/setter to function
templates, but have not found the proper way to do it.
I have successfully added instance getters/setters and instance
variable callbacks. Also, I was able to get the static function calls
working.

Let's assume class Foo;

// I have successfully installed C++ callbacks for objects in the
following statements:
var f = new Foo;
f.someVar = x;
var y = f.someVar;
f.bar();
Foo.staticCall();

But I have not been able to do this:
Foo.staticVariable = x;
var y = Foo.staticVariable;

I've been looking all day at v8.h,, process.cc, this mailing list, but
have not found the proper code snippets to make it happen.

This is an example of how I'm setting the above statements that work
-----------------------
// Create the Javascript:Camera class.
v8::Handle<v8::FunctionTemplate> cameraFunctionTpl =
v8::FunctionTemplate::New(jsi_Camera_construct);
v8::Handle<v8::ObjectTemplate> cameraInstanceTpl = cameraFunctionTpl-
>InstanceTemplate();

// Image = Camera.snapshot()
cameraFunctionTpl->Set(v8::String::New("snapshot"),
v8::FunctionTemplate::New(jsi_Camera_snapshot));

// Set the name of the class and a slot for the C++ class that will
hold it's data.
cameraFunctionTpl->SetClassName(v8::String::New("Camera"));
cameraInstanceTpl->SetInternalFieldCount(1); // create an internal
field for the C++ object

// $camera.target = node:Node; node:Node = $camera.target;
cameraFunctionTpl->PrototypeTemplate()->SetAccessor(v8::String::New
("target"), jsi_Camera_get_target, jsi_Camera_set_target);

// Add the Camera class template to the clobal object template.
global->Set(v8::String::New("Camera"), cameraFunctionTpl);
----------------------

Also, if does anyone know the difference, functionally, between:
cameraFunctionTpl->PrototypeTemplate()->SetAccessor(v8::String::New
("target"), jsi_Camera_get_target, jsi_Camera_set_target);
-- and --
cameraFunctionTpl->InstanceTemplate()->SetAccessor(v8::String::New
("target"), jsi_Camera_get_target, jsi_Camera_set_target);



Any help is appreciated.

Thanks,
Vlad

vlad

unread,
Nov 20, 2008, 9:57:53 AM11/20/08
to v8-users
Is there anyone out there that knows how to do this, or if this is
even possible?

august

unread,
Nov 20, 2008, 10:13:16 AM11/20/08
to v8-u...@googlegroups.com

vlad,

I think the attached example is what you want. But, I can't say if
it's the "proper" way as I'm still learning the api. This was just
one of my test files.

-august.


>
> Is there anyone out there that knows how to do this, or if this is
> even possible?
>

testv8.cpp
test.js

august

unread,
Nov 20, 2008, 10:19:39 AM11/20/08
to v8-u...@googlegroups.com

whoops, sorry. I jumped the gun on that last email. I don't know set
accesors for static vars.

nsab...@gmail.com

unread,
Dec 18, 2008, 1:01:21 PM12/18/08
to v8-users
Hi,

I'm trying to do the same. Vlad, did you ever figure it out?

Cheers,
Neb

Christian Plesner Hansen

unread,
Dec 18, 2008, 2:00:54 PM12/18/08
to v8-u...@googlegroups.com
V8 doesn't support this directly but I can think of two ways to get
something approaching accessors on functions. If you only need a
getter it can be implemented by replacing the __proto__ of the
function with an object that has the getter. That won't work for
setters though. Alternatively, if you don't need to be able to call
the function as a constructor you can use an object with a
CallAsFunction handler instead of a real function.

vlad

unread,
Jan 11, 2009, 7:48:52 AM1/11/09
to v8-users
I'm not sure what this means:

"Alternatively, if you don't need to be able to call
the function as a constructor you can use an object with a
CallAsFunction handler instead of a real function. "

Can someone elaborate on this a bit more.

Reply all
Reply to author
Forward
0 new messages