[njs] Fixed njs_function_copy().

Dmitry Volyntsev xeioex at nginx.com
Wed Apr 10 14:28:17 UTC 2019


details:   https://hg.nginx.org/njs/rev/0781f269a0e2
branches:  
changeset: 875:0781f269a0e2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Sun Apr 07 13:38:04 2019 +0800
description:
Fixed njs_function_copy().

diffstat:

 njs/njs_function.c       |  14 ++++++++++----
 njs/test/njs_unit_test.c |   6 ++++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diffs (72 lines):

diff -r d92dc72dd58d -r 0781f269a0e2 njs/njs_function.c
--- a/njs/njs_function.c	Tue Apr 09 15:00:27 2019 +0300
+++ b/njs/njs_function.c	Sun Apr 07 13:38:04 2019 +0800
@@ -15,6 +15,11 @@ static njs_ret_t njs_normalize_args(njs_
     uint8_t *args_types, nxt_uint_t nargs);
 
 
+#define njs_function_closures(vm, function)                                 \
+    (njs_closure_t **) ((function->closure) ? function->closures            \
+                                            : vm->active_frame->closures)
+
+
 njs_function_t *
 njs_function_alloc(njs_vm_t *vm, njs_function_lambda_t *lambda,
     njs_closure_t *closures[], nxt_bool_t shared)
@@ -98,6 +103,7 @@ njs_function_copy(njs_vm_t *vm, const nj
 {
     size_t          size;
     nxt_uint_t      n, nesting;
+    njs_closure_t   **closures;
     njs_function_t  *copy;
 
     nesting = (function->native) ? 0 : function->u.lambda->nesting;
@@ -106,7 +112,6 @@ njs_function_copy(njs_vm_t *vm, const nj
 
     copy = nxt_mp_alloc(vm->mem_pool, size);
     if (nxt_slow_path(copy == NULL)) {
-        njs_memory_error(vm);
         return NULL;
     }
 
@@ -120,11 +125,13 @@ njs_function_copy(njs_vm_t *vm, const nj
 
     copy->closure = 1;
 
+    closures = njs_function_closures(vm, function);
+
     n = 0;
 
     do {
         /* GC: retain closure. */
-        copy->closures[n] = vm->active_frame->closures[n];
+        copy->closures[n] = closures[n];
         n++;
     } while (n < nesting);
 
@@ -470,8 +477,7 @@ njs_function_lambda_call(njs_vm_t *vm, n
     nesting = lambda->nesting;
 
     if (nesting != 0) {
-        closures = (function->closure) ? function->closures
-                                       : vm->active_frame->closures;
+        closures = njs_function_closures(vm, function);
         do {
             closure = *closures++;
 
diff -r d92dc72dd58d -r 0781f269a0e2 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Apr 09 15:00:27 2019 +0300
+++ b/njs/test/njs_unit_test.c	Sun Apr 07 13:38:04 2019 +0800
@@ -6362,6 +6362,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("function f() { var a; function baz() { a = 1; return a; } return baz.bind()(); } f()"),
       nxt_string("1") },
 
+    { nxt_string("(function(){ var a = 1; return (function() { return a; })})().bind()()"),
+      nxt_string("1") },
+
+    { nxt_string("function f() { var a = 1; function baz() { return a; } return baz; } f().bind()()"),
+      nxt_string("1") },
+
     { nxt_string("function f(a, b) { return a + b }"
                  "f(3,4) === f.bind()(3,4)"),
       nxt_string("true") },


More information about the nginx-devel mailing list