Bug 868799 part 1. Introduce a RootedTypedArray class. r=terrence

This commit is contained in:
Boris Zbarsky 2013-08-29 00:30:04 -04:00
parent 5b2946f0a8
commit 315a027d5a

View File

@ -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<typename T,
JSObject* UnboxArray(JSObject*, uint32_t*, T**)>
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<uint8_t, JS_GetArrayBufferData,
JS_GetObjectAsArrayBuffer, JS_NewArrayBuffer>
ArrayBuffer;
// A class for rooting an existing TypedArray struct
template<typename ArrayType>
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<typename Inner> struct Nullable;
template<typename ArrayType>
class MOZ_STACK_CLASS TypedArrayRooter<Nullable<ArrayType> > :
private JS::CustomAutoRooter
{
public:
TypedArrayRooter(JSContext* cx,
Nullable<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
{
if (!mArray->IsNull()) {
mArray->Value().TraceSelf(trc);
}
}
private:
Nullable<ArrayType>* const mArray;
};
// Class for easily setting up a rooted typed array object on the stack
template<typename ArrayType>
class MOZ_STACK_CLASS RootedTypedArray : public ArrayType,
private TypedArrayRooter<ArrayType>
{
public:
RootedTypedArray(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
ArrayType(),
TypedArrayRooter<ArrayType>(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<ArrayType>(cx,
MOZ_THIS_IN_INITIALIZER_LIST()
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
{
}
};
} // namespace dom
} // namespace mozilla