Skip to content
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
4 changes: 2 additions & 2 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ async function getWasmBinary(binaryFile) {
if (!wasmBinary) {
// Fetch the binary using readAsync
try {
var response = await readAsync(binaryFile);
return new Uint8Array(response);
// Await so a rejection is caught and falls back to getBinarySync.
return await readAsync(binaryFile);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Returning readAsync(binaryFile) directly means that on the web, where readAsync returns an ArrayBuffer, getWasmBinary will now return an ArrayBuffer instead of a Uint8Array.

While WebAssembly.instantiate accepts both, other parts of the application or user-defined code might access Module.wasmBinary (or the returned value of getWasmBinary) and expect a Uint8Array (for example, checking .length or calling .subarray()). Since ArrayBuffer does not have these properties/methods, this change could introduce runtime errors or silent bugs on the web.

To avoid copying the Buffer under Node (since Buffer is a subclass of Uint8Array) while still ensuring a Uint8Array is returned on the web, we can check if the response is already an instance of Uint8Array and only wrap it in new Uint8Array() if it is not. This preserves the performance optimization under Node without changing the return type on the web.

      var response = await readAsync(binaryFile);
      return response instanceof Uint8Array ? response : new Uint8Array(response);

@Project516 Project516 Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The return value never reaches Module.wasmBinary or user code. getWasmBinary has one caller, instantiateArrayBuffer, which passes the result straight to WebAssembly.instantiate(binary, imports), and that accepts an ArrayBuffer as well as a typed array. The function is internal (it is not in any exported runtime symbol list), and wasmBinary is a separate variable that this code only reads.

So on the web the value now stays an ArrayBuffer all the way into instantiate, and nothing else sees it. I kept the plain return because it is smaller in every codesize expectation. If you would rather keep a Uint8Array on the web, I can switch to response instanceof Uint8Array ? response : new Uint8Array(response).

Comment thread
sbc100 marked this conversation as resolved.
Resolved
} catch {
// Fall back to getBinarySync below;
}
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19117,
"a.out.js.gz": 8088,
"a.out.js": 19093,
"a.out.js.gz": 8090,
"a.out.nodebug.wasm": 133990,
"a.out.nodebug.wasm.gz": 51234,
"total": 153107,
"total_gz": 59322,
"total": 153083,
"total_gz": 59324,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19094,
"a.out.js.gz": 8072,
"a.out.js": 19070,
"a.out.js.gz": 8074,
"a.out.nodebug.wasm": 133418,
"a.out.nodebug.wasm.gz": 50931,
"total": 152512,
"total_gz": 59003,
"total": 152488,
"total_gz": 59005,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22810,
"a.out.js.gz": 9040,
"a.out.js": 22786,
"a.out.js.gz": 9044,
"a.out.nodebug.wasm": 174365,
"a.out.nodebug.wasm.gz": 58527,
"total": 197175,
"total_gz": 67567,
"total": 197151,
"total_gz": 67571,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18916,
"a.out.js.gz": 7997,
"a.out.js": 18892,
"a.out.js.gz": 8001,
"a.out.nodebug.wasm": 149635,
"a.out.nodebug.wasm.gz": 56293,
"total": 168551,
"total_gz": 64290,
"total": 168527,
"total_gz": 64294,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm_legacy.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18994,
"a.out.js.gz": 8020,
"a.out.js": 18970,
"a.out.js.gz": 8024,
"a.out.nodebug.wasm": 147417,
"a.out.nodebug.wasm.gz": 55962,
"total": 166411,
"total_gz": 63982,
"total": 166387,
"total_gz": 63986,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_lto.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18456,
"a.out.js.gz": 7766,
"a.out.js": 18432,
"a.out.js.gz": 7767,
"a.out.nodebug.wasm": 101025,
"a.out.nodebug.wasm.gz": 38272,
"total": 119481,
"total_gz": 46038,
"total": 119457,
"total_gz": 46039,
"sent": [
"a (emscripten_resize_heap)",
"b (_setitimer_js)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22860,
"a.out.js.gz": 9060,
"a.out.js": 22836,
"a.out.js.gz": 9064,
"a.out.nodebug.wasm": 241374,
"a.out.nodebug.wasm.gz": 80940,
"total": 264234,
"total_gz": 90000,
"total": 264210,
"total_gz": 90004,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_noexcept.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19117,
"a.out.js.gz": 8088,
"a.out.js": 19093,
"a.out.js.gz": 8090,
"a.out.nodebug.wasm": 135897,
"a.out.nodebug.wasm.gz": 51846,
"total": 155014,
"total_gz": 59934,
"total": 154990,
"total_gz": 59936,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 6590,
"a.out.js.gz": 3132,
"a.out.js": 6566,
"a.out.js.gz": 3138,
"a.out.nodebug.wasm": 174015,
"a.out.nodebug.wasm.gz": 64745,
"total": 180605,
"total_gz": 67877,
"total": 180581,
"total_gz": 67883,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_file_preload.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ async function getWasmBinary(binaryFile) {
if (!wasmBinary) {
// Fetch the binary using readAsync
try {
var response = await readAsync(binaryFile);
return new Uint8Array(response);
// Await so a rejection is caught and falls back to getBinarySync.
return await readAsync(binaryFile);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Returning readAsync(binaryFile) directly means that on the web, where readAsync returns an ArrayBuffer, getWasmBinary will now return an ArrayBuffer instead of a Uint8Array.

While WebAssembly.instantiate accepts both, other parts of the application or user-defined code might access Module.wasmBinary (or the returned value of getWasmBinary) and expect a Uint8Array (for example, checking .length or calling .subarray()). Since ArrayBuffer does not have these properties/methods, this change could introduce runtime errors or silent bugs on the web.

To avoid copying the Buffer under Node (since Buffer is a subclass of Uint8Array) while still ensuring a Uint8Array is returned on the web, we can check if the response is already an instance of Uint8Array and only wrap it in new Uint8Array() if it is not. This preserves the performance optimization under Node without changing the return type on the web.

      var response = await readAsync(binaryFile);
      return response instanceof Uint8Array ? response : new Uint8Array(response);

} catch {}
}
// Otherwise, getBinarySync should be able to get it synchronously
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_file_preload.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22169,
"a.out.js.gz": 9244,
"a.out.js": 22145,
"a.out.js.gz": 9246,
"a.out.nodebug.wasm": 1198,
"a.out.nodebug.wasm.gz": 730,
"total": 23367,
"total_gz": 9974,
"total": 23343,
"total_gz": 9976,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_files_js_fs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 17768,
"a.out.js.gz": 7413,
"a.out.js": 17744,
"a.out.js.gz": 7414,
"a.out.nodebug.wasm": 381,
"a.out.nodebug.wasm.gz": 258,
"total": 18149,
"total_gz": 7671,
"total": 18125,
"total_gz": 7672,
"sent": [
"a (fd_write)",
"b (fd_read)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_files_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 4965,
"a.out.js.gz": 2389,
"a.out.js": 4941,
"a.out.js.gz": 2396,
"a.out.nodebug.wasm": 49989,
"a.out.nodebug.wasm.gz": 17676,
"total": 54954,
"total_gz": 20065,
"total": 54930,
"total_gz": 20072,
"sent": [
"a (emscripten_date_now)",
"b (emscripten_err)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O0.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23395,
"a.out.js.gz": 8502,
"a.out.js": 23364,
"a.out.js.gz": 8504,
"a.out.nodebug.wasm": 14275,
"a.out.nodebug.wasm.gz": 7098,
"total": 37670,
"total_gz": 15600,
"total": 37639,
"total_gz": 15602,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 5545,
"a.out.js.gz": 2250,
"a.out.js": 5514,
"a.out.js.gz": 2252,
"a.out.nodebug.wasm": 2050,
"a.out.nodebug.wasm.gz": 1231,
"total": 7595,
"total_gz": 3481,
"total": 7564,
"total_gz": 3483,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3867,
"a.out.js.gz": 1973,
"a.out.js": 3843,
"a.out.js.gz": 1977,
"a.out.nodebug.wasm": 1444,
"a.out.nodebug.wasm.gz": 922,
"total": 5311,
"total_gz": 2895,
"total": 5287,
"total_gz": 2899,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O3.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3809,
"a.out.js.gz": 1930,
"a.out.js": 3785,
"a.out.js.gz": 1937,
"a.out.nodebug.wasm": 1198,
"a.out.nodebug.wasm.gz": 730,
"total": 5007,
"total_gz": 2660,
"total": 4983,
"total_gz": 2667,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_Os.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3809,
"a.out.js.gz": 1930,
"a.out.js": 3785,
"a.out.js.gz": 1937,
"a.out.nodebug.wasm": 1188,
"a.out.nodebug.wasm.gz": 732,
"total": 4997,
"total_gz": 2662,
"total": 4973,
"total_gz": 2669,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_Oz.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3440,
"a.out.js.gz": 1731,
"a.out.js": 3416,
"a.out.js.gz": 1734,
"a.out.nodebug.wasm": 1188,
"a.out.nodebug.wasm.gz": 732,
"total": 4628,
"total_gz": 2463,
"total": 4604,
"total_gz": 2466,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_dylink.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 26229,
"a.out.js.gz": 11137,
"a.out.js": 26205,
"a.out.js.gz": 11141,
"a.out.nodebug.wasm": 16986,
"a.out.nodebug.wasm.gz": 8667,
"total": 43215,
"total_gz": 19804,
"total": 43191,
"total_gz": 19808,
"sent": [
"__syscall_stat64",
"emscripten_resize_heap",
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 272917,
"a.out.js": 272893,
"a.out.nodebug.wasm": 589443,
"total": 862360,
"total": 862336,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_export_nothing.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 2648,
"a.out.js.gz": 1293,
"a.out.js": 2624,
"a.out.js.gz": 1296,
"a.out.nodebug.wasm": 43,
"a.out.nodebug.wasm.gz": 59,
"total": 2691,
"total_gz": 1352,
"total": 2667,
"total_gz": 1355,
"sent": [],
"imports": [],
"exports": [
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3809,
"a.out.js.gz": 1930,
"a.out.js": 3785,
"a.out.js.gz": 1937,
"a.out.nodebug.wasm": 1198,
"a.out.nodebug.wasm.gz": 730,
"total": 5007,
"total_gz": 2660,
"total": 4983,
"total_gz": 2667,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_libcxxabi_message_O3.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3016,
"a.out.js.gz": 1481,
"a.out.js": 2992,
"a.out.js.gz": 1486,
"a.out.nodebug.wasm": 89,
"a.out.nodebug.wasm.gz": 98,
"total": 3105,
"total_gz": 1579,
"total": 3081,
"total_gz": 1584,
"sent": [],
"imports": [],
"exports": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3099,
"a.out.js.gz": 1533,
"a.out.js": 3075,
"a.out.js.gz": 1536,
"a.out.nodebug.wasm": 222,
"a.out.nodebug.wasm.gz": 206,
"total": 3321,
"total_gz": 1739,
"total": 3297,
"total_gz": 1742,
"sent": [
"proc_exit"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_mem_O3.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"a.out.js": 3863,
"a.out.js": 3838,
"a.out.js.gz": 1906,
"a.out.nodebug.wasm": 5268,
"a.out.nodebug.wasm.gz": 2431,
"total": 9131,
"total": 9106,
"total_gz": 4337,
"sent": [
"a (emscripten_resize_heap)"
Expand Down
Loading
Loading