如何从c ++类内部获取v8对象

时间:2012-11-01 07:59:11

标签: c++ v8

抱歉,我的英语很差,希望有人能理解我说的话 像这样的代码

 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);

方法“测试”mybe是一个异步调用,在anthor线程中运行,当它运行完成时我调用了js方法“onconnect” 我如何在方法Test()?

中调用js mothod:onconnect

1 个答案:

答案 0 :(得分:1)

我是从内存中写的,某些类型或通话可能略有不同。假设测试收到Point参考:

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);
   }
}