如何在webkit平台上将匿名函数作为参数从javascript传递到C ++
示例:
window.test('helloworld', function(){
alert('ye');
});
“test”是将C ++注入到web for javascript中,javascript将两个参数传递给C ++
当C ++以异步方式执行时,我希望C ++调用第二个参数是匿名函数吗?
或者C ++只接收哪个类型为String的参数?
答案 0 :(得分:1)
将自定义方法添加到DOMWindow接口(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();
}