]> git.kaiwu.me - njs.git/commitdiff
Fixed function "constructor" property handler while setting.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 24 Aug 2020 11:28:21 +0000 (11:28 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 24 Aug 2020 11:28:21 +0000 (11:28 +0000)
src/njs_function.c
src/njs_object.c
src/njs_object.h

index 796624e6a49a7f44524e116ba5ebe51efe9a6a55..db1ef31f2992a00ad71227b58850ba07e0d83757 100644 (file)
@@ -853,8 +853,7 @@ njs_function_prototype_create(njs_vm_t *vm, njs_object_prop_t *prop,
 
     if (setval == &proto_value && njs_is_object(proto)) {
         /* Only in getter context. */
-        cons = njs_property_constructor_create(vm, njs_object_hash(proto),
-                                               value);
+        cons = njs_property_constructor_set(vm, njs_object_hash(proto), value);
         if (njs_slow_path(cons == NULL)) {
             return NJS_ERROR;
         }
index 154be583ddff230a1470c9a23b3ead33fc995d4c..e77e373726747cbff662321a859cda2ffe1d267a 100644 (file)
@@ -2216,6 +2216,22 @@ njs_object_prototype_create_constructor(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_object_t            *object;
     njs_object_prototype_t  *prototype;
 
+    if (setval != NULL) {
+        if (!njs_is_object(value)) {
+            njs_type_error(vm, "Cannot create propery \"constructor\" on %s",
+                           njs_type_string(value->type));
+            return NJS_ERROR;
+        }
+
+        cons = njs_property_constructor_set(vm, njs_object_hash(value), setval);
+        if (njs_slow_path(cons == NULL)) {
+            return NJS_ERROR;
+        }
+
+        *retval = *cons;
+        return NJS_OK;
+    }
+
     if (njs_is_object(value)) {
         object = njs_object(value);
 
@@ -2231,8 +2247,6 @@ njs_object_prototype_create_constructor(njs_vm_t *vm, njs_object_prop_t *prop,
 
         } while (object != NULL);
 
-        njs_thread_log_alert("prototype not found");
-
         return NJS_ERROR;
 
     } else {
@@ -2242,23 +2256,21 @@ njs_object_prototype_create_constructor(njs_vm_t *vm, njs_object_prop_t *prop,
 
 found:
 
-    if (setval == NULL) {
-        njs_set_function(&constructor, &vm->constructors[index]);
-        setval = &constructor;
-    }
+    njs_set_function(&constructor, &vm->constructors[index]);
+    setval = &constructor;
 
-    cons = njs_property_constructor_create(vm, &prototype->object.hash, setval);
-    if (njs_fast_path(cons != NULL)) {
-        *retval = *cons;
-        return NJS_OK;
+    cons = njs_property_constructor_set(vm, &prototype->object.hash, setval);
+    if (njs_slow_path(cons == NULL)) {
+        return NJS_ERROR;
     }
 
-    return NJS_ERROR;
+    *retval = *cons;
+    return NJS_OK;
 }
 
 
 njs_value_t *
-njs_property_constructor_create(njs_vm_t *vm, njs_lvlhsh_t *hash,
+njs_property_constructor_set(njs_vm_t *vm, njs_lvlhsh_t *hash,
     njs_value_t *constructor)
 {
     njs_int_t                 ret;
index ddea784e9353b397bd79110b15b3d675d6079bc7..d4c40aed6e158969151aa649cf376a929870a288 100644 (file)
@@ -63,7 +63,7 @@ njs_int_t njs_object_prototype_proto(njs_vm_t *vm, njs_object_prop_t *prop,
 njs_int_t njs_object_prototype_create_constructor(njs_vm_t *vm,
     njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
     njs_value_t *retval);
-njs_value_t *njs_property_constructor_create(njs_vm_t *vm, njs_lvlhsh_t *hash,
+njs_value_t *njs_property_constructor_set(njs_vm_t *vm, njs_lvlhsh_t *hash,
     njs_value_t *constructor);
 njs_int_t njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused);