Skip to content

Support --preload-file in the shell environment and d8 #15337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var Module = typeof {{{ EXPORT_NAME }}} !== 'undefined' ? {{{ EXPORT_NAME }}} :
#include "promise_polyfill.js"
#endif

#if ENVIRONMENT_MAY_BE_SHELL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, how about an internal settings called INCLUDE_XHR_POLYFILL which can be set when the file packager is used? Then we can avoid this unless is needed?

#include "xhr_polyfill.js"
#endif

#if MODULARIZE
// Set up the promise that indicates the Module is initialized
var readyPromiseResolve, readyPromiseReject;
Expand Down
43 changes: 43 additions & 0 deletions src/xhr_polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license
* Copyright 2021 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/

// In d8 and other shells xhr might not exist. Polyfill it so that preload-file
// works.
// This is hackish and affects the global scope, but this file is only included
// when using the shell environment, which is not on by default, and only takes
// effect when XMLHttpRequest is not defined, which basically means when testing
// in the shell locally.
if (typeof XMLHttpRequest === 'undefined') {
XMLHttpRequest = function() {
return {
open: function(mode, path, async) {
this.mode = mode;
this.path = path;
this.async = async;
},
send: function() {
if (!this.async) {
this.doSend();
} else {
var that = this;
setTimeout(function() {
that.doSend();
if (that.onload) that.onload();
}, 0);
}
},
doSend: function() {
if (this.responseType == 'arraybuffer') {
this.response = read(this.path, 'binary');
} else {
this.responseText = read(this.path);
}
this.status = 200;
},
};
};
}

11 changes: 7 additions & 4 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1448,10 +1448,13 @@ def test_include_file(self, args):
}
''')

self.run_process([EMXX, 'main.cpp'] + args)
# run in node.js to ensure we verify that file preloading works there
result = self.run_js('a.out.js', engine=config.NODE_JS)
self.assertContained('|hello from a file wi|', result)
self.run_process([EMXX, 'main.cpp', '-sENVIRONMENT=node,shell'] + args)
# run in all engines to ensure we verify that file preloading works in
# node and d8
for engine in config.JS_ENGINES:
print(engine)
result = self.run_js('a.out.js', engine=engine)
self.assertContained('|hello from a file wi|', result)

def test_embed_file_dup(self):
ensure_dir(self.in_dir('tst', 'test1'))
Expand Down