[njs] Fixed getter/setter proto.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 1 11:49:01 UTC 2019


details:   https://hg.nginx.org/njs/rev/151ac933c8f3
branches:  
changeset: 1216:151ac933c8f3
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Oct 31 19:53:21 2019 +0300
description:
Fixed getter/setter proto.

This closes #238 issue on Github.

diffstat:

 src/njs_function.c       |   7 +++++++
 src/njs_object_prop.c    |  37 +++++++++++++++++++++++++++----------
 src/test/njs_unit_test.c |   8 ++++++++
 3 files changed, 42 insertions(+), 10 deletions(-)

diffs (96 lines):

diff -r 297f7dd356c0 -r 151ac933c8f3 src/njs_function.c
--- a/src/njs_function.c	Thu Oct 31 18:17:34 2019 +0300
+++ b/src/njs_function.c	Thu Oct 31 19:53:21 2019 +0300
@@ -90,6 +90,13 @@ njs_function_value_copy(njs_vm_t *vm, nj
         return NULL;
     }
 
+    if (copy->ctor) {
+        copy->object.shared_hash = vm->shared->function_instance_hash;
+
+    } else {
+        copy->object.shared_hash = vm->shared->arrow_instance_hash;
+    }
+
     value->data.u.function = copy;
 
     return copy;
diff -r 297f7dd356c0 -r 151ac933c8f3 src/njs_object_prop.c
--- a/src/njs_object_prop.c	Thu Oct 31 18:17:34 2019 +0300
+++ b/src/njs_object_prop.c	Thu Oct 31 19:53:21 2019 +0300
@@ -381,8 +381,6 @@ njs_prop_private_copy(njs_vm_t *vm, njs_
     njs_object_prop_t   *prop, *shared, *name;
     njs_lvlhsh_query_t  lhq;
 
-    static const njs_value_t  name_string = njs_string("name");
-
     prop = njs_mp_align(vm->mem_pool, sizeof(njs_value_t),
                         sizeof(njs_object_prop_t));
     if (njs_slow_path(prop == NULL)) {
@@ -403,6 +401,32 @@ njs_prop_private_copy(njs_vm_t *vm, njs_
         return NJS_ERROR;
     }
 
+    if (njs_is_accessor_descriptor(prop)) {
+        if (njs_is_function(&prop->getter)) {
+            function = njs_function_value_copy(vm, &prop->getter);
+            if (njs_slow_path(function == NULL)) {
+                return NJS_ERROR;
+            }
+
+            if (njs_is_function(&prop->setter)
+                && function->native && njs_function(&prop->setter)->native
+                && function->u.native == njs_function(&prop->setter)->u.native)
+            {
+                prop->setter = prop->getter;
+                return NJS_OK;
+            }
+        }
+
+        if (njs_is_function(&prop->setter)) {
+            function = njs_function_value_copy(vm, &prop->setter);
+            if (njs_slow_path(function == NULL)) {
+                return NJS_ERROR;
+            }
+        }
+
+        return NJS_OK;
+    }
+
     if (!njs_is_function(&prop->value)) {
         return NJS_OK;
     }
@@ -412,14 +436,7 @@ njs_prop_private_copy(njs_vm_t *vm, njs_
         return NJS_ERROR;
     }
 
-    if (function->ctor) {
-        function->object.shared_hash = vm->shared->function_instance_hash;
-
-    } else {
-        function->object.shared_hash = vm->shared->arrow_instance_hash;
-    }
-
-    name = njs_object_prop_alloc(vm, &name_string, &prop->name, 0);
+    name = njs_object_prop_alloc(vm, &njs_string_name, &prop->name, 0);
     if (njs_slow_path(name == NULL)) {
         return NJS_ERROR;
     }
diff -r 297f7dd356c0 -r 151ac933c8f3 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu Oct 31 18:17:34 2019 +0300
+++ b/src/test/njs_unit_test.c	Thu Oct 31 19:53:21 2019 +0300
@@ -8111,6 +8111,14 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("(function() {}).arguments"),
       njs_str("TypeError: \"caller\", \"callee\", \"arguments\" properties may not be accessed") },
 
+    { njs_str("var desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Math.min), 'caller');"
+              "desc.get === desc.set"),
+      njs_str("true") },
+
+    { njs_str("var desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Math.min), 'caller');"
+              "1/desc.get"),
+      njs_str("NaN") },
+
     { njs_str("var p = Object.getPrototypeOf(function() {});"
               "var d = Object.getOwnPropertyDescriptor(p, 'caller');"
               "typeof d.get == 'function' && typeof d.get == typeof d.set"


More information about the nginx-devel mailing list