toindex-bytelength-sab.js (2737B)
1 // |reftest| skip-if(!this.hasOwnProperty('SharedArrayBuffer')) -- SharedArrayBuffer is not enabled unconditionally 2 // Copyright (C) 2016 the V8 project authors. All rights reserved. 3 // Copyright (C) 2017 Mozilla Corporation. All rights reserved. 4 // This code is governed by the BSD license found in the LICENSE file. 5 6 /*--- 7 esid: sec-dataview-buffer-byteoffset-bytelength 8 description: > 9 ToIndex conversions on byteLength 10 info: | 11 24.2.2.1 DataView ( buffer, byteOffset, byteLength ) 12 13 ... 14 8. If byteLength is either not present or undefined, then 15 a. Let viewByteLength be bufferByteLength - offset. 16 9. Else, 17 a. Let viewByteLength be ? ToIndex(byteLength). 18 b. If offset + viewByteLength > bufferByteLength, throw a RangeError 19 exception. 20 ... 21 22 ToIndex( value ) 23 24 1. If value is undefined, then 25 a. Let index be 0. 26 2. Else, 27 a. Let integerIndex be ? ToInteger(value). 28 b. If integerIndex < 0, throw a RangeError exception. 29 c. Let index be ! ToLength(integerIndex). 30 d. If SameValueZero(integerIndex, index) is false, throw a RangeError exception. 31 3. Return index. 32 features: [SharedArrayBuffer] 33 ---*/ 34 35 var obj1 = { 36 valueOf: function() { 37 return 3; 38 } 39 }; 40 41 var obj2 = { 42 toString: function() { 43 return 4; 44 } 45 }; 46 47 var sample; 48 var ab = new SharedArrayBuffer(42); 49 50 sample = new DataView(ab, 0, -0); 51 assert.sameValue(sample.byteLength, 0, "-0"); 52 53 sample = new DataView(ab, 0, obj1); 54 assert.sameValue(sample.byteLength, 3, "object's valueOf"); 55 56 sample = new DataView(ab, 0, obj2); 57 assert.sameValue(sample.byteLength, 4, "object's toString"); 58 59 sample = new DataView(ab, 0, ""); 60 assert.sameValue(sample.byteLength, 0, "the Empty string"); 61 62 sample = new DataView(ab, 0, "0"); 63 assert.sameValue(sample.byteLength, 0, "string '0'"); 64 65 sample = new DataView(ab, 0, "1"); 66 assert.sameValue(sample.byteLength, 1, "string '1'"); 67 68 sample = new DataView(ab, 0, true); 69 assert.sameValue(sample.byteLength, 1, "true"); 70 71 sample = new DataView(ab, 0, false); 72 assert.sameValue(sample.byteLength, 0, "false"); 73 74 sample = new DataView(ab, 0, NaN); 75 assert.sameValue(sample.byteLength, 0, "NaN"); 76 77 sample = new DataView(ab, 0, null); 78 assert.sameValue(sample.byteLength, 0, "null"); 79 80 sample = new DataView(ab, 0, 0.1); 81 assert.sameValue(sample.byteLength, 0, "0.1"); 82 83 sample = new DataView(ab, 0, 0.9); 84 assert.sameValue(sample.byteLength, 0, "0.9"); 85 86 sample = new DataView(ab, 0, 1.1); 87 assert.sameValue(sample.byteLength, 1, "1.1"); 88 89 sample = new DataView(ab, 0, 1.9); 90 assert.sameValue(sample.byteLength, 1, "1.9"); 91 92 sample = new DataView(ab, 0, -0.1); 93 assert.sameValue(sample.byteLength, 0, "-0.1"); 94 95 sample = new DataView(ab, 0, -0.99999); 96 assert.sameValue(sample.byteLength, 0, "-0.99999"); 97 98 reportCompare(0, 0);