diff options
-rwxr-xr-x | emcc | 3 | ||||
-rw-r--r-- | src/jsifier.js | 3 | ||||
-rw-r--r-- | src/library.js | 11 | ||||
-rw-r--r-- | src/library_browser.js | 16 | ||||
-rw-r--r-- | src/settings.js | 2 | ||||
-rwxr-xr-x | tests/runner.py | 11 |
6 files changed, 45 insertions, 1 deletions
@@ -717,6 +717,9 @@ try: if DEBUG: print >> sys.stderr, 'emcc: will generate JavaScript' + if final_suffix == 'html': + shared.Settings.GENERATING_HTML = 1 + extra_files_to_link = [] if not LEAVE_INPUTS_RAW and not AUTODEBUG: diff --git a/src/jsifier.js b/src/jsifier.js index 77d67bec..89930918 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -73,6 +73,9 @@ function JSify(data, functionsOnly, givenFunctions) { } else { libFuncsToInclude = ['memcpy', 'memset', 'malloc', 'free']; } + if (GENERATING_HTML) { + libFuncsToInclude.push('$Browser'); + } libFuncsToInclude.forEach(function(ident) { data.functionStubs.push({ intertype: 'functionStub', diff --git a/src/library.js b/src/library.js index bac57b59..7e39bdca 100644 --- a/src/library.js +++ b/src/library.js @@ -257,11 +257,20 @@ LibraryManager.library = { var properties = {isDevice: false, contents: data}; return FS.createFile(parent, name, properties, canRead, canWrite); }, - // Creates a file record for lazy-loading from a URL. + // Creates a file record for lazy-loading from a URL. XXX This requires a synchronous + // XHR, which is not possible in browsers except in a web worker! Use preloading, + // either --preload-file in emcc or FS.createPreloadedFile createLazyFile: function(parent, name, url, canRead, canWrite) { var properties = {isDevice: false, url: url}; return FS.createFile(parent, name, properties, canRead, canWrite); }, + // Preloads a file asynchronously. You can call this before run, for example in + // preRun. run will be delayed until this file arrives and is set up. + createPreloadedFile: function(parent, name, url, canRead, canWrite) { + Browser.asyncLoad(url, function(data) { + FS.createDataFile(parent, name, data, canRead, canWrite); + }); + }, // Creates a link to a sepcific local path. createLink: function(parent, name, target, canRead, canWrite) { var properties = {isDevice: false, link: target}; diff --git a/src/library_browser.js b/src/library_browser.js index eb7f0293..6b2dfeff 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -138,6 +138,22 @@ mergeInto(LibraryManager.library, { 0; // delta; }, + asyncLoad: function(url, callback) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'arraybuffer'; + xhr.onload = function() { + var arrayBuffer = xhr.response; + assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); + callback(new Uint8Array(arrayBuffer)); + removeRunDependency(); + }; + xhr.onerror = function(event) { + assert(arrayBuffer, 'Loading data file "' + url + '" failed.'); + }; + xhr.send(null); + addRunDependency(); + } }, emscripten_async_run_script__deps: ['emscripten_run_script'], diff --git a/src/settings.js b/src/settings.js index 452472d9..881f7b17 100644 --- a/src/settings.js +++ b/src/settings.js @@ -205,6 +205,8 @@ var LINKABLE = 0; // If set to 1, this file can be linked with others, either as // LINKABLE of 0 is very useful in that we can reduce the size of the // generated code very significantly, by removing everything not actually used. +var GENERATING_HTML = 0; // Set to 1 when generating .html and not just .js + var RUNTIME_TYPE_INFO = 0; // Whether to expose type info to the script at run time. This // increases the size of the generated script, but allows you // to more easily perform operations from handwritten JS on diff --git a/tests/runner.py b/tests/runner.py index 8e4b42f0..434f71a1 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6971,6 +6971,17 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--preload-file', os.path.join(self.get_dir(), 'somefile.txt'), '-o', 'page.html']).communicate() self.run_browser('page.html', 'You should see |load me right before|.', '/report_result?1') + # With FS.preloadFile + + open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' + Module.preRun = function() { + FS.createPreloadedFile('/', 'someotherfile.txt', 'somefile.txt', true, false); + }; + ''') + make_main('someotherfile.txt') + Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--pre-js', 'pre.js', '-o', 'page.html']).communicate() + self.run_browser('page.html', 'You should see |load me right before|.', '/report_result?1') + def test_multifile(self): # a few files inside a directory self.clear() |