[njs] Fixed njs_builtin_match().

Dmitry Volyntsev xeioex at nginx.com
Mon Aug 5 12:53:05 UTC 2019


details:   https://hg.nginx.org/njs/rev/08505f72a3e4
branches:  
changeset: 1100:08505f72a3e4
user:      hongzhidao <hongzhidao at gmail.com>
date:      Sun Aug 04 04:06:30 2019 -0400
description:
Fixed njs_builtin_match().

Previously native functions were identified by comparing pointers to
njs_function_t. This is not correct because njs_function_t maybe copied
from shared prototypes. Instead pointers to native functions should be
compared.

diffstat:

 src/njs_builtin.c               |  25 ++++++++++++++-----------
 src/test/njs_interactive_test.c |   5 +++++
 2 files changed, 19 insertions(+), 11 deletions(-)

diffs (71 lines):

diff -r ca8ce2161d0c -r 08505f72a3e4 src/njs_builtin.c
--- a/src/njs_builtin.c	Fri Aug 02 23:36:42 2019 +0800
+++ b/src/njs_builtin.c	Sun Aug 04 04:06:30 2019 -0400
@@ -954,6 +954,7 @@ njs_builtin_match(const njs_object_init_
     const njs_object_prop_t **prop, const njs_object_init_t **object)
 {
     njs_uint_t               i;
+    njs_function_t           *fun;
     const njs_object_init_t  *o, **p;
     const njs_object_prop_t  *pr;
 
@@ -967,7 +968,9 @@ njs_builtin_match(const njs_object_init_
                 continue;
             }
 
-            if (function != njs_function(&pr->value)) {
+            fun = njs_function(&pr->value);
+
+            if (function->u.native != fun->u.native) {
                 continue;
             }
 
@@ -993,6 +996,16 @@ njs_builtin_match_native_function(njs_vm
     const njs_object_prop_t    *prop;
     const njs_function_init_t  *fun;
 
+    fun = njs_native_functions;
+
+    for (p = njs_function_init; *p != NULL; p++, fun++) {
+        if (function->u.native == fun->native) {
+            *name = (*p)->name;
+
+            return NJS_OK;
+        }
+    }
+
     middle = njs_str_value(".");
 
     ret = njs_builtin_match(njs_object_init, function, &prop, &obj);
@@ -1014,16 +1027,6 @@ njs_builtin_match_native_function(njs_vm
         goto found;
     }
 
-    fun = njs_native_functions;
-
-    for (p = njs_function_init; *p != NULL; p++, fun++) {
-        if (function->u.native == fun->native) {
-            *name = (*p)->name;
-
-            return NJS_OK;
-        }
-    }
-
     ret = njs_builtin_match(njs_module_init, function, &prop, &obj);
 
     if (ret == NJS_OK) {
diff -r ca8ce2161d0c -r 08505f72a3e4 src/test/njs_interactive_test.c
--- a/src/test/njs_interactive_test.c	Fri Aug 02 23:36:42 2019 +0800
+++ b/src/test/njs_interactive_test.c	Sun Aug 04 04:06:30 2019 -0400
@@ -160,6 +160,11 @@ static njs_interactive_test_t  njs_test[
                  "    at Math.log (native)\n"
                  "    at main (native)\n") },
 
+    { njs_str("var bound = Math.max.bind(null, {toString(){return {}}}); bound(1)" ENTER),
+      njs_str("TypeError: Cannot convert object to primitive value\n"
+                 "    at Math.max (native)\n"
+                 "    at main (native)\n") },
+
     { njs_str("eval()" ENTER),
       njs_str("InternalError: Not implemented\n"
                  "    at eval (native)\n"


More information about the nginx-devel mailing list