From f5b1b4bfec439b2747d473ae729fbf83bb3d5db1 Mon Sep 17 00:00:00 2001 From: hongzhidao Date: Sun, 4 Aug 2019 04:06:30 -0400 Subject: [PATCH] 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. --- src/njs_builtin.c | 25 ++++++++++++++----------- src/test/njs_interactive_test.c | 5 +++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/njs_builtin.c b/src/njs_builtin.c index c04cf372..cd6928d2 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -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) { diff --git a/src/test/njs_interactive_test.c b/src/test/njs_interactive_test.c index 18d40d22..aced9ddc 100644 --- a/src/test/njs_interactive_test.c +++ b/src/test/njs_interactive_test.c @@ -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" -- 2.47.3