Allow override for ENVIRONMENT_IS*

This commit is contained in:
XVincentX 2015-11-23 10:35:55 +01:00
Родитель 42fb486c53
Коммит 0a1e11fc9b
3 изменённых файлов: 44 добавлений и 4 удалений

Просмотреть файл

@ -209,4 +209,5 @@ a license to everyone to use it as detailed in LICENSE.)
* Louis Lagrange <lagrange.louis@gmail.com>
* Ying-Ruei Liang <thumbd03803@gmail.com>
* Stuart Geipel <lapimlu@gmail.com>
* Vincenzo Chianese <vincenz.chianese@icloud.com>

Просмотреть файл

@ -103,3 +103,21 @@ Other methods
:param obj: The JavaScript-wrapped C++ object to be destroyed.
Overriding execution environment
================================
The generated program is able to detect its execution environment by checking the presence of some typical objects of the environment itself (such as ``window`` for browsers).
However, sometimes it may be needed to override the detected environment: a typical use case would be module bundlers (like webpack): they are executed by nodejs but the final output is for browser.
In order to do that, you can dictate your preferred execution environment by setting the ``Module.ENVIRONMENT`` variable to one of those allowed values:
``WEB``
``WORKER``
``NODE``
``SHELL``
In that case, ``Module`` will honor your preference and skip auto detection.

Просмотреть файл

@ -34,13 +34,35 @@ for (var key in Module) {
// The environment setup code below is customized to use Module.
// *** Environment setup code ***
var ENVIRONMENT_IS_WEB = typeof window === 'object';
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = false;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
// Three configurations we can be running in:
// 1) We could be the application main() thread running in the main JS UI thread. (ENVIRONMENT_IS_WORKER == false and ENVIRONMENT_IS_PTHREAD == false)
// 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false)
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
var ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
if (Module['ENVIRONMENT']) {
if (Module['ENVIRONMENT'] === 'WEB') {
ENVIRONMENT_IS_WEB = true;
} else if (Module['ENVIRONMENT'] === 'WORKER') {
ENVIRONMENT_IS_WORKER = true;
} else if (Module['ENVIRONMENT'] === 'NODE') {
ENVIRONMENT_IS_NODE = true;
} else if (Module['ENVIRONMENT'] === 'SHELL') {
ENVIRONMENT_IS_SHELL = true;
} else {
throw new Error('The provided Module[\'ENVIRONMENT\'] value is not valid. It must be one of: WEB|WORKER|NODE|SHELL.');
}
} else {
ENVIRONMENT_IS_WEB = typeof window === 'object';
ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
}
#if USE_PTHREADS
var ENVIRONMENT_IS_PTHREAD;
if (!ENVIRONMENT_IS_PTHREAD) ENVIRONMENT_IS_PTHREAD = false; // ENVIRONMENT_IS_PTHREAD=true will have been preset in pthread-main.js. Make it false in the main runtime thread.
@ -48,7 +70,6 @@ var PthreadWorkerInit; // Collects together variables that are needed at initial
if (!ENVIRONMENT_IS_PTHREAD) PthreadWorkerInit = {};
var currentScriptUrl = ENVIRONMENT_IS_WORKER ? undefined : document.currentScript.src;
#endif
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
if (ENVIRONMENT_IS_NODE) {
// Expose functionality in the same simple way that the shells work