| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --harmony-sharedarraybuffer | 5 // Flags: --harmony-sharedarraybuffer |
| 6 // | 6 // |
| 7 | 7 |
| 8 function toRangeWrapped(value) { | 8 function toRangeWrapped(value) { |
| 9 var range = this.max - this.min + 1; | 9 var range = this.max - this.min + 1; |
| 10 while (value < this.min) { | 10 while (value < this.min) { |
| 11 value += range; | 11 value += range; |
| 12 } | 12 } |
| 13 while (value > this.max) { | 13 while (value > this.max) { |
| 14 value -= range; | 14 value -= range; |
| 15 } | 15 } |
| 16 return value; | 16 return value; |
| 17 } | 17 } |
| 18 | 18 |
| 19 function toRangeClamped(value) { | |
| 20 if (value < this.min) return this.min; | |
| 21 if (value > this.max) return this.max; | |
| 22 return value; | |
| 23 } | |
| 24 | |
| 25 function makeConstructorObject(constr, min, max, toRange) { | 19 function makeConstructorObject(constr, min, max, toRange) { |
| 26 var o = {constr: constr, min: min, max: max}; | 20 var o = {constr: constr, min: min, max: max}; |
| 27 o.toRange = toRange.bind(o); | 21 o.toRange = toRangeWrapped.bind(o); |
| 28 return o; | 22 return o; |
| 29 } | 23 } |
| 30 | 24 |
| 31 var IntegerTypedArrayConstructors = [ | 25 var IntegerTypedArrayConstructors = [ |
| 32 makeConstructorObject(Int8Array, -128, 127, toRangeWrapped), | 26 makeConstructorObject(Int8Array, -128, 127), |
| 33 makeConstructorObject(Int16Array, -32768, 32767, toRangeWrapped), | 27 makeConstructorObject(Int16Array, -32768, 32767), |
| 34 makeConstructorObject(Int32Array, -0x80000000, 0x7fffffff, toRangeWrapped), | 28 makeConstructorObject(Int32Array, -0x80000000, 0x7fffffff), |
| 35 makeConstructorObject(Uint8Array, 0, 255, toRangeWrapped), | 29 makeConstructorObject(Uint8Array, 0, 255), |
| 36 makeConstructorObject(Uint8ClampedArray, 0, 255, toRangeClamped), | 30 makeConstructorObject(Uint16Array, 0, 65535), |
| 37 makeConstructorObject(Uint16Array, 0, 65535, toRangeWrapped), | 31 makeConstructorObject(Uint32Array, 0, 0xffffffff), |
| 38 makeConstructorObject(Uint32Array, 0, 0xffffffff, toRangeWrapped), | |
| 39 ]; | 32 ]; |
| 40 | 33 |
| 41 (function TestBadArray() { | 34 (function TestBadArray() { |
| 42 var ab = new ArrayBuffer(16); | 35 var ab = new ArrayBuffer(16); |
| 43 var u32a = new Uint32Array(16); | 36 var u32a = new Uint32Array(16); |
| 44 var sab = new SharedArrayBuffer(128); | 37 var sab = new SharedArrayBuffer(128); |
| 45 var sf32a = new Float32Array(sab); | 38 var sf32a = new Float32Array(sab); |
| 46 var sf64a = new Float64Array(sab); | 39 var sf64a = new Float64Array(sab); |
| 40 var u8ca = new Uint8ClampedArray(sab); |
| 47 | 41 |
| 48 // Atomic ops required integer shared typed arrays | 42 // Atomic ops required integer shared typed arrays |
| 49 [undefined, 1, 'hi', 3.4, ab, u32a, sab, sf32a, sf64a].forEach(function(o) { | 43 var badArrayTypes = [ |
| 44 undefined, 1, 'hi', 3.4, ab, u32a, sab, sf32a, sf64a, u8ca |
| 45 ]; |
| 46 badArrayTypes.forEach(function(o) { |
| 50 assertThrows(function() { Atomics.compareExchange(o, 0, 0, 0); }, | 47 assertThrows(function() { Atomics.compareExchange(o, 0, 0, 0); }, |
| 51 TypeError); | 48 TypeError); |
| 52 assertThrows(function() { Atomics.load(o, 0); }, TypeError); | 49 assertThrows(function() { Atomics.load(o, 0); }, TypeError); |
| 53 assertThrows(function() { Atomics.store(o, 0, 0); }, TypeError); | 50 assertThrows(function() { Atomics.store(o, 0, 0); }, TypeError); |
| 54 assertThrows(function() { Atomics.add(o, 0, 0); }, TypeError); | 51 assertThrows(function() { Atomics.add(o, 0, 0); }, TypeError); |
| 55 assertThrows(function() { Atomics.sub(o, 0, 0); }, TypeError); | 52 assertThrows(function() { Atomics.sub(o, 0, 0); }, TypeError); |
| 56 assertThrows(function() { Atomics.and(o, 0, 0); }, TypeError); | 53 assertThrows(function() { Atomics.and(o, 0, 0); }, TypeError); |
| 57 assertThrows(function() { Atomics.or(o, 0, 0); }, TypeError); | 54 assertThrows(function() { Atomics.or(o, 0, 0); }, TypeError); |
| 58 assertThrows(function() { Atomics.xor(o, 0, 0); }, TypeError); | 55 assertThrows(function() { Atomics.xor(o, 0, 0); }, TypeError); |
| 59 assertThrows(function() { Atomics.exchange(o, 0, 0); }, TypeError); | 56 assertThrows(function() { Atomics.exchange(o, 0, 0); }, TypeError); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 }); | 119 }); |
| 123 })(); | 120 })(); |
| 124 | 121 |
| 125 (function TestGoodIndex() { | 122 (function TestGoodIndex() { |
| 126 var sab = new SharedArrayBuffer(64); | 123 var sab = new SharedArrayBuffer(64); |
| 127 var si32a = new Int32Array(sab); | 124 var si32a = new Int32Array(sab); |
| 128 var si32a2 = new Int32Array(sab, 32); | 125 var si32a2 = new Int32Array(sab, 32); |
| 129 | 126 |
| 130 var testOp = function(op, ia, index, expectedIndex, name) { | 127 var testOp = function(op, ia, index, expectedIndex, name) { |
| 131 for (var i = 0; i < ia.length; ++i) | 128 for (var i = 0; i < ia.length; ++i) |
| 132 ia[i] = 22; | 129 ia[i] = i * 2; |
| 133 | 130 |
| 134 ia[expectedIndex] = 0; | 131 ia[expectedIndex] = 0; |
| 135 assertEquals(0, op(ia, index, 0, 0), name); | 132 var result = op(ia, index, 0, 0); |
| 133 assertEquals(0, result, name); |
| 136 assertEquals(0, ia[expectedIndex], name); | 134 assertEquals(0, ia[expectedIndex], name); |
| 137 | 135 |
| 138 for (var i = 0; i < ia.length; ++i) { | 136 for (var i = 0; i < ia.length; ++i) { |
| 139 if (i == expectedIndex) continue; | 137 if (i == expectedIndex) continue; |
| 140 assertEquals(22, ia[i], name); | 138 assertEquals(i * 2, ia[i], name); |
| 141 } | 139 } |
| 142 }; | 140 }; |
| 143 | 141 |
| 144 // These values all map to index 0 | 142 // These values all map to index 0 |
| 145 [-0, 0, 0.0, null, false].forEach(function(i) { | 143 [-0, 0, 0.0, null, false].forEach(function(i) { |
| 146 var name = String(i); | 144 var name = String(i); |
| 147 [si32a, si32a2].forEach(function(array) { | 145 [si32a, si32a2].forEach(function(array) { |
| 148 testOp(Atomics.compareExchange, array, i, 0, name); | 146 testOp(Atomics.compareExchange, array, i, 0, name); |
| 149 testOp(Atomics.load, array, i, 0, name); | 147 testOp(Atomics.load, array, i, 0, name); |
| 150 testOp(Atomics.store, array, i, 0, name); | 148 testOp(Atomics.store, array, i, 0, name); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 clearArray(array.buffer); | 213 clearArray(array.buffer); |
| 216 var name = Object.prototype.toString.call(array); | 214 var name = Object.prototype.toString.call(array); |
| 217 for (var i = 0; i < array.length; ++i) { | 215 for (var i = 0; i < array.length; ++i) { |
| 218 array[i] = 0; | 216 array[i] = 0; |
| 219 assertEquals(0, Atomics.load(array, i), name); | 217 assertEquals(0, Atomics.load(array, i), name); |
| 220 array[i] = 50; | 218 array[i] = 50; |
| 221 assertEquals(50, Atomics.load(array, i), name); | 219 assertEquals(50, Atomics.load(array, i), name); |
| 222 } | 220 } |
| 223 }) | 221 }) |
| 224 }); | 222 }); |
| 223 |
| 224 // Test Smi range |
| 225 (function () { |
| 226 var sab = new SharedArrayBuffer(4); |
| 227 var i32 = new Int32Array(sab); |
| 228 var u32 = new Uint32Array(sab); |
| 229 |
| 230 function testLoad(signedValue, unsignedValue) { |
| 231 u32[0] = unsignedValue; |
| 232 assertEquals(unsignedValue, Atomics.load(u32, 0)); |
| 233 assertEquals(signedValue, Atomics.load(i32, 0)); |
| 234 } |
| 235 |
| 236 testLoad(0x3fffffff, 0x3fffffff); // 2**30-1 (always smi) |
| 237 testLoad(0x40000000, 0x40000000); // 2**30 (smi if signed and 32-bits) |
| 238 testLoad(0x80000000, -0x80000000); // 2**31 (smi if signed and 32-bits) |
| 239 testLoad(0xffffffff, -1); // 2**31 (smi if signed) |
| 240 }); |
| 225 })(); | 241 })(); |
| 226 | 242 |
| 227 (function TestStore() { | 243 (function TestStore() { |
| 228 IntegerTypedArrayConstructors.forEach(function(t) { | 244 IntegerTypedArrayConstructors.forEach(function(t) { |
| 229 var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); | 245 var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); |
| 230 var sta = new t.constr(sab); | 246 var sta = new t.constr(sab); |
| 231 var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT); | 247 var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT); |
| 232 | 248 |
| 233 [sta, sta2].forEach(function(array) { | 249 [sta, sta2].forEach(function(array) { |
| 234 clearArray(array.buffer); | 250 clearArray(array.buffer); |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 // Exchange | 548 // Exchange |
| 533 sta[0] = val = 0x12; | 549 sta[0] = val = 0x12; |
| 534 operand = 0x22 + offset; | 550 operand = 0x22 + offset; |
| 535 valWrapped = t.toRange(operand); | 551 valWrapped = t.toRange(operand); |
| 536 assertEquals(val, Atomics.exchange(sta, 0, operand), name); | 552 assertEquals(val, Atomics.exchange(sta, 0, operand), name); |
| 537 assertEquals(valWrapped, sta[0], name); | 553 assertEquals(valWrapped, sta[0], name); |
| 538 } | 554 } |
| 539 | 555 |
| 540 }); | 556 }); |
| 541 })(); | 557 })(); |
| OLD | NEW |