/js/src/jstypedarray.h

http://github.com/zpao/v8monkey · C Header · 364 lines · 219 code · 67 blank · 78 comment · 1 complexity · 42d8a4db2828f137fbdc9a3352fb6e80 MD5 · raw file

  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. #ifndef jstypedarray_h
  40. #define jstypedarray_h
  41. #include "jsapi.h"
  42. #include "jsclass.h"
  43. #include "gc/Barrier.h"
  44. typedef struct JSProperty JSProperty;
  45. namespace js {
  46. /*
  47. * ArrayBuffer
  48. *
  49. * This class holds the underlying raw buffer that the TypedArray
  50. * subclasses access. It can be created explicitly and passed to a
  51. * TypedArray subclass, or can be created implicitly by constructing a
  52. * TypedArray with a size.
  53. */
  54. struct JS_FRIEND_API(ArrayBuffer) {
  55. static Class slowClass;
  56. static JSPropertySpec jsprops[];
  57. static JSFunctionSpec jsfuncs[];
  58. static JSBool prop_getByteLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
  59. static JSBool fun_slice(JSContext *cx, uintN argc, Value *vp);
  60. static JSBool class_constructor(JSContext *cx, uintN argc, Value *vp);
  61. static JSObject *create(JSContext *cx, int32_t nbytes, uint8_t *contents = NULL);
  62. static JSObject *createSlice(JSContext *cx, JSObject *arrayBuffer,
  63. uint32_t begin, uint32_t end);
  64. ArrayBuffer()
  65. {
  66. }
  67. ~ArrayBuffer();
  68. static void
  69. obj_trace(JSTracer *trc, JSObject *obj);
  70. static JSBool
  71. obj_lookupGeneric(JSContext *cx, JSObject *obj, jsid id,
  72. JSObject **objp, JSProperty **propp);
  73. static JSBool
  74. obj_lookupProperty(JSContext *cx, JSObject *obj, PropertyName *name,
  75. JSObject **objp, JSProperty **propp);
  76. static JSBool
  77. obj_lookupElement(JSContext *cx, JSObject *obj, uint32_t index,
  78. JSObject **objp, JSProperty **propp);
  79. static JSBool
  80. obj_lookupSpecial(JSContext *cx, JSObject *obj, SpecialId sid, JSObject **objp,
  81. JSProperty **propp);
  82. static JSBool
  83. obj_defineGeneric(JSContext *cx, JSObject *obj, jsid id, const Value *v,
  84. PropertyOp getter, StrictPropertyOp setter, uintN attrs);
  85. static JSBool
  86. obj_defineProperty(JSContext *cx, JSObject *obj, PropertyName *name, const Value *v,
  87. PropertyOp getter, StrictPropertyOp setter, uintN attrs);
  88. static JSBool
  89. obj_defineElement(JSContext *cx, JSObject *obj, uint32_t index, const Value *v,
  90. PropertyOp getter, StrictPropertyOp setter, uintN attrs);
  91. static JSBool
  92. obj_defineSpecial(JSContext *cx, JSObject *obj, SpecialId sid, const Value *v,
  93. PropertyOp getter, StrictPropertyOp setter, uintN attrs);
  94. static JSBool
  95. obj_getGeneric(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id, Value *vp);
  96. static JSBool
  97. obj_getProperty(JSContext *cx, JSObject *obj, JSObject *receiver, PropertyName *name,
  98. Value *vp);
  99. static JSBool
  100. obj_getElement(JSContext *cx, JSObject *obj, JSObject *receiver, uint32_t index, Value *vp);
  101. static JSBool
  102. obj_getElementIfPresent(JSContext *cx, JSObject *obj, JSObject *receiver, uint32_t index,
  103. Value *vp, bool *present);
  104. static JSBool
  105. obj_getSpecial(JSContext *cx, JSObject *obj, JSObject *receiver, SpecialId sid, Value *vp);
  106. static JSBool
  107. obj_setGeneric(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict);
  108. static JSBool
  109. obj_setProperty(JSContext *cx, JSObject *obj, PropertyName *name, Value *vp, JSBool strict);
  110. static JSBool
  111. obj_setElement(JSContext *cx, JSObject *obj, uint32_t index, Value *vp, JSBool strict);
  112. static JSBool
  113. obj_setSpecial(JSContext *cx, JSObject *obj, SpecialId sid, Value *vp, JSBool strict);
  114. static JSBool
  115. obj_getGenericAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
  116. static JSBool
  117. obj_getPropertyAttributes(JSContext *cx, JSObject *obj, PropertyName *name, uintN *attrsp);
  118. static JSBool
  119. obj_getElementAttributes(JSContext *cx, JSObject *obj, uint32_t index, uintN *attrsp);
  120. static JSBool
  121. obj_getSpecialAttributes(JSContext *cx, JSObject *obj, SpecialId sid, uintN *attrsp);
  122. static JSBool
  123. obj_setGenericAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
  124. static JSBool
  125. obj_setPropertyAttributes(JSContext *cx, JSObject *obj, PropertyName *name, uintN *attrsp);
  126. static JSBool
  127. obj_setElementAttributes(JSContext *cx, JSObject *obj, uint32_t index, uintN *attrsp);
  128. static JSBool
  129. obj_setSpecialAttributes(JSContext *cx, JSObject *obj, SpecialId sid, uintN *attrsp);
  130. static JSBool
  131. obj_deleteProperty(JSContext *cx, JSObject *obj, PropertyName *name, Value *rval, JSBool strict);
  132. static JSBool
  133. obj_deleteElement(JSContext *cx, JSObject *obj, uint32_t index, Value *rval, JSBool strict);
  134. static JSBool
  135. obj_deleteSpecial(JSContext *cx, JSObject *obj, SpecialId sid, Value *rval, JSBool strict);
  136. static JSBool
  137. obj_enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
  138. Value *statep, jsid *idp);
  139. static JSType
  140. obj_typeOf(JSContext *cx, JSObject *obj);
  141. static JSObject *
  142. getArrayBuffer(JSObject *obj);
  143. };
  144. /*
  145. * TypedArray
  146. *
  147. * The non-templated base class for the specific typed implementations.
  148. * This class holds all the member variables that are used by
  149. * the subclasses.
  150. */
  151. struct JS_FRIEND_API(TypedArray) {
  152. enum {
  153. TYPE_INT8 = 0,
  154. TYPE_UINT8,
  155. TYPE_INT16,
  156. TYPE_UINT16,
  157. TYPE_INT32,
  158. TYPE_UINT32,
  159. TYPE_FLOAT32,
  160. TYPE_FLOAT64,
  161. /*
  162. * Special type that's a uint8, but assignments are clamped to 0 .. 255.
  163. * Treat the raw data type as a uint8.
  164. */
  165. TYPE_UINT8_CLAMPED,
  166. TYPE_MAX
  167. };
  168. enum {
  169. /* Properties of the typed array stored in reserved slots. */
  170. FIELD_LENGTH = 0,
  171. FIELD_BYTEOFFSET,
  172. FIELD_BYTELENGTH,
  173. FIELD_TYPE,
  174. FIELD_BUFFER,
  175. FIELD_MAX,
  176. NUM_FIXED_SLOTS = 7
  177. };
  178. // and MUST NOT be used to construct new objects.
  179. static Class fastClasses[TYPE_MAX];
  180. // These are the slow/original classes, used
  181. // fo constructing new objects
  182. static Class slowClasses[TYPE_MAX];
  183. static JSPropertySpec jsprops[];
  184. static JSObject *getTypedArray(JSObject *obj);
  185. static JSBool prop_getBuffer(JSContext *cx, JSObject *obj, jsid id, Value *vp);
  186. static JSBool prop_getByteOffset(JSContext *cx, JSObject *obj, jsid id, Value *vp);
  187. static JSBool prop_getByteLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
  188. static JSBool prop_getLength(JSContext *cx, JSObject *obj, jsid id, Value *vp);
  189. static JSBool obj_lookupGeneric(JSContext *cx, JSObject *obj, jsid id,
  190. JSObject **objp, JSProperty **propp);
  191. static JSBool obj_lookupProperty(JSContext *cx, JSObject *obj, PropertyName *name,
  192. JSObject **objp, JSProperty **propp);
  193. static JSBool obj_lookupElement(JSContext *cx, JSObject *obj, uint32_t index,
  194. JSObject **objp, JSProperty **propp);
  195. static JSBool obj_lookupSpecial(JSContext *cx, JSObject *obj, SpecialId sid,
  196. JSObject **objp, JSProperty **propp);
  197. static JSBool obj_getGenericAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
  198. static JSBool obj_getPropertyAttributes(JSContext *cx, JSObject *obj, PropertyName *name, uintN *attrsp);
  199. static JSBool obj_getElementAttributes(JSContext *cx, JSObject *obj, uint32_t index, uintN *attrsp);
  200. static JSBool obj_getSpecialAttributes(JSContext *cx, JSObject *obj, SpecialId sid, uintN *attrsp);
  201. static JSBool obj_setGenericAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp);
  202. static JSBool obj_setPropertyAttributes(JSContext *cx, JSObject *obj, PropertyName *name, uintN *attrsp);
  203. static JSBool obj_setElementAttributes(JSContext *cx, JSObject *obj, uint32_t index, uintN *attrsp);
  204. static JSBool obj_setSpecialAttributes(JSContext *cx, JSObject *obj, SpecialId sid, uintN *attrsp);
  205. static uint32_t getLength(JSObject *obj);
  206. static uint32_t getByteOffset(JSObject *obj);
  207. static uint32_t getByteLength(JSObject *obj);
  208. static uint32_t getType(JSObject *obj);
  209. static JSObject * getBuffer(JSObject *obj);
  210. static void * getDataOffset(JSObject *obj);
  211. public:
  212. static bool
  213. isArrayIndex(JSContext *cx, JSObject *obj, jsid id, jsuint *ip = NULL);
  214. static inline uint32_t slotWidth(int atype) {
  215. switch (atype) {
  216. case js::TypedArray::TYPE_INT8:
  217. case js::TypedArray::TYPE_UINT8:
  218. case js::TypedArray::TYPE_UINT8_CLAMPED:
  219. return 1;
  220. case js::TypedArray::TYPE_INT16:
  221. case js::TypedArray::TYPE_UINT16:
  222. return 2;
  223. case js::TypedArray::TYPE_INT32:
  224. case js::TypedArray::TYPE_UINT32:
  225. case js::TypedArray::TYPE_FLOAT32:
  226. return 4;
  227. case js::TypedArray::TYPE_FLOAT64:
  228. return 8;
  229. default:
  230. JS_NOT_REACHED("invalid typed array type");
  231. return 0;
  232. }
  233. }
  234. static inline int slotWidth(JSObject *obj) {
  235. return slotWidth(getType(obj));
  236. }
  237. static int lengthOffset();
  238. static int dataOffset();
  239. };
  240. extern bool
  241. IsFastTypedArrayClass(const Class *clasp);
  242. } // namespace js
  243. /* Friend API methods */
  244. JS_FRIEND_API(JSObject *)
  245. js_InitTypedArrayClasses(JSContext *cx, JSObject *obj);
  246. JS_FRIEND_API(JSBool)
  247. js_IsTypedArray(JSObject *obj);
  248. JS_FRIEND_API(JSBool)
  249. js_IsArrayBuffer(JSObject *obj);
  250. JS_FRIEND_API(JSObject *)
  251. js_CreateArrayBuffer(JSContext *cx, uint32_t nbytes);
  252. /*
  253. * Create a new typed array of type atype (one of the TypedArray
  254. * enumerant values above), with nelements elements.
  255. */
  256. JS_FRIEND_API(JSObject *)
  257. js_CreateTypedArray(JSContext *cx, jsint atype, uint32_t nelements);
  258. /*
  259. * Create a new typed array of type atype (one of the TypedArray
  260. * enumerant values above), and copy in values from the given JSObject,
  261. * which must either be a typed array or an array-like object.
  262. */
  263. JS_FRIEND_API(JSObject *)
  264. js_CreateTypedArrayWithArray(JSContext *cx, jsint atype, JSObject *arrayArg);
  265. /*
  266. * Create a new typed array of type atype (one of the TypedArray
  267. * enumerant values above), using a given ArrayBuffer for storage.
  268. * The byteoffset and length values are optional; if -1 is passed, an
  269. * offset of 0 and enough elements to use up the remainder of the byte
  270. * array are used as the default values.
  271. */
  272. JS_FRIEND_API(JSObject *)
  273. js_CreateTypedArrayWithBuffer(JSContext *cx, jsint atype, JSObject *bufArg,
  274. jsint byteoffset, jsint length);
  275. extern int32_t JS_FASTCALL
  276. js_TypedArray_uint8_clamp_double(const double x);
  277. JS_FRIEND_API(JSBool)
  278. JS_IsArrayBufferObject(JSObject *obj);
  279. JS_FRIEND_API(JSObject *)
  280. JS_NewArrayBuffer(JSContext *cx, jsuint nbytes);
  281. JS_FRIEND_API(uint32_t)
  282. JS_GetArrayBufferByteLength(JSObject *obj);
  283. JS_FRIEND_API(uint8_t *)
  284. JS_GetArrayBufferData(JSObject *obj);
  285. JS_FRIEND_API(uint32_t)
  286. JS_GetTypedArrayLength(JSObject *obj);
  287. JS_FRIEND_API(uint32_t)
  288. JS_GetTypedArrayByteOffset(JSObject *obj);
  289. JS_FRIEND_API(uint32_t)
  290. JS_GetTypedArrayByteLength(JSObject *obj);
  291. JS_FRIEND_API(uint32_t)
  292. JS_GetTypedArrayType(JSObject *obj);
  293. JS_FRIEND_API(JSObject *)
  294. JS_GetTypedArrayBuffer(JSObject *obj);
  295. JS_FRIEND_API(void *)
  296. JS_GetTypedArrayData(JSObject *obj);
  297. #endif /* jstypedarray_h */