I am using emscripten to provide Javascript bindings for some libraries. Emsripten packages the code into a namespace (global var), called 'Module'.
I want to change the naming so that I can use a name that reflects what the library is used for, and also to prevent variable name collisions further down the line, as I write bindings for other libraries.
I can't find anywhere in the documentation, that shows how to do this. Does anyone know how I can change the default namespace used by emscripten?
When an Emscripten application starts up it looks at the values on the Module object and applies them. Note that changing the values after startup will not work in general; in a build with ASSERTIONS enabled you will get an error in that case. Module is also used to provide access to Emscripten API functions (for example ccall ()) in a safe way.
First call emcc --check, which runs basic sanity checks and prints out useful environment information. If that doesn’t help, follow the instructions in Verifying the Emscripten Development Environment.
By default Emscripten sets EXIT_RUNTIME=0, which means that we don’t include code to shut down the runtime. That means that when main () exits, we don’t flush the stdio streams, or call the destructors of global C++ objects, or call atexit callbacks.
Emscripten cannot compile inline assembly code (because it is CPU specific, and Emscripten is not a CPU emulator). You will need to find where inline assembly is used, and disable it or replace it with platform-independent code.
You can change the EXPORT_NAME
setting from the default of Module
. You can do this on the command line as an options to emcc
:
emcc -s EXPORT_NAME="'MyEmscriptenModule'" <other options...>
and then the module will be available on the global scope by whatever name you specified:
window.MyEmscriptenModule == {...}
Note that if you set the MODULARIZE
setting to be 1, then whatever is set as EXPORT_NAME
will be created as a function in the global scope that you must call to initialise the module. You can pass a settings object to this function, and it will return the module instance back:
var myModuleInstance = window.MyEmscriptenModule({noInitialRun: true});
If you're using some a module loader, such as RequireJS, and don't want to add anything to the global namespace at all, an alternative is to use the --pre-js <file>
and --post-js <file>
options to wrap the final Javascript, as in this answer to a question on Emscripten with module loaders.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With