Skip to content

Commit 0c5cb79

Browse files
authored
[asyncify] Refactor argument restoring under wasm64. NFC (#21984)
This change was split out from my larger change to switch to table64. When we use MEMORY64 + ASYNCIFY=1 we have add multiple wrappers to the wasm exports. This change basically moves the argument restoration to the outside rather than doing int the wrapper. This means that all the different wrappers see the restored arguments, not just the asyncify wrapper (which is the first/lowest level wrapper.
1 parent 3052664 commit 0c5cb79

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/library_async.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ addToLibrary({
9595
}
9696
},
9797
#if ASYNCIFY == 1 && MEMORY64
98-
saveOrRestoreRewindArguments(funcName, passedArguments) {
99-
if (passedArguments.length === 0) {
100-
return Asyncify.rewindArguments[funcName] || []
101-
}
98+
saveRewindArguments(funcName, passedArguments) {
10299
return Asyncify.rewindArguments[funcName] = Array.from(passedArguments)
103100
},
101+
restoreRewindArguments(funcName) {
102+
return Asyncify.rewindArguments[funcName] || []
103+
},
104104
#endif
105105
instrumentWasmExports(exports) {
106106
#if ASYNCIFY_DEBUG
@@ -130,14 +130,9 @@ addToLibrary({
130130
try {
131131
#endif
132132
#if ASYNCIFY == 1 && MEMORY64
133-
// When re-winding, the arguments to a function are ignored. For i32 arguments we
134-
// can just call the function with no args at all since and the engine will produce zeros
135-
// for all arguments. However, for i64 arguments we get `undefined cannot be converted to
136-
// BigInt`.
137-
return original(...Asyncify.saveOrRestoreRewindArguments(x, args));
138-
#else
139-
return original(...args);
133+
Asyncify.saveRewindArguments(x, args);
140134
#endif
135+
return original(...args);
141136
#if ASYNCIFY == 1
142137
} finally {
143138
if (!ABORT) {
@@ -265,12 +260,16 @@ addToLibrary({
265260
{{{ makeSetValue('ptr', C_STRUCTS.asyncify_data_s.rewind_id, 'rewindId', 'i32') }}};
266261
},
267262

263+
getDataRewindFuncName(ptr) {
264+
var id = {{{ makeGetValue('ptr', C_STRUCTS.asyncify_data_s.rewind_id, 'i32') }}};
265+
var name = Asyncify.callStackIdToName[id];
266+
return name;
267+
},
268+
268269
#if RELOCATABLE
269270
getDataRewindFunc__deps: [ '$resolveGlobalSymbol' ],
270271
#endif
271-
getDataRewindFunc(ptr) {
272-
var id = {{{ makeGetValue('ptr', C_STRUCTS.asyncify_data_s.rewind_id, 'i32') }}};
273-
var name = Asyncify.callStackIdToName[id];
272+
getDataRewindFunc(name) {
274273
var func = wasmExports[name];
275274
#if RELOCATABLE
276275
// Exported functions in side modules are not listed in `wasmExports`,
@@ -283,14 +282,23 @@ addToLibrary({
283282
},
284283

285284
doRewind(ptr) {
286-
var start = Asyncify.getDataRewindFunc(ptr);
285+
var name = Asyncify.getDataRewindFuncName(ptr);
286+
var func = Asyncify.getDataRewindFunc(name);
287287
#if ASYNCIFY_DEBUG
288-
dbg('ASYNCIFY: start:', start);
288+
dbg('ASYNCIFY: doRewind:', name);
289289
#endif
290290
// Once we have rewound and the stack we no longer need to artificially
291291
// keep the runtime alive.
292292
{{{ runtimeKeepalivePop(); }}}
293-
return start();
293+
#if MEMORY64
294+
// When re-winding, the arguments to a function are ignored. For i32 arguments we
295+
// can just call the function with no args at all since and the engine will produce zeros
296+
// for all arguments. However, for i64 arguments we get `undefined cannot be converted to
297+
// BigInt`.
298+
return func(...Asyncify.restoreRewindArguments(name));
299+
#else
300+
return func();
301+
#endif
294302
},
295303

296304
// This receives a function to call to start the async operation, and
@@ -441,7 +449,7 @@ addToLibrary({
441449
},
442450
makeAsyncFunction(original) {
443451
#if ASYNCIFY_DEBUG
444-
dbg('asyncify: returnPromiseOnSuspend for', original);
452+
dbg('asyncify: makeAsyncFunction for', original);
445453
#endif
446454
return WebAssembly.promising(original);
447455
},

0 commit comments

Comments
 (0)