|
23 | 23 |
|
24 | 24 | const { |
25 | 25 | Array, |
26 | | - ArrayBufferIsView, |
27 | 26 | ArrayIsArray, |
28 | 27 | ArrayPrototypeForEach, |
29 | 28 | MathFloor, |
@@ -65,6 +64,7 @@ const { |
65 | 64 | indexOfBuffer, |
66 | 65 | indexOfNumber, |
67 | 66 | indexOfString, |
| 67 | + staticCopy, |
68 | 68 | swap16: _swap16, |
69 | 69 | swap32: _swap32, |
70 | 70 | swap64: _swap64, |
@@ -203,42 +203,6 @@ function toInteger(n, defaultVal) { |
203 | 203 | return defaultVal; |
204 | 204 | } |
205 | 205 |
|
206 | | -function copyImpl(source, target, targetStart, sourceStart, sourceEnd) { |
207 | | - if (!ArrayBufferIsView(source)) |
208 | | - throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source); |
209 | | - if (!ArrayBufferIsView(target)) |
210 | | - throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); |
211 | | - |
212 | | - if (targetStart === undefined) { |
213 | | - targetStart = 0; |
214 | | - } else { |
215 | | - targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0); |
216 | | - if (targetStart < 0) |
217 | | - throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart); |
218 | | - } |
219 | | - |
220 | | - if (sourceStart === undefined) { |
221 | | - sourceStart = 0; |
222 | | - } else { |
223 | | - sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0); |
224 | | - if (sourceStart < 0 || sourceStart > source.byteLength) |
225 | | - throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart); |
226 | | - } |
227 | | - |
228 | | - if (sourceEnd === undefined) { |
229 | | - sourceEnd = source.byteLength; |
230 | | - } else { |
231 | | - sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0); |
232 | | - if (sourceEnd < 0) |
233 | | - throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd); |
234 | | - } |
235 | | - |
236 | | - if (targetStart >= target.byteLength || sourceStart >= sourceEnd) |
237 | | - return 0; |
238 | | - |
239 | | - return _copyActual(source, target, targetStart, sourceStart, sourceEnd); |
240 | | -} |
241 | | - |
242 | 206 | function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint8Copy = false) { |
243 | 207 | if (sourceEnd - sourceStart > target.byteLength - targetStart) |
244 | 208 | sourceEnd = sourceStart + target.byteLength - targetStart; |
@@ -618,6 +582,61 @@ Buffer.concat = function concat(list, length) { |
618 | 582 | return buffer; |
619 | 583 | }; |
620 | 584 |
|
| 585 | +Buffer.copy = function copy(source, target, targetStart, sourceStart, sourceEnd) { |
| 586 | + if (!isAnyArrayBuffer(source) && !isArrayBufferView(source)) { |
| 587 | + throw new ERR_INVALID_ARG_TYPE('source', ['ArrayBuffer', 'SharedArrayBuffer', 'TypedArray'], source); |
| 588 | + } |
| 589 | + |
| 590 | + if (!isAnyArrayBuffer(target) && !isArrayBufferView(target)) { |
| 591 | + throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'ArrayBuffer', 'SharedArrayBuffer', 'TypedArray'], target); |
| 592 | + } |
| 593 | + |
| 594 | + if (targetStart === undefined) { |
| 595 | + targetStart = 0; |
| 596 | + } else { |
| 597 | + targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0); |
| 598 | + if (targetStart < 0) { |
| 599 | + throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart); |
| 600 | + } |
| 601 | + } |
| 602 | + |
| 603 | + const sourceByteLengthValue = source.byteLength; |
| 604 | + |
| 605 | + if (sourceStart === undefined) { |
| 606 | + sourceStart = 0; |
| 607 | + } else { |
| 608 | + sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0); |
| 609 | + if (sourceStart < 0 || sourceStart > sourceByteLengthValue) { |
| 610 | + throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${sourceByteLengthValue}`, sourceStart); |
| 611 | + } |
| 612 | + } |
| 613 | + |
| 614 | + if (sourceEnd === undefined) { |
| 615 | + sourceEnd = sourceByteLengthValue; |
| 616 | + } else { |
| 617 | + sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0); |
| 618 | + if (sourceEnd < 0) { |
| 619 | + throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd); |
| 620 | + } |
| 621 | + |
| 622 | + if (sourceEnd > sourceByteLengthValue) { |
| 623 | + sourceEnd = sourceByteLengthValue; |
| 624 | + } |
| 625 | + } |
| 626 | + |
| 627 | + if (sourceStart >= sourceEnd) { |
| 628 | + return 0; |
| 629 | + } |
| 630 | + |
| 631 | + const targetByteLengthValue = target.byteLength; |
| 632 | + |
| 633 | + if (targetStart >= targetByteLengthValue) { |
| 634 | + return 0; |
| 635 | + } |
| 636 | + |
| 637 | + return staticCopy(source, target, targetStart, sourceStart, sourceEnd); |
| 638 | +}; |
| 639 | + |
621 | 640 | function base64ByteLength(str, bytes) { |
622 | 641 | // Handle padding |
623 | 642 | if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D) |
@@ -827,7 +846,7 @@ ObjectDefineProperty(Buffer.prototype, 'offset', { |
827 | 846 |
|
828 | 847 | Buffer.prototype.copy = |
829 | 848 | function copy(target, targetStart, sourceStart, sourceEnd) { |
830 | | - return copyImpl(this, target, targetStart, sourceStart, sourceEnd); |
| 849 | + return Buffer.copy(this, target, targetStart, sourceStart, sourceEnd); |
831 | 850 | }; |
832 | 851 |
|
833 | 852 | // No need to verify that "buf.length <= MAX_UINT32" since it's a read-only |
|
0 commit comments