Skip to content

[wasm64] Fix wget and run wget browser tests #20544

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

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,8 @@ jobs:
browser64.test_fetch_sync_xhr
browser64.test_fetch_implicit_append
browser64.test_pthread_*
browser64.test_wget_*
browser64.test_emscripten_async_wget_*
"
test-browser-firefox:
executor: bionic
Expand Down
66 changes: 31 additions & 35 deletions src/library_wget.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var LibraryWget = {
function doCallback(callback) {
if (callback) {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
withStackSave(function() {
{{{ makeDynCall('vi', 'callback') }}}(stringToUTF8OnStack(_file));
callUserCallback(() => {
withStackSave(() => {
{{{ makeDynCall('vp', 'callback') }}}(stringToUTF8OnStack(_file));
});
});
}
Expand All @@ -45,15 +45,11 @@ var LibraryWget = {
destinationDirectory,
PATH.basename(_file),
_url, true, true,
function() {
doCallback(onload);
},
function() {
doCallback(onerror);
},
() => doCallback(onload),
() => doCallback(onerror),
false, // dontCreateFile
false, // canOwn
function() { // preFinish
() => { // preFinish
// if a file exists there, we overwrite it
try {
FS_unlink(_file);
Expand All @@ -66,29 +62,29 @@ var LibraryWget = {

emscripten_async_wget_data__deps: ['$asyncLoad', 'malloc', 'free', '$callUserCallback'],
emscripten_async_wget_data__proxy: 'sync',
emscripten_async_wget_data: (url, arg, onload, onerror) => {
emscripten_async_wget_data: (url, userdata, onload, onerror) => {
{{{ runtimeKeepalivePush() }}}
asyncLoad(UTF8ToString(url), function(byteArray) {
asyncLoad(UTF8ToString(url), (byteArray) => {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
callUserCallback(() => {
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
{{{ makeDynCall('viii', 'onload') }}}(arg, buffer, byteArray.length);
{{{ makeDynCall('vppi', 'onload') }}}(userdata, buffer, byteArray.length);
_free(buffer);
});
}, function() {
}, () => {
if (onerror) {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
{{{ makeDynCall('vi', 'onerror') }}}(arg);
callUserCallback(() => {
{{{ makeDynCall('vp', 'onerror') }}}(userdata);
});
}
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
},

emscripten_async_wget2__deps: ['$PATH_FS', '$wget', '$withStackSave', '$stringToUTF8OnStack'],
emscripten_async_wget2__proxy: 'sync',
emscripten_async_wget2: (url, file, request, param, arg, onload, onerror, onprogress) => {
emscripten_async_wget2: (url, file, request, param, userdata, onload, onerror, onprogress) => {
{{{ runtimeKeepalivePush() }}}

var _url = UTF8ToString(url);
Expand All @@ -107,7 +103,7 @@ var LibraryWget = {
var destinationDirectory = PATH.dirname(_file);

// LOAD
http.onload = function http_onload(e) {
http.onload = (e) => {
{{{ runtimeKeepalivePop() }}}
if (http.status >= 200 && http.status < 300) {
// if a file exists there, we overwrite it
Expand All @@ -119,34 +115,34 @@ var LibraryWget = {

FS.createDataFile( _file.substr(0, index), _file.substr(index + 1), new Uint8Array(/** @type{ArrayBuffer}*/(http.response)), true, true, false);
if (onload) {
withStackSave(function() {
{{{ makeDynCall('viii', 'onload') }}}(handle, arg, stringToUTF8OnStack(_file));
withStackSave(() => {
{{{ makeDynCall('vipp', 'onload') }}}(handle, userdata, stringToUTF8OnStack(_file));
});
}
} else {
if (onerror) {{{ makeDynCall('viii', 'onerror') }}}(handle, arg, http.status);
if (onerror) {{{ makeDynCall('vipi', 'onerror') }}}(handle, userdata, http.status);
}

delete wget.wgetRequests[handle];
};

// ERROR
http.onerror = function http_onerror(e) {
http.onerror = (e) => {
{{{ runtimeKeepalivePop() }}}
if (onerror) {{{ makeDynCall('viii', 'onerror') }}}(handle, arg, http.status);
if (onerror) {{{ makeDynCall('vipi', 'onerror') }}}(handle, userdata, http.status);
delete wget.wgetRequests[handle];
};

// PROGRESS
http.onprogress = function http_onprogress(e) {
http.onprogress = (e) => {
if (e.lengthComputable || (e.lengthComputable === undefined && e.total != 0)) {
var percentComplete = (e.loaded / e.total)*100;
if (onprogress) {{{ makeDynCall('viii', 'onprogress') }}}(handle, arg, percentComplete);
if (onprogress) {{{ makeDynCall('vipi', 'onprogress') }}}(handle, userdata, percentComplete);
}
};

// ABORT
http.onabort = function http_onabort(e) {
http.onabort = (e) => {
{{{ runtimeKeepalivePop() }}}
delete wget.wgetRequests[handle];
};
Expand All @@ -166,7 +162,7 @@ var LibraryWget = {

emscripten_async_wget2_data__deps: ['$wget', 'malloc', 'free'],
emscripten_async_wget2_data__proxy: 'sync',
emscripten_async_wget2_data: (url, request, param, arg, free, onload, onerror, onprogress) => {
emscripten_async_wget2_data: (url, request, param, userdata, free, onload, onerror, onprogress) => {
var _url = UTF8ToString(url);
var _request = UTF8ToString(request);
var _param = UTF8ToString(param);
Expand All @@ -184,18 +180,18 @@ var LibraryWget = {
if (http.statusText) {
statusText = stringToUTF8OnStack(http.statusText);
}
{{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, statusText);
{{{ makeDynCall('vipip', 'onerror') }}}(handle, userdata, http.status, statusText);
});
}
}

// LOAD
http.onload = function http_onload(e) {
http.onload = (e) => {
if (http.status >= 200 && http.status < 300 || (http.status === 0 && _url.substr(0,4).toLowerCase() != "http")) {
var byteArray = new Uint8Array(/** @type{ArrayBuffer} */(http.response));
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
if (onload) {{{ makeDynCall('viiii', 'onload') }}}(handle, arg, buffer, byteArray.length);
if (onload) {{{ makeDynCall('vippi', 'onload') }}}(handle, userdata, buffer, byteArray.length);
if (free) _free(buffer);
} else {
onerrorjs();
Expand All @@ -204,18 +200,18 @@ var LibraryWget = {
};

// ERROR
http.onerror = function http_onerror(e) {
http.onerror = (e) => {
onerrorjs();
delete wget.wgetRequests[handle];
};

// PROGRESS
http.onprogress = function http_onprogress(e) {
if (onprogress) {{{ makeDynCall('viiii', 'onprogress') }}}(handle, arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0);
http.onprogress = (e) => {
if (onprogress) {{{ makeDynCall('viiii', 'onprogress') }}}(handle, userdata, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0);
};

// ABORT
http.onabort = function http_onabort(e) {
http.onabort = (e) => {
delete wget.wgetRequests[handle];
};

Expand Down
16 changes: 8 additions & 8 deletions system/include/emscripten/wget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ extern "C" {

void emscripten_async_wget(const char* url, const char* file, em_str_callback_func onload, em_str_callback_func onerror);

typedef void (*em_async_wget_onload_func)(void*, void*, int);
void emscripten_async_wget_data(const char* url, void *arg, em_async_wget_onload_func onload, em_arg_callback_func onerror);
typedef void (*em_async_wget_onload_func)(void* userdata, void* data, int size);
void emscripten_async_wget_data(const char* url, void *userdata, em_async_wget_onload_func onload, em_arg_callback_func onerror);

typedef void (*em_async_wget2_onload_func)(unsigned, void*, const char*);
typedef void (*em_async_wget2_onstatus_func)(unsigned, void*, int);
typedef void (*em_async_wget2_onload_func)(unsigned handle, void* userdata, const char* data);
typedef void (*em_async_wget2_onstatus_func)(unsigned handle, void* userdata, int status);

int emscripten_async_wget2(const char* url, const char* file, const char* requesttype, const char* param, void *arg, em_async_wget2_onload_func onload, em_async_wget2_onstatus_func onerror, em_async_wget2_onstatus_func onprogress);
int emscripten_async_wget2(const char* url, const char* file, const char* requesttype, const char* param, void *userdata, em_async_wget2_onload_func onload, em_async_wget2_onstatus_func onerror, em_async_wget2_onstatus_func onprogress);

typedef void (*em_async_wget2_data_onload_func)(unsigned, void*, void*, unsigned);
typedef void (*em_async_wget2_data_onerror_func)(unsigned, void*, int, const char*);
typedef void (*em_async_wget2_data_onprogress_func)(unsigned, void*, int, int);
typedef void (*em_async_wget2_data_onload_func)(unsigned handle, void* userdata, void* data, unsigned size);
typedef void (*em_async_wget2_data_onerror_func)(unsigned handle, void* userdata, int status, const char* status_text);
typedef void (*em_async_wget2_data_onprogress_func)(unsigned handle, void* userdata, int loaded, int total);

int emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, em_async_wget2_data_onload_func onload, em_async_wget2_data_onerror_func onerror, em_async_wget2_data_onprogress_func onprogress);

Expand Down
2 changes: 1 addition & 1 deletion test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,7 +2547,7 @@ def test_emscripten_async_wget2_data(self):
self.btest('test_emscripten_async_wget2_data.cpp', expected='0')

def test_emscripten_async_wget_side_module(self):
self.run_process([EMCC, test_file('browser_module.c'), '-o', 'lib.wasm', '-O2', '-sSIDE_MODULE'])
self.emcc(test_file('browser_module.c'), ['-o', 'lib.wasm', '-O2', '-sSIDE_MODULE'])
self.btest_exit('browser_main.c', args=['-O2', '-sMAIN_MODULE=2'])

@parameterized({
Expand Down