14

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?

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341

1 Answers1

17

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.

Community
  • 1
  • 1
Michal Charemza
  • 25,940
  • 14
  • 98
  • 165
  • +1 for link back to the **relevant parts** in the documentation. I don't know why I missed that the first ime round. Note to self: RTFM **carefully** – Homunculus Reticulli May 10 '15 at 16:54
  • When I use `EXPORT_NAME=Lib` I always get back an error like: `NameError: name 'Lib' is not defined`. Is there something I need to do extra beyond just setting this option? – CMCDragonkai Sep 04 '17 at 11:07
  • 3
    Oh I actually need `EXPORT_NAME="'Lib'"`, the extra single quotes are important! – CMCDragonkai Sep 12 '17 at 06:09
  • 1
    Also wanted to point out that `noInitialRun` means not running `main` immediately, it does not mean not running `run` which is Emscripten's program startup initialisation. – CMCDragonkai Sep 12 '17 at 06:13
  • **NOTE** One may be interested in [the `-s MODULARIZE=1` option](https://github.com/kripken/emscripten/blob/6dc4ac5f9e4d8484e273e4dcc554f809738cedd6/src/settings.js#L539-L563) as well. It does not pollute the global scope, exposes only the module instantiation function with a name specified with the `-s EXPORT_NAME="'……'"` option: `const module_object = EXPORT_NAME();`. – Константин Ван Apr 05 '18 at 16:15
  • 1
    For me EXPORT_NAME only works if I use MODULARIZE or MODULARIZE_INSTANCE together with it. – Thor_Bux Jun 29 '18 at 19:51
  • idk but i had to add `-s MODULARIZE=1` in order for EXPORT_NAME to work; however, i then had to use `new Lib()` – Dee Jan 27 '22 at 12:49