Skip to content
Merged
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
16 changes: 10 additions & 6 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ function ccall(ident, returnType, argTypes, args, opts) {
}
#endif
#if ASYNCIFY && WASM_BACKEND
if (typeof Asyncify === 'object' && Asyncify.currData) {
// The WASM function ran asynchronous and unwound its stack.
var asyncMode = opts && opts.async;
var runningAsync = typeof Asyncify === 'object' && Asyncify.currData;
var prevRunningAsync = typeof Asyncify === 'object' && Asyncify.asyncFinalizers.length > 0;
#if ASSERTIONS
assert(!asyncMode || !prevRunningAsync, 'Cannot have multiple async ccalls in flight at once');
#endif
if (runningAsync && !prevRunningAsync) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a comment here to explain, perhaps something like // Check if we started an async operation right now.

// Check if we started an async operation just now.
// If so, the WASM function ran asynchronous and unwound its stack.
// We need to return a Promise that resolves the return value
// once the stack is rewound and execution finishes.
#if ASSERTIONS
assert(opts && opts.async, 'The call to ' + ident + ' is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.');
// Once the asyncFinalizers are called, asyncFinalizers gets reset to [].
// If they are not empty, then another async ccall is in-flight and not finished.
assert(Asyncify.asyncFinalizers.length === 0, 'Cannot have multiple async ccalls in flight at once');
assert(asyncMode, 'The call to ' + ident + ' is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.');
#endif
return new Promise(function(resolve) {
Asyncify.asyncFinalizers.push(function(ret) {
Expand Down