Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jstypedarray.h
blob838ead1598605e47758b23701fe2aef1c6265252
1 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 40; indent-tabs-mode: nil -*- */
2 /* vim: set ts=40 sw=4 et tw=99: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla WebGL impl
18 * The Initial Developer of the Original Code is
19 * Mozilla Foundation
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir@pobox.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jstypedarray_h
41 #define jstypedarray_h
43 #include "jsapi.h"
44 #include "jsvalue.h"
46 typedef struct JSProperty JSProperty;
48 namespace js {
51 * ArrayBuffer
53 * This class holds the underlying raw buffer that the TypedArray
54 * subclasses access. It can be created explicitly and passed to a
55 * TypedArray subclass, or can be created implicitly by constructing a
56 * TypedArray with a size.
58 struct JS_FRIEND_API(ArrayBuffer) {
59 static Class jsclass;
60 static JSPropertySpec jsprops[];
62 static JSBool prop_getByteLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
63 static void class_finalize(JSContext *cx, JSObject *obj);
65 static JSBool class_constructor(JSContext *cx, uintN argc, Value *vp);
67 static bool create(JSContext *cx, uintN argc, Value *argv, Value *rval);
69 static ArrayBuffer *fromJSObject(JSObject *obj);
71 ArrayBuffer()
72 : data(0), byteLength()
76 ~ArrayBuffer();
78 bool allocateStorage(JSContext *cx, uint32 bytes);
79 void freeStorage(JSContext *cx);
81 void *offsetData(uint32 offs) {
82 return (void*) (((intptr_t)data) + offs);
85 void *data;
86 uint32 byteLength;
90 * TypedArray
92 * The non-templated base class for the specific typed implementations.
93 * This class holds all the member variables that are used by
94 * the subclasses.
97 struct JS_FRIEND_API(TypedArray) {
98 enum {
99 TYPE_INT8 = 0,
100 TYPE_UINT8,
101 TYPE_INT16,
102 TYPE_UINT16,
103 TYPE_INT32,
104 TYPE_UINT32,
105 TYPE_FLOAT32,
106 TYPE_FLOAT64,
109 * Special type that's a uint8, but assignments are clamped to 0 .. 255.
110 * Treat the raw data type as a uint8.
112 TYPE_UINT8_CLAMPED,
114 TYPE_MAX
117 // and MUST NOT be used to construct new objects.
118 static Class fastClasses[TYPE_MAX];
120 // These are the slow/original classes, used
121 // fo constructing new objects
122 static Class slowClasses[TYPE_MAX];
124 static JSPropertySpec jsprops[];
126 static TypedArray *fromJSObject(JSObject *obj);
128 static JSBool prop_getBuffer(JSContext *cx, JSObject *obj, jsid id, Value *vp);
129 static JSBool prop_getByteOffset(JSContext *cx, JSObject *obj, jsid id, Value *vp);
130 static JSBool prop_getByteLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
131 static JSBool prop_getLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
133 static JSBool obj_lookupProperty(JSContext *cx, JSObject *obj, jsid id,
134 JSObject **objp, JSProperty **propp);
136 static void obj_trace(JSTracer *trc, JSObject *obj);
138 static JSBool obj_getAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
140 static JSBool obj_setAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
142 static int32 lengthOffset() { return offsetof(TypedArray, length); }
143 static int32 dataOffset() { return offsetof(TypedArray, data); }
144 static int32 typeOffset() { return offsetof(TypedArray, type); }
146 public:
147 TypedArray() : buffer(0) { }
149 bool isArrayIndex(JSContext *cx, jsid id, jsuint *ip = NULL);
150 bool valid() { return buffer != 0; }
152 ArrayBuffer *buffer;
153 JSObject *bufferJS;
154 uint32 byteOffset;
155 uint32 byteLength;
156 uint32 length;
157 uint32 type;
159 void *data;
161 inline int slotWidth() const {
162 switch (type) {
163 case js::TypedArray::TYPE_INT8:
164 case js::TypedArray::TYPE_UINT8:
165 case js::TypedArray::TYPE_UINT8_CLAMPED:
166 return 1;
167 case js::TypedArray::TYPE_INT16:
168 case js::TypedArray::TYPE_UINT16:
169 return 2;
170 case js::TypedArray::TYPE_INT32:
171 case js::TypedArray::TYPE_UINT32:
172 case js::TypedArray::TYPE_FLOAT32:
173 return 4;
174 case js::TypedArray::TYPE_FLOAT64:
175 return 8;
176 default:
177 JS_NOT_REACHED("invalid typed array");
178 return 0;
183 } // namespace js
185 /* Friend API methods */
187 JS_FRIEND_API(JSObject *)
188 js_InitTypedArrayClasses(JSContext *cx, JSObject *obj);
190 JS_FRIEND_API(JSBool)
191 js_IsTypedArray(JSObject *obj);
193 JS_FRIEND_API(JSBool)
194 js_IsArrayBuffer(JSObject *obj);
196 JS_FRIEND_API(JSObject *)
197 js_CreateArrayBuffer(JSContext *cx, jsuint nbytes);
200 * Create a new typed array of type atype (one of the TypedArray
201 * enumerant values above), with nelements elements.
203 JS_FRIEND_API(JSObject *)
204 js_CreateTypedArray(JSContext *cx, jsint atype, jsuint nelements);
207 * Create a new typed array of type atype (one of the TypedArray
208 * enumerant values above), and copy in values from the given JSObject,
209 * which must either be a typed array or an array-like object.
211 JS_FRIEND_API(JSObject *)
212 js_CreateTypedArrayWithArray(JSContext *cx, jsint atype, JSObject *arrayArg);
215 * Create a new typed array of type atype (one of the TypedArray
216 * enumerant values above), using a given ArrayBuffer for storage.
217 * The byteoffset and length values are optional; if -1 is passed, an
218 * offset of 0 and enough elements to use up the remainder of the byte
219 * array are used as the default values.
221 JS_FRIEND_API(JSObject *)
222 js_CreateTypedArrayWithBuffer(JSContext *cx, jsint atype, JSObject *bufArg,
223 jsint byteoffset, jsint length);
226 * Reparent a typed array to a new scope. This should only be used to reparent
227 * a typed array that does not share its underlying ArrayBuffer with another
228 * typed array to avoid having a parent mismatch with the other typed array and
229 * its ArrayBuffer.
231 JS_FRIEND_API(JSBool)
232 js_ReparentTypedArrayToScope(JSContext *cx, JSObject *obj, JSObject *scope);
234 extern int32 JS_FASTCALL
235 js_TypedArray_uint8_clamp_double(const double x);
237 #endif /* jstypedarray_h */