An asm.js application is very fast (near native C++ speed):
http://kripken.github.io/mloc_emscripten_talk/micro4b.png
But how is it possible to write one in C++, convert it to LLVM code, then do some trick with emscripten/asm.js ? I haven’t found any tutorial about it.
And if I write the code in C++, then how to use the js API-s, for example XMLHttpRequest, WebSockets, Canvas or WebGL?
2
I believe you are mistaken in your understanding of asm.js.
First off, from their FAQ
Q. Is asm.js a new language?
A. No, it’s just (a subset of) JavaScript.
And you asked clarification added :
But how is it possible to write one [an asm.js application] in C++
You don’t write an “asm.js application”, rather asm.js is a target1 to compile your C++ code to.
This article by John Resig provides a number of details that may better explain how asm.js would be used.
Starting with this image:
you can see that asm.js is a translation target of emscripten. Emscripten handles translating the LLVM bytecode into JavaScript, and asm.js is a subset of JavaScript. Staying within asm.js’ restricted subset of JavaScript allows the code to be optimized and run faster.
You also asked:
And if I write the code in C++, then how to use the js API-s
Again, you’re kind of missing the point. Asm.js enables porting existing C/C++ applications into JavaScript so they can be run within a browser. You wouldn’t normally be able to use JS APIs within your C/C++ code, and there’s nothing magical about asm.js to allow that.
If you have a new application to write that needs JS APIs then you should write the application in JS and not futz with trying to write in C++ and then port to JavaScript.
And going back to Resig’s article, there are two key quotes for your question:
the kind of applications that are going to target Asm.js, in the near future, are those that will benefit from the portability of running in a browser but which have a level of complexity in which a direct port to JavaScript would be infeasible
and
As you can probably see from the code above Asm.js isn’t designed to be written by hand. … The most common use case for Asm.js right now is in applications complied from C/C++ to JavaScript. Almost none of these applications interact with the DOM in a meaningful way, beyond using WebGL and the like.
What you might want to consider doing instead is having a JavaScript program that calls the JS APIs that you need along with making calls to the C++ that you compiled to JavaScript. Have a look at this emscripten tutorial to see how to call C++ code from JavaScript.
For some additional research, emscripten has a tutorial that might help you get started with understanding how to take C++ code, run it through LLVM, and then target asm.js.
1 Strictly speaking, that’s not true. The C/C++ code is unaware of what it’s going to be compiled to, so I can’t really call asm.js a target. Another tool (emscripten) takes the LLVM output and then translates to asm.js compliant JavaScript. But I’m going to call it a target because it’s easier to understand.
2
Yes, you can write C++ code and compile it to asm.js, using emscripten. I haven’t tried it myself, and I’m not sure how ready this is for prime time. It seems to be good enough to run a bunch of games though.
Here is a tutorial: http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html . Looking at the tutorial, it seems quite easy to compile C++ code:
// hello.cpp
#include<stdio.h>
int main() {
printf("hello, world!n");
return 1;
}
$ ./emcc tests/hello.cpp -o hello.html
5
The easiest way would be to use WCPP, a package that lets you import C++ nearly directly into your Node project.
Our C++
// addTwo.cpp
export int addTwo(int a, int b) {
return a + b;
}
In the terminal (to compile our C++)
$ wcpp
Our JavaScript
const ourModule = await require('wcpp')('./addTwo.cpp')
console.log(ourModule.addTwo(2, 3))
For more information, see the NPM Package or the Git Repo