js/src/jsapi-tests/testTypedArrays.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi-tests/testTypedArrays.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "jsfriendapi.h"
    1.12 +
    1.13 +#include "jsapi-tests/tests.h"
    1.14 +
    1.15 +using namespace js;
    1.16 +
    1.17 +BEGIN_TEST(testTypedArrays)
    1.18 +{
    1.19 +    bool ok = true;
    1.20 +
    1.21 +    ok = ok &&
    1.22 +        TestPlainTypedArray<JS_NewInt8Array, int8_t, JS_GetInt8ArrayData>(cx) &&
    1.23 +        TestPlainTypedArray<JS_NewUint8Array, uint8_t, JS_GetUint8ArrayData>(cx) &&
    1.24 +        TestPlainTypedArray<JS_NewUint8ClampedArray, uint8_t, JS_GetUint8ClampedArrayData>(cx) &&
    1.25 +        TestPlainTypedArray<JS_NewInt16Array, int16_t, JS_GetInt16ArrayData>(cx) &&
    1.26 +        TestPlainTypedArray<JS_NewUint16Array, uint16_t, JS_GetUint16ArrayData>(cx) &&
    1.27 +        TestPlainTypedArray<JS_NewInt32Array, int32_t, JS_GetInt32ArrayData>(cx) &&
    1.28 +        TestPlainTypedArray<JS_NewUint32Array, uint32_t, JS_GetUint32ArrayData>(cx) &&
    1.29 +        TestPlainTypedArray<JS_NewFloat32Array, float, JS_GetFloat32ArrayData>(cx) &&
    1.30 +        TestPlainTypedArray<JS_NewFloat64Array, double, JS_GetFloat64ArrayData>(cx);
    1.31 +
    1.32 +    size_t nbytes = sizeof(double) * 8;
    1.33 +    RootedObject buffer(cx, JS_NewArrayBuffer(cx, nbytes));
    1.34 +    CHECK(JS_IsArrayBufferObject(buffer));
    1.35 +
    1.36 +    RootedObject proto(cx);
    1.37 +    JS_GetPrototype(cx, buffer, &proto);
    1.38 +    CHECK(!JS_IsArrayBufferObject(proto));
    1.39 +    RootedObject dummy(cx, JS_GetParent(proto));
    1.40 +    CHECK(!JS_IsArrayBufferObject(dummy));
    1.41 +
    1.42 +    CHECK_EQUAL(JS_GetArrayBufferByteLength(buffer), nbytes);
    1.43 +    memset(JS_GetStableArrayBufferData(cx, buffer), 1, nbytes);
    1.44 +
    1.45 +    ok = ok &&
    1.46 +        TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray, int8_t, JS_GetInt8ArrayData>(cx) &&
    1.47 +        TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray, uint8_t, JS_GetUint8ArrayData>(cx) &&
    1.48 +        TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer, JS_NewUint8ClampedArrayFromArray, uint8_t, JS_GetUint8ClampedArrayData>(cx) &&
    1.49 +        TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray, int16_t, JS_GetInt16ArrayData>(cx) &&
    1.50 +        TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer, JS_NewUint16ArrayFromArray, uint16_t, JS_GetUint16ArrayData>(cx) &&
    1.51 +        TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray, int32_t, JS_GetInt32ArrayData>(cx) &&
    1.52 +        TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer, JS_NewUint32ArrayFromArray, uint32_t, JS_GetUint32ArrayData>(cx) &&
    1.53 +        TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer, JS_NewFloat32ArrayFromArray, float, JS_GetFloat32ArrayData>(cx) &&
    1.54 +        TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer, JS_NewFloat64ArrayFromArray, double, JS_GetFloat64ArrayData>(cx);
    1.55 +
    1.56 +    return ok;
    1.57 +}
    1.58 +
    1.59 +template<JSObject *Create(JSContext *, uint32_t),
    1.60 +         typename Element,
    1.61 +         Element *GetData(JSObject *)>
    1.62 +bool
    1.63 +TestPlainTypedArray(JSContext *cx)
    1.64 +{
    1.65 +    {
    1.66 +        RootedObject notArray(cx, Create(cx, UINT32_MAX));
    1.67 +        CHECK(!notArray);
    1.68 +    }
    1.69 +
    1.70 +    RootedObject array(cx, Create(cx, 7));
    1.71 +    CHECK(JS_IsTypedArrayObject(array));
    1.72 +    RootedObject proto(cx);
    1.73 +    JS_GetPrototype(cx, array, &proto);
    1.74 +    CHECK(!JS_IsTypedArrayObject(proto));
    1.75 +    RootedObject dummy(cx, JS_GetParent(proto));
    1.76 +    CHECK(!JS_IsTypedArrayObject(dummy));
    1.77 +
    1.78 +    CHECK_EQUAL(JS_GetTypedArrayLength(array), 7);
    1.79 +    CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0);
    1.80 +    CHECK_EQUAL(JS_GetTypedArrayByteLength(array), sizeof(Element) * 7);
    1.81 +
    1.82 +    Element *data;
    1.83 +    CHECK(data = GetData(array));
    1.84 +    *data = 13;
    1.85 +    RootedValue v(cx);
    1.86 +    CHECK(JS_GetElement(cx, array, 0, &v));
    1.87 +    CHECK_SAME(v, INT_TO_JSVAL(13));
    1.88 +
    1.89 +    return true;
    1.90 +}
    1.91 +
    1.92 +template<JSObject *CreateWithBuffer(JSContext *, JS::HandleObject, uint32_t, int32_t),
    1.93 +         JSObject *CreateFromArray(JSContext *, JS::HandleObject),
    1.94 +         typename Element,
    1.95 +         Element *GetData(JSObject *)>
    1.96 +bool
    1.97 +TestArrayFromBuffer(JSContext *cx)
    1.98 +{
    1.99 +    size_t elts = 8;
   1.100 +    size_t nbytes = elts * sizeof(Element);
   1.101 +    RootedObject buffer(cx, JS_NewArrayBuffer(cx, nbytes));
   1.102 +    uint8_t *bufdata;
   1.103 +    CHECK(bufdata = JS_GetStableArrayBufferData(cx, buffer));
   1.104 +    memset(bufdata, 1, nbytes);
   1.105 +
   1.106 +    {
   1.107 +        RootedObject notArray(cx, CreateWithBuffer(cx, buffer, UINT32_MAX, -1));
   1.108 +        CHECK(!notArray);
   1.109 +    }
   1.110 +
   1.111 +    RootedObject array(cx, CreateWithBuffer(cx, buffer, 0, -1));
   1.112 +    CHECK_EQUAL(JS_GetTypedArrayLength(array), elts);
   1.113 +    CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0);
   1.114 +    CHECK_EQUAL(JS_GetTypedArrayByteLength(array), nbytes);
   1.115 +    CHECK_EQUAL(JS_GetArrayBufferViewBuffer(cx, array), (JSObject*) buffer);
   1.116 +
   1.117 +    Element *data;
   1.118 +    CHECK(data = GetData(array));
   1.119 +    CHECK(bufdata = JS_GetStableArrayBufferData(cx, buffer));
   1.120 +    CHECK_EQUAL((void*) data, (void*) bufdata);
   1.121 +
   1.122 +    CHECK_EQUAL(*bufdata, 1);
   1.123 +    CHECK_EQUAL(*reinterpret_cast<uint8_t*>(data), 1);
   1.124 +
   1.125 +    RootedObject shortArray(cx, CreateWithBuffer(cx, buffer, 0, elts / 2));
   1.126 +    CHECK_EQUAL(JS_GetTypedArrayLength(shortArray), elts / 2);
   1.127 +    CHECK_EQUAL(JS_GetTypedArrayByteOffset(shortArray), 0);
   1.128 +    CHECK_EQUAL(JS_GetTypedArrayByteLength(shortArray), nbytes / 2);
   1.129 +
   1.130 +    RootedObject ofsArray(cx, CreateWithBuffer(cx, buffer, nbytes / 2, -1));
   1.131 +    CHECK_EQUAL(JS_GetTypedArrayLength(ofsArray), elts / 2);
   1.132 +    CHECK_EQUAL(JS_GetTypedArrayByteOffset(ofsArray), nbytes / 2);
   1.133 +    CHECK_EQUAL(JS_GetTypedArrayByteLength(ofsArray), nbytes / 2);
   1.134 +
   1.135 +    // Make sure all 3 views reflect the same buffer at the expected locations
   1.136 +    JS::RootedValue v(cx, INT_TO_JSVAL(39));
   1.137 +    JS_SetElement(cx, array, 0, v);
   1.138 +    JS::RootedValue v2(cx);
   1.139 +    CHECK(JS_GetElement(cx, array, 0, &v2));
   1.140 +    CHECK_SAME(v, v2);
   1.141 +    CHECK(JS_GetElement(cx, shortArray, 0, &v2));
   1.142 +    CHECK_SAME(v, v2);
   1.143 +    CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[0]));
   1.144 +
   1.145 +    v = INT_TO_JSVAL(40);
   1.146 +    JS_SetElement(cx, array, elts / 2, v);
   1.147 +    CHECK(JS_GetElement(cx, array, elts / 2, &v2));
   1.148 +    CHECK_SAME(v, v2);
   1.149 +    CHECK(JS_GetElement(cx, ofsArray, 0, &v2));
   1.150 +    CHECK_SAME(v, v2);
   1.151 +    CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[elts / 2]));
   1.152 +
   1.153 +    v = INT_TO_JSVAL(41);
   1.154 +    JS_SetElement(cx, array, elts - 1, v);
   1.155 +    CHECK(JS_GetElement(cx, array, elts - 1, &v2));
   1.156 +    CHECK_SAME(v, v2);
   1.157 +    CHECK(JS_GetElement(cx, ofsArray, elts / 2 - 1, &v2));
   1.158 +    CHECK_SAME(v, v2);
   1.159 +    CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[elts - 1]));
   1.160 +
   1.161 +    JS::RootedObject copy(cx, CreateFromArray(cx, array));
   1.162 +    CHECK(JS_GetElement(cx, array, 0, &v));
   1.163 +    CHECK(JS_GetElement(cx, copy, 0, &v2));
   1.164 +    CHECK_SAME(v, v2);
   1.165 +
   1.166 +    /* The copy should not see changes in the original */
   1.167 +    v2 = INT_TO_JSVAL(42);
   1.168 +    JS_SetElement(cx, array, 0, v2);
   1.169 +    CHECK(JS_GetElement(cx, copy, 0, &v2));
   1.170 +    CHECK_SAME(v2, v); /* v is still the original value from 'array' */
   1.171 +
   1.172 +    return true;
   1.173 +}
   1.174 +
   1.175 +END_TEST(testTypedArrays)

mercurial