Does Awesomium allow / use C ++ variables / methods in JS?

Awesomium easily allows C ++ code to call Javascript methods, but I have not found a definite answer as to whether it can do the opposite. This site seems to say that you can, but viewing the text and examples does not illuminate me.

So I'm looking for a specific answer: can I call C ++ variables / methods in my Javascript (JQuery) or not?

If you could include a simple example, that would also be very appreciated.

Thanks!

+4
source share
1 answer

You definitely can ... you just need to create an extra layer on top of WebView :: setObjectCallback and WebViewListener :: onCallback using delegates / function pointers.

I wrote a quick class JSDelegate.h ( see it here ), which can be used to connect "onCallback" events directly to C ++ member members.

The basic idea is to support matching callback names to delegates:

typedef std::map<std::wstring, Awesomium::JSDelegate> DelegateMap; DelegateMap _delegateMap; 

And call the appropriate function from your WebViewListener :: onCallback:

 void MyListener::onCallback(Awesomium::WebView* caller, const std::wstring& objectName, const std::wstring& callbackName, const Awesomium::JSArguments& args) { DelegateMap::iterator i = _delegateMap.find(callbackName); if(i != _delegateMap.end()) i->second(caller, args); } 

And then, every time you want to associate a specific C ++ function, you would do it like this:

 // Member function we wish to bind, must have this signature for JSDelegate void MyClass::myFunction(Awesomium::WebView* caller, const Awesomium::JSArguments& args) { // handle args here } // Instantiate MyClass instance in C++ MyClass* myClass = new MyClass(); // Create corresponding 'MyClass' object in Javascript webView->createObject(L"MyClass"); // Do the following for each member function: // Bind MyClass::myFunction delegate to MyClass.myFunction in JS _delegateMap[L"myFunction"] = Awesomium::JSDelegate(myClass, &MyClass::myFunction); webView->setObjectCallback(L"MyClass", L"myFunction"); 

Then you should be able to call MyClass :: myFunction directly from Javascript as follows:

 MyClass.myFunction("foo", 1, 2 3) 

Hope this helps! I have not tested any code, but I wrote it using the Awesomium v1.6 RC4 SDK .

+14
source

Source: https://habr.com/ru/post/1346757/


All Articles