| 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 |
| 19 function makeConstructorObject(constr, min, max, toRange) { | 25 function makeConstructorObject(constr, min, max, toRange) { |
| 20 var o = {constr: constr, min: min, max: max}; | 26 var o = {constr: constr, min: min, max: max}; |
| 21 o.toRange = toRangeWrapped.bind(o); | 27 o.toRange = toRange.bind(o); |
| 22 return o; | 28 return o; |
| 23 } | 29 } |
| 24 | 30 |
| 25 var IntegerTypedArrayConstructors = [ | 31 var IntegerTypedArrayConstructors = [ |
| 26 makeConstructorObject(Int8Array, -128, 127), | 32 makeConstructorObject(Int8Array, -128, 127, toRangeWrapped), |
| 27 makeConstructorObject(Int16Array, -32768, 32767), | 33 makeConstructorObject(Int16Array, -32768, 32767, toRangeWrapped), |
| 28 makeConstructorObject(Int32Array, -0x80000000, 0x7fffffff), | 34 makeConstructorObject(Int32Array, -0x80000000, 0x7fffffff, toRangeWrapped), |
| 29 makeConstructorObject(Uint8Array, 0, 255), | 35 makeConstructorObject(Uint8Array, 0, 255, toRangeWrapped), |
| 30 makeConstructorObject(Uint16Array, 0, 65535), | 36 makeConstructorObject(Uint8ClampedArray, 0, 255, toRangeClamped), |
| 31 makeConstructorObject(Uint32Array, 0, 0xffffffff), | 37 makeConstructorObject(Uint16Array, 0, 65535, toRangeWrapped), |
| 38 makeConstructorObject(Uint32Array, 0, 0xffffffff, toRangeWrapped), |
| 32 ]; | 39 ]; |
| 33 | 40 |
| 34 (function TestBadArray() { | 41 (function TestBadArray() { |
| 35 var ab = new ArrayBuffer(16); | 42 var ab = new ArrayBuffer(16); |
| 36 var u32a = new Uint32Array(16); | 43 var u32a = new Uint32Array(16); |
| 37 var sab = new SharedArrayBuffer(128); | 44 var sab = new SharedArrayBuffer(128); |
| 38 var sf32a = new Float32Array(sab); | 45 var sf32a = new Float32Array(sab); |
| 39 var sf64a = new Float64Array(sab); | 46 var sf64a = new Float64Array(sab); |
| 40 var u8ca = new Uint8ClampedArray(sab); | |
| 41 | 47 |
| 42 // Atomic ops required integer shared typed arrays | 48 // Atomic ops required integer shared typed arrays |
| 43 var badArrayTypes = [ | 49 [undefined, 1, 'hi', 3.4, ab, u32a, sab, sf32a, sf64a].forEach(function(o) { |
| 44 undefined, 1, 'hi', 3.4, ab, u32a, sab, sf32a, sf64a, u8ca | |
| 45 ]; | |
| 46 badArrayTypes.forEach(function(o) { | |
| 47 assertThrows(function() { Atomics.compareExchange(o, 0, 0, 0); }, | 50 assertThrows(function() { Atomics.compareExchange(o, 0, 0, 0); }, |
| 48 TypeError); | 51 TypeError); |
| 49 assertThrows(function() { Atomics.load(o, 0); }, TypeError); | 52 assertThrows(function() { Atomics.load(o, 0); }, TypeError); |
| 50 assertThrows(function() { Atomics.store(o, 0, 0); }, TypeError); | 53 assertThrows(function() { Atomics.store(o, 0, 0); }, TypeError); |
| 51 assertThrows(function() { Atomics.add(o, 0, 0); }, TypeError); | 54 assertThrows(function() { Atomics.add(o, 0, 0); }, TypeError); |
| 52 assertThrows(function() { Atomics.sub(o, 0, 0); }, TypeError); | 55 assertThrows(function() { Atomics.sub(o, 0, 0); }, TypeError); |
| 53 assertThrows(function() { Atomics.and(o, 0, 0); }, TypeError); | 56 assertThrows(function() { Atomics.and(o, 0, 0); }, TypeError); |
| 54 assertThrows(function() { Atomics.or(o, 0, 0); }, TypeError); | 57 assertThrows(function() { Atomics.or(o, 0, 0); }, TypeError); |
| 55 assertThrows(function() { Atomics.xor(o, 0, 0); }, TypeError); | 58 assertThrows(function() { Atomics.xor(o, 0, 0); }, TypeError); |
| 56 assertThrows(function() { Atomics.exchange(o, 0, 0); }, TypeError); | 59 assertThrows(function() { Atomics.exchange(o, 0, 0); }, TypeError); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 }); | 122 }); |
| 120 })(); | 123 })(); |
| 121 | 124 |
| 122 (function TestGoodIndex() { | 125 (function TestGoodIndex() { |
| 123 var sab = new SharedArrayBuffer(64); | 126 var sab = new SharedArrayBuffer(64); |
| 124 var si32a = new Int32Array(sab); | 127 var si32a = new Int32Array(sab); |
| 125 var si32a2 = new Int32Array(sab, 32); | 128 var si32a2 = new Int32Array(sab, 32); |
| 126 | 129 |
| 127 var testOp = function(op, ia, index, expectedIndex, name) { | 130 var testOp = function(op, ia, index, expectedIndex, name) { |
| 128 for (var i = 0; i < ia.length; ++i) | 131 for (var i = 0; i < ia.length; ++i) |
| 129 ia[i] = i * 2; | 132 ia[i] = 22; |
| 130 | 133 |
| 131 ia[expectedIndex] = 0; | 134 ia[expectedIndex] = 0; |
| 132 var result = op(ia, index, 0, 0); | 135 assertEquals(0, op(ia, index, 0, 0), name); |
| 133 assertEquals(0, result, name); | |
| 134 assertEquals(0, ia[expectedIndex], name); | 136 assertEquals(0, ia[expectedIndex], name); |
| 135 | 137 |
| 136 for (var i = 0; i < ia.length; ++i) { | 138 for (var i = 0; i < ia.length; ++i) { |
| 137 if (i == expectedIndex) continue; | 139 if (i == expectedIndex) continue; |
| 138 assertEquals(i * 2, ia[i], name); | 140 assertEquals(22, ia[i], name); |
| 139 } | 141 } |
| 140 }; | 142 }; |
| 141 | 143 |
| 142 // These values all map to index 0 | 144 // These values all map to index 0 |
| 143 [-0, 0, 0.0, null, false].forEach(function(i) { | 145 [-0, 0, 0.0, null, false].forEach(function(i) { |
| 144 var name = String(i); | 146 var name = String(i); |
| 145 [si32a, si32a2].forEach(function(array) { | 147 [si32a, si32a2].forEach(function(array) { |
| 146 testOp(Atomics.compareExchange, array, i, 0, name); | 148 testOp(Atomics.compareExchange, array, i, 0, name); |
| 147 testOp(Atomics.load, array, i, 0, name); | 149 testOp(Atomics.load, array, i, 0, name); |
| 148 testOp(Atomics.store, array, i, 0, name); | 150 testOp(Atomics.store, array, i, 0, name); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 clearArray(array.buffer); | 215 clearArray(array.buffer); |
| 214 var name = Object.prototype.toString.call(array); | 216 var name = Object.prototype.toString.call(array); |
| 215 for (var i = 0; i < array.length; ++i) { | 217 for (var i = 0; i < array.length; ++i) { |
| 216 array[i] = 0; | 218 array[i] = 0; |
| 217 assertEquals(0, Atomics.load(array, i), name); | 219 assertEquals(0, Atomics.load(array, i), name); |
| 218 array[i] = 50; | 220 array[i] = 50; |
| 219 assertEquals(50, Atomics.load(array, i), name); | 221 assertEquals(50, Atomics.load(array, i), name); |
| 220 } | 222 } |
| 221 }) | 223 }) |
| 222 }); | 224 }); |
| 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 }); | |
| 241 })(); | 225 })(); |
| 242 | 226 |
| 243 (function TestStore() { | 227 (function TestStore() { |
| 244 IntegerTypedArrayConstructors.forEach(function(t) { | 228 IntegerTypedArrayConstructors.forEach(function(t) { |
| 245 var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); | 229 var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); |
| 246 var sta = new t.constr(sab); | 230 var sta = new t.constr(sab); |
| 247 var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT); | 231 var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT); |
| 248 | 232 |
| 249 [sta, sta2].forEach(function(array) { | 233 [sta, sta2].forEach(function(array) { |
| 250 clearArray(array.buffer); | 234 clearArray(array.buffer); |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 // Exchange | 532 // Exchange |
| 549 sta[0] = val = 0x12; | 533 sta[0] = val = 0x12; |
| 550 operand = 0x22 + offset; | 534 operand = 0x22 + offset; |
| 551 valWrapped = t.toRange(operand); | 535 valWrapped = t.toRange(operand); |
| 552 assertEquals(val, Atomics.exchange(sta, 0, operand), name); | 536 assertEquals(val, Atomics.exchange(sta, 0, operand), name); |
| 553 assertEquals(valWrapped, sta[0], name); | 537 assertEquals(valWrapped, sta[0], name); |
| 554 } | 538 } |
| 555 | 539 |
| 556 }); | 540 }); |
| 557 })(); | 541 })(); |
| OLD | NEW |