如何在 webkit 平台上将匿名函数作为参数从 javascript 传递给 C++
例子:
window.test('helloworld', function(){
alert('ye');
});
“测试”是为javascript注入(inject)web的C++,javascript传递两个参数给C++
当 C++ 异步执行时,我希望 C++ 调用第二个参数,即匿名函数?
或者C++只接收String类型的参数?
最佳答案
将自定义方法添加到 DOMWindow 接口(interface)(WebKit/Source/WebCore/page/DOMWindow.idl):
... interface DOMWindow{ ... [Custom] void test(); ... }; ...
在 WebKit/Source/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp 中,在命名空间 WebCore 中添加一个方法:
... v8::Handle<v8::Value> V8DOMWindow::testCallback(const v8::Arguments& args){ v8::Local<v8::String> str; v8::Local<v8::Function> jsFn; if(!args[0]->IsString() || !args[1]->IsFunction()) return v8::Undefined(); str = args[0]->ToString(); jsFn = v8::Local<v8::Function>::Cast(args[1]); v8::Persistent<v8::Function> pFn = v8::Persistent<v8::Function>::New(jsFn); pFn->Call(v8::Context::GetCurrent()->Global(), 0, NULL); // Execute the passed in js function return v8::Undefined(); }
关于javascript - 如何将匿名函数作为参数从 javascript 传递到 webkit 上的 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080460/