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.
This commit is contained in:
Boris Zbarsky 2018-12-10 14:13:05 -05:00
parent 7f5c7dd8aa
commit d28b50be98
2 changed files with 28 additions and 17 deletions

View File

@ -309,13 +309,28 @@ class MOZ_STACK_CLASS CallArgs
return args; 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: public:
/* /*
* Returns true if there are at least |required| arguments passed in. If * Returns true if there are at least |required| arguments passed in. If
* false, it reports an error message on the context. * false, it reports an error message on the context.
*/ */
JS_PUBLIC_API bool requireAtLeast(JSContext* cx, const char* fnname, JS_PUBLIC_API inline bool requireAtLeast(JSContext* cx, const char* fnname,
unsigned required) const; 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) { MOZ_ALWAYS_INLINE CallArgs CallArgsFromVp(unsigned argc, Value* vp) {

View File

@ -124,21 +124,17 @@ using JS::SourceText;
#define JS_ADDRESSOF_VA_LIST(ap) (&(ap)) #define JS_ADDRESSOF_VA_LIST(ap) (&(ap))
#endif #endif
JS_PUBLIC_API bool JS::CallArgs::requireAtLeast(JSContext* cx, JS_PUBLIC_API void JS::CallArgs::reportMoreArgsNeeded(JSContext* cx,
const char* fnname, const char* fnname,
unsigned required) const { unsigned required,
if (length() < required) { unsigned actual) {
char requiredArgsStr[40]; char requiredArgsStr[40];
SprintfLiteral(requiredArgsStr, "%u", required); SprintfLiteral(requiredArgsStr, "%u", required);
char actualArgsStr[40]; char actualArgsStr[40];
SprintfLiteral(actualArgsStr, "%u", length()); SprintfLiteral(actualArgsStr, "%u", actual);
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_MORE_ARGS_NEEDED, fnname, requiredArgsStr, JSMSG_MORE_ARGS_NEEDED, fnname, requiredArgsStr,
required == 1 ? "" : "s", actualArgsStr); required == 1 ? "" : "s", actualArgsStr);
return false;
}
return true;
} }
static bool ErrorTakesArguments(unsigned msg) { static bool ErrorTakesArguments(unsigned msg) {