简体   繁体   English

如何将匿名函数作为参数从javascript传递到webkit上的c ++?

[英]How to pass an anonymous function as parameter from javascript to c++ on webkit?

How to pass an anonymous function as parameter from javascript to C++, on webkit platform 如何在webkit平台上将匿名函数作为参数从javascript传递到C ++

example: 例:

window.test('helloworld', function(){
    alert('ye');
});

The "test" is C++ injected into web for javascript, and javascript pass two parameters to C++ “测试”是将C ++注入到web for javascript中,javascript将两个参数传递给C ++

I want C++ call the second parameter which is anonymous function when C++ execute as asynchronous ? 我希望C ++调用第二个参数,当C ++作为异步执行时它是匿名函数?

Or C++ just receive parameter which type is String? 或者C ++只接收哪个类型为String的参数?

  1. Add Custom method to DOMWindow interface(WebKit/Source/WebCore/page/DOMWindow.idl): 将自定义方法添加到DOMWindow接口(WebKit / Source / WebCore / page / DOMWindow.idl):

     ... interface DOMWindow{ ... [Custom] void test(); ... }; ... 
  2. In WebKit/Source/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp, add a method in namespace WebCore: 在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(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM