| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 87760cc commit f83d035
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -216,7 +216,7 @@ function fromArrayLike(obj) { | |||
| 216 | 216 | } | |
| 217 | 217 | ||
| 218 | 218 | function fromArrayBuffer(obj, byteOffset, length) { | |
| 219 | - byteOffset >>>= 0; | ||
| 219 | + byteOffset = internalUtil.toInteger(byteOffset); | ||
| 220 | 220 | ||
| 221 | 221 | const maxLength = obj.byteLength - byteOffset; | |
| 222 | 222 | ||
@@ -226,7 +226,7 @@ function fromArrayBuffer(obj, byteOffset, length) { | |||
| 226 | 226 | if (length === undefined) { | |
| 227 | 227 | length = maxLength; | |
| 228 | 228 | } else { | |
| 229 | - length >>>= 0; | ||
| 229 | + length = internalUtil.toLength(length); | ||
| 230 | 230 | if (length > maxLength) | |
| 231 | 231 | throw new RangeError("'length' is out of bounds"); | |
| 232 | 232 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -165,3 +165,21 @@ exports.normalizeEncoding = function normalizeEncoding(enc) { | |||
| 165 | 165 | } | |
| 166 | 166 | } | |
| 167 | 167 | }; | |
| 168 | + | ||
| 169 | + /* | ||
| 170 | + * Implementation of ToInteger as per ECMAScript Specification | ||
| 171 | + * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger | ||
| 172 | + */ | ||
| 173 | + const toInteger = exports.toInteger = function toInteger(argument) { | ||
| 174 | + const number = +argument; | ||
| 175 | + return Number.isNaN(number) ? 0 : Math.trunc(number); | ||
| 176 | + }; | ||
| 177 | + | ||
| 178 | + /* | ||
| 179 | + * Implementation of ToLength as per ECMAScript Specification | ||
| 180 | + * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tolength | ||
| 181 | + */ | ||
| 182 | + exports.toLength = function toLength(argument) { | ||
| 183 | + const len = toInteger(argument); | ||
| 184 | + return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER); | ||
| 185 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + | ||
| 6 | + function test(arrayBuffer, offset, length) { | ||
| 7 | + const uint8Array = new Uint8Array(arrayBuffer, offset, length); | ||
| 8 | + for (let i = 0; i < length; i += 1) { | ||
| 9 | + uint8Array[i] = 1; | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + const buffer = Buffer.from(arrayBuffer, offset, length); | ||
| 13 | + for (let i = 0; i < length; i += 1) { | ||
| 14 | + assert.strictEqual(buffer[i], 1); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + const acceptableOOMErrors = [ | ||
| 19 | + 'Array buffer allocation failed', | ||
| 20 | + 'Invalid array buffer length' | ||
| 21 | + ]; | ||
| 22 | + | ||
| 23 | + const testCases = [ | ||
| 24 | + [200, 50, 100], | ||
| 25 | + [4294967296 /* 1 << 32 */, 2147483648 /* 1 << 31 */, 1000], | ||
| 26 | + [8589934592 /* 1 << 33 */, 4294967296 /* 1 << 32 */, 1000] | ||
| 27 | + ]; | ||
| 28 | + | ||
| 29 | + for (let index = 0, arrayBuffer; index < testCases.length; index += 1) { | ||
| 30 | + const [size, offset, length] = testCases[index]; | ||
| 31 | + | ||
| 32 | + try { | ||
| 33 | + arrayBuffer = new ArrayBuffer(size); | ||
| 34 | + } catch (e) { | ||
| 35 | + if (e instanceof RangeError && acceptableOOMErrors.includes(e.message)) | ||
| 36 | + return common.skip(`Unable to allocate ${size} bytes for ArrayBuffer`); | ||
| 37 | + throw e; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + test(arrayBuffer, offset, length); | ||
| 41 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + require('../common'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const {toInteger} = require('internal/util'); | ||
| 7 | + | ||
| 8 | + const expectZero = [ | ||
| 9 | + '0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, | ||
| 10 | + '', ' ', undefined, null | ||
| 11 | + ]; | ||
| 12 | + expectZero.forEach(function(value) { | ||
| 13 | + assert.strictEqual(toInteger(value), 0); | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + assert.strictEqual(toInteger(Infinity), Infinity); | ||
| 17 | + assert.strictEqual(toInteger(-Infinity), -Infinity); | ||
| 18 | + | ||
| 19 | + const expectSame = [ | ||
| 20 | + '0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true | ||
| 21 | + ]; | ||
| 22 | + expectSame.forEach(function(value) { | ||
| 23 | + assert.strictEqual(toInteger(value), +value, `${value} is not an Integer`); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + const expectIntegers = new Map([ | ||
| 27 | + [[1], 1], [[-1], -1], [['1'], 1], [['-1'], -1], | ||
| 28 | + [3.14, 3], [-3.14, -3], ['3.14', 3], ['-3.14', -3], | ||
| 29 | + ]); | ||
| 30 | + expectIntegers.forEach(function(expected, value) { | ||
| 31 | + assert.strictEqual(toInteger(value), expected); | ||
| 32 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + require('../common'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const {toLength} = require('internal/util'); | ||
| 7 | + const maxValue = Number.MAX_SAFE_INTEGER; | ||
| 8 | + | ||
| 9 | + const expectZero = [ | ||
| 10 | + '0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, | ||
| 11 | + '', ' ', undefined, null, -1, -1.25, -1.1, -1.9, -Infinity | ||
| 12 | + ]; | ||
| 13 | + expectZero.forEach(function(value) { | ||
| 14 | + assert.strictEqual(toLength(value), 0); | ||
| 15 | + }); | ||
| 16 | + | ||
| 17 | + assert.strictEqual(toLength(maxValue - 1), maxValue - 1); | ||
| 18 | + assert.strictEqual(maxValue, maxValue); | ||
| 19 | + assert.strictEqual(toLength(Infinity), maxValue); | ||
| 20 | + assert.strictEqual(toLength(maxValue + 1), maxValue); | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + [ | ||
| 24 | + '0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true | ||
| 25 | + ].forEach(function(value) { | ||
| 26 | + assert.strictEqual(toLength(value), +value > 0 ? +value : 0); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + const expectIntegers = new Map([ | ||
| 30 | + [[1], 1], [[-1], 0], [['1'], 1], [['-1'], 0], | ||
| 31 | + [3.14, 3], [-3.14, 0], ['3.14', 3], ['-3.14', 0], | ||
| 32 | + ]); | ||
| 33 | + expectIntegers.forEach(function(expected, value) { | ||
| 34 | + assert.strictEqual(toLength(value), expected); | ||
| 35 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments