From 53ef87d59e26004eaf20b3e9381a5f270b22374d Mon Sep 17 00:00:00 2001 From: Ajay Patel Date: Wed, 11 Sep 2019 12:48:43 -0700 Subject: [PATCH 1/3] Update preamble.js Makes changes related to discussion in #9412 --- src/preamble.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/preamble.js b/src/preamble.js index 599b25f454658..e7e5d96dedfd4 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -138,15 +138,17 @@ function ccall(ident, returnType, argTypes, args, opts) { } #endif #if ASYNCIFY && WASM_BACKEND - if (typeof Asyncify === 'object' && Asyncify.currData) { +#if ASSERTIONS + // 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((!opts || !opts.async) || Asyncify.asyncFinalizers.length === 0, 'Cannot have multiple async ccalls in flight at once'); +#endif + if (typeof Asyncify === 'object' && Asyncify.currData && Asyncify.asyncFinalizers.length === 0) { // 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'); #endif return new Promise(function(resolve) { Asyncify.asyncFinalizers.push(function(ret) { From 0cd5d453639afbf14690b715b57953909bdfa601 Mon Sep 17 00:00:00 2001 From: Ajay Patel Date: Sat, 14 Sep 2019 11:27:03 -0700 Subject: [PATCH 2/3] Update preamble.js Simplify condition logic into variables --- src/preamble.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/preamble.js b/src/preamble.js index e7e5d96dedfd4..a40a63b58d815 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -138,17 +138,18 @@ function ccall(ident, returnType, argTypes, args, opts) { } #endif #if ASYNCIFY && WASM_BACKEND + var asyncMode = opts && opts.async; + var runningAsync = typeof Asyncify === 'object' && Asyncify.currData; + var prevRunningAsync = Asyncify.asyncFinalizers.length > 0; #if ASSERTIONS - // 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((!opts || !opts.async) || Asyncify.asyncFinalizers.length === 0, 'Cannot have multiple async ccalls in flight at once'); + assert(!asyncMode || !prevRunningAsync, 'Cannot have multiple async ccalls in flight at once'); #endif - if (typeof Asyncify === 'object' && Asyncify.currData && Asyncify.asyncFinalizers.length === 0) { + if (runningAsync && !prevRunningAsync) { // 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.'); + 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) { From d12316e7031be4128e40c4ba0d9a96a7d1e9e633 Mon Sep 17 00:00:00 2001 From: Ajay Patel Date: Mon, 16 Sep 2019 18:11:08 -0700 Subject: [PATCH 3/3] Update preamble.js --- src/preamble.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/preamble.js b/src/preamble.js index 18b2da10b552b..532737d1211dc 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -140,12 +140,13 @@ function ccall(ident, returnType, argTypes, args, opts) { #if ASYNCIFY && WASM_BACKEND var asyncMode = opts && opts.async; var runningAsync = typeof Asyncify === 'object' && Asyncify.currData; - var prevRunningAsync = Asyncify.asyncFinalizers.length > 0; + 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) { - // The WASM function ran asynchronous and unwound its stack. + // 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