Setting custom accessor callbacks inside the JavaScript context

36 views
Skip to first unread message

Christoph Martens

unread,
Sep 27, 2012, 9:37:15 PM9/27/12
to v8-u...@googlegroups.com
Hey everyone,

I wanted to ask if anyone of you knows a possibility to have the capability of defining accessors of Objects inside the javascript side.

Why am I asking?

For my V8GL environment, I have the glut bindings in place. That means they are available inside the javascript context via:

glut.get(glut.WINDOW_WIDTH);

Now I want to emulate the WebGL API on top of the usual GL/GLES/GLUT stuff. But, for example, I want to offer the glut functionality wrapped with JavaScript polyfills. I think that this is the best way to get there to not having obsolete or duplicated code on the C++ side.

For example, I wanted to do something like this:

var Window = function(width, height) {
};

Window.__SETACCESSOR('x', function() {
    glut.get(glut.WINDOW_X);
});

I know that on the native side, you can use setAccessor() on templates, but I don't know if there's a possibility to offer the same functionality here on the javascript side somehow via extending the native Object in JS or something.

Does anyone of you have a clue where to start or if that is possible?

Thanks!
Christoph

Michael Schwartz

unread,
Sep 27, 2012, 9:50:04 PM9/27/12
to v8-u...@googlegroups.com
https://github.com/mschwartz/SilkJS-Harmony

Note that SilkJS turns on harmony extensions to make this all possible.  If you are using some other JS implementation, you'll have to see if you need to start up with some extra command line switches or whatever.



Jakob Kummerow

unread,
Sep 28, 2012, 4:34:48 AM9/28/12
to v8-u...@googlegroups.com
Defining getters (and setters) is possible in plain JavaScript, it is not V8 specific at all and does not need custom extensions. See for example https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects?redirectlocale=en-US&redirectslug=Core_JavaScript_1.5_Guide%2FWorking_with_Objects#Defining_getters_and_setters .

Christoph Martens

unread,
Sep 28, 2012, 6:35:46 AM9/28/12
to v8-u...@googlegroups.com

Yeah, I just realized that there is Object.defineProperty which I already knew, but havent used it before.

Thanks for the hints, this is exactly what I wanted :-)

Michael Schwartz

unread,
Sep 28, 2012, 8:22:50 AM9/28/12
to v8-u...@googlegroups.com
John Resig wrote about getters and setters back in 2007. 


Yeah, you can define getters and setters, but you cannot use those to trap accesses to every arbitrary property of an object or function.

The difference between harmony and getters/setters can be a significant code savings.

Proxy.create({
  get: function(receiver, what) {
    return glut['WINDOW_' + what.toUppercase()];
  }
});

versus dozens (perhaps) of getter methods, one per property.

I'm just putting the info out there.

Reply all
Reply to author
Forward
0 new messages