2012-05-04 09:36:56 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* 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 mozilla_dom_TypedArray_h
|
|
|
|
#define mozilla_dom_TypedArray_h
|
|
|
|
|
2013-08-05 10:40:01 -07:00
|
|
|
#include "jsapi.h"
|
2013-09-05 23:43:04 -07:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "js/RootingAPI.h"
|
2013-09-02 22:01:18 -07:00
|
|
|
#include "js/Tracer.h"
|
2013-08-28 21:30:04 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-08-05 10:40:02 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-08-23 19:42:40 -07:00
|
|
|
#include "nsWrapperCache.h"
|
2012-05-04 09:36:56 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-28 21:30:04 -07:00
|
|
|
/*
|
|
|
|
* Class that just handles the JSObject storage and tracing for typed arrays
|
|
|
|
*/
|
|
|
|
struct TypedArrayObjectStorage : AllTypedArraysBase {
|
|
|
|
protected:
|
|
|
|
JSObject* mObj;
|
|
|
|
|
2013-08-28 21:30:05 -07:00
|
|
|
TypedArrayObjectStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-28 21:30:04 -07:00
|
|
|
public:
|
|
|
|
inline void TraceSelf(JSTracer* trc)
|
|
|
|
{
|
2013-10-28 09:37:58 -07:00
|
|
|
if (mObj) {
|
|
|
|
JS_CallObjectTracer(trc, &mObj, "TypedArray.mObj");
|
|
|
|
}
|
2013-08-28 21:30:04 -07:00
|
|
|
}
|
2013-08-28 21:30:05 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
TypedArrayObjectStorage(const TypedArrayObjectStorage&) MOZ_DELETE;
|
2013-08-28 21:30:04 -07:00
|
|
|
};
|
|
|
|
|
2012-05-04 09:36:56 -07:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* a subclass of the base class that supports creation of a relevant typed array
|
|
|
|
* or array buffer object.
|
|
|
|
*/
|
2012-07-31 20:45:20 -07:00
|
|
|
template<typename T,
|
2012-11-14 09:56:26 -08:00
|
|
|
JSObject* UnboxArray(JSObject*, uint32_t*, T**)>
|
2013-08-28 21:30:04 -07:00
|
|
|
struct TypedArray_base : public TypedArrayObjectStorage {
|
2012-11-14 09:56:26 -08:00
|
|
|
TypedArray_base(JSObject* obj)
|
2012-07-31 20:45:20 -07:00
|
|
|
{
|
2013-08-05 10:40:01 -07:00
|
|
|
DoInit(obj);
|
|
|
|
}
|
|
|
|
|
2013-08-28 21:30:04 -07:00
|
|
|
TypedArray_base()
|
2013-08-05 10:40:01 -07:00
|
|
|
{
|
2013-08-28 21:30:04 -07:00
|
|
|
mObj = nullptr;
|
2012-07-31 20:45:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mData;
|
|
|
|
uint32_t mLength;
|
2012-07-31 20:06:35 -07:00
|
|
|
|
2012-07-31 20:45:20 -07:00
|
|
|
public:
|
2013-08-05 10:40:01 -07:00
|
|
|
inline bool Init(JSObject* obj)
|
2013-08-05 10:40:01 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!inited());
|
|
|
|
DoInit(obj);
|
2013-08-05 10:40:01 -07:00
|
|
|
return inited();
|
2013-08-05 10:40:01 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 20:45:20 -07:00
|
|
|
inline bool inited() const {
|
|
|
|
return !!mObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T *Data() const {
|
|
|
|
MOZ_ASSERT(inited());
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t Length() const {
|
|
|
|
MOZ_ASSERT(inited());
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSObject *Obj() const {
|
|
|
|
MOZ_ASSERT(inited());
|
|
|
|
return mObj;
|
|
|
|
}
|
2013-08-05 10:40:01 -07:00
|
|
|
|
2013-08-05 10:40:01 -07:00
|
|
|
inline bool WrapIntoNewCompartment(JSContext* cx)
|
|
|
|
{
|
2013-11-11 00:04:41 -08:00
|
|
|
return JS_WrapObject(cx,
|
|
|
|
JS::MutableHandle<JSObject*>::fromMarkedLocation(&mObj));
|
2013-08-05 10:40:01 -07:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:40:01 -07:00
|
|
|
protected:
|
|
|
|
inline void DoInit(JSObject* obj)
|
|
|
|
{
|
|
|
|
mObj = UnboxArray(obj, &mLength, &mData);
|
|
|
|
}
|
2013-08-28 21:30:05 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
TypedArray_base(const TypedArray_base&) MOZ_DELETE;
|
2012-05-04 09:36:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-31 20:45:20 -07:00
|
|
|
template<typename T,
|
2012-11-14 09:56:26 -08:00
|
|
|
T* GetData(JSObject*),
|
|
|
|
JSObject* UnboxArray(JSObject*, uint32_t*, T**),
|
2012-05-04 09:36:56 -07:00
|
|
|
JSObject* CreateNew(JSContext*, uint32_t)>
|
2012-07-31 20:45:20 -07:00
|
|
|
struct TypedArray : public TypedArray_base<T,UnboxArray> {
|
2012-11-14 09:56:26 -08:00
|
|
|
TypedArray(JSObject* obj) :
|
|
|
|
TypedArray_base<T,UnboxArray>(obj)
|
2012-05-04 09:36:56 -07:00
|
|
|
{}
|
2012-07-31 20:45:20 -07:00
|
|
|
|
2013-08-05 10:40:01 -07:00
|
|
|
TypedArray() :
|
|
|
|
TypedArray_base<T,UnboxArray>()
|
|
|
|
{}
|
|
|
|
|
2012-05-04 09:36:56 -07:00
|
|
|
static inline JSObject*
|
2012-06-25 09:37:46 -07:00
|
|
|
Create(JSContext* cx, nsWrapperCache* creator, uint32_t length,
|
2013-08-22 22:17:07 -07:00
|
|
|
const T* data = nullptr) {
|
2013-05-30 14:46:48 -07:00
|
|
|
JS::Rooted<JSObject*> creatorWrapper(cx);
|
2012-08-21 18:42:53 -07:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2012-06-25 09:37:46 -07:00
|
|
|
if (creator && (creatorWrapper = creator->GetWrapperPreserveColor())) {
|
2012-08-21 18:42:53 -07:00
|
|
|
ac.construct(cx, creatorWrapper);
|
2012-06-25 09:37:46 -07:00
|
|
|
}
|
2013-08-22 22:17:07 -07:00
|
|
|
|
|
|
|
return CreateCommon(cx, creatorWrapper, length, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline JSObject*
|
|
|
|
Create(JSContext* cx, JS::Handle<JSObject*> creator, uint32_t length,
|
|
|
|
const T* data = nullptr) {
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (creator) {
|
|
|
|
ac.construct(cx, creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CreateCommon(cx, creator, length, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline JSObject*
|
|
|
|
CreateCommon(JSContext* cx, JS::Handle<JSObject*> creator, uint32_t length,
|
|
|
|
const T* data) {
|
2012-05-04 09:36:56 -07:00
|
|
|
JSObject* obj = CreateNew(cx, length);
|
|
|
|
if (!obj) {
|
2013-08-22 22:17:07 -07:00
|
|
|
return nullptr;
|
2012-05-04 09:36:56 -07:00
|
|
|
}
|
|
|
|
if (data) {
|
2012-11-14 09:56:26 -08:00
|
|
|
T* buf = static_cast<T*>(GetData(obj));
|
2012-05-04 09:36:56 -07:00
|
|
|
memcpy(buf, data, length*sizeof(T));
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2013-08-28 21:30:05 -07:00
|
|
|
|
|
|
|
TypedArray(const TypedArray&) MOZ_DELETE;
|
2012-05-04 09:36:56 -07:00
|
|
|
};
|
|
|
|
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<int8_t, JS_GetInt8ArrayData, JS_GetObjectAsInt8Array,
|
2012-05-04 09:36:56 -07:00
|
|
|
JS_NewInt8Array>
|
|
|
|
Int8Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<uint8_t, JS_GetUint8ArrayData,
|
|
|
|
JS_GetObjectAsUint8Array, JS_NewUint8Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Uint8Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<uint8_t, JS_GetUint8ClampedArrayData,
|
|
|
|
JS_GetObjectAsUint8ClampedArray, JS_NewUint8ClampedArray>
|
2012-05-04 09:36:56 -07:00
|
|
|
Uint8ClampedArray;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<int16_t, JS_GetInt16ArrayData,
|
|
|
|
JS_GetObjectAsInt16Array, JS_NewInt16Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Int16Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<uint16_t, JS_GetUint16ArrayData,
|
|
|
|
JS_GetObjectAsUint16Array, JS_NewUint16Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Uint16Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<int32_t, JS_GetInt32ArrayData,
|
|
|
|
JS_GetObjectAsInt32Array, JS_NewInt32Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Int32Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<uint32_t, JS_GetUint32ArrayData,
|
|
|
|
JS_GetObjectAsUint32Array, JS_NewUint32Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Uint32Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<float, JS_GetFloat32ArrayData,
|
|
|
|
JS_GetObjectAsFloat32Array, JS_NewFloat32Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Float32Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<double, JS_GetFloat64ArrayData,
|
|
|
|
JS_GetObjectAsFloat64Array, JS_NewFloat64Array>
|
2012-05-04 09:36:56 -07:00
|
|
|
Float64Array;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray_base<uint8_t, JS_GetObjectAsArrayBufferView>
|
2012-05-04 09:36:56 -07:00
|
|
|
ArrayBufferView;
|
2012-07-31 20:45:20 -07:00
|
|
|
typedef TypedArray<uint8_t, JS_GetArrayBufferData,
|
|
|
|
JS_GetObjectAsArrayBuffer, JS_NewArrayBuffer>
|
2012-05-04 09:36:56 -07:00
|
|
|
ArrayBuffer;
|
|
|
|
|
2013-08-28 21:30:04 -07:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-04 09:36:56 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_TypedArray_h */
|