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
16 * The Original Code is Mozilla WebGL impl
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
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
46 typedef struct JSProperty JSProperty
;
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
) {
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
);
72 : data(0), byteLength()
78 bool allocateStorage(JSContext
*cx
, uint32 bytes
);
79 void freeStorage(JSContext
*cx
);
81 void *offsetData(uint32 offs
) {
82 return (void*) (((intptr_t)data
) + offs
);
92 * The non-templated base class for the specific typed implementations.
93 * This class holds all the member variables that are used by
97 struct JS_FRIEND_API(TypedArray
) {
109 * Special type that's a uint8, but assignments are clamped to 0 .. 255.
110 * Treat the raw data type as a uint8.
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
); }
147 TypedArray() : buffer(0) { }
149 bool isArrayIndex(JSContext
*cx
, jsid id
, jsuint
*ip
= NULL
);
150 bool valid() { return buffer
!= 0; }
161 inline int slotWidth() const {
163 case js::TypedArray::TYPE_INT8
:
164 case js::TypedArray::TYPE_UINT8
:
165 case js::TypedArray::TYPE_UINT8_CLAMPED
:
167 case js::TypedArray::TYPE_INT16
:
168 case js::TypedArray::TYPE_UINT16
:
170 case js::TypedArray::TYPE_INT32
:
171 case js::TypedArray::TYPE_UINT32
:
172 case js::TypedArray::TYPE_FLOAT32
:
174 case js::TypedArray::TYPE_FLOAT64
:
177 JS_NOT_REACHED("invalid typed array");
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
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 */