]> git.kaiwu.me - njs.git/commitdiff
Passing value and default attributes to njs_object_prop_alloc().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 13 Jun 2017 14:49:05 +0000 (17:49 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 13 Jun 2017 14:49:05 +0000 (17:49 +0300)
njs/njs_object.c
njs/njs_object.h
njs/njs_regexp.c
njs/njs_vm.c

index 3313283c1f69d026564a36a98f1cf432396f631b..f14421a3358fbae49614da421cf99e0dae6d67a9 100644 (file)
@@ -181,7 +181,8 @@ njs_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
 
 
 njs_object_prop_t *
-njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name)
+njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name,
+    const njs_value_t *value, uint8_t attributes)
 {
     njs_object_prop_t  *prop;
 
@@ -189,15 +190,16 @@ njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name)
                                sizeof(njs_object_prop_t));
 
     if (nxt_fast_path(prop != NULL)) {
-        prop->value = njs_value_void;
+        /* GC: retain. */
+        prop->value = *value;
 
         /* GC: retain. */
         prop->name = *name;
 
         prop->type = NJS_PROPERTY;
-        prop->enumerable = 1;
-        prop->writable = 1;
-        prop->configurable = 1;
+        prop->enumerable = attributes;
+        prop->writable = attributes;
+        prop->configurable = attributes;
     }
 
     return prop;
@@ -494,16 +496,12 @@ njs_define_property(njs_vm_t *vm, njs_object_t *object, njs_value_t *name,
     ret = nxt_lvlhsh_find(&object->hash, &lhq);
 
     if (ret != NXT_OK) {
-        prop = njs_object_prop_alloc(vm, name);
+        prop = njs_object_prop_alloc(vm, name, &njs_value_void, 0);
 
         if (nxt_slow_path(prop == NULL)) {
             return NXT_ERROR;
         }
 
-        prop->configurable = 0;
-        prop->enumerable = 0;
-        prop->writable = 0;
-
         lhq.value = prop;
 
     } else {
@@ -647,7 +645,7 @@ njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
 
     static const njs_value_t   prototype_string = njs_string("prototype");
 
-    prop = njs_object_prop_alloc(vm, &prototype_string);
+    prop = njs_object_prop_alloc(vm, &prototype_string, &njs_value_void, 0);
     if (nxt_slow_path(prop == NULL)) {
         return NULL;
     }
@@ -658,10 +656,6 @@ njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
     prop->value.type = prototype->type;
     prop->value.data.truth = 1;
 
-    prop->enumerable = 0;
-    prop->writable = 0;
-    prop->configurable = 0;
-
     lhq.value = prop;
     lhq.key_hash = NJS_PROTOTYPE_HASH;
     lhq.key = nxt_string_value("prototype");
@@ -835,7 +829,7 @@ njs_property_constructor_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
 
     static const njs_value_t  constructor_string = njs_string("constructor");
 
-    prop = njs_object_prop_alloc(vm, &constructor_string);
+    prop = njs_object_prop_alloc(vm, &constructor_string, constructor, 1);
     if (nxt_slow_path(prop == NULL)) {
         return NULL;
     }
index c88fc8f4eee5c19df38f87fd46996ba57415847c..3dd98fb683e4472ee6b043d4cd0501f0e1b94aef 100644 (file)
@@ -47,7 +47,8 @@ nxt_int_t njs_object_hash_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
     const njs_object_prop_t *prop, nxt_uint_t n);
 njs_ret_t njs_object_constructor(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
-njs_object_prop_t *njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name);
+njs_object_prop_t *njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name,
+        const njs_value_t *value, uint8_t attributes);
 njs_ret_t njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value);
 njs_ret_t njs_object_prototype_create(njs_vm_t *vm, njs_value_t *value);
 njs_value_t *njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
index e0ad1cbd9ee93b023874693fbdb0d814269e19c2..35599005f3ed8f95f0829544f8ea8306aa7bcc5a 100644 (file)
@@ -749,7 +749,7 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_regexp_t *regexp, njs_utf8_t utf8,
         }
     }
 
-    prop = njs_object_prop_alloc(vm, &njs_string_index);
+    prop = njs_object_prop_alloc(vm, &njs_string_index, &njs_value_void, 1);
     if (nxt_slow_path(prop == NULL)) {
         goto fail;
     }
@@ -774,13 +774,11 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_regexp_t *regexp, njs_utf8_t utf8,
         goto fail;
     }
 
-    prop = njs_object_prop_alloc(vm, &njs_string_input);
+    prop = njs_object_prop_alloc(vm, &njs_string_input, &regexp->string, 1);
     if (nxt_slow_path(prop == NULL)) {
         goto fail;
     }
 
-    njs_string_copy(&prop->value, &regexp->string);
-
     lhq.key_hash = NJS_INPUT_HASH;
     lhq.key = nxt_string_value("input");
     lhq.value = prop;
index b712b369fd191b6d091f17095dca26d221688ffd..208aca0bf2ddf90d07f0af40f78a72248f4d0fc4 100644 (file)
@@ -683,7 +683,7 @@ njs_vmcode_property_set(njs_vm_t *vm, njs_value_t *object,
         break;
 
     case NXT_DECLINED:
-        prop = njs_object_prop_alloc(vm, &pq.value);
+        prop = njs_object_prop_alloc(vm, &pq.value, &njs_value_void, 1);
         if (nxt_slow_path(prop == NULL)) {
             return NXT_ERROR;
         }