]> git.kaiwu.me - njs.git/commitdiff
Fixed njs_function_copy().
authorDmitry Volyntsev <xeioex@nginx.com>
Sun, 7 Apr 2019 05:38:04 +0000 (13:38 +0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Sun, 7 Apr 2019 05:38:04 +0000 (13:38 +0800)
njs/njs_function.c
njs/test/njs_unit_test.c

index 7847e78f46b5809981c6328c1ef036b9858826ab..1f10c0ebe2e302dad5660b9eaca6a06bed0e5f42 100644 (file)
@@ -15,6 +15,11 @@ static njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args,
     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 njs_function_t *function)
 {
     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 njs_function_t *function)
 
     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 njs_function_t *function)
 
     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, njs_index_t retval,
     nesting = lambda->nesting;
 
     if (nesting != 0) {
-        closures = (function->closure) ? function->closures
-                                       : vm->active_frame->closures;
+        closures = njs_function_closures(vm, function);
         do {
             closure = *closures++;
 
index fb668e6879ca3cc3b800f7f552dca86f20235f41..b1903164e2f98faceb8c8726fec75e7b1eb913cd 100644 (file)
@@ -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") },