I create an instance of a JavaScript object, like;
myObj1 = new myObject("Object1");
I then call a node.js c++ addon, which does a callback to the Javascript object...
myAddon = require('myAddon');
myAddon.greet(myObj1.name);
The c++ callback code looks like:
Local<Function> callback = Local<Function>::Cast(args[0]);
Isolate* isolate = args.GetIsolate();
const int argc = 1;
const char *parm = "Hello";
v8::Local<v8::Value> argv[argc] = { v8::String::NewFromUtf8(isolate, parm) };
callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
But the problem is that the binding doesn't callback to the instance of the myObject, rather it appears to callback to the base JavaScript class. So there is no instance data.
From what I have been able to read over the past 2 days, the first parameter to the Call() method somehow becomes the "this" pointer in v8. So I would guess the problem might be that I am using a Global context
Does anyone have a clue how to correctly callback to a JavaScript object on the heap?
myAddon.greet(), and (2) you are attempting to access the objectmyObj1from this C++ code? If that is the case, then ismyObj1.namea function?myObj1in your C++ code. The correct way to do this would be to bindthisfrom the JavaScript side:myAddon.greet(myObj1.name.bind(myObj1));, just as you must do for every other kind of callback. Then you simply don't care about it on the C++ side.