From 293ddefb00cb5854c0f58a63a1e912088c857d69 Mon Sep 17 00:00:00 2001 From: Vladimir Vukicevic Date: Sun, 5 Sep 2010 16:37:52 -0400 Subject: [PATCH] b=593396; implement BYTES_PER_ELEMENT on typed arrays; r=shaver --- js/src/jstypedarray.cpp | 14 ++++++++++++++ js/src/tests/js1_8_5/extensions/typedarray.js | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/js/src/jstypedarray.cpp b/js/src/jstypedarray.cpp index c675a8b650f1..ada5b6939467 100644 --- a/js/src/jstypedarray.cpp +++ b/js/src/jstypedarray.cpp @@ -482,6 +482,7 @@ class TypedArrayTemplate : public TypedArray { public: + typedef NativeType ThisType; typedef TypedArrayTemplate ThisTypeArray; static const int ArrayTypeID() { return TypeIDOfType(); } static const bool ArrayTypeIsUnsigned() { return TypeIsUnsigned(); } @@ -1548,6 +1549,19 @@ do { \ NULL, NULL); \ if (!proto) \ return NULL; \ + JSObject *ctor = JS_GetConstructor(cx, proto); \ + if (!ctor || \ + !JS_DefineProperty(cx, ctor, "BYTES_PER_ELEMENT", \ + INT_TO_JSVAL(sizeof(_typedArray::ThisType)), \ + JS_PropertyStub, JS_PropertyStub, \ + JSPROP_PERMANENT | JSPROP_READONLY) || \ + !JS_DefineProperty(cx, proto, "BYTES_PER_ELEMENT", \ + INT_TO_JSVAL(sizeof(_typedArray::ThisType)), \ + JS_PropertyStub, JS_PropertyStub, \ + JSPROP_PERMANENT | JSPROP_READONLY)) \ + { \ + return NULL; \ + } \ proto->setPrivate(0); \ } while (0) diff --git a/js/src/tests/js1_8_5/extensions/typedarray.js b/js/src/tests/js1_8_5/extensions/typedarray.js index d8eb2dfbd3fb..0bcdf943108a 100644 --- a/js/src/tests/js1_8_5/extensions/typedarray.js +++ b/js/src/tests/js1_8_5/extensions/typedarray.js @@ -312,6 +312,12 @@ function test() checkThrows(function() new Int32Array(Int32Array)); checkThrows(function() new Int32Array(Float64Array)); + check(function() Int32Array.BYTES_PER_ELEMENT == 4); + check(function() (new Int32Array(4)).BYTES_PER_ELEMENT == 4); + check(function() (new Int32Array()).BYTES_PER_ELEMENT == 4); + check(function() (new Int32Array(0)).BYTES_PER_ELEMENT == 4); + check(function() Int16Array.BYTES_PER_ELEMENT == Uint16Array.BYTES_PER_ELEMENT); + print ("done"); reportCompare(0, TestFailCount, "typed array tests");