最終更新:2017-06-26 (月) 00:34:50 (89d)  

Emscripten はてなブックマークを見る
Top / Emscripten

An LLVM-to-JavaScript Compiler

LLVM中間コードJavaScriptに変換するツール

https://github.com/kripken/emscripten/

概要

  • Emscripten is an LLVM to JavaScript compiler. It takes LLVM bitcode (which can be generated from C/C++ using Clang, or any other language that can be converted into LLVM bitcode) and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).

チュートリアル

必要環境

Emscripten/インストール

Windows環境 (Visual Studio 2010)

ビルドツール

最適化

  • - O1 - ちょっとだけ最適化
  • - O2 - Closure Compilerを呼び出す
  • - O3 - 実験的

コマンド

メモ

出力

Module

引数

  • arguments: The commandline arguments (if the compiled code checks argc, argc, it will be seeing arguments)
  • print: Called when something is printed to standard output.
  • preInit?: A function (or array of functions) to call before global initializers run, but after basic initialization of the JS runtime (so you can do FS.* stuff, but no C++ initializers were called yet).
  • preRun?: A function (or array of functions) to call right before calling run, but after defining and setting up the environment, including global initializers. This is useful, for example, to set up directories and files using the FileSystem? API (since that needs the FileSystem? API to be defined, but also needs to be done before the program starts to run; if you need to affect global initializers, though, you should use preInit).
  • noInitialRun?: If set to true, run will not be called (so memory will not be initialized and the compiled code's main is not executed), and you should call it yourself later.
  • noExitRuntime?: If set to true, the runtime is not shut down after run is called. Shutting down the runtime calls shutdown callbacks, for example atexit calls. If you want to be able to continue to use the code after run finishes, it is safer to set this.
    var Module = {
      'print': function(text) { alert(text) }
    };

cwrap

  • wrap a compiled C function
  • useful to wrap a function once and call it several times.
    int_sqrt = Module.cwrap('int_sqrt', 'number', ['number'])
    int_sqrt(12)

ccall

  • receives another parameter with the parameters to pass to the function
  • useful for a single call to a function.

メモ

  • -O2とかで最適化すると関数名も書き換えられるのでコンパイル時に難読化しない関数を指定する
  • -s EXPORTED_FUNCTIONS="['_main','_other_function']"

getValue/setValue

  • getValue(ptr, type)
  • setValue(ptr, value, type)

サポートしているライブラリ

デモ

参考