diff --git a/dom/bindings/TypedArray.h b/dom/bindings/TypedArray.h index 80321c6a4d1..0c16b99bc69 100644 --- a/dom/bindings/TypedArray.h +++ b/dom/bindings/TypedArray.h @@ -9,12 +9,27 @@ #include "jsfriendapi.h" #include "jsapi.h" +#include "mozilla/Attributes.h" #include "mozilla/dom/BindingDeclarations.h" #include "nsWrapperCache.h" namespace mozilla { namespace dom { +/* + * Class that just handles the JSObject storage and tracing for typed arrays + */ +struct TypedArrayObjectStorage : AllTypedArraysBase { +protected: + JSObject* mObj; + +public: + inline void TraceSelf(JSTracer* trc) + { + JS_CallObjectTracer(trc, &mObj, "TypedArray.mObj"); + } +}; + /* * Various typed array classes for argument conversion. We have a base class * that has a way of initializing a TypedArray from an existing typed array, and @@ -23,21 +38,20 @@ namespace dom { */ template -struct TypedArray_base : AllTypedArraysBase { +struct TypedArray_base : public TypedArrayObjectStorage { TypedArray_base(JSObject* obj) { DoInit(obj); } - TypedArray_base() : - mObj(nullptr) + TypedArray_base() { + mObj = nullptr; } private: T* mData; uint32_t mLength; - JSObject* mObj; public: inline bool Init(JSObject* obj) @@ -71,11 +85,6 @@ public: return JS_WrapObject(cx, &mObj); } - inline void TraceSelf(JSTracer* trc) - { - JS_CallObjectTracer(trc, &mObj, "TypedArray.mObj"); - } - protected: inline void DoInit(JSObject* obj) { @@ -169,6 +178,75 @@ typedef TypedArray ArrayBuffer; +// A class for rooting an existing TypedArray struct +template +class MOZ_STACK_CLASS TypedArrayRooter : private JS::CustomAutoRooter +{ +public: + TypedArrayRooter(JSContext* cx, + ArrayType* aArray MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : + JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT), + mArray(aArray) + { + } + + virtual void trace(JSTracer* trc) MOZ_OVERRIDE + { + mArray->TraceSelf(trc); + } + +private: + TypedArrayObjectStorage* const mArray; +}; + +// And a specialization for dealing with nullable typed arrays +template struct Nullable; +template +class MOZ_STACK_CLASS TypedArrayRooter > : + private JS::CustomAutoRooter +{ +public: + TypedArrayRooter(JSContext* cx, + Nullable* aArray MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : + JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT), + mArray(aArray) + { + } + + virtual void trace(JSTracer* trc) MOZ_OVERRIDE + { + if (!mArray->IsNull()) { + mArray->Value().TraceSelf(trc); + } + } + +private: + Nullable* const mArray; +}; + +// Class for easily setting up a rooted typed array object on the stack +template +class MOZ_STACK_CLASS RootedTypedArray : public ArrayType, + private TypedArrayRooter +{ +public: + RootedTypedArray(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : + ArrayType(), + TypedArrayRooter(cx, + MOZ_THIS_IN_INITIALIZER_LIST() + MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) + { + } + + RootedTypedArray(JSContext* cx, JSObject* obj MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : + ArrayType(obj), + TypedArrayRooter(cx, + MOZ_THIS_IN_INITIALIZER_LIST() + MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) + { + } +}; + } // namespace dom } // namespace mozilla