]> git.kaiwu.me - njs.git/commitdiff
Introduced ToIndex() conversion primitive from the spec.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 6 Dec 2019 11:44:54 +0000 (14:44 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 6 Dec 2019 11:44:54 +0000 (14:44 +0300)
src/njs_main.h
src/njs_number.c
src/njs_number.h
src/njs_value.c
src/njs_value.h
src/njs_value_conversion.h [new file with mode: 0644]
src/njs_vmcode.c

index 4b9c3d170b60fc2c2f655a781161d0f5317706ec..fc83fc4d63df54ce44e03fd018a7c620f184e9eb 100644 (file)
@@ -44,6 +44,9 @@
 #include <njs_value.h>
 
 #include <njs_vm.h>
+#include <njs_error.h>
+#include <njs_number.h>
+#include <njs_value_conversion.h>
 #include <njs_vmcode.h>
 #include <njs_variable.h>
 #include <njs_lexer.h>
 #include <njs_generator.h>
 
 #include <njs_boolean.h>
-#include <njs_number.h>
 #include <njs_symbol.h>
 #include <njs_string.h>
 #include <njs_object.h>
 #include <njs_object_hash.h>
 #include <njs_array.h>
 #include <njs_function.h>
-#include <njs_error.h>
 #include <njs_regexp.h>
 #include <njs_regexp_pattern.h>
 #include <njs_date.h>
index 8e3476d9f8cea72ba2cd24a9d7295e35d5082dee..60a850c776484c8cec7e354cc239e745f2ec17ce 100644 (file)
@@ -21,7 +21,7 @@ static njs_int_t njs_number_to_string_radix(njs_vm_t *vm, njs_value_t *string,
 
 
 uint32_t
-njs_value_to_index(const njs_value_t *value)
+njs_key_to_index(const njs_value_t *value)
 {
     double       num;
     njs_array_t  *array;
@@ -47,7 +47,7 @@ njs_value_to_index(const njs_value_t *value)
 
             if (array->length == 1 && njs_is_valid(&array->start[0])) {
                 /* A single value array is the zeroth array value. */
-                return njs_value_to_index(&array->start[0]);
+                return njs_key_to_index(&array->start[0]);
             }
         }
     }
index dcce83fce0615fcb5132d0ad130a230011b4e851..ea0f021a4e54f6f7a65c6265e3953068e7d240e0 100644 (file)
@@ -8,7 +8,7 @@
 #define _NJS_NUMBER_H_INCLUDED_
 
 
-uint32_t njs_value_to_index(const njs_value_t *value);
+uint32_t njs_key_to_index(const njs_value_t *value);
 double njs_number_dec_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_oct_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_bin_parse(const u_char **start, const u_char *end);
index b822a67d9ef5420e945a8bed4b8ef01e9ce4a8ec..7f2b653a739a5fbefae7f27e9f25530b13f98c9c 100644 (file)
@@ -535,7 +535,7 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *value,
 
     case NJS_STRING:
         if (njs_fast_path(!njs_is_null_or_undefined_or_boolean(key))) {
-            index = njs_value_to_index(key);
+            index = njs_key_to_index(key);
 
             if (njs_fast_path(index < NJS_STRING_MAX_LENGTH)) {
                 return njs_string_property_query(vm, pq, value, index);
@@ -637,7 +637,7 @@ njs_object_property_query(njs_vm_t *vm, njs_property_query_t *pq,
         if (!njs_is_null_or_undefined_or_boolean(key)) {
             switch (proto->type) {
             case NJS_ARRAY:
-                index = njs_value_to_index(key);
+                index = njs_key_to_index(key);
                 if (njs_fast_path(index < NJS_ARRAY_MAX_INDEX)) {
                     array = (njs_array_t *) proto;
                     return njs_array_property_query(vm, pq, array, index);
@@ -646,7 +646,7 @@ njs_object_property_query(njs_vm_t *vm, njs_property_query_t *pq,
                 break;
 
             case NJS_OBJECT_STRING:
-                index = njs_value_to_index(key);
+                index = njs_key_to_index(key);
                 if (njs_fast_path(index < NJS_STRING_MAX_LENGTH)) {
                     ov = (njs_object_value_t *) proto;
                     ret = njs_string_property_query(vm, pq, &ov->value, index);
index d9cc6a341b90901db695ceacc0011b66d1520dce..77790ba402151d8d3414f51ae7dbec136798ad93 100644 (file)
@@ -913,173 +913,6 @@ njs_int_t njs_value_species_constructor(njs_vm_t *vm, njs_value_t *object,
     njs_value_t *default_constructor, njs_value_t *dst);
 
 
-#include "njs_number.h"
-
-
-njs_inline njs_int_t
-njs_value_to_number(njs_vm_t *vm, njs_value_t *value, double *dst)
-{
-    njs_int_t    ret;
-    njs_value_t  primitive;
-
-    if (njs_slow_path(!njs_is_primitive(value))) {
-        ret = njs_value_to_primitive(vm, &primitive, value, 0);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-
-        value = &primitive;
-    }
-
-    if (njs_slow_path(!njs_is_numeric(value))) {
-
-        if (njs_slow_path(njs_is_symbol(value))) {
-            njs_symbol_conversion_failed(vm, 0);
-            return NJS_ERROR;
-        }
-
-        *dst = NAN;
-
-        if (njs_is_string(value)) {
-            *dst = njs_string_to_number(value, 0);
-        }
-
-        return NJS_OK;
-    }
-
-    *dst = njs_number(value);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_numeric(njs_vm_t *vm, njs_value_t *value, njs_value_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    njs_set_number(dst, num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_integer(njs_vm_t *vm, njs_value_t *value, int64_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    *dst = njs_number_to_integer(num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_length(njs_vm_t *vm, njs_value_t *value, uint32_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    *dst = njs_number_to_length(num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_int32(njs_vm_t *vm, njs_value_t *value, int32_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    *dst = njs_number_to_int32(num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_uint32(njs_vm_t *vm, njs_value_t *value, uint32_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    *dst = njs_number_to_uint32(num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_uint16(njs_vm_t *vm, njs_value_t *value, uint16_t *dst)
-{
-    double     num;
-    njs_int_t  ret;
-
-    ret = njs_value_to_number(vm, value, &num);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return ret;
-    }
-
-    *dst = njs_number_to_uint16(num);
-
-    return NJS_OK;
-}
-
-
-njs_inline njs_int_t
-njs_value_to_string(njs_vm_t *vm, njs_value_t *dst, njs_value_t *value)
-{
-    njs_int_t    ret;
-    njs_value_t  primitive;
-
-    if (njs_slow_path(!njs_is_primitive(value))) {
-        if (njs_slow_path(value->type == NJS_OBJECT_SYMBOL)) {
-            /* should fail */
-            value = njs_object_value(value);
-
-        } else {
-            ret = njs_value_to_primitive(vm, &primitive, value, 1);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
-
-            value = &primitive;
-        }
-    }
-
-    return njs_primitive_value_to_string(vm, dst, value);
-}
-
-
 njs_inline njs_bool_t
 njs_values_same_non_numeric(const njs_value_t *val1, const njs_value_t *val2)
 {
diff --git a/src/njs_value_conversion.h b/src/njs_value_conversion.h
new file mode 100644 (file)
index 0000000..5cc5cfc
--- /dev/null
@@ -0,0 +1,202 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) NGINX, Inc.
+ */
+
+#ifndef _NJS_VALUE_CONVERSION_H_INCLUDED_
+#define _NJS_VALUE_CONVERSION_H_INCLUDED_
+
+
+njs_inline njs_int_t
+njs_value_to_number(njs_vm_t *vm, njs_value_t *value, double *dst)
+{
+    njs_int_t    ret;
+    njs_value_t  primitive;
+
+    if (njs_slow_path(!njs_is_primitive(value))) {
+        ret = njs_value_to_primitive(vm, &primitive, value, 0);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
+        }
+
+        value = &primitive;
+    }
+
+    if (njs_slow_path(!njs_is_numeric(value))) {
+
+        if (njs_slow_path(njs_is_symbol(value))) {
+            njs_symbol_conversion_failed(vm, 0);
+            return NJS_ERROR;
+        }
+
+        *dst = NAN;
+
+        if (njs_is_string(value)) {
+            *dst = njs_string_to_number(value, 0);
+        }
+
+        return NJS_OK;
+    }
+
+    *dst = njs_number(value);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_numeric(njs_vm_t *vm, njs_value_t *value, njs_value_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    njs_set_number(dst, num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_integer(njs_vm_t *vm, njs_value_t *value, int64_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    *dst = njs_number_to_integer(num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_length(njs_vm_t *vm, njs_value_t *value, uint32_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    *dst = njs_number_to_length(num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_index(njs_vm_t *vm, njs_value_t *value, uint32_t *dst)
+{
+    int64_t    integer_index;
+    njs_int_t  ret;
+
+    if (njs_slow_path(njs_is_undefined(value))) {
+        *dst = 0;
+
+    } else {
+        ret = njs_value_to_integer(vm, value, &integer_index);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
+        }
+
+        if (integer_index < 0 || integer_index > UINT32_MAX) {
+            njs_range_error(vm, "invalid index");
+            return NJS_ERROR;
+        }
+
+        *dst = (uint32_t) integer_index;
+    }
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_int32(njs_vm_t *vm, njs_value_t *value, int32_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    *dst = njs_number_to_int32(num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_uint32(njs_vm_t *vm, njs_value_t *value, uint32_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    *dst = njs_number_to_uint32(num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_uint16(njs_vm_t *vm, njs_value_t *value, uint16_t *dst)
+{
+    double     num;
+    njs_int_t  ret;
+
+    ret = njs_value_to_number(vm, value, &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    *dst = njs_number_to_uint16(num);
+
+    return NJS_OK;
+}
+
+
+njs_inline njs_int_t
+njs_value_to_string(njs_vm_t *vm, njs_value_t *dst, njs_value_t *value)
+{
+    njs_int_t    ret;
+    njs_value_t  primitive;
+
+    if (njs_slow_path(!njs_is_primitive(value))) {
+        if (njs_slow_path(value->type == NJS_OBJECT_SYMBOL)) {
+            /* should fail */
+            value = njs_object_value(value);
+
+        } else {
+            ret = njs_value_to_primitive(vm, &primitive, value, 1);
+            if (njs_slow_path(ret != NJS_OK)) {
+                return ret;
+            }
+
+            value = &primitive;
+        }
+    }
+
+    return njs_primitive_value_to_string(vm, dst, value);
+}
+
+
+#endif /* _NJS_VALUE_CONVERSION_H_INCLUDED_ */
index 725727e4a642a9cf95db776ced9f321a12cbb844..4cddea08e17e38566d0057c9584e8ba0d676762b 100644 (file)
@@ -1146,7 +1146,7 @@ njs_vmcode_property_init(njs_vm_t *vm, njs_value_t *value, njs_value_t *key,
 
     switch (value->type) {
     case NJS_ARRAY:
-        index = njs_value_to_index(key);
+        index = njs_key_to_index(key);
         if (njs_slow_path(index == NJS_ARRAY_INVALID_INDEX)) {
             njs_internal_error(vm,
                                "invalid index while property initialization");