Calling C++ from Javascript?

Asked

Viewed 1,318 times

7

I have a simulation in C++. I passed this simulation to Javascript so that the user could play with it in the browser, but it became much slower. If possible, therefore, I would like to call the function itself in C++ within Javascript and do this on the client side. It is possible?

  • 3

    I think the way is through Nacl (Google Native Client). Although I’ve never used it, I’ve only heard of it, they say it’s possible to run native code on the client yes. I hope someone who used it can say something about it.

  • Are you talking about something like QT or are you even considering calling a C++ program from a javascript site? ( I guarantee that most browsers will not authorize this second option).

  • @Wakim, Google Native Client does not run on the customer side as requested in the question.

  • 2

    @Kyllopardiun, are you sure? Check out the description: https://developer.chrome.com/native-client.

  • If you see the tutorial will see that it generates a microservice for this and so I would not classify it as on the client side.

  • 2

    Native Client is just for running native code on the client, directly in the browser. It is probably the solution in this case, although it is limited to Chrome. I’m not going to write an answer because I’ve never used it, I hope someone will turn up already. @Kyllopardiun The tutorial server is only for sending the page and binary code to the client, it does not execute anything.

Show 1 more comment

2 answers

2

Before leaving for Nacl, which is exclusive to Chrome, try using Emscripten, an LLVM-based compiler that transforms C and C++ code into asm.js. Asm.js is a subset of Javascript that modern Engins can optimize well and runs at near native performance.

To installation is quite quiet. The site also has a tutoring.

  • Interesting, it’s not quite what I was looking for but it doesn’t hurt to try. I will test and then return with the result (if there really was significant performance gain).

1

Besides the options pointed out, it would also be possible to use the Node-Webkit embarking the Node.js + Chrome engine.

Runs as a normal program, but looks like a Browser, limited only to the application you want to show.

This way you can control both the javascript client and the C++ backend and package everything as one application. The only limitation is that your user would have to install the executable, but if that’s not a problem, I believe it is the option that gives you the most flexibility with the least possible headache, compared to Nacl(Learning overhead) or Emscripten(Runtime overhead)

  • If the user needs to install the executable, why not distribute the original version written in C++?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.