Communication between C++ and JavaScript - javascript

I need help on implementing an interprocess communication mechanism between JavaScript and C++ code. Basically I need to make a bidirectional communication. So JavaScript should be able to send and receive message from C++ (Win 32) code and vice versa.

You should look at Google's V8 JavaScript runtime, which powers their Chrome browser, it is implemented in C++. JSON libraries will simplify de/serialization.

Qt contains the QtScript module, which is a native C++ implementation of JavaScript. I've used this successfully to script a C++ application, with communication in both directions.
If you're curious, the code is part of my HP 15C simulator project.

If you have the option of using Qt, I strongly recommend it. It's comprehensive and well tested. QtScript module. QtScript is a superset of javascript. The implementation uses Webkit's javascript core.

I presume you are doing this on a Windows OS. Does any kind of IPC help or are you specific on anything? Try named pipes; or shared memory with a sync object; or try a common file system memory, such as a binary file with a sync object to access it (although this is not recommended but is sure an option).
You may also create COM objects / ActiveX controls and access this from JavaScript (browser).

Related

Can I call a c++ function from JavaScript?

There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.
A few options I can think of:
1) Find a different library in JavaScript.
2) Port the C++ code over to JavaScript. How easy this is depends on
how complex the C++ code is or how much of it you need.
3) Wrap the C++ code in a web service on a server and call it using AJAX. I'm not personally familiar with any C++ web service frameworks, but barring that, you could create Java, Python, or .NET bindings for the library (or just for the portion that you need), or bindings in another language. Then you would write your web service in whatever language you chose.
3B) Alternative to #3 - If the library has a command-line interface, or if there exists a command-line program that uses the library, your web service could call that and you wouldn't have to write a language binding. Note however that there are performance & security problems to be aware of with this option.
4) If you have the C++ source code for the library, you could try to compile the library to JavaScript using Emscripten.
I would probably try those in that order, roughly. I might try #3 last cause it could be the trickiest.
Also, if you do #3 or #3B, you'll want to be sure your use of the library is thread-safe.
Disclaimer: I've never tried any of these except #3B.
You'd be better off creating a 'C' wrapper for the C++ library and calling that from the Javascript.
C++ is notoriously difficult to interface with due to the language standard lacking a tight ABI definition. C does not have this limitation. For this reason, 'C' ends up being the lingua franca when you want two programming languages to talk with each other.
Usually browsers doesn't allow that, because that's very insecure.
You might compile C++ in asm.js and use it as JavaScript library.
Alternatively you can create browser extension, which will run or call desired code.
Yes its possible but before you can call them from javascript. You do this by creating an interface file to tell javascript about the interface.

Pygame interpreter

I want to be able to use a program in my web browser (and Safari on iOS).
How can I do this?
I believe that you can convert the program into JS and have a built in telling you when you find pygame, and then refer to this directory:
C:\python27\lib\site-packages\pygame
What you are trying to do is impossible. I had the same plan but eventually had to give up.
My suggestion: Learn JavaScript (very good tutorial: http://www.codecademy.com/en/tracks/javascript from this website: http://www.codecademy.com ) then use Phaser: https://phaser.io/
That's the route I took after figuring out, that there's no way to get Pygame working in a browser.
I haven't used it, but I found Brython. Then you don't convert anything.
I think it's impossible since you're not using python at all, but js. Also, Pygame is a set of Python modules built on top of the SDL library. It can't be embedded into web browsers.
You can't do that. There are cross-compilers that convert Python to Javascript, but they won't support Pygame. Pygame is built on the more low-level SDL library, which is not made for browsers.
https://github.com/bendmorris/pyj2d
I never used it but have seen this awhile back
Description:
PyJ2D module was modelled on Pygame commands that permits scripts coded in Python/Pygame to run in the Java virtual machine (JVM) using the Jython interpreter. This permits the deployment of JVM applications without extensive editing of the script. The current version of the module only supports a subset of Pygame functionality.
I tried this https://github.com/jggatc/pyjsdl .
It works with pyjamas http://pyjs.org/ .

Calling C++ from JavaScript / HTML using GTK+ Webkit Webview (and Qt WebKit bridge)

I am currently evaluating different approaches / solutions to call C++ functions from JavaScript code embedded in an HTML page. The application must be run on Ubuntu Server 12.04 LTS.
I am not an expert in Linux based development. This is the first time I am trying to work on something to interface between JavaScript and C++ code.
The C++ code basically resides in the .so files ( dynamic libraries) that would provide interface methods to access certain hardware and file system. The GUI needs to be in HTML and I am searching for different solutions that are possible that can call C++ from HTML. I searched and ended up deciding to try 2 approaches, both using the WebKit engine.
Approach 1: Using Qt 5.0.2 Webkit Bridge - the WebView control
The GUI framework will be on Qt framework, the main application window will contain a webview control that would run an HTML code which in turn contains the JavaScript code.
The interface between Web page and C++ is done using the addToJavaScriptWindowObject() function.
I created a sample application and tested this solution and it seems to works fine.
Approach 2: Using GTK+ WebKit WebView
I downloaded and installed the GTK 3.0 library.
I got the webkitgtk 2.0.1 and have installed it.
I have created a test application with GTK without webkit, it works well.
I am trying to create a webkit webview control using GTK.
When trying my Approach 1 with Qt, there was quite a good set of documentation and samples to do what I wanted to. But after starting with Approach 2 using GTK+, I feel am moving slower comparitively. I personally feel that the documentation part is not that straight forward for the kind of application I am trying to develop.
Other Approaches:
I also want to try to check if either using Applets (to call the .so files directly) or using the V8 JavaScript engine to interface between JavaScript and C++ are viable options(https://code.google.com/p/v8/)
I have tried the following resources:
http://webkitgtk.org/
http://www.webkit.org/
https://live.gnome.org/WebKitGtk/ProgrammingGuide/Tutorial
I want to know how exactly to do this interface part of calling a C++ functions (in .so files) when a button is clicked in a HTML web page containing JavaScript. What kind of signal am I supposed to look for. If I am using a WebkitWebview control, how do I map a button click to a c++ function?
Can someone point me to the right direction?
I would really appreciate your time and knowledge.
Regards.
Webkit GTK 2 changed significantly in terms of API's. So I am not sure if this will work with Webkit GTK 2. However this will definitely work in Webkit GTK 1.* versions. Don't know anything about QT.
For your need of connecting html view with C/C++ side of the world, you can use two approaches. Please take a look at function webkit_dom_event_target_add_event_listener. There is example at https://live.gnome.org/WebKitGtk/ProgrammingGuide/Cookbook
Another approach you can take is to use alert on click of the button and send a string as information. On C side, you can hookup the alert listener and parse the message and decide what needs to be done. I have written lot of code in python which takes this approach since call mentioned above is not exposed to python.
I agree documentation is bit sparse for webkit gtk. However if you know how you can acomplish something in javascript, usually you can map the javascript DOM management and event calls to C side. This includes generating elements dynamically, managing events such onclick etc. You just have to dig through the header files and find matching call.
If you need to use C++ code or native applications in your web application you can try to create a service over the C++ code and access it throught a REST (for example) API.
You can use a common web application framework (Spring/Java, Django/Python, etc.) to develop your web application and use Apache Thrift to interface your library.
the best solution for you is g-xml it is a good solution by GAMA but sorry it is not free.

Javascript C++ binding?

I have some C++ code that I want to expose to client side of a web app. Ideally, I want to write Javascript wrapper objects for my C++ classes so that I can use them clientside.
Has this been done before?. Does anyone have a link to show how this may be achieved?
There is a library to convert C++ code to javascript, it might help:
emscripten
Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey 1 . 8 . 5 and more
SpiderMonkey? is Mozilla Project's Javascript/ECMAScript engine.
Libjspp allows C++ developers to embed SpiderMonkey? simply and easily into their applications. Libjspp allows to run multiple Javascript Engines within same process which suits one engine per thread para dime which is helpful in achieving true parallisim. Also Libjspp no way stops user from running multiple threads within engine.
http://code.google.com/p/libjspp/
I guess that RPC is what you want. You'll need to wrap your functions on the server side using some sort of framework. I've not yet used it, but this one looks promising.
On the client side you use proxy objects to dispatch the function calls. The communication is handled usually either via XML-RPC or JSON-RPC. I used this client side framework and was quite content but I'm sure you'll find many others.
This is an old topi, however, I was in the exact situation right now, and all of the solutions I found on the net complicated or outdated.
Recently, I ran across a library which supports V8 engine (including the new isolation API, which makes 90% of the libraries I found outdated) and provides great exposure and interaction API.
https://github.com/QuartzTechnologies/v8bridge
I hope that my solution will help anybody.
There's a relatively new library for doing this called nbind. Maybe that would suit you? It looks very good to me, and I'm just about to start using it.
I think you want a C++ JSON parser. You should be able to find one here http://www.json.org/. It may not do all you want because it just serializes and deserializes C++ objects without any behavior, but it should be good enough. See https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser for some discussion.
If the C++ code has to be on the client, then there is no simple way to do this for a web app. A solution may involve coding plugins for the browsers you want to support, which may then be accessed from javascript code.
If, for example, you need this for a client application, that is another case. Such a thing has been done and involves linking your application to (or running from outside) with for example chromium library, or any other javascript execution engine. That way you can create bindings to C++ classes and use such objects from javascript and vice-versa. Note that this is also not a trivial solution and may be a big effort to implement (also requires additional resources).
You could for example wrap the C++ classes in PHP or Python, and then implement an API over HTTP to access the required functions.
Or if you insist on exposing the functions as JavaScript you could try using Node.js, and create an C++ add-on to wrap you classes. See the Node.js documentation here: http://nodejs.org/api/addons.html#addons_wrapping_c_objects
But either way, I don't think avoid creating some sort of API (HTTP SOAP, XML RPC) to access the functions on your server.
Though QML is not exactly Javascript, Qt is not plain C++, but what they do together seem just like what you need

How to configure a javascript engine not to run the code if it contains IO or system setting related functions?

I just started using a javascript engine from Microsoft(of IE9?) for developing both desktop and browser applications. I'm looking for a javascript engine which you can configure so that it won't allow any script if it contains IO or system setting related functions.
Is there any one?
If yes, please let me know which and how to do that.For details. I must call non-compiled javascript string code from compiled code( C# or C++ or else).
I know that you can tweak the security settings in the v8 javascript engine! You should check out the embedders guide: http://code.google.com/apis/v8/embed.html#security + I don't think it has I/O and System facilities unless you explicitly implement this in the C++ embedding code... This is how http://nodejs.org has done it.

Categories

Resources