How to run C++ code in browser using asm.js?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật