简体   繁体   中英

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

example:

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

The "test" is C++ injected into web for javascript, and javascript pass two parameters to C++

I want C++ call the second parameter which is anonymous function when C++ execute as asynchronous ?

Or C++ just receive parameter which type is String?

  1. Add Custom method to DOMWindow interface(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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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