From b593dd4aba0f5c730c1d90072cdee7dd9a93beed Mon Sep 17 00:00:00 2001 From: Vadim Zhestikov Date: Tue, 2 Jul 2024 14:55:03 -0700 Subject: [PATCH] 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 bfdf3f355..c677be573 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 530cf6ff8..2f0e318c4 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") },