]> git.kaiwu.me - njs.git/commitdiff
Improved njs_value_property_i64_delete() for fast-arrays.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 11 Nov 2022 01:53:35 +0000 (17:53 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 11 Nov 2022 01:53:35 +0000 (17:53 -0800)
src/njs_value.c
src/njs_value.h
src/njs_vmcode.c

index cd7fbeddecf19013947f30fffd28043cd486560c..32d9102bea8f2c08fd3ba2446fca473d7fbcc6d7 100644 (file)
@@ -1329,20 +1329,41 @@ njs_int_t
 njs_value_property_delete(njs_vm_t *vm, njs_value_t *value, njs_value_t *key,
     njs_value_t *removed, njs_bool_t thrw)
 {
+    double                num;
+    uint32_t              index;
     njs_int_t             ret;
-    njs_value_t           primitive;
+    njs_array_t           *array;
     njs_object_prop_t     *prop;
     njs_property_query_t  pq;
 
-    if (njs_slow_path(!njs_is_key(key))) {
-        ret = njs_value_to_key(vm, &primitive, key);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return NJS_ERROR;
+    njs_assert(njs_is_index_or_key(key));
+
+    if (njs_fast_path(njs_is_number(key))) {
+        if (njs_slow_path(!(njs_is_fast_array(value)))) {
+            goto slow_path;
         }
 
-        key = &primitive;
+        num = njs_number(key);
+
+        if (njs_slow_path(!njs_number_is_integer_index(num))) {
+            goto slow_path;
+        }
+
+        index = (uint32_t) num;
+
+        array = njs_array(value);
+
+        if (njs_slow_path(index >= array->length)) {
+            goto slow_path;
+        }
+
+        njs_value_assign(&array->start[index], &njs_value_invalid);
+
+        return NJS_OK;
     }
 
+slow_path:
+
     njs_property_query_init(&pq, NJS_PROPERTY_QUERY_DELETE, 0, 1);
 
     ret = njs_property_query(vm, &pq, value, key);
index e02a0a26e9ed34049ef5e52c8087a9fb43eca8f1..b1dfcd1614d75f83b43d3efb268fe1d28ab941ed 100644 (file)
@@ -1142,13 +1142,9 @@ njs_inline njs_int_t
 njs_value_property_i64_delete(njs_vm_t *vm, njs_value_t *value, int64_t index,
     njs_value_t *removed)
 {
-    njs_int_t    ret;
     njs_value_t  key;
 
-    ret = njs_int64_to_string(vm, &key, index);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
+    njs_set_number(&key, index);
 
     return njs_value_property_delete(vm, value, &key, removed, 1);
 }
index 86bc86b854512fe3a98cdb7d49cfdd8ca969af4c..634788e0a46a213f197a130ff818772183f0dcd0 100644 (file)
@@ -932,6 +932,20 @@ NEXT_LBL;
         njs_vmcode_operand(vm, vmcode->operand3, value2);
         njs_vmcode_operand(vm, vmcode->operand2, value1);
 
+        if (njs_slow_path(!njs_is_index_or_key(value2))) {
+            if (njs_slow_path(njs_is_null_or_undefined(value1))) {
+                (void) njs_throw_cannot_property(vm, value1, value2, "delete");
+                goto error;
+            }
+
+            ret = njs_value_to_key(vm, &primitive1, value2);
+            if (njs_slow_path(ret != NJS_OK)) {
+                goto error;
+            }
+
+            value2 = &primitive1;
+        }
+
         ret = njs_value_property_delete(vm, value1, value2, NULL, 1);
         if (njs_fast_path(ret != NJS_ERROR)) {
             vm->retval = njs_value_true;