2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-18 14:00:38 +04:00
|
|
|
/* 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__
|
|
|
|
|
2013-11-22 01:19:46 +04:00
|
|
|
#include "js/GCAPI.h"
|
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
/*
|
|
|
|
* 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<T> to correctly handle post-barriering of the JSObject
|
|
|
|
* pointer, when present.
|
2014-06-07 13:34:57 +04:00
|
|
|
*
|
|
|
|
* No implementation of rootKind() is provided, which prevents
|
|
|
|
* Root<nsXBLMaybeCompiled<UncompiledT>> from being used.
|
2013-06-18 14:00:38 +04:00
|
|
|
*/
|
|
|
|
template <class UncompiledT>
|
|
|
|
class nsXBLMaybeCompiled
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsXBLMaybeCompiled() : mUncompiled(BIT_UNCOMPILED) {}
|
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsXBLMaybeCompiled(UncompiledT* uncompiled)
|
2013-06-18 14:00:38 +04:00
|
|
|
: mUncompiled(reinterpret_cast<uintptr_t>(uncompiled) | BIT_UNCOMPILED) {}
|
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit nsXBLMaybeCompiled(JSObject* compiled) : mCompiled(compiled) {}
|
2013-06-18 14:00:38 +04:00
|
|
|
|
|
|
|
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<UncompiledT*>(unmasked);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* GetJSFunction() const
|
2013-11-22 01:19:46 +04:00
|
|
|
{
|
|
|
|
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
|
2013-06-18 14:00:38 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
friend struct js::BarrierMethods<nsXBLMaybeCompiled<UncompiledT>>;
|
2013-06-18 14:00:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Add support for JS::Heap<nsXBLMaybeCompiled>. */
|
2016-04-26 19:18:48 +03:00
|
|
|
namespace JS {
|
2013-06-18 14:00:38 +04:00
|
|
|
|
|
|
|
template <class UncompiledT>
|
2015-12-28 21:11:40 +03:00
|
|
|
struct GCPolicy<nsXBLMaybeCompiled<UncompiledT>>
|
2013-06-18 14:00:38 +04:00
|
|
|
{
|
|
|
|
static nsXBLMaybeCompiled<UncompiledT> initial() { return nsXBLMaybeCompiled<UncompiledT>(); }
|
2015-12-28 21:11:40 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 19:18:48 +03:00
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
template <class UncompiledT>
|
|
|
|
struct BarrierMethods<nsXBLMaybeCompiled<UncompiledT>>
|
|
|
|
{
|
|
|
|
typedef struct BarrierMethods<JSObject *> Base;
|
2013-06-18 14:00:38 +04:00
|
|
|
|
2015-06-18 20:23:49 +03:00
|
|
|
static void postBarrier(nsXBLMaybeCompiled<UncompiledT>* functionp,
|
|
|
|
nsXBLMaybeCompiled<UncompiledT> prev,
|
|
|
|
nsXBLMaybeCompiled<UncompiledT> next)
|
2013-06-18 14:00:38 +04:00
|
|
|
{
|
2015-06-18 20:23:49 +03:00
|
|
|
if (next.IsCompiled()) {
|
|
|
|
Base::postBarrier(&functionp->UnsafeGetJSFunction(),
|
|
|
|
prev.IsCompiled() ? prev.UnsafeGetJSFunction() : nullptr,
|
|
|
|
next.UnsafeGetJSFunction());
|
|
|
|
} else if (prev.IsCompiled()) {
|
|
|
|
Base::postBarrier(&prev.UnsafeGetJSFunction(),
|
|
|
|
prev.UnsafeGetJSFunction(),
|
|
|
|
nullptr);
|
|
|
|
}
|
2013-06-18 14:00:38 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class UncompiledT>
|
2015-12-28 21:11:40 +03:00
|
|
|
class HeapBase<nsXBLMaybeCompiled<UncompiledT>>
|
2013-06-18 14:00:38 +04:00
|
|
|
{
|
2015-12-28 21:11:40 +03:00
|
|
|
const JS::Heap<nsXBLMaybeCompiled<UncompiledT>>& wrapper() const {
|
|
|
|
return *static_cast<const JS::Heap<nsXBLMaybeCompiled<UncompiledT>>*>(this);
|
2013-06-18 14:00:38 +04:00
|
|
|
}
|
|
|
|
|
2015-12-28 21:11:40 +03:00
|
|
|
JS::Heap<nsXBLMaybeCompiled<UncompiledT>>& wrapper() {
|
|
|
|
return *static_cast<JS::Heap<nsXBLMaybeCompiled<UncompiledT>>*>(this);
|
2013-06-18 14:00:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsXBLMaybeCompiled<UncompiledT>* extract() const {
|
|
|
|
return wrapper().address();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLMaybeCompiled<UncompiledT>* extract() {
|
|
|
|
return wrapper().unsafeGet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool IsCompiled() const { return extract()->IsCompiled(); }
|
|
|
|
UncompiledT* GetUncompiled() const { return extract()->GetUncompiled(); }
|
|
|
|
JSObject* GetJSFunction() const { return extract()->GetJSFunction(); }
|
2013-11-22 01:19:46 +04:00
|
|
|
JSObject* GetJSFunctionPreserveColor() const { return extract()->GetJSFunctionPreserveColor(); }
|
2013-06-18 14:00:38 +04:00
|
|
|
|
|
|
|
void SetUncompiled(UncompiledT* source) {
|
2015-02-05 02:27:00 +03:00
|
|
|
wrapper() = nsXBLMaybeCompiled<UncompiledT>(source);
|
2013-06-18 14:00:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetJSFunction(JSObject* function) {
|
2015-02-05 02:27:00 +03:00
|
|
|
wrapper() = nsXBLMaybeCompiled<UncompiledT>(function);
|
2013-06-18 14:00:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& AsHeapObject()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(extract()->IsCompiled());
|
|
|
|
return *reinterpret_cast<JS::Heap<JSObject*>*>(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
#endif // nsXBLMaybeCompiled_h__
|