From d28b50be980ce5afdca046f6712e043889db1ee6 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 10 Dec 2018 14:13:05 -0500 Subject: [PATCH] Bug 1511401 part 3. Inline the fast (no error) path of requireAtLeast. r=nbp We don't want to pay the cost of a function call here in DOM bindings. --- js/public/CallArgs.h | 19 +++++++++++++++++-- js/src/jsapi.cpp | 26 +++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/js/public/CallArgs.h b/js/public/CallArgs.h index 8799437d5fbf..924d15d65412 100644 --- a/js/public/CallArgs.h +++ b/js/public/CallArgs.h @@ -309,13 +309,28 @@ class MOZ_STACK_CLASS CallArgs return args; } + /* + * Helper for requireAtLeast to report the actual exception. + */ + static JS_PUBLIC_API void reportMoreArgsNeeded(JSContext* cx, + const char* fnname, + unsigned required, + unsigned actual); + public: /* * Returns true if there are at least |required| arguments passed in. If * false, it reports an error message on the context. */ - JS_PUBLIC_API bool requireAtLeast(JSContext* cx, const char* fnname, - unsigned required) const; + JS_PUBLIC_API inline bool requireAtLeast(JSContext* cx, const char* fnname, + unsigned required) const { + if (MOZ_LIKELY(required <= length())) { + return true; + } + + reportMoreArgsNeeded(cx, fnname, required, length()); + return false; + } }; MOZ_ALWAYS_INLINE CallArgs CallArgsFromVp(unsigned argc, Value* vp) { diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index baebf60b628e..6db7e23ddf31 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -124,21 +124,17 @@ using JS::SourceText; #define JS_ADDRESSOF_VA_LIST(ap) (&(ap)) #endif -JS_PUBLIC_API bool JS::CallArgs::requireAtLeast(JSContext* cx, - const char* fnname, - unsigned required) const { - if (length() < required) { - char requiredArgsStr[40]; - SprintfLiteral(requiredArgsStr, "%u", required); - char actualArgsStr[40]; - SprintfLiteral(actualArgsStr, "%u", length()); - JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, - JSMSG_MORE_ARGS_NEEDED, fnname, requiredArgsStr, - required == 1 ? "" : "s", actualArgsStr); - return false; - } - - return true; +JS_PUBLIC_API void JS::CallArgs::reportMoreArgsNeeded(JSContext* cx, + const char* fnname, + unsigned required, + unsigned actual) { + char requiredArgsStr[40]; + SprintfLiteral(requiredArgsStr, "%u", required); + char actualArgsStr[40]; + SprintfLiteral(actualArgsStr, "%u", actual); + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, + JSMSG_MORE_ARGS_NEEDED, fnname, requiredArgsStr, + required == 1 ? "" : "s", actualArgsStr); } static bool ErrorTakesArguments(unsigned msg) {