[njs] Added njs_lvalue_arg() macro.

Alexander Borisov alexander.borisov at nginx.com
Mon Oct 28 13:11:03 UTC 2019


details:   https://hg.nginx.org/njs/rev/c43ebb4722fc
branches:  
changeset: 1203:c43ebb4722fc
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Mon Oct 28 16:10:17 2019 +0300
description:
Added njs_lvalue_arg() macro.

diffstat:

 src/njs.h        |   5 +++-
 src/njs_json.c   |  15 ++++---------
 src/njs_object.c |  15 ++++---------
 src/njs_regexp.c |  30 +++++++++------------------
 src/njs_string.c |  59 ++++++++++++++++++-------------------------------------
 5 files changed, 43 insertions(+), 81 deletions(-)

diffs (274 lines):

diff -r 7e7d0dac4572 -r c43ebb4722fc src/njs.h
--- a/src/njs.h	Fri Oct 25 16:20:37 2019 +0300
+++ b/src/njs.h	Mon Oct 28 16:10:17 2019 +0300
@@ -51,10 +51,13 @@ extern const njs_value_t            njs_
                  : (njs_value_t *) &njs_value_undefined)
 
 #define njs_value_assign(dst, src)                                            \
-    *((njs_opaque_value_t *) dst) = *((njs_opaque_value_t *) src);
+    *((njs_opaque_value_t *) dst) = *((njs_opaque_value_t *) src)
 
 #define njs_value_arg(val) ((njs_value_t *) val)
 
+#define njs_lvalue_arg(lvalue, args, nargs, n)                                \
+    ((n < nargs) ? njs_argument(args, n)                                      \
+                 : (njs_value_assign(lvalue, &njs_value_undefined), lvalue))
 
 #define njs_vm_error(vm, fmt, ...)                                            \
     njs_vm_value_error_set(vm, njs_vm_retval(vm), fmt, ##__VA_ARGS__)
diff -r 7e7d0dac4572 -r c43ebb4722fc src/njs_json.c
--- a/src/njs_json.c	Fri Oct 25 16:20:37 2019 +0300
+++ b/src/njs_json.c	Mon Oct 28 16:10:17 2019 +0300
@@ -147,7 +147,7 @@ njs_json_parse(njs_vm_t *vm, njs_value_t
     njs_index_t unused)
 {
     njs_int_t             ret;
-    njs_value_t           *text, *value, *wrapper;
+    njs_value_t           *text, *value, *wrapper, lvalue;
     const u_char          *p, *end;
     njs_json_parse_t      *parse, json_parse;
     const njs_value_t     *reviver;
@@ -162,17 +162,12 @@ njs_json_parse(njs_vm_t *vm, njs_value_t
         return NJS_ERROR;
     }
 
-    text = njs_arg(args, nargs, 1);
+    text = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
     if (njs_slow_path(!njs_is_string(text))) {
-        if (njs_is_undefined(text)) {
-            text = njs_value_arg(&njs_string_undefined);
-
-        } else {
-            ret = njs_value_to_string(vm, text, text);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, text, text);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
diff -r 7e7d0dac4572 -r c43ebb4722fc src/njs_object.c
--- a/src/njs_object.c	Fri Oct 25 16:20:37 2019 +0300
+++ b/src/njs_object.c	Mon Oct 28 16:10:17 2019 +0300
@@ -1089,7 +1089,7 @@ njs_object_define_property(njs_vm_t *vm,
     njs_index_t unused)
 {
     njs_int_t    ret;
-    njs_value_t  *value, *name, *desc;
+    njs_value_t  *value, *name, *desc, lvalue;
 
     if (!njs_is_object(njs_arg(args, nargs, 1))) {
         njs_type_error(vm, "cannot convert %s argument to object",
@@ -1111,17 +1111,12 @@ njs_object_define_property(njs_vm_t *vm,
         return NJS_ERROR;
     }
 
-    name = njs_arg(args, nargs, 2);
+    name = njs_lvalue_arg(&lvalue, args, nargs, 2);
 
     if (njs_slow_path(!njs_is_string(name))) {
-        if (njs_is_undefined(name)) {
-            name = njs_value_arg(&njs_string_undefined);
-
-        } else {
-            ret = njs_value_to_string(vm, name, name);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, name, name);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
diff -r 7e7d0dac4572 -r c43ebb4722fc src/njs_regexp.c
--- a/src/njs_regexp.c	Fri Oct 25 16:20:37 2019 +0300
+++ b/src/njs_regexp.c	Mon Oct 28 16:10:17 2019 +0300
@@ -845,7 +845,7 @@ njs_regexp_prototype_test(njs_vm_t *vm, 
     njs_int_t               ret;
     njs_uint_t              n;
     njs_regex_t             *regex;
-    njs_value_t             *value;
+    njs_value_t             *value, lvalue;
     const njs_value_t       *retval;
     njs_string_prop_t       string;
     njs_regexp_pattern_t    *pattern;
@@ -858,17 +858,12 @@ njs_regexp_prototype_test(njs_vm_t *vm, 
 
     retval = &njs_value_false;
 
-    value = njs_arg(args, nargs, 1);
+    value = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
     if (!njs_is_string(value)) {
-        if (njs_is_undefined(value)) {
-            value = njs_value_arg(&njs_string_undefined);
-
-        } else {
-            ret = njs_value_to_string(vm, value, value);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, value, value);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
@@ -921,7 +916,7 @@ njs_regexp_prototype_exec(njs_vm_t *vm, 
 {
     njs_int_t               ret;
     njs_utf8_t              utf8;
-    njs_value_t             *value;
+    njs_value_t             *value, lvalue;
     njs_regexp_t            *regexp;
     njs_string_prop_t       string;
     njs_regexp_utf8_t       type;
@@ -933,17 +928,12 @@ njs_regexp_prototype_exec(njs_vm_t *vm, 
         return NJS_ERROR;
     }
 
-    value = njs_arg(args, nargs, 1);
+    value = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
     if (!njs_is_string(value)) {
-        if (njs_is_undefined(value)) {
-            value = njs_value_arg(&njs_string_undefined);
-
-        } else {
-            ret = njs_value_to_string(vm, value, value);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, value, value);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
diff -r 7e7d0dac4572 -r c43ebb4722fc src/njs_string.c
--- a/src/njs_string.c	Fri Oct 25 16:20:37 2019 +0300
+++ b/src/njs_string.c	Mon Oct 28 16:10:17 2019 +0300
@@ -2065,9 +2065,8 @@ njs_string_prototype_last_index_of(njs_v
     double             pos;
     ssize_t            index, start, length, search_length;
     njs_int_t          ret;
-    njs_value_t        *value;
+    njs_value_t        *value, *search_string, lvalue;
     const u_char       *p, *end;
-    const njs_value_t  *search_string;
     njs_string_prop_t  string, search;
 
     ret = njs_string_object_validate(vm, njs_arg(args, nargs, 0));
@@ -2079,18 +2078,12 @@ njs_string_prototype_last_index_of(njs_v
 
     length = njs_string_prop(&string, njs_argument(args, 0));
 
-    search_string = njs_arg(args, nargs, 1);
+    search_string = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
     if (njs_slow_path(!njs_is_string(search_string))) {
-        if (njs_is_undefined(search_string)) {
-            search_string = &njs_string_undefined;
-
-        } else {
-            ret = njs_value_to_string(vm, njs_value_arg(search_string),
-                                      njs_value_arg(search_string));
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, search_string, search_string);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
@@ -2301,7 +2294,7 @@ njs_string_starts_or_ends_with(njs_vm_t 
 {
     int64_t            index, length, search_length;
     njs_int_t          ret;
-    njs_value_t        *value;
+    njs_value_t        *value, lvalue;
     const u_char       *p, *end;
     const njs_value_t  *retval;
     njs_string_prop_t  string, search;
@@ -2313,17 +2306,12 @@ njs_string_starts_or_ends_with(njs_vm_t 
         return ret;
     }
 
-    value = njs_arg(args, nargs, 1);
+    value = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
     if (njs_slow_path(!njs_is_string(value))) {
-        if (njs_is_undefined(value)) {
-            value = njs_value_arg(&njs_string_undefined);
-
-        } else {
-            ret = njs_value_to_string(vm, value, value);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return ret;
-            }
+        ret = njs_value_to_string(vm, value, value);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
@@ -3478,6 +3466,7 @@ njs_string_prototype_replace(njs_vm_t *v
     njs_int_t             ret;
     njs_uint_t            ncaptures;
     njs_value_t           *this, *search, *replace;
+    njs_value_t           search_lvalue, replace_lvalue;
     njs_regex_t           *regex;
     njs_string_prop_t     string;
     njs_string_replace_t  *r, string_replace;
@@ -3493,8 +3482,8 @@ njs_string_prototype_replace(njs_vm_t *v
         goto original;
     }
 
-    search = njs_arg(args, nargs, 1);
-    replace = njs_arg(args, nargs, 2);
+    search = njs_lvalue_arg(&search_lvalue, args, nargs, 1);
+    replace = njs_lvalue_arg(&replace_lvalue, args, nargs, 2);
 
     (void) njs_string_prop(&string, this);
 
@@ -3530,14 +3519,9 @@ njs_string_prototype_replace(njs_vm_t *v
         ncaptures = 1;
 
         if (!njs_is_string(search)) {
-            if (njs_is_undefined(search)) {
-                search = njs_value_arg(&njs_string_undefined);
-
-            } else {
-                ret = njs_value_to_string(vm, search, search);
-                if (njs_slow_path(ret != NJS_OK)) {
-                    return ret;
-                }
+            ret = njs_value_to_string(vm, search, search);
+            if (njs_slow_path(ret != NJS_OK)) {
+                return ret;
             }
         }
     }
@@ -3559,14 +3543,9 @@ njs_string_prototype_replace(njs_vm_t *v
 
     } else {
         if (njs_slow_path(!njs_is_string(replace))) {
-            if (njs_is_undefined(replace)) {
-                replace = njs_value_arg(&njs_string_undefined);
-
-            } else {
-                ret = njs_value_to_string(vm, replace, replace);
-                if (njs_slow_path(ret != NJS_OK)) {
-                    return ret;
-                }
+            ret = njs_value_to_string(vm, replace, replace);
+            if (njs_slow_path(ret != NJS_OK)) {
+                return ret;
             }
         }
 


More information about the nginx-devel mailing list