diff options
-rw-r--r-- | src/library.js | 20 | ||||
-rw-r--r-- | src/shell.js | 26 |
2 files changed, 41 insertions, 5 deletions
diff --git a/src/library.js b/src/library.js index 938a3c92..26358bd6 100644 --- a/src/library.js +++ b/src/library.js @@ -359,7 +359,25 @@ LibraryManager.library = { var success = true; if (typeof XMLHttpRequest !== 'undefined') { // Browser. - throw 'Cannot do synchronous binary XHRs in modern browsers. Use --embed-file or --preload-file in emcc'; + if (!ENVIRONMENT_IS_WORKER) throw 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc'; + + // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available. + var xhr = new XMLHttpRequest(); + xhr.open('GET', obj.url, false); + + // Some hints to the browser that we want binary data. + if (typeof Uint8Array != 'undefined') xhr.responseType = 'arraybuffer'; + if (xhr.overrideMimeType) { + xhr.overrideMimeType('text/plain; charset=x-user-defined'); + } + + xhr.send(null); + if (xhr.status != 200 && xhr.status != 0) success = false; + if (xhr.response !== undefined) { + obj.contents = new Uint8Array(xhr.response || []); + } else { + obj.contents = intArrayFromString(xhr.responseText || '', true); + } } else if (Module['read']) { // Command-line. try { diff --git a/src/shell.js b/src/shell.js index 891a6328..065462e1 100644 --- a/src/shell.js +++ b/src/shell.js @@ -44,7 +44,9 @@ if (ENVIRONMENT_IS_NODE) { if (!Module['arguments']) { Module['arguments'] = process['argv'].slice(2); } -} else if (ENVIRONMENT_IS_SHELL) { +} + +if (ENVIRONMENT_IS_SHELL) { Module['print'] = print; if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm @@ -62,7 +64,9 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = arguments; } } -} else if (ENVIRONMENT_IS_WEB) { +} + +if (ENVIRONMENT_IS_WEB) { if (!Module['print']) { Module['print'] = function(x) { console.log(x); @@ -74,7 +78,9 @@ if (ENVIRONMENT_IS_NODE) { console.log(x); }; } +} +if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { Module['read'] = function(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, false); @@ -87,12 +93,24 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = arguments; } } -} else if (ENVIRONMENT_IS_WORKER) { +} + +if (ENVIRONMENT_IS_WORKER) { // We can do very little here... + var TRY_USE_DUMP = false; + if (!Module['print']) { + Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) { + dump(x); + }) : (function(x) { + self.postMessage(x); + })); + } Module['load'] = importScripts; +} -} else { +if (!ENVIRONMENT_IS_WORKER && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_SHELL) { + // Unreachable because SHELL is dependant on the others throw 'Unknown runtime environment. Where are we?'; } |