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.
-
8No, it is not possible.RevanProdigalKnight– RevanProdigalKnight2015-05-07 12:33:26 +00:00Commented May 7, 2015 at 12:33
-
From an native-enabled Extension, perhaps, otherwise No.Alex K.– Alex K.2015-05-07 12:35:09 +00:00Commented May 7, 2015 at 12:35
-
2Short answer: usually. It depends on the javascript engine built into the browser. Each will have it's own way of doing things (consult developer docs).Richard Hodges– Richard Hodges2015-05-07 12:39:14 +00:00Commented May 7, 2015 at 12:39
-
7This sounds like a security catastrophe waiting to happen.Joost– Joost2015-05-07 12:45:16 +00:00Commented May 7, 2015 at 12:45
-
1If Chrome-only is fine, have a look at PNaClgd1– gd12015-08-26 22:04:32 +00:00Commented Aug 26, 2015 at 22:04
4 Answers
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.
Comments
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.
2 Comments
this or self. So, for example, Sieve::computePrimes() in C++ maps to csieve_compute_primes(csieve*) in C. Now you have a C library that you can bind to from any language that makes it easy to do, C++ by default, Python using its ctypes package or Go using its cgo package. And of course, JavaScript and asm.js.