tags:

views:

41

answers:

1

Google was nice enough to explain how to wrap C++ class methods with accessors that can be used from the V8 Javascript engine.

However, they don't mention how to determine the name of the JavaScript object that will have these accessor properties available.

How do I tell V8 Javascript what the name of the C++ class instance (from the sample) is? Or if it already has a name, what is it?

These two lines set up the accessors:

point_templ.SetAccessor(String::New("x"), GetPointX, SetPointX);
point_templ.SetAccessor(String::New("y"), GetPointY, SetPointY);

I assume they can be used like this from JavaScript:

someObject.x = someObject.y * 2;

How do I determine what "someObject" is?

I feel there is some code missing that finishes linking the C++ code with the V8 Javascript wrapper.

For example, in the sample code to access static global variables there was a line that explicitly exposed the accessor functions to V8 JavaScript:

Handle<ObjectTemplate> global_templ = ObjectTemplate::New();
global_templ->SetAccessor(String::New("x"), XGetter, XSetter);
global_templ->SetAccessor(String::New("y"), YGetter, YSetter);
Persistent<Context> context = Context::New(NULL, global_templ)
+2  A: 

OK, I found the missing piece of the puzzle:

context->Global()->Set(String::New("p"), obj);

This line exposes the object wrapper obj created in the previous steps to V8 JavaScript's global context as the object "p." I named it "p" here, but it could be any valid JavaScript identifier. (source)

Leftium