0

Here is the task: I want to run .js file which was created by Emscripten from .cpp file, from another .js file.

i.e.: I have ping.cpp file, which simply displays text "ping". I use Emscripten to create ping.js To do it, I type em++ ping.cpp and here it is - ping.js.

Now I can run it using node ping.js, but I want it to run from my second .js file which is called init.js and I can't understand how should I do it. Because ping.js doesn't have main functions which display "ping" and which I can call from another .js file or for example .html file, instead of this it has 68500 lines of code.

So, is there any chance for me to run ping.js from init.js?

Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
Leonid Manieiev
  • 101
  • 2
  • 6

1 Answers1

0

You should probably load your ping.js file with a script tag in your HTML file, just like any other script. The trick is to make sure that you make any functions you want to use accessible to OTHER scripts that you load. To do so, you need to set an EXPORTED_FUNCTIONS compilation flag to indicate which function names you want to preserve. Specifically

em++ -s EXPORTED_FUNCTIONS="['_ping']" ping.cpp -o ping.js

Note the underscore ('_') that needed to be added in front of the function name to compensate for name mangling.

In the other JS file where you want to use ping, you need to set it up. Make sure the compiled script loads first, and then do:

ping = Module.cwrap('ping', 'null', ['string']);

This will then allow you to use ping, assuming it has a void return type (hence the null) and a single c-style string argument (hence the ['string']).

If you really do want to load it from another JavaScript file, see this answer: How do I include a JavaScript file in another JavaScript file?

Community
  • 1
  • 1
Charles Ofria
  • 1,936
  • 12
  • 24
  • I have tried to type `em++ -s EXPORTED_FUNCTIONS="['_main', '_ping']" ping.cpp -o ping.js` but get this error: `WARNING root: function requested to be exported, but not implemented: "_ping"` – Leonid Manieiev Aug 20 '15 at 14:02
  • Ah! I think I misunderstood your original question. In the list of EXPORTED_FUNCTIONS, I was assuming that you had a function called `ping` that would print the text "ping" on the screen. You should just list the names of the functions you want to be able to use (with the leading underscores). If you have a main function, it should automatically run (by default), any other functions you would need to call from your JS code to have them run. – Charles Ofria Aug 20 '15 at 14:48
  • Well, I create `ping.cpp`, witch contain `void ping(std::string arg)`.Function ping() do this: `{std::cout << arg;}`. Then I type `em++ -s EXPORTED_FUNCTIONS="['_ping']" ping.cpp -o ping.js` and its create `ping.js`. After that I went to my first .js file called `init.js` and type there, in `function init()`, this: `_ping = Module.cwrap('ping', 'null', ['string']); _ping('Hello from Java Script side');` After that I went to `index.html` declare my `init.js` and run `init()` function, witch should run `_ping()`, but it doesnt show me `Hello from Java Script side` What I did wrong? – Leonid Manieiev Aug 20 '15 at 15:32
  • In your HTML file, do you have both `` and ``? The first will load all of the C++/Emscripten code, and the second will have your init function. – Charles Ofria Aug 20 '15 at 19:57
  • ALSO, if you want a set of simple working examples (which may help you figure out the problem) here's a tutorial I wrote: http://devosoft.org/an-introduction-to-web-development-with-emscripten/ – Charles Ofria Aug 20 '15 at 19:58
  • Yes, I have both scripts in my html file. Thanks, Ill try to figure it out. – Leonid Manieiev Aug 20 '15 at 20:24
  • I wonder if it has to do with your script having not loaded all of the parts yet. Instead of running `init()`, try: `document.addEventListener("DOMContentLoaded", function(event) { init(); });` This will make sure that it waits for everything to be loaded before init() is called. – Charles Ofria Aug 20 '15 at 20:57
  • Drats. In trying some of this out directly, I think the problem is with the cout; it doesn't seem to work unless you let Emscripten generate the html file. In all of my implementations, I've always had JavaScript handle all IO, while using C++ for the more computationally intensive portion (thus making each language do what it's better at). Alas, I don't know of a better way to get C++ to output to the browser. – Charles Ofria Aug 20 '15 at 22:38
  • The funny thing is, that Ive just tried to use C language with it printf() method and it didnt work as well. I think, that maybe I just forget to add something to my init.js file or to my index.html file, but I cant find what exactly. Seems that I did everything like you said and like official web site says. – Leonid Manieiev Aug 21 '15 at 17:48