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
|
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2013-05-31 01:46:48 +04:00
|
|
|
#include "js/RootingAPI.h"
|
2012-05-04 20:36:56 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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,
|
2012-11-14 21:56:26 +04:00
|
|
|
JSObject* UnboxArray(JSObject*, uint32_t*, T**)>
|
2012-05-04 20:36:56 +04:00
|
|
|
struct TypedArray_base {
|
2012-11-14 21:56:26 +04:00
|
|
|
TypedArray_base(JSObject* obj)
|
2012-08-01 07:45:20 +04:00
|
|
|
{
|
2013-08-05 21:40:01 +04:00
|
|
|
DoInit(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypedArray_base() :
|
|
|
|
mObj(nullptr)
|
|
|
|
{
|
2012-08-01 07:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mData;
|
|
|
|
uint32_t mLength;
|
|
|
|
JSObject* mObj;
|
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 void Init(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!inited());
|
|
|
|
DoInit(obj);
|
|
|
|
}
|
|
|
|
|
2012-08-01 07:45:20 +04: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 21:40:01 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
inline void DoInit(JSObject* obj)
|
|
|
|
{
|
|
|
|
mObj = UnboxArray(obj, &mLength, &mData);
|
|
|
|
}
|
2012-05-04 20:36:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
template<typename T,
|
2012-11-14 21:56:26 +04:00
|
|
|
T* GetData(JSObject*),
|
|
|
|
JSObject* UnboxArray(JSObject*, uint32_t*, T**),
|
2012-05-04 20:36:56 +04:00
|
|
|
JSObject* CreateNew(JSContext*, uint32_t)>
|
2012-08-01 07:45:20 +04:00
|
|
|
struct TypedArray : public TypedArray_base<T,UnboxArray> {
|
2012-11-14 21:56:26 +04:00
|
|
|
TypedArray(JSObject* obj) :
|
|
|
|
TypedArray_base<T,UnboxArray>(obj)
|
2012-05-04 20:36:56 +04:00
|
|
|
{}
|
2012-08-01 07:45:20 +04:00
|
|
|
|
2013-08-05 21:40:01 +04:00
|
|
|
TypedArray() :
|
|
|
|
TypedArray_base<T,UnboxArray>()
|
|
|
|
{}
|
|
|
|
|
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,
|
2012-08-08 04:57:27 +04:00
|
|
|
const T* data = NULL) {
|
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
|
|
|
}
|
2012-05-04 20:36:56 +04:00
|
|
|
JSObject* obj = CreateNew(cx, length);
|
|
|
|
if (!obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<int8_t, JS_GetInt8ArrayData, JS_GetObjectAsInt8Array,
|
2012-05-04 20:36:56 +04:00
|
|
|
JS_NewInt8Array>
|
|
|
|
Int8Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<uint8_t, JS_GetUint8ArrayData,
|
|
|
|
JS_GetObjectAsUint8Array, JS_NewUint8Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint8Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<uint8_t, JS_GetUint8ClampedArrayData,
|
|
|
|
JS_GetObjectAsUint8ClampedArray, JS_NewUint8ClampedArray>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint8ClampedArray;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<int16_t, JS_GetInt16ArrayData,
|
|
|
|
JS_GetObjectAsInt16Array, JS_NewInt16Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Int16Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<uint16_t, JS_GetUint16ArrayData,
|
|
|
|
JS_GetObjectAsUint16Array, JS_NewUint16Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint16Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<int32_t, JS_GetInt32ArrayData,
|
|
|
|
JS_GetObjectAsInt32Array, JS_NewInt32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Int32Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<uint32_t, JS_GetUint32ArrayData,
|
|
|
|
JS_GetObjectAsUint32Array, JS_NewUint32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Uint32Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<float, JS_GetFloat32ArrayData,
|
|
|
|
JS_GetObjectAsFloat32Array, JS_NewFloat32Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Float32Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<double, JS_GetFloat64ArrayData,
|
|
|
|
JS_GetObjectAsFloat64Array, JS_NewFloat64Array>
|
2012-05-04 20:36:56 +04:00
|
|
|
Float64Array;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray_base<uint8_t, JS_GetObjectAsArrayBufferView>
|
2012-05-04 20:36:56 +04:00
|
|
|
ArrayBufferView;
|
2012-08-01 07:45:20 +04:00
|
|
|
typedef TypedArray<uint8_t, JS_GetArrayBufferData,
|
|
|
|
JS_GetObjectAsArrayBuffer, JS_NewArrayBuffer>
|
2012-05-04 20:36:56 +04:00
|
|
|
ArrayBuffer;
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_TypedArray_h */
|