21

An asm.js application is very fast (near native C++ speed):

enter image description here

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?

  • 3
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat May 13 '13 at 15:35
  • This third-party tutorial appears to address some of these questions: devosoft.org/an-introduction-to-web-development-with-emscripten – nobar Sep 13 '15 at 2:33
36

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:
C++ => clang / LLVM => emscripten => JS engine

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.

  • ASM.js is a compile target for C/C++. So no your not writing C++ in asm.js your compiling C++ to asm.js – Calvin May 28 '13 at 12:39
  • Only one mention comes to mind for applications started from scratch. In the case of games, having the code in C++ could be helpful for deploying on multiple platforms. – Vlad Nicula Feb 13 '17 at 19:44
6

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
  • 4
    That's actually C code. A C++ compiler is about two or three orders of magnitude more complex. Luckily, emscripten avoids that hard problem by compiling LLVM, and there's an existing C++-to-LLVM compiler. – MSalters May 13 '13 at 21:05
  • 3
    @MSalters: It's also valid C++ code. Imagine that! Wow! – Thomas Eding Dec 10 '14 at 5:41
  • @ThomasEding: You missed the point. The smaller the language you have to support, the easier it is to compile that language. The intersection of C and C++ is necessarily not bigger than either of those two. – MSalters Dec 10 '14 at 23:55
  • Let's suppose that this code was pure C++, that a C compiler would not handle, would the use of emcc be valid? – Hamza Ouaghad Jan 18 '16 at 14:08
  • @HamzaOuaghad - yes. a simple hello world with c++'s cout & string classes works just fine with this emcc command line. using version 1.35.0. – orion elenzil Nov 1 '16 at 23:38
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.