js/src/jsapi-tests/testTypedArrays.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "jsfriendapi.h"
michael@0 9
michael@0 10 #include "jsapi-tests/tests.h"
michael@0 11
michael@0 12 using namespace js;
michael@0 13
michael@0 14 BEGIN_TEST(testTypedArrays)
michael@0 15 {
michael@0 16 bool ok = true;
michael@0 17
michael@0 18 ok = ok &&
michael@0 19 TestPlainTypedArray<JS_NewInt8Array, int8_t, JS_GetInt8ArrayData>(cx) &&
michael@0 20 TestPlainTypedArray<JS_NewUint8Array, uint8_t, JS_GetUint8ArrayData>(cx) &&
michael@0 21 TestPlainTypedArray<JS_NewUint8ClampedArray, uint8_t, JS_GetUint8ClampedArrayData>(cx) &&
michael@0 22 TestPlainTypedArray<JS_NewInt16Array, int16_t, JS_GetInt16ArrayData>(cx) &&
michael@0 23 TestPlainTypedArray<JS_NewUint16Array, uint16_t, JS_GetUint16ArrayData>(cx) &&
michael@0 24 TestPlainTypedArray<JS_NewInt32Array, int32_t, JS_GetInt32ArrayData>(cx) &&
michael@0 25 TestPlainTypedArray<JS_NewUint32Array, uint32_t, JS_GetUint32ArrayData>(cx) &&
michael@0 26 TestPlainTypedArray<JS_NewFloat32Array, float, JS_GetFloat32ArrayData>(cx) &&
michael@0 27 TestPlainTypedArray<JS_NewFloat64Array, double, JS_GetFloat64ArrayData>(cx);
michael@0 28
michael@0 29 size_t nbytes = sizeof(double) * 8;
michael@0 30 RootedObject buffer(cx, JS_NewArrayBuffer(cx, nbytes));
michael@0 31 CHECK(JS_IsArrayBufferObject(buffer));
michael@0 32
michael@0 33 RootedObject proto(cx);
michael@0 34 JS_GetPrototype(cx, buffer, &proto);
michael@0 35 CHECK(!JS_IsArrayBufferObject(proto));
michael@0 36 RootedObject dummy(cx, JS_GetParent(proto));
michael@0 37 CHECK(!JS_IsArrayBufferObject(dummy));
michael@0 38
michael@0 39 CHECK_EQUAL(JS_GetArrayBufferByteLength(buffer), nbytes);
michael@0 40 memset(JS_GetStableArrayBufferData(cx, buffer), 1, nbytes);
michael@0 41
michael@0 42 ok = ok &&
michael@0 43 TestArrayFromBuffer<JS_NewInt8ArrayWithBuffer, JS_NewInt8ArrayFromArray, int8_t, JS_GetInt8ArrayData>(cx) &&
michael@0 44 TestArrayFromBuffer<JS_NewUint8ArrayWithBuffer, JS_NewUint8ArrayFromArray, uint8_t, JS_GetUint8ArrayData>(cx) &&
michael@0 45 TestArrayFromBuffer<JS_NewUint8ClampedArrayWithBuffer, JS_NewUint8ClampedArrayFromArray, uint8_t, JS_GetUint8ClampedArrayData>(cx) &&
michael@0 46 TestArrayFromBuffer<JS_NewInt16ArrayWithBuffer, JS_NewInt16ArrayFromArray, int16_t, JS_GetInt16ArrayData>(cx) &&
michael@0 47 TestArrayFromBuffer<JS_NewUint16ArrayWithBuffer, JS_NewUint16ArrayFromArray, uint16_t, JS_GetUint16ArrayData>(cx) &&
michael@0 48 TestArrayFromBuffer<JS_NewInt32ArrayWithBuffer, JS_NewInt32ArrayFromArray, int32_t, JS_GetInt32ArrayData>(cx) &&
michael@0 49 TestArrayFromBuffer<JS_NewUint32ArrayWithBuffer, JS_NewUint32ArrayFromArray, uint32_t, JS_GetUint32ArrayData>(cx) &&
michael@0 50 TestArrayFromBuffer<JS_NewFloat32ArrayWithBuffer, JS_NewFloat32ArrayFromArray, float, JS_GetFloat32ArrayData>(cx) &&
michael@0 51 TestArrayFromBuffer<JS_NewFloat64ArrayWithBuffer, JS_NewFloat64ArrayFromArray, double, JS_GetFloat64ArrayData>(cx);
michael@0 52
michael@0 53 return ok;
michael@0 54 }
michael@0 55
michael@0 56 template<JSObject *Create(JSContext *, uint32_t),
michael@0 57 typename Element,
michael@0 58 Element *GetData(JSObject *)>
michael@0 59 bool
michael@0 60 TestPlainTypedArray(JSContext *cx)
michael@0 61 {
michael@0 62 {
michael@0 63 RootedObject notArray(cx, Create(cx, UINT32_MAX));
michael@0 64 CHECK(!notArray);
michael@0 65 }
michael@0 66
michael@0 67 RootedObject array(cx, Create(cx, 7));
michael@0 68 CHECK(JS_IsTypedArrayObject(array));
michael@0 69 RootedObject proto(cx);
michael@0 70 JS_GetPrototype(cx, array, &proto);
michael@0 71 CHECK(!JS_IsTypedArrayObject(proto));
michael@0 72 RootedObject dummy(cx, JS_GetParent(proto));
michael@0 73 CHECK(!JS_IsTypedArrayObject(dummy));
michael@0 74
michael@0 75 CHECK_EQUAL(JS_GetTypedArrayLength(array), 7);
michael@0 76 CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0);
michael@0 77 CHECK_EQUAL(JS_GetTypedArrayByteLength(array), sizeof(Element) * 7);
michael@0 78
michael@0 79 Element *data;
michael@0 80 CHECK(data = GetData(array));
michael@0 81 *data = 13;
michael@0 82 RootedValue v(cx);
michael@0 83 CHECK(JS_GetElement(cx, array, 0, &v));
michael@0 84 CHECK_SAME(v, INT_TO_JSVAL(13));
michael@0 85
michael@0 86 return true;
michael@0 87 }
michael@0 88
michael@0 89 template<JSObject *CreateWithBuffer(JSContext *, JS::HandleObject, uint32_t, int32_t),
michael@0 90 JSObject *CreateFromArray(JSContext *, JS::HandleObject),
michael@0 91 typename Element,
michael@0 92 Element *GetData(JSObject *)>
michael@0 93 bool
michael@0 94 TestArrayFromBuffer(JSContext *cx)
michael@0 95 {
michael@0 96 size_t elts = 8;
michael@0 97 size_t nbytes = elts * sizeof(Element);
michael@0 98 RootedObject buffer(cx, JS_NewArrayBuffer(cx, nbytes));
michael@0 99 uint8_t *bufdata;
michael@0 100 CHECK(bufdata = JS_GetStableArrayBufferData(cx, buffer));
michael@0 101 memset(bufdata, 1, nbytes);
michael@0 102
michael@0 103 {
michael@0 104 RootedObject notArray(cx, CreateWithBuffer(cx, buffer, UINT32_MAX, -1));
michael@0 105 CHECK(!notArray);
michael@0 106 }
michael@0 107
michael@0 108 RootedObject array(cx, CreateWithBuffer(cx, buffer, 0, -1));
michael@0 109 CHECK_EQUAL(JS_GetTypedArrayLength(array), elts);
michael@0 110 CHECK_EQUAL(JS_GetTypedArrayByteOffset(array), 0);
michael@0 111 CHECK_EQUAL(JS_GetTypedArrayByteLength(array), nbytes);
michael@0 112 CHECK_EQUAL(JS_GetArrayBufferViewBuffer(cx, array), (JSObject*) buffer);
michael@0 113
michael@0 114 Element *data;
michael@0 115 CHECK(data = GetData(array));
michael@0 116 CHECK(bufdata = JS_GetStableArrayBufferData(cx, buffer));
michael@0 117 CHECK_EQUAL((void*) data, (void*) bufdata);
michael@0 118
michael@0 119 CHECK_EQUAL(*bufdata, 1);
michael@0 120 CHECK_EQUAL(*reinterpret_cast<uint8_t*>(data), 1);
michael@0 121
michael@0 122 RootedObject shortArray(cx, CreateWithBuffer(cx, buffer, 0, elts / 2));
michael@0 123 CHECK_EQUAL(JS_GetTypedArrayLength(shortArray), elts / 2);
michael@0 124 CHECK_EQUAL(JS_GetTypedArrayByteOffset(shortArray), 0);
michael@0 125 CHECK_EQUAL(JS_GetTypedArrayByteLength(shortArray), nbytes / 2);
michael@0 126
michael@0 127 RootedObject ofsArray(cx, CreateWithBuffer(cx, buffer, nbytes / 2, -1));
michael@0 128 CHECK_EQUAL(JS_GetTypedArrayLength(ofsArray), elts / 2);
michael@0 129 CHECK_EQUAL(JS_GetTypedArrayByteOffset(ofsArray), nbytes / 2);
michael@0 130 CHECK_EQUAL(JS_GetTypedArrayByteLength(ofsArray), nbytes / 2);
michael@0 131
michael@0 132 // Make sure all 3 views reflect the same buffer at the expected locations
michael@0 133 JS::RootedValue v(cx, INT_TO_JSVAL(39));
michael@0 134 JS_SetElement(cx, array, 0, v);
michael@0 135 JS::RootedValue v2(cx);
michael@0 136 CHECK(JS_GetElement(cx, array, 0, &v2));
michael@0 137 CHECK_SAME(v, v2);
michael@0 138 CHECK(JS_GetElement(cx, shortArray, 0, &v2));
michael@0 139 CHECK_SAME(v, v2);
michael@0 140 CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[0]));
michael@0 141
michael@0 142 v = INT_TO_JSVAL(40);
michael@0 143 JS_SetElement(cx, array, elts / 2, v);
michael@0 144 CHECK(JS_GetElement(cx, array, elts / 2, &v2));
michael@0 145 CHECK_SAME(v, v2);
michael@0 146 CHECK(JS_GetElement(cx, ofsArray, 0, &v2));
michael@0 147 CHECK_SAME(v, v2);
michael@0 148 CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[elts / 2]));
michael@0 149
michael@0 150 v = INT_TO_JSVAL(41);
michael@0 151 JS_SetElement(cx, array, elts - 1, v);
michael@0 152 CHECK(JS_GetElement(cx, array, elts - 1, &v2));
michael@0 153 CHECK_SAME(v, v2);
michael@0 154 CHECK(JS_GetElement(cx, ofsArray, elts / 2 - 1, &v2));
michael@0 155 CHECK_SAME(v, v2);
michael@0 156 CHECK_EQUAL(long(JSVAL_TO_INT(v)), long(reinterpret_cast<Element*>(data)[elts - 1]));
michael@0 157
michael@0 158 JS::RootedObject copy(cx, CreateFromArray(cx, array));
michael@0 159 CHECK(JS_GetElement(cx, array, 0, &v));
michael@0 160 CHECK(JS_GetElement(cx, copy, 0, &v2));
michael@0 161 CHECK_SAME(v, v2);
michael@0 162
michael@0 163 /* The copy should not see changes in the original */
michael@0 164 v2 = INT_TO_JSVAL(42);
michael@0 165 JS_SetElement(cx, array, 0, v2);
michael@0 166 CHECK(JS_GetElement(cx, copy, 0, &v2));
michael@0 167 CHECK_SAME(v2, v); /* v is still the original value from 'array' */
michael@0 168
michael@0 169 return true;
michael@0 170 }
michael@0 171
michael@0 172 END_TEST(testTypedArrays)

mercurial