]> git.kaiwu.me - njs.git/commitdiff
Fixed njs_builtin_match().
authorhongzhidao <hongzhidao@gmail.com>
Sun, 4 Aug 2019 08:06:30 +0000 (04:06 -0400)
committerhongzhidao <hongzhidao@gmail.com>
Sun, 4 Aug 2019 08:06:30 +0000 (04:06 -0400)
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.

src/njs_builtin.c
src/test/njs_interactive_test.c

index c04cf3720349ff13117426b38bb7a55ea14a20b5..cd6928d2ea2d6b5a10a506af5378e9573ac8ed68 100644 (file)
@@ -954,6 +954,7 @@ njs_builtin_match(const njs_object_init_t **objects, njs_function_t *function,
     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_t **objects, njs_function_t *function,
                 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_t *vm, njs_function_t *function,
     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_t *vm, njs_function_t *function,
         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) {
index 18d40d2216ac988537d945e8257a07f5613c0222..aced9ddc43c17cf4918a3db16038d071cc6d961c 100644 (file)
@@ -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"