--- a/js/lib.js +++ b/js/lib.js @@ -41,7 +41,7 @@ fs_export_file: function(filename, buf, buf_len) { - var _filename = Pointer_stringify(filename); + var _filename = UTF8ToString(filename); // console.log("exporting " + _filename); var data = HEAPU8.subarray(buf, buf + buf_len); var file = new Blob([data], { type: "application/octet-stream" }); @@ -58,20 +58,20 @@ }, 50); }, - emscripten_async_wget3_data: function(url, request, user, password, post_data, post_data_len, arg, free, onload, onerror, onprogress) { - var _url = Pointer_stringify(url); - var _request = Pointer_stringify(request); + temu_wget_data: function(url, request, user, password, post_data, post_data_len, arg, free, onload, onerror, onprogress) { + var _url = UTF8ToString(url); + var _request = UTF8ToString(request); var _user; var _password; var http = new XMLHttpRequest(); if (user) - _user = Pointer_stringify(user); + _user = UTF8ToString(user); else _user = null; if (password) - _password = Pointer_stringify(password); + _password = UTF8ToString(password); else _password = null; @@ -81,7 +81,7 @@ http.setRequestHeader("Authorization", "Basic " + btoa(_user + ':' + _password)); } - var handle = Browser.getNextWgetRequestHandle(); + var handle = (Module.temuWgetHandle = (Module.temuWgetHandle || 0) + 1); // LOAD http.onload = function http_onload(e) { @@ -89,37 +89,32 @@ var byteArray = new Uint8Array(http.response); var buffer = _malloc(byteArray.length); HEAPU8.set(byteArray, buffer); - if (onload) Runtime.dynCall('viiii', onload, [handle, arg, buffer, byteArray.length]); + if (onload) {{{ makeDynCall('viiii', 'onload') }}}(handle, arg, buffer, byteArray.length); if (free) _free(buffer); } else { - if (onerror) Runtime.dynCall('viiii', onerror, [handle, arg, http.status, http.statusText]); + if (onerror) {{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, 0); } - delete Browser.wgetRequests[handle]; + delete (Module.temuWgetRequests = Module.temuWgetRequests || {})[handle]; }; // ERROR http.onerror = function http_onerror(e) { if (onerror) { - Runtime.dynCall('viiii', onerror, [handle, arg, http.status, http.statusText]); + {{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, 0); } - delete Browser.wgetRequests[handle]; + delete (Module.temuWgetRequests = Module.temuWgetRequests || {})[handle]; }; // PROGRESS http.onprogress = function http_onprogress(e) { - if (onprogress) Runtime.dynCall('viiii', onprogress, [handle, arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0]); + if (onprogress) {{{ makeDynCall('viiii', 'onprogress') }}}(handle, arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0); }; // ABORT http.onabort = function http_onabort(e) { - delete Browser.wgetRequests[handle]; + delete (Module.temuWgetRequests = Module.temuWgetRequests || {})[handle]; }; - // Useful because the browser can limit the number of redirection - try { - if (http.channel instanceof Ci.nsIHttpChannel) - http.channel.redirectionLimit = 0; - } catch (ex) { /* whatever */ } if (_request == "POST") { var _post_data = HEAPU8.subarray(post_data, post_data + post_data_len); @@ -132,7 +127,7 @@ http.send(null); } - Browser.wgetRequests[handle] = http; + (Module.temuWgetRequests = Module.temuWgetRequests || {})[handle] = http; return handle; }, @@ -181,16 +176,16 @@ file_buffer_get_new_handle: function() { var h; - if (typeof Browser.fbuf_table == "undefined") { - Browser.fbuf_table = new Object(); - Browser.fbuf_next_handle = 1; + if (typeof Module.temuFbuf == "undefined") { + Module.temuFbuf = new Object(); + Module.temuFbufNext = 1; } for(;;) { - h = Browser.fbuf_next_handle; - Browser.fbuf_next_handle++; - if (Browser.fbuf_next_handle == 0x80000000) - Browser.fbuf_next_handle = 1; - if (typeof Browser.fbuf_table[h] == "undefined") { + h = Module.temuFbufNext; + Module.temuFbufNext++; + if (Module.temuFbufNext == 0x80000000) + Module.temuFbufNext = 1; + if (typeof Module.temuFbuf[h] == "undefined") { // console.log("new handle=" + h); return h; } @@ -212,15 +207,15 @@ size = HEAPU32[(bs + 4) >> 2]; if (new_size == 0) { if (h != 0) { - delete Browser.fbuf_table[h]; + delete Module.temuFbuf[h]; h = 0; } } else if (size == 0) { h = _file_buffer_get_new_handle(); new_data = new Uint8Array(new_size); - Browser.fbuf_table[h] = new_data; + Module.temuFbuf[h] = new_data; } else if (size != new_size) { - data = Browser.fbuf_table[h]; + data = Module.temuFbuf[h]; new_data = new Uint8Array(new_size); if (new_size > size) { new_data.set(data, 0); @@ -228,13 +223,14 @@ for(i = 0; i < new_size; i = (i + 1) | 0) new_data[i] = data[i]; } - Browser.fbuf_table[h] = new_data; + Module.temuFbuf[h] = new_data; } HEAPU32[bs >> 2] = h; HEAPU32[(bs + 4) >> 2] = new_size; return 0; }, + file_buffer_reset__deps: ['file_buffer_resize', 'file_buffer_init'], file_buffer_reset: function(bs) { _file_buffer_resize(bs, 0); @@ -246,7 +242,7 @@ var h, data, i; h = HEAPU32[bs >> 2]; if (h) { - data = Browser.fbuf_table[h]; + data = Module.temuFbuf[h]; for(i = 0; i < size; i = (i + 1) | 0) { data[offset + i] = HEAPU8[buf + i]; } @@ -258,7 +254,7 @@ var h, data, i; h = HEAPU32[bs >> 2]; if (h) { - data = Browser.fbuf_table[h]; + data = Module.temuFbuf[h]; for(i = 0; i < size; i = (i + 1) | 0) { HEAPU8[buf + i] = data[offset + i]; } @@ -270,7 +266,7 @@ var h, data, i; h = HEAPU32[bs >> 2]; if (h) { - data = Browser.fbuf_table[h]; + data = Module.temuFbuf[h]; for(i = 0; i < size; i = (i + 1) | 0) { data[offset + i] = val; } --- a/fs_wget.c +++ b/fs_wget.c @@ -92,7 +92,7 @@ s->cb(s->opaque, 0, data, size); } -extern int emscripten_async_wget3_data(const char* url, const char* requesttype, const char *user, const char *password, const uint8_t *post_data, int post_data_len, 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); +extern int temu_wget_data(const char* url, const char* requesttype, const char *user, const char *password, const uint8_t *post_data, int post_data_len, 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); XHRState *fs_wget2(const char *url, const char *user, const char *password, WGetReadCallback *read_cb, uint64_t post_data_len, @@ -116,7 +116,7 @@ } fs_wget_update_downloading_count(1); - emscripten_async_wget3_data(url, request, user, password, + temu_wget_data(url, request, user, password, post_data, post_data_len, s, 1, fs_wget_onload, fs_wget_onerror, NULL); if (post_data_len != 0) --- a/b/Makefile.emcc +++ b/Makefile.emcc @@ -0,0 +1,23 @@ +# riscvemu64-wasm with a modern emscripten (6.x), from the 2019 TinyEMU source. +EMCC=emcc +EMCFLAGS=-O2 -w -DEMSCRIPTEN -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -MMD \ + -fno-strict-aliasing -DCONFIG_FS_NET -DMAX_XLEN=64 -DCONFIG_RISCV_MAX_XLEN=64 +EMLDFLAGS=-O3 -s EXPORTED_FUNCTIONS=_console_queue_char,_vm_start,_fs_import_file,_display_key_event,_display_mouse_event,_display_wheel_event,_net_write_packet,_net_set_carrier,_malloc,_free \ + -s EXPORTED_RUNTIME_METHODS=ccall,cwrap \ + -s INITIAL_MEMORY=67108864 -s ALLOW_MEMORY_GROWTH=1 \ + -s ENVIRONMENT=web -s FILESYSTEM=0 --no-entry \ + --js-library js/lib.js + +OBJS=jsemu.o softfp.o virtio.o fs.o fs_net.o fs_wget.o fs_utils.o simplefb.o \ + pci.o json.o block_net.o iomem.o cutils.o aes.o sha256.o \ + riscv_cpu.o riscv_machine.o machine.o + +all: riscvemu64-wasm.js + +%.o: %.c + $(EMCC) $(EMCFLAGS) -c -o $@ $< + +riscvemu64-wasm.js: $(OBJS) js/lib.js + $(EMCC) $(EMLDFLAGS) -o $@ $(OBJS) + +-include $(wildcard *.d)