11

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?

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • Is this C++ code on the server? If not, how are you getting it to the client? – Kendall Frey Aug 17 '12 at 12:18
  • I think he is talking about RMI. It would be auto-generated Javascript objects on the client that talk to C++ code on the server with some serialization mechanism that he doesn't have to write. – John Watts Aug 17 '12 at 12:20
  • @JohnWatts: Yes, you are right, it is effectively RMI. The C++ library will exist on the server and the Javascript classes will provide an API to interact with the backend library. – Homunculus Reticulli Aug 17 '12 at 12:22
  • Can't you just pipe to terminal and run and compile c++ through terminal? Isn't that essentially everything you could possibly need to do? – Wolfpack'08 Feb 12 '16 at 18:54

9 Answers9

5

There is a library to convert C++ code to javascript, it might help: emscripten

Mansuro
  • 4,558
  • 4
  • 36
  • 76
2

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/

Anand Rathi
  • 790
  • 4
  • 11
1

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.

Community
  • 1
  • 1
Constantinius
  • 34,183
  • 8
  • 77
  • 85
1

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.

OzB
  • 2,140
  • 1
  • 22
  • 38
1

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.

Alan Mimms
  • 651
  • 9
  • 22
0

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.

Community
  • 1
  • 1
John Watts
  • 8,717
  • 1
  • 31
  • 35
  • hm, JSON implements only the data exchanged between the two components. I think OP is talking about easy method exposing/interfacing. – Constantinius Aug 17 '12 at 12:26
  • @JohnWatts: Thats a good starting point, but I don't know if it suits what I'm trying to do - which is to run a script in the browser - the main thing being that the script will use the "wrapped" C++ objects. – Homunculus Reticulli Aug 17 '12 at 12:26
  • @Constantinius: yes, you got the point I was making (as I was typing it out!) – Homunculus Reticulli Aug 17 '12 at 12:27
0

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).

Lyubomir Vasilev
  • 3,000
  • 17
  • 24
0

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.

nutrina
  • 1,002
  • 1
  • 12
  • 26
  • 1
    Why using an additional language/framework? I'm sure there is a more direct possible approach than using another dependency. – Constantinius Aug 17 '12 at 12:52
  • I have just mentioned PHP and Python because I think it is easier to map C++ classes in PHP or Python rather than JS. – nutrina Aug 17 '12 at 13:06
  • What is the use of mapping classes to PHP/Python when he needs them in JavaScript? – Constantinius Aug 17 '12 at 13:08
  • Also except if you "convert" all the C++ code to JS, so that you can move it completely to the browser I don't think there is a more direct approach. You need some kind of framework to run the code ion the server handle the remote method calls. – nutrina Aug 17 '12 at 13:17
  • He can make AJAX requests from JavScript to the server. The handling of the requests can be done on the server in whatever framework he is using, using the wrapped classes ... – nutrina Aug 17 '12 at 13:19
  • And why not use a C/C++ framework? That's the point I'm trying to make. Using PHP/Python or whatever is just an unnecessary step and overhead (implementing and running it) and just *postpones* the actual problem. – Constantinius Aug 17 '12 at 13:21
  • Yes, that is true. A direct C/C++ framework might be easier and direct approach. – nutrina Aug 17 '12 at 13:30
-1

Though QML is not exactly Javascript, Qt is not plain C++, but what they do together seem just like what you need

rightaway717
  • 2,631
  • 3
  • 29
  • 43