diff -r 000000000000 -r 6474c204b198 dom/xbl/nsXBLMaybeCompiled.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dom/xbl/nsXBLMaybeCompiled.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,156 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsXBLMaybeCompiled_h__ +#define nsXBLMaybeCompiled_h__ + +#include "js/GCAPI.h" + +/* + * A union containing either a pointer representing uncompiled source or a + * JSObject* representing the compiled result. The class is templated on the + * source object type. + * + * The purpose of abstracting this as a separate class is to allow it to be + * wrapped in a JS::Heap to correctly handle post-barriering of the JSObject + * pointer, when present. + */ +template +class nsXBLMaybeCompiled +{ +public: + nsXBLMaybeCompiled() : mUncompiled(BIT_UNCOMPILED) {} + + nsXBLMaybeCompiled(UncompiledT* uncompiled) + : mUncompiled(reinterpret_cast(uncompiled) | BIT_UNCOMPILED) {} + + nsXBLMaybeCompiled(JSObject* compiled) : mCompiled(compiled) {} + + bool IsCompiled() const + { + return !(mUncompiled & BIT_UNCOMPILED); + } + + UncompiledT* GetUncompiled() const + { + MOZ_ASSERT(!IsCompiled(), "Attempt to get compiled function as uncompiled"); + uintptr_t unmasked = mUncompiled & ~BIT_UNCOMPILED; + return reinterpret_cast(unmasked); + } + + JSObject* GetJSFunction() const + { + MOZ_ASSERT(IsCompiled(), "Attempt to get uncompiled function as compiled"); + if (mCompiled) { + JS::ExposeObjectToActiveJS(mCompiled); + } + return mCompiled; + } + + // This is appropriate for use in tracing methods, etc. + JSObject* GetJSFunctionPreserveColor() const + { + MOZ_ASSERT(IsCompiled(), "Attempt to get uncompiled function as compiled"); + return mCompiled; + } + +private: + JSObject*& UnsafeGetJSFunction() + { + MOZ_ASSERT(IsCompiled(), "Attempt to get uncompiled function as compiled"); + return mCompiled; + } + + enum { BIT_UNCOMPILED = 1 << 0 }; + + union + { + // An pointer that represents the function before being compiled, with + // BIT_UNCOMPILED set. + uintptr_t mUncompiled; + + // The JS object for the compiled result. + JSObject* mCompiled; + }; + + friend class js::GCMethods >; +}; + +/* Add support for JS::Heap. */ +namespace js { + +template +struct GCMethods > : public GCMethods +{ + typedef struct GCMethods Base; + + static nsXBLMaybeCompiled initial() { return nsXBLMaybeCompiled(); } + + static bool poisoned(nsXBLMaybeCompiled function) + { + return function.IsCompiled() && Base::poisoned(function.GetJSFunction()); + } + + static bool needsPostBarrier(nsXBLMaybeCompiled function) + { + return function.IsCompiled() && Base::needsPostBarrier(function.GetJSFunction()); + } + +#ifdef JSGC_GENERATIONAL + static void postBarrier(nsXBLMaybeCompiled* functionp) + { + Base::postBarrier(&functionp->UnsafeGetJSFunction()); + } + + static void relocate(nsXBLMaybeCompiled* functionp) + { + Base::relocate(&functionp->UnsafeGetJSFunction()); + } +#endif +}; + +template +class HeapBase > +{ + const JS::Heap >& wrapper() const { + return *static_cast >*>(this); + } + + JS::Heap >& wrapper() { + return *static_cast >*>(this); + } + + const nsXBLMaybeCompiled* extract() const { + return wrapper().address(); + } + + nsXBLMaybeCompiled* extract() { + return wrapper().unsafeGet(); + } + +public: + bool IsCompiled() const { return extract()->IsCompiled(); } + UncompiledT* GetUncompiled() const { return extract()->GetUncompiled(); } + JSObject* GetJSFunction() const { return extract()->GetJSFunction(); } + JSObject* GetJSFunctionPreserveColor() const { return extract()->GetJSFunctionPreserveColor(); } + + void SetUncompiled(UncompiledT* source) { + wrapper().set(nsXBLMaybeCompiled(source)); + } + + void SetJSFunction(JSObject* function) { + wrapper().set(nsXBLMaybeCompiled(function)); + } + + JS::Heap& AsHeapObject() + { + MOZ_ASSERT(extract()->IsCompiled()); + return *reinterpret_cast*>(this); + } +}; + +} /* namespace js */ + +#endif // nsXBLMaybeCompiled_h__