[njs] Fixed order of code execution after await in try block.

Alexander Borisov alexander.borisov at nginx.com
Wed Sep 1 18:26:41 UTC 2021


details:   https://hg.nginx.org/njs/rev/4b018107386d
branches:  
changeset: 1699:4b018107386d
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Wed Sep 01 21:25:10 2021 +0300
description:
Fixed order of code execution after await in try block.

The bug was introduced in 92d10cd761e2.

diffstat:

 src/njs_async.c                    |   4 +++-
 test/js/async_await_try_resolve.js |  15 +++++++++++++++
 test/njs_expect_test.exp           |   2 ++
 3 files changed, 20 insertions(+), 1 deletions(-)

diffs (49 lines):

diff -r 92d10cd761e2 -r 4b018107386d src/njs_async.c
--- a/src/njs_async.c	Wed Sep 01 16:31:08 2021 +0300
+++ b/src/njs_async.c	Wed Sep 01 21:25:10 2021 +0300
@@ -90,7 +90,7 @@ njs_await_fulfilled(njs_vm_t *vm, njs_va
 
     vm->top_frame->retval = &vm->retval;
 
-    ret = njs_vmcode_interpreter(vm, async->pc);
+    ret = njs_vmcode_interpreter(vm, ctx->pc);
 
     vm->levels[NJS_LEVEL_LOCAL] = cur_local;
     vm->levels[NJS_LEVEL_CLOSURE] = cur_closures;
@@ -148,6 +148,8 @@ njs_await_rejected(njs_vm_t *vm, njs_val
         return NJS_ERROR;
     }
 
+    ctx->pc = ctx->await->pc;
+
     return njs_await_fulfilled(vm, args, nargs, unused);
 }
 
diff -r 92d10cd761e2 -r 4b018107386d test/js/async_await_try_resolve.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/async_await_try_resolve.js	Wed Sep 01 21:25:10 2021 +0300
@@ -0,0 +1,15 @@
+async function af() {
+    let key;
+
+    try {
+        key = await Promise.resolve("key");
+        key += ": resolve";
+
+    } catch (e) {
+        key += ": exception";
+    }
+
+    return key;
+};
+
+af().then(v => console.log(v));
diff -r 92d10cd761e2 -r 4b018107386d test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Wed Sep 01 16:31:08 2021 +0300
+++ b/test/njs_expect_test.exp	Wed Sep 01 21:25:10 2021 +0300
@@ -1171,3 +1171,5 @@ njs_run {"./test/js/async_await_try_thro
 finally
 end"
 
+njs_run {"./test/js/async_await_try_resolve.js"} \
+"key: resolve"


More information about the nginx-devel mailing list