From 9ca9e9379453cf0ff63a9c91c3f4d9070824f42a Mon Sep 17 00:00:00 2001 From: Eddy Bruel Date: Tue, 7 Jun 2016 18:21:03 +0200 Subject: [PATCH] Bug 1271653 - Implement a C++ interface for global/allocationSite/errorMessageName;r=jimb --- js/src/vm/Debugger.cpp | 142 +++++++++++++++++++++++++---------------- js/src/vm/Debugger.h | 5 ++ 2 files changed, 93 insertions(+), 54 deletions(-) diff --git a/js/src/vm/Debugger.cpp b/js/src/vm/Debugger.cpp index ef480ee7d304..c63db98e8205 100644 --- a/js/src/vm/Debugger.cpp +++ b/js/src/vm/Debugger.cpp @@ -8128,13 +8128,6 @@ DebuggerObject_getBoundArguments(JSContext* cx, unsigned argc, Value* vp) return true; } -static bool -null(CallArgs& args) -{ - args.rval().setNull(); - return true; -} - #ifdef SPIDERMONKEY_PROMISE static bool DebuggerObject_getIsPromise(JSContext* cx, unsigned argc, Value* vp) @@ -8281,39 +8274,26 @@ DebuggerObject_getPromiseDependentPromises(JSContext* cx, unsigned argc, Value* static bool DebuggerObject_getGlobal(JSContext* cx, unsigned argc, Value* vp) { - THIS_DEBUGOBJECT_OWNER_REFERENT(cx, argc, vp, "get global", args, dbg, obj); + THIS_DEBUGOBJECT(cx, argc, vp, "get global", args, object) - RootedValue v(cx, ObjectValue(obj->global())); - if (!dbg->wrapDebuggeeValue(cx, &v)) + RootedObject result(cx); + if (!DebuggerObject::global(cx, object, &result)) return false; - args.rval().set(v); + + args.rval().setObject(*result); return true; } -/* static */ SavedFrame* -Debugger::getObjectAllocationSite(JSObject& obj) -{ - JSObject* metadata = GetAllocationMetadata(&obj); - if (!metadata) - return nullptr; - - MOZ_ASSERT(!metadata->is()); - return SavedFrame::isSavedFrameAndNotProto(*metadata) - ? &metadata->as() - : nullptr; -} - static bool DebuggerObject_getAllocationSite(JSContext* cx, unsigned argc, Value* vp) { - THIS_DEBUGOBJECT_REFERENT(cx, argc, vp, "get allocationSite", args, obj); + THIS_DEBUGOBJECT(cx, argc, vp, "get allocationSite", args, object) - RootedObject allocSite(cx, Debugger::getObjectAllocationSite(*obj)); - if (!allocSite) - return null(args); - if (!cx->compartment()->wrap(cx, &allocSite)) + RootedObject result(cx); + if (!DebuggerObject::allocationSite(cx, object, &result)) return false; - args.rval().setObject(*allocSite); + + args.rval().setObjectOrNull(result); return true; } @@ -8323,33 +8303,16 @@ DebuggerObject_getAllocationSite(JSContext* cx, unsigned argc, Value* vp) static bool DebuggerObject_getErrorMessageName(JSContext *cx, unsigned argc, Value* vp) { - THIS_DEBUGOBJECT_REFERENT(cx, argc, vp, "get errorMessageName", args, referent); + THIS_DEBUGOBJECT(cx, argc, vp, "get errorMessageName", args, object) - JSObject* obj = referent; - if (IsCrossCompartmentWrapper(obj)) - obj = CheckedUnwrap(obj); - - if (!obj) { - JS_ReportError(cx, "Permission denied to access object"); + RootedString result(cx); + if (!DebuggerObject::errorMessageName(cx, object, &result)) return false; - } - JSErrorReport* report = nullptr; - if (obj->is()) - report = obj->as().getErrorReport(); - - if (report) { - const JSErrorFormatString* efs = GetErrorMessage(nullptr, report->errorNumber); - if (efs) { - RootedString str(cx, JS_NewStringCopyZ(cx, efs->name)); - if (!cx->compartment()->wrap(cx, &str)) - return false; - args.rval().setString(str); - return true; - } - } - - args.rval().setUndefined(); + if (result) + args.rval().setString(result); + else + args.rval().setUndefined(); return true; } @@ -8923,6 +8886,16 @@ DebuggerObject::className(JSContext* cx, Handle object, return true; } +/* static */ bool +DebuggerObject::global(JSContext* cx, Handle object, MutableHandleObject result) +{ + RootedObject referent(cx, object->referent()); + Debugger* dbg = object->owner(); + + result.set(&referent->global()); + return dbg->wrapDebuggeeObject(cx, result); +} + /* static */ bool DebuggerObject::name(JSContext* cx, Handle object, MutableHandleString result) { @@ -9027,6 +9000,67 @@ DebuggerObject::boundArguments(JSContext* cx, Handle object, return true; } +/* static */ SavedFrame* +Debugger::getObjectAllocationSite(JSObject& obj) +{ + JSObject* metadata = GetAllocationMetadata(&obj); + if (!metadata) + return nullptr; + + MOZ_ASSERT(!metadata->is()); + return SavedFrame::isSavedFrameAndNotProto(*metadata) + ? &metadata->as() + : nullptr; +} + +/* static */ bool +DebuggerObject::allocationSite(JSContext* cx, Handle object, + MutableHandleObject result) +{ + RootedObject referent(cx, object->referent()); + + RootedObject allocSite(cx, Debugger::getObjectAllocationSite(*referent)); + if (!cx->compartment()->wrap(cx, &allocSite)) + return false; + + result.set(allocSite); + return true; +} + +/* static */ bool +DebuggerObject::errorMessageName(JSContext* cx, Handle object, + MutableHandleString result) +{ + RootedObject referent(cx, object->referent()); + + JSObject* obj = referent; + if (IsCrossCompartmentWrapper(obj)) + obj = CheckedUnwrap(obj); + + if (!obj) { + JS_ReportError(cx, "Permission denied to access object"); + return false; + } + + if (obj->is()) { + JSErrorReport* report = obj->as().getErrorReport(); + if (report) { + const JSErrorFormatString* efs = GetErrorMessage(nullptr, report->errorNumber); + if (efs) { + RootedString str(cx, JS_NewStringCopyZ(cx, efs->name)); + if (!cx->compartment()->wrap(cx, &str)) + return false; + + result.set(str); + return true; + } + } + } + + result.set(nullptr); + return true; +} + /* static */ bool DebuggerObject::isExtensible(JSContext* cx, Handle object, bool& result) { diff --git a/js/src/vm/Debugger.h b/js/src/vm/Debugger.h index fa273bd2bf84..6198f2baa4dc 100644 --- a/js/src/vm/Debugger.h +++ b/js/src/vm/Debugger.h @@ -1059,6 +1059,7 @@ class DebuggerObject : public NativeObject static bool isGlobal(JSContext* cx, Handle object); static bool className(JSContext* cx, Handle object, MutableHandleString result); + static bool global(JSContext* cx, Handle object, MutableHandleObject result); static bool name(JSContext* cx, Handle object, MutableHandleString result); static bool displayName(JSContext* cx, Handle object, MutableHandleString result); @@ -1071,6 +1072,10 @@ class DebuggerObject : public NativeObject MutableHandleValue result); static bool boundArguments(JSContext* cx, Handle object, MutableHandle result); + static bool allocationSite(JSContext* cx, Handle object, + MutableHandleObject result); + static bool errorMessageName(JSContext* cx, Handle object, + MutableHandleString result); static bool isExtensible(JSContext* cx, Handle object, bool& result); static bool isSealed(JSContext* cx, Handle object, bool& result);