[njs] Introduced ToIndex() conversion primitive from the spec.

Dmitry Volyntsev xeioex at nginx.com
Fri Dec 6 12:17:15 UTC 2019


details:   https://hg.nginx.org/njs/rev/19b741311270
branches:  
changeset: 1279:19b741311270
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Dec 06 14:44:54 2019 +0300
description:
Introduced ToIndex() conversion primitive from the spec.

diffstat:

 src/njs_main.h             |    5 +-
 src/njs_number.c           |    4 +-
 src/njs_number.h           |    2 +-
 src/njs_value.c            |    6 +-
 src/njs_value.h            |  167 -------------------------------------
 src/njs_value_conversion.h |  202 +++++++++++++++++++++++++++++++++++++++++++++
 src/njs_vmcode.c           |    2 +-
 7 files changed, 212 insertions(+), 176 deletions(-)

diffs (486 lines):

diff -r 990a4b7a4053 -r 19b741311270 src/njs_main.h
--- a/src/njs_main.h	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_main.h	Fri Dec 06 14:44:54 2019 +0300
@@ -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>
@@ -51,14 +54,12 @@
 #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>
diff -r 990a4b7a4053 -r 19b741311270 src/njs_number.c
--- a/src/njs_number.c	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_number.c	Fri Dec 06 14:44:54 2019 +0300
@@ -21,7 +21,7 @@ static njs_int_t njs_number_to_string_ra
 
 
 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 *va
 
             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]);
             }
         }
     }
diff -r 990a4b7a4053 -r 19b741311270 src/njs_number.h
--- a/src/njs_number.h	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_number.h	Fri Dec 06 14:44:54 2019 +0300
@@ -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);
diff -r 990a4b7a4053 -r 19b741311270 src/njs_value.c
--- a/src/njs_value.c	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_value.c	Fri Dec 06 14:44:54 2019 +0300
@@ -535,7 +535,7 @@ njs_property_query(njs_vm_t *vm, njs_pro
 
     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, 
         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, 
                 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);
diff -r 990a4b7a4053 -r 19b741311270 src/njs_value.h
--- a/src/njs_value.h	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_value.h	Fri Dec 06 14:44:54 2019 +0300
@@ -913,173 +913,6 @@ njs_int_t njs_value_species_constructor(
     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 -r 990a4b7a4053 -r 19b741311270 src/njs_value_conversion.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/njs_value_conversion.h	Fri Dec 06 14:44:54 2019 +0300
@@ -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_ */
diff -r 990a4b7a4053 -r 19b741311270 src/njs_vmcode.c
--- a/src/njs_vmcode.c	Fri Dec 06 14:44:11 2019 +0300
+++ b/src/njs_vmcode.c	Fri Dec 06 14:44:54 2019 +0300
@@ -1146,7 +1146,7 @@ njs_vmcode_property_init(njs_vm_t *vm, n
 
     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");


More information about the nginx-devel mailing list