2014-12-23 90 views
0

如何暴露V8上下文中的類方法?如何暴露V8中的類方法

我很想創建類似於下面的行,但它不會工作的事實,lambda捕獲不能被視爲功能指針。

Global()->Set("foo",v8::FunctionTemplate::New([this](const v8::Arguments &args) -> v8::Handle<v8::Value>{this->foo()}; return v8::Undefined())); 

這不是關於曝光文字類型struct Point {int x,y}但關於曝光類方法,或簡單的可調用的對象。

+0

你可以添加一個鏈接到'Set'的文檔。適用的解決方案取決於你有什麼功能。 –

回答

0

這就是我做到的。

context->Global()->Set(v8::String::New("_this"),v8::External::Wrap(this)); 
context->Global()->Set(v8::String::New("foo"),v8::FunctionTemplate::New(
    [](const v8::Arguments &args) -> v8::Handle<v8::Value>{ 
     reinterpret_cast<Foo*>(reinterpret_cast<v8::External*>(*(args.This()->Get(v8::String::New("engine"))))->Value())->foo(); 
     return v8::Undefined(); 
})->GetFunction());