From e2f3fcd527025d60331c4b21af924de84683ef4f Mon Sep 17 00:00:00 2001 From: Terrence Cole Date: Tue, 21 Jul 2015 12:57:00 -0700 Subject: [PATCH] Bug 1186154 - Templatize Rooted's context parameter to reduce copy-and-paste; r=jonco --- js/public/RootingAPI.h | 80 +++++++++++------------------------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/js/public/RootingAPI.h b/js/public/RootingAPI.h index 0c67b88e7b3a..108baf739dcd 100644 --- a/js/public/RootingAPI.h +++ b/js/public/RootingAPI.h @@ -643,81 +643,43 @@ class MOZ_STACK_CLASS Rooted : public js::RootedBase "Rooted takes pointer or Traceable types but not Traceable* type"); /* Note: CX is a subclass of either ContextFriendFields or PerThreadDataFriendFields. */ - template - void registerWithRootLists(CX* cx) { + void registerWithRootLists(js::RootLists& roots) { js::ThingRootKind kind = js::RootKind::rootKind(); - this->stack = &cx->roots.stackRoots_[kind]; + this->stack = &roots.stackRoots_[kind]; this->prev = *stack; *stack = reinterpret_cast*>(this); } + static js::RootLists& rootListsForRootingContext(JSContext* cx) { + return js::ContextFriendFields::get(cx)->roots; + } + static js::RootLists& rootListsForRootingContext(js::ContextFriendFields* cx) { + return cx->roots; + } + static js::RootLists& rootListsForRootingContext(JSRuntime* rt) { + return js::PerThreadDataFriendFields::getMainThread(rt)->roots; + } + static js::RootLists& rootListsForRootingContext(js::PerThreadDataFriendFields* pt) { + return pt->roots; + } + public: - explicit Rooted(JSContext* cx + template + explicit Rooted(const RootingContext& cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : ptr(js::GCMethods::initial()) { MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(js::ContextFriendFields::get(cx)); + registerWithRootLists(rootListsForRootingContext(cx)); } - template - Rooted(JSContext* cx, S&& initial + template + Rooted(const RootingContext& cx, S&& initial MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : ptr(mozilla::Forward(initial)) { MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(js::ContextFriendFields::get(cx)); - } - - explicit Rooted(js::ContextFriendFields* cx - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(js::GCMethods::initial()) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(cx); - } - - template - Rooted(js::ContextFriendFields* cx, S&& initial - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(mozilla::Forward(initial)) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(cx); - } - - explicit Rooted(js::PerThreadDataFriendFields* pt - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(js::GCMethods::initial()) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(pt); - } - - template - Rooted(js::PerThreadDataFriendFields* pt, S&& initial - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(mozilla::Forward(initial)) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(pt); - } - - explicit Rooted(JSRuntime* rt - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(js::GCMethods::initial()) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(js::PerThreadDataFriendFields::getMainThread(rt)); - } - - template - Rooted(JSRuntime* rt, S&& initial - MOZ_GUARD_OBJECT_NOTIFIER_PARAM) - : ptr(mozilla::Forward(initial)) - { - MOZ_GUARD_OBJECT_NOTIFIER_INIT; - registerWithRootLists(js::PerThreadDataFriendFields::getMainThread(rt)); + registerWithRootLists(rootListsForRootingContext(cx)); } ~Rooted() {