[njs] Fixed RegExp.prototype.test() according to the specification.

Dmitry Volyntsev xeioex at nginx.com
Mon Jun 7 19:16:13 UTC 2021


details:   https://hg.nginx.org/njs/rev/de64420d0f2b
branches:  
changeset: 1651:de64420d0f2b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Sat Jun 05 11:55:08 2021 +0000
description:
Fixed RegExp.prototype.test() according to the specification.

diffstat:

 src/njs_regexp.c |  91 +++++++++----------------------------------------------
 1 files changed, 15 insertions(+), 76 deletions(-)

diffs (111 lines):

diff -r 02444445df29 -r de64420d0f2b src/njs_regexp.c
--- a/src/njs_regexp.c	Sat Jun 05 11:58:00 2021 +0000
+++ b/src/njs_regexp.c	Sat Jun 05 11:55:08 2021 +0000
@@ -873,92 +873,31 @@ static njs_int_t
 njs_regexp_prototype_test(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    int                     *captures;
-    int64_t                 last_index;
-    njs_int_t               ret, match;
-    njs_uint_t              n;
-    njs_regex_t             *regex;
-    njs_regexp_t            *regexp;
-    njs_value_t             *value, lvalue;
-    const njs_value_t       *retval;
-    njs_string_prop_t       string;
-    njs_regexp_pattern_t    *pattern;
-    njs_regex_match_data_t  *match_data;
+    njs_int_t    ret;
+    njs_value_t  *r, *string, lvalue, retval;
 
-    if (!njs_is_regexp(njs_arg(args, nargs, 0))) {
-        njs_type_error(vm, "\"this\" argument is not a regexp");
+    r = njs_argument(args, 0);
+
+    if (njs_slow_path(!njs_is_object(r))) {
+        njs_type_error(vm, "\"this\" argument is not an object");
         return NJS_ERROR;
     }
 
-    retval = &njs_value_false;
-
-    value = njs_lvalue_arg(&lvalue, args, nargs, 1);
+    string = njs_lvalue_arg(&lvalue, args, nargs, 1);
 
-    if (!njs_is_string(value)) {
-        ret = njs_value_to_string(vm, value, value);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
+    ret = njs_value_to_string(vm, string, string);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
     }
 
-    (void) njs_string_prop(&string, value);
-
-    n = (string.length != 0);
-
-    regexp = njs_regexp(njs_argument(args, 0));
-    pattern = njs_regexp_pattern(&args[0]);
-
-    regex = &pattern->regex[n];
-    match_data = vm->single_match_data;
-
-    if (njs_regex_is_valid(regex)) {
-        if (njs_regex_backrefs(regex) != 0) {
-            match_data = njs_regex_match_data(regex, vm->regex_context);
-            if (njs_slow_path(match_data == NULL)) {
-                njs_memory_error(vm);
-                return NJS_ERROR;
-            }
-        }
-
-        match = njs_regexp_match(vm, regex, string.start, 0, string.size,
-                                 match_data);
-        if (match >= 0) {
-            retval = &njs_value_true;
-
-        } else if (match != NJS_REGEX_NOMATCH) {
-            ret = NJS_ERROR;
-            goto done;
-        }
-
-        if (pattern->global) {
-            ret = njs_value_to_length(vm, &regexp->last_index, &last_index);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return NJS_ERROR;
-            }
-
-            if (match >= 0) {
-                captures = njs_regex_captures(match_data);
-                last_index += captures[1];
-
-            } else {
-                last_index = 0;
-            }
-
-            njs_set_number(&regexp->last_index, last_index);
-        }
+    ret = njs_regexp_exec(vm, r, string, &retval);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
     }
 
-    ret = NJS_OK;
-
-    vm->retval = *retval;
-
-done:
+    njs_set_boolean(&vm->retval, !njs_is_null(&retval));
 
-    if (match_data != vm->single_match_data) {
-        njs_regex_match_data_free(match_data, vm->regex_context);
-    }
-
-    return ret;
+    return NJS_OK;
 }
 
 


More information about the nginx-devel mailing list