Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the default namespace 'Module' in emscripten

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?

like image 524
Homunculus Reticulli Avatar asked May 10 '15 14:05

Homunculus Reticulli


People also ask

How does the Emscripten module work?

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.

How do I verify the Emscripten development environment?

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.

What happens when Emscripten main() exits?

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.

How do I run inline assembly code in Emscripten?

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.


1 Answers

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.

like image 145
Michal Charemza Avatar answered Sep 30 '22 19:09

Michal Charemza