From 832040440f0b9c66f5a1b358f43640a6b6e61fdd Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Sun, 1 Mar 2015 06:47:07 -0600 Subject: [PATCH] Bug 1170307 - Inline the common path of NonNullObject; use it instead of ReportObjectRequired in the Debugger. r=shu. --HG-- extra : rebase_source : ae247e5b84d9d9988dc28e2eec2cdcd5bf3c8f8b extra : amend_source : 0f951eeac1aca42a6a74c09441cf34b248f41935 --- js/src/jsobj.cpp | 26 ++++++++----------- js/src/jsobj.h | 17 +++++++++++-- js/src/vm/Debugger.cpp | 58 +++++++++++++----------------------------- 3 files changed, 42 insertions(+), 59 deletions(-) diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp index 94e76452f15c..4d6d00139f9c 100644 --- a/js/src/jsobj.cpp +++ b/js/src/jsobj.cpp @@ -92,19 +92,16 @@ JS_ObjectToOuterObject(JSContext* cx, HandleObject obj) return GetOuterObject(cx, obj); } -JSObject* -js::NonNullObject(JSContext* cx, const Value& v) +void +js::ReportNotObject(JSContext* cx, const Value& v) { - if (v.isPrimitive()) { - RootedValue value(cx, v); - UniquePtr bytes = - DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, value, nullptr); - if (!bytes) - return nullptr; + MOZ_ASSERT(!v.isObject()); + + RootedValue value(cx, v); + UniquePtr bytes = + DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, value, nullptr); + if (bytes) JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT, bytes.get()); - return nullptr; - } - return &v.toObject(); } const char* @@ -292,12 +289,9 @@ js::ToPropertyDescriptor(JSContext* cx, HandleValue descval, bool checkAccessors MutableHandle desc) { // step 2 - if (!descval.isObject()) { - JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT, - InformalValueTypeName(descval)); + RootedObject obj(cx, NonNullObject(cx, descval)); + if (!obj) return false; - } - RootedObject obj(cx, &descval.toObject()); // step 3 desc.clear(); diff --git a/js/src/jsobj.h b/js/src/jsobj.h index 539b2f1ad364..1006eacd61d3 100644 --- a/js/src/jsobj.h +++ b/js/src/jsobj.h @@ -1270,8 +1270,21 @@ XDRObjectLiteral(XDRState* xdr, MutableHandleObject obj); extern bool ReportGetterOnlyAssignment(JSContext* cx, bool strict); -extern JSObject* -NonNullObject(JSContext* cx, const Value& v); +/* + * Report a TypeError: "so-and-so is not an object". + * Using NotNullObject is usually less code. + */ +extern void +ReportNotObject(JSContext* cx, const Value& v); + +inline JSObject* +NonNullObject(JSContext* cx, const Value& v) +{ + if (v.isObject()) + return &v.toObject(); + ReportNotObject(cx, v); + return nullptr; +} extern const char* InformalValueTypeName(const Value& v); diff --git a/js/src/vm/Debugger.cpp b/js/src/vm/Debugger.cpp index 7e8a48bbb41d..69c19f02ed65 100644 --- a/js/src/vm/Debugger.cpp +++ b/js/src/vm/Debugger.cpp @@ -125,13 +125,6 @@ GetOrCreateFunctionScript(JSContext* cx, HandleFunction fun) return fun->nonLazyScript(); } -bool -js::ReportObjectRequired(JSContext* cx) -{ - JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT, "value"); - return false; -} - static bool ValueToIdentifier(JSContext* cx, HandleValue v, MutableHandleId id) { @@ -2243,7 +2236,6 @@ Debugger::updateObservesAsmJSOnDebuggees(IsObserving observing) } } - /*** Allocations Tracking *************************************************************************/ @@ -2645,11 +2637,9 @@ const Class Debugger::jsclass = { /* static */ Debugger* Debugger::fromThisValue(JSContext* cx, const CallArgs& args, const char* fnname) { - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, args.thisv()); + if (!thisobj) return nullptr; - } - JSObject* thisobj = &args.thisv().toObject(); if (thisobj->getClass() != &Debugger::jsclass) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Debugger", fnname, thisobj->getClass()->name); @@ -3206,10 +3196,9 @@ Debugger::construct(JSContext* cx, unsigned argc, Value* vp) /* Check that the arguments, if any, are cross-compartment wrappers. */ for (unsigned i = 0; i < args.length(); i++) { - const Value& arg = args[i]; - if (!arg.isObject()) - return ReportObjectRequired(cx); - JSObject* argobj = &arg.toObject(); + JSObject* argobj = NonNullObject(cx, args[i]); + if (!argobj) + return false; if (!argobj->is()) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_DEBUG_CCW_REQUIRED, "Debugger"); @@ -4565,11 +4554,9 @@ Debugger::wrapScript(JSContext* cx, HandleScript script) static JSObject* DebuggerScript_check(JSContext* cx, const Value& v, const char* clsname, const char* fnname) { - if (!v.isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, v); + if (!thisobj) return nullptr; - } - JSObject* thisobj = &v.toObject(); if (thisobj->getClass() != &DebuggerScript_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, clsname, fnname, thisobj->getClass()->name); @@ -5592,12 +5579,9 @@ DebuggerSource_construct(JSContext* cx, unsigned argc, Value* vp) static NativeObject* DebuggerSource_checkThis(JSContext* cx, const CallArgs& args, const char* fnname) { - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, args.thisv()); + if (!thisobj) return nullptr; - } - - JSObject* thisobj = &args.thisv().toObject(); if (thisobj->getClass() != &DebuggerSource_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Debugger.Source", fnname, thisobj->getClass()->name); @@ -5890,11 +5874,9 @@ const Class DebuggerFrame_class = { static NativeObject* CheckThisFrame(JSContext* cx, const CallArgs& args, const char* fnname, bool checkLive) { - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, args.thisv()); + if (!thisobj) return nullptr; - } - JSObject* thisobj = &args.thisv().toObject(); if (thisobj->getClass() != &DebuggerFrame_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Debugger.Frame", fnname, thisobj->getClass()->name); @@ -6113,11 +6095,9 @@ DebuggerArguments_getArg(JSContext* cx, unsigned argc, Value* vp) int32_t i = args.callee().as().getExtendedSlot(0).toInt32(); /* Check that the this value is an Arguments object. */ - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + RootedObject argsobj(cx, NonNullObject(cx, args.thisv())); + if (!argsobj) return false; - } - RootedObject argsobj(cx, &args.thisv().toObject()); if (argsobj->getClass() != &DebuggerArguments_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Arguments", "getArgument", argsobj->getClass()->name); @@ -6660,11 +6640,9 @@ const Class DebuggerObject_class = { static NativeObject* DebuggerObject_checkThis(JSContext* cx, const CallArgs& args, const char* fnname) { - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, args.thisv()); + if (!thisobj) return nullptr; - } - JSObject* thisobj = &args.thisv().toObject(); if (thisobj->getClass() != &DebuggerObject_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Debugger.Object", fnname, thisobj->getClass()->name); @@ -7608,11 +7586,9 @@ static NativeObject* DebuggerEnv_checkThis(JSContext* cx, const CallArgs& args, const char* fnname, bool requireDebuggee = true) { - if (!args.thisv().isObject()) { - ReportObjectRequired(cx); + JSObject* thisobj = NonNullObject(cx, args.thisv()); + if (!thisobj) return nullptr; - } - JSObject* thisobj = &args.thisv().toObject(); if (thisobj->getClass() != &DebuggerEnv_class) { JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, "Debugger.Environment", fnname, thisobj->getClass()->name);