Search code examples
c++v8

How to get v8 object from c + + class internal


sorry my english is poor, hope someone can understand what I said the code like this

 class Point
 {
 public:
int x_, y_;
Point(int x, int y):x_(x), y_(y){}
void Test(Local<Object> & obj)
{
    //Local<Value> value = ref->Get(String::New("onconnect"));

    printf("%d", ref->Get(String::New("getX")));
}
 };


 Handle<Value> PointConstructor(const Arguments& args)
 { 

     Point *p = new Point(x, y);

     object->SetInternalField(0, External::New(p));

     p->Test();
     return object; 
 } 

 ...

 Handle<String> script = String::New("var p = new Point(20, 0); p.onconnect = function(){}; log(p.getX())");


   Handle<Script> compiled_script = Script::Compile(script);

method "Test" mybe a async call, run in anthor thread, when it run completed i what call the js method"onconnect" how can i invoke the js mothod:onconnect in method Test()?


Solution

  • I write this from memory, some types or calls may be slightly different. Assuming Test receives the Point reference:

    void Test(Local<Object> obj)
    {
       String funName = String::New("onconnect");
       if (obj->Has(funName)){
         Local<Value> fun = obj->Get(funName);
         // would suggest to check additionally whether it actually is a function
         Local<Value> args[] = {}; // empty arguments
         Local<Value> result = fun->AsObject()->CallAsFunction(obj, 0, args);
       }
    }