emscripten module changes - inolen/emscripten GitHub Wiki

  1. Export load(callback) function as a way for user's who've opted out of the initial run to bootstrap the application and trigger the supplied callback once all preloading is done. This will help remove the confusion between run, callMain and _main.
  2. If the initial run has been disabled, do absolutely nothing (no preInit, no preRun) until the user calls load().
  3. Stop depending on a pre-existing global Module to pass in runtime options:
  • allow the options to be set at build time, or
  • since INVOKE_RUN=0 now does no immediate work, the user can set the options on the Module object before calling load().
  1. Convert preInit and preRun from options to functions exported by Module. The preInit and preRun functions could be run as an async series enforcing a simple callback convention allowing the id-based tracking going on in addRunDependency and removeRunDependency to be cleaned up.
  2. Come up with a default closure for modules. Currently users do this through pre/post js or --js-transform, however, if task 3 is completed, would there be a reason to not have a default closure generated?
  3. Provide new name option for default global closure export (default to JS script basename?)

Tasks 3 and 4 seem like they would involve the most breakage in tests, but removing any upfront configuration and automatic execution seems sane, and makes using multiple modules, or loading modules through things like require.js or node's require much easier.

Default closure proposal

(function () {

var Module = {};

// Removed.
// try {
//   this['Module'] = Module;
// } catch(e) {
//   this['Module'] = Module = {};
// }

...

//
// I grabbed these from another script I'd written, perhaps we could alter them to
// better integrate with the pre-existing ENVIRONMENT_IS_* variables.
//

// require.js
if (typeof define !== 'undefined' && define.amd) {
	define(function () {
		return Module;
	});
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
	module.exports = Module;
}
// browser
else if (Module.name) {
	this[Module.name] = Module;
}

// Dynamic compiling, say as a Function() object.
return Module;
})();

load fn usage example (initial run disabled)

<script type="text/javascript" src="/module.js"></script>
<script>
	// Since we've disabled the inital run, we can set options on the actual module
	// object before calling load().
	module.noFSInit = true;

	// preInit / preRun task queueing
	// to note, these functions could still be invoked from inside the module through --pre-js and --post-js as they are currently in many of the tests
	module.preInit(function (cb) {
		console.log('preInit hook');
		cb();
	});

	module.preRun(function (cb) {
		xhr('filea', function (data) {
			FS.addFile('filea', data);
			cb();
		});
	});

	// load() will bootstrap the app and trigger the internal and user-defined preInit / preRun
	// functions, and once done, trigger the supplied callback.
	module.load(function () {
		module.run(['arg1', 'arg2']);
	});
</script>
⚠️ **GitHub.com Fallback** ⚠️