Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jstypedarray.h
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
5  *
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/
10  *
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.
15  *
16  * The Original Code is Mozilla WebGL impl
17  *
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.
22  *
23  * Contributor(s):
24  *   Vladimir Vukicevic <vladimir@pobox.com>
25  *
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.
37  *
38  * ***** END LICENSE BLOCK ***** */
39
40 #ifndef jstypedarray_h
41 #define jstypedarray_h
42
43 #include "jsapi.h"
44 #include "jsvalue.h"
45
46 typedef struct JSProperty JSProperty;
47
48 namespace js {
49
50 /*
51  * ArrayBuffer
52  *
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.
57  */
58 struct JS_FRIEND_API(ArrayBuffer) {
59     static Class jsclass;
60     static JSPropertySpec jsprops[];
61
62     static JSBool prop_getByteLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
63     static void class_finalize(JSContext *cx, JSObject *obj);
64
65     static JSBool class_constructor(JSContext *cx, uintN argc, Value *vp);
66
67     static JSObject *create(JSContext *cx, int32 nbytes);
68
69     static ArrayBuffer *fromJSObject(JSObject *obj);
70
71     ArrayBuffer()
72         : data(0), byteLength()
73     {
74     }
75
76     ~ArrayBuffer();
77
78     bool allocateStorage(JSContext *cx, uint32 bytes);
79     void freeStorage(JSContext *cx);
80
81     void *offsetData(uint32 offs) {
82         return (void*) (((intptr_t)data) + offs);
83     }
84
85     void *data;
86     uint32 byteLength;
87 };
88
89 /*
90  * TypedArray
91  *
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.
95  */
96
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,
107
108         /*
109          * Special type that's a uint8, but assignments are clamped to 0 .. 255.
110          * Treat the raw data type as a uint8.
111          */
112         TYPE_UINT8_CLAMPED,
113
114         TYPE_MAX
115     };
116
117     // and MUST NOT be used to construct new objects.
118     static Class fastClasses[TYPE_MAX];
119
120     // These are the slow/original classes, used
121     // fo constructing new objects
122     static Class slowClasses[TYPE_MAX];
123
124     static JSPropertySpec jsprops[];
125
126     static TypedArray *fromJSObject(JSObject *obj);
127
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);
132
133     static JSBool obj_lookupProperty(JSContext *cx, JSObject *obj, jsid id,
134                                      JSObject **objp, JSProperty **propp);
135
136     static void obj_trace(JSTracer *trc, JSObject *obj);
137
138     static JSBool obj_getAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
139
140     static JSBool obj_setAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
141
142     static int32 lengthOffset() { return offsetof(TypedArray, length); }
143     static int32 dataOffset() { return offsetof(TypedArray, data); }
144     static int32 typeOffset() { return offsetof(TypedArray, type); }
145
146   public:
147     TypedArray() : buffer(0) { }
148
149     bool isArrayIndex(JSContext *cx, jsid id, jsuint *ip = NULL);
150     bool valid() { return buffer != 0; }
151
152     ArrayBuffer *buffer;
153     JSObject *bufferJS;
154     uint32 byteOffset;
155     uint32 byteLength;
156     uint32 length;
157     uint32 type;
158
159     void *data;
160
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;
179         }
180     }
181 };
182
183 } // namespace js
184
185 /* Friend API methods */
186
187 JS_FRIEND_API(JSObject *)
188 js_InitTypedArrayClasses(JSContext *cx, JSObject *obj);
189
190 JS_FRIEND_API(JSBool)
191 js_IsTypedArray(JSObject *obj);
192
193 JS_FRIEND_API(JSBool)
194 js_IsArrayBuffer(JSObject *obj);
195
196 JS_FRIEND_API(JSObject *)
197 js_CreateArrayBuffer(JSContext *cx, jsuint nbytes);
198
199 /*
200  * Create a new typed array of type atype (one of the TypedArray
201  * enumerant values above), with nelements elements.
202  */
203 JS_FRIEND_API(JSObject *)
204 js_CreateTypedArray(JSContext *cx, jsint atype, jsuint nelements);
205
206 /*
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.
210  */
211 JS_FRIEND_API(JSObject *)
212 js_CreateTypedArrayWithArray(JSContext *cx, jsint atype, JSObject *arrayArg);
213
214 /*
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.
220  */
221 JS_FRIEND_API(JSObject *)
222 js_CreateTypedArrayWithBuffer(JSContext *cx, jsint atype, JSObject *bufArg,
223                               jsint byteoffset, jsint length);
224
225 /*
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.
230  */
231 JS_FRIEND_API(JSBool)
232 js_ReparentTypedArrayToScope(JSContext *cx, JSObject *obj, JSObject *scope);
233
234 extern int32 JS_FASTCALL
235 js_TypedArray_uint8_clamp_double(const double x);
236
237 #endif /* jstypedarray_h */