Homunculus Reticulli
Homunculus Reticulli

Reputation: 68416

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?

Upvotes: 11

Views: 19637

Answers (9)

Alan Mimms
Alan Mimms

Reputation: 703

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.

Upvotes: 1

rightaway717
rightaway717

Reputation: 2831

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

Upvotes: -1

OzB
OzB

Reputation: 2221

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.

Upvotes: 1

Anand Rathi
Anand Rathi

Reputation: 786

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/

Upvotes: 2

Mansuro
Mansuro

Reputation: 4617

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

Upvotes: 5

Constantinius
Constantinius

Reputation: 35059

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.

Upvotes: 1

nutrina
nutrina

Reputation: 1042

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.

Upvotes: 0

Lyubomir Vasilev
Lyubomir Vasilev

Reputation: 3030

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

Upvotes: 0

John Watts
John Watts

Reputation: 8875

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.

Upvotes: 0

Related Questions