2012-05-04 20:36:56 +04: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 21:40:01 +04:00
|
|
|
#include "jsapi.h"
|
2013-09-06 10:43:04 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "js/RootingAPI.h"
|
2014-04-12 02:19:05 +04:00
|
|
|
#include "js/TracingAPI.h"
|
2013-08-29 08:30:04 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-05-24 01:32:38 +04:00
|
|
|
#include "mozilla/Move.h"
|
2013-08-05 21:40:02 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-08-24 06:42:40 +04:00
|
|
|
#include "nsWrapperCache.h"
|
2012-05-04 20:36:56 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-29 08:30:04 +04:00
|
|
|
/*
|
|
|
|
* Class that just handles the JSObject storage and tracing for typed arrays
|
|
|
|
*/
|
|
|
|
struct TypedArrayObjectStorage : AllTypedArraysBase {
|
|
|
|
protected:
|
2014-06-04 05:47:59 +04:00
|
|
|
JSObject* mTypedObj;
|
|
|
|
JSObject* mWrappedObj;
|
2013-08-29 08:30:04 +04:00
|
|
|
|
2014-06-04 05:47:59 +04:00
|
|
|
TypedArrayObjectStorage()
|
|
|
|
: mTypedObj(nullptr),
|
|
|
|
mWrappedObj(nullptr)
|
2013-08-29 08:30:05 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
explicit TypedArrayObjectStorage(TypedArrayObjectStorage&& aOther)
|
2014-06-04 05:47:59 +04:00
|
|
|
: mTypedObj(aOther.mTypedObj),
|
|
|
|
mWrappedObj(aOther.mWrappedObj)
|
2014-05-24 01:32:38 +04:00
|
|
|
{
|
2014-06-04 05:47:59 +04:00
|
|
|
aOther.mTypedObj = nullptr;
|
|
|
|
aOther.mWrappedObj = nullptr;
|
2014-05-24 01:32:38 +04:00
|
|
|
}
|
|
|
|
|
2013-08-29 08:30:04 +04:00
|
|
|
public:
|
|
|
|
inline void TraceSelf(JSTracer* trc)
|
|
|
|
{
|
2014-06-04 05:47:59 +04:00
|
|
|
if (mTypedObj) {
|
|
|
|
JS_CallObjectTracer(trc, &mTypedObj, "TypedArray.mTypedObj");
|
|
|
|
}
|
|
|
|
if (mWrappedObj) {
|
|
|
|
JS_CallObjectTracer(trc, &mTypedObj, "TypedArray.mWrappedObj");
|
2013-10-28 20:37:58 +04:00
|
|
|
}
|
2013-08-29 08:30:04 +04:00
|
|
|
}
|
2013-08-29 08:30:05 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
TypedArrayObjectStorage(const TypedArrayObjectStorage&) MOZ_DELETE;
|
2013-08-29 08:30:04 +04:00
|
|
|
};
|
|
|
|
|
2012-05-04 20:36:56 +04: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-08-01 07:45:20 +04:00
|
|
|
template<typename T,
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
JSObject* UnwrapArray(JSObject*),
|
|
|
|
void GetLengthAndData(JSObject*, uint32_t*, T**)>
|
2013-08-29 08:30:04 +04:00
|
|
|
struct TypedArray_base : public TypedArrayObjectStorage {
|
2014-03-10 16:55:46 +04:00
|
|
|
typedef T element_type;
|
|
|
|
|
2013-08-29 08:30:04 +04:00
|
|
|
TypedArray_base()
|
2014-06-04 05:31:43 +04:00
|
|
|
: mData(nullptr),
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
mLength(0),
|
|
|
|
mComputed(false)
|
2013-08-05 21:40:01 +04:00
|
|
|
{
|
2012-08-01 07:45:20 +04:00
|
|
|
}
|
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
explicit TypedArray_base(TypedArray_base&& aOther)
|
|
|
|
: TypedArrayObjectStorage(Move(aOther)),
|
|
|
|
mData(aOther.mData),
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
mLength(aOther.mLength),
|
|
|
|
mComputed(aOther.mComputed)
|
2014-05-24 01:32:38 +04:00
|
|
|
{
|
|
|
|
aOther.mData = nullptr;
|
|
|
|
aOther.mLength = 0;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aOther.mComputed = false;
|
2014-05-24 01:32:38 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
private:
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
mutable T* mData;
|
|
|
|
mutable uint32_t mLength;
|
|
|
|
mutable bool mComputed;
|
2012-08-01 07:06:35 +04:00
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
public:
|
2013-08-05 21:40:01 +04:00
|
|
|
inline bool Init(JSObject* obj)
|
2013-08-05 21:40:01 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!inited());
|
2014-06-04 05:47:59 +04:00
|
|
|
mTypedObj = mWrappedObj = UnwrapArray(obj);
|
2013-08-05 21:40:01 +04:00
|
|
|
return inited();
|
2013-08-05 21:40:01 +04:00
|
|
|
}
|
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
inline bool inited() const {
|
2014-06-04 05:47:59 +04:00
|
|
|
return !!mTypedObj;
|
2012-08-01 07:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline T *Data() const {
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
MOZ_ASSERT(mComputed);
|
2012-08-01 07:45:20 +04:00
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t Length() const {
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
MOZ_ASSERT(mComputed);
|
2012-08-01 07:45:20 +04:00
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSObject *Obj() const {
|
|
|
|
MOZ_ASSERT(inited());
|
2014-06-04 05:47:59 +04:00
|
|
|
return mWrappedObj;
|
2012-08-01 07:45:20 +04:00
|
|
|
}
|
2013-08-05 21:40:01 +04:00
|
|
|
|
2013-08-05 21:40:01 +04:00
|
|
|
inline bool WrapIntoNewCompartment(JSContext* cx)
|
|
|
|
{
|
2013-11-11 12:04:41 +04:00
|
|
|
return JS_WrapObject(cx,
|
2014-06-04 05:47:59 +04:00
|
|
|
JS::MutableHandle<JSObject*>::fromMarkedLocation(&mWrappedObj));
|
2013-08-05 21:40:01 +04:00
|
|
|
}
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
inline void ComputeLengthAndData() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(inited());
|
|
|
|
MOZ_ASSERT(!mComputed);
|
2014-06-04 05:47:59 +04:00
|
|
|
GetLengthAndData(mTypedObj, &mLength, &mData);
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
mComputed = true;
|
|
|
|
}
|
|
|
|
|
2013-08-29 08:30:05 +04:00
|
|
|
private:
|
|
|
|
TypedArray_base(const TypedArray_base&) MOZ_DELETE;
|
2012-05-04 20:36:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
template<typename T,
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
JSObject* UnwrapArray(JSObject*),
|
2012-11-14 21:56:26 +04:00
|
|
|
T* GetData(JSObject*),
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
void GetLengthAndData(JSObject*, uint32_t*, T**),
|
2012-05-04 20:36:56 +04:00
|
|
|
JSObject* CreateNew(JSContext*, uint32_t)>
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
struct TypedArray : public TypedArray_base<T, UnwrapArray, GetLengthAndData> {
|
|
|
|
private:
|
|
|
|
typedef TypedArray_base<T, UnwrapArray, GetLengthAndData> Base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TypedArray()
|
|
|
|
: Base()
|
2013-08-05 21:40:01 +04:00
|
|
|
{}
|
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
explicit TypedArray(TypedArray&& aOther)
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
: Base(Move(aOther))
|
2014-05-24 01:32:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-04 20:36:56 +04:00
|
|
|
static inline JSObject*
|
2012-06-25 20:37:46 +04:00
|
|
|
Create(JSContext* cx, nsWrapperCache* creator, uint32_t length,
|
2013-08-23 09:17:07 +04:00
|
|
|
const T* data = nullptr) {
|
2013-05-31 01:46:48 +04:00
|
|
|
JS::Rooted<JSObject*> creatorWrapper(cx);
|
2012-08-22 05:42:53 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2012-06-25 20:37:46 +04:00
|
|
|
if (creator && (creatorWrapper = creator->GetWrapperPreserveColor())) {
|
2012-08-22 05:42:53 +04:00
|
|
|
ac.construct(cx, creatorWrapper);
|
2012-06-25 20:37:46 +04:00
|
|
|
}
|
2013-08-23 09:17:07 +04:00
|
|
|
|
2014-04-10 08:58:42 +04:00
|
|
|
return CreateCommon(cx, length, data);
|
2013-08-23 09:17:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline JSObject*
|
2014-04-10 08:58:42 +04:00
|
|
|
Create(JSContext* cx, uint32_t length, const T* data = nullptr) {
|
|
|
|
return CreateCommon(cx, length, data);
|
2013-08-23 09:17:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline JSObject*
|
2014-04-10 08:58:42 +04:00
|
|
|
CreateCommon(JSContext* cx, uint32_t length, const T* data) {
|
2012-05-04 20:36:56 +04:00
|
|
|
JSObject* obj = CreateNew(cx, length);
|
|
|
|
if (!obj) {
|
2013-08-23 09:17:07 +04:00
|
|
|
return nullptr;
|
2012-05-04 20:36:56 +04:00
|
|
|
}
|
|
|
|
if (data) {
|
2012-11-14 21:56:26 +04:00
|
|
|
T* buf = static_cast<T*>(GetData(obj));
|
2012-05-04 20:36:56 +04:00
|
|
|
memcpy(buf, data, length*sizeof(T));
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2013-08-29 08:30:05 +04:00
|
|
|
|
|
|
|
TypedArray(const TypedArray&) MOZ_DELETE;
|
2012-05-04 20:36:56 +04:00
|
|
|
};
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<int8_t, js::UnwrapInt8Array, JS_GetInt8ArrayData,
|
|
|
|
js::GetInt8ArrayLengthAndData, JS_NewInt8Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Int8Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<uint8_t, js::UnwrapUint8Array, JS_GetUint8ArrayData,
|
|
|
|
js::GetUint8ArrayLengthAndData, JS_NewUint8Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint8Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<uint8_t, js::UnwrapUint8ClampedArray, JS_GetUint8ClampedArrayData,
|
|
|
|
js::GetUint8ClampedArrayLengthAndData, JS_NewUint8ClampedArray>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint8ClampedArray;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<int16_t, js::UnwrapInt16Array, JS_GetInt16ArrayData,
|
|
|
|
js::GetInt16ArrayLengthAndData, JS_NewInt16Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Int16Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<uint16_t, js::UnwrapUint16Array, JS_GetUint16ArrayData,
|
|
|
|
js::GetUint16ArrayLengthAndData, JS_NewUint16Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint16Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<int32_t, js::UnwrapInt32Array, JS_GetInt32ArrayData,
|
|
|
|
js::GetInt32ArrayLengthAndData, JS_NewInt32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Int32Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<uint32_t, js::UnwrapUint32Array, JS_GetUint32ArrayData,
|
|
|
|
js::GetUint32ArrayLengthAndData, JS_NewUint32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint32Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<float, js::UnwrapFloat32Array, JS_GetFloat32ArrayData,
|
|
|
|
js::GetFloat32ArrayLengthAndData, JS_NewFloat32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Float32Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<double, js::UnwrapFloat64Array, JS_GetFloat64ArrayData,
|
|
|
|
js::GetFloat64ArrayLengthAndData, JS_NewFloat64Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Float64Array;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray_base<uint8_t, js::UnwrapArrayBufferView, js::GetArrayBufferViewLengthAndData>
|
2012-05-04 20:36:56 +04:00
|
|
|
ArrayBufferView;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
typedef TypedArray<uint8_t, js::UnwrapArrayBuffer, JS_GetArrayBufferData,
|
|
|
|
js::GetArrayBufferLengthAndData, JS_NewArrayBuffer>
|
2012-05-04 20:36:56 +04:00
|
|
|
ArrayBuffer;
|
|
|
|
|
2014-03-10 16:55:46 +04:00
|
|
|
// A class for converting an nsTArray to a TypedArray
|
|
|
|
// Note: A TypedArrayCreator must not outlive the nsTArray it was created from.
|
|
|
|
// So this is best used to pass from things that understand nsTArray to
|
|
|
|
// things that understand TypedArray, as with Promise::ArgumentToJSValue.
|
|
|
|
template<typename TypedArrayType>
|
|
|
|
class TypedArrayCreator
|
|
|
|
{
|
|
|
|
typedef nsTArray<typename TypedArrayType::element_type> ArrayType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TypedArrayCreator(const ArrayType& aArray)
|
|
|
|
: mArray(aArray)
|
|
|
|
{}
|
|
|
|
|
2014-04-09 02:27:19 +04:00
|
|
|
JSObject* Create(JSContext* aCx) const
|
2014-03-10 16:55:46 +04:00
|
|
|
{
|
2014-04-16 21:56:52 +04:00
|
|
|
return TypedArrayType::Create(aCx, mArray.Length(), mArray.Elements());
|
2014-03-10 16:55:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ArrayType& mArray;
|
|
|
|
};
|
|
|
|
|
2013-08-29 08:30:04 +04: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 20:36:56 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_TypedArray_h */
|