Compile library in C into javascript with emscripten

Categories Development

I used emscripten to compile libdasm into javascript.

Emscripten uses a clang frontend to compile C/C++ source code to LLVM bitcode, and then leverage its javascript backend to generate *.js or *.html files.

The following is some basic usage case of emcc.

When I tried to use   emcc libdasm.c simple.c -o simple.js  to compile a simple example of using libdasm. the following warning occured:

These functions are defined as inline function which is platform dependent. we can find the reason why these symbol are unresolved here. In a word, emcc does not compile inline functions.

So we have to rewrite these five functions using platform-independent code. (just delete the __inline__ key ). and we can use node.js to verify.

It worked.

Let’s compare the generated javascript funtion with c source code:

 

To use this library, we need to export the functions e.g. “get_instruction()” . And then we can load the library and call the these function in our javascript code. In this case, we need to compile the libdasm library and export several disassemble APIs to the outside.

To export a function (e.g.  get_instruction), use:

However, an interface to interact with libdasm function still remain to be implemented. Let’s take a trivial example: first call get_instruction() to get instruction from data, then call get_instruction_string() to write the disassembly code to a string buffer.

No Comments

Leave a Reply