[njs] Fixed Function constructor handling when called without arguments.

noreply at nginx.com noreply at nginx.com
Sat Jul 6 01:28:02 UTC 2024


details:   https://github.com/nginx/njs/commit/b593dd4aba0f5c730c1d90072cdee7dd9a93beed
branches:  master
commit:    b593dd4aba0f5c730c1d90072cdee7dd9a93beed
user:      Vadim Zhestikov <v.zhestikov at f5.com>
date:      Tue, 2 Jul 2024 14:55:03 -0700
description:
Fixed Function constructor handling when called without arguments.

Corrected the behavior of Function.constructor() when invoked without arguments
relative to an object.

---
 src/njs_function.c       | 8 +++++---
 src/test/njs_unit_test.c | 3 +++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/njs_function.c b/src/njs_function.c
index bfdf3f35..c677be57 100644
--- a/src/njs_function.c
+++ b/src/njs_function.c
@@ -1054,9 +1054,11 @@ njs_function_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
     njs_chb_append_literal(&chain, "){");
 
-    ret = njs_value_to_chain(vm, &chain, njs_argument(args, nargs - 1));
-    if (njs_slow_path(ret < NJS_OK)) {
-        return ret;
+    if (nargs > 1) {
+        ret = njs_value_to_chain(vm, &chain, njs_argument(args, nargs - 1));
+        if (njs_slow_path(ret < NJS_OK)) {
+            return ret;
+        }
     }
 
     njs_chb_append_literal(&chain, "})");
diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c
index 530cf6ff..2f0e318c 100644
--- a/src/test/njs_unit_test.c
+++ b/src/test/njs_unit_test.c
@@ -14147,6 +14147,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Function.constructor === Function"),
       njs_str("true") },
 
+    { njs_str("Function.constructor()"),
+      njs_str("[object Function]") },
+
     { njs_str("function f() {} f.__proto__ === Function.prototype"),
       njs_str("true") },
 


More information about the nginx-devel mailing list