diff options
Diffstat (limited to 'src/bun.js/builtins/js')
| -rw-r--r-- | src/bun.js/builtins/js/JSBufferPrototype.js | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/bun.js/builtins/js/JSBufferPrototype.js b/src/bun.js/builtins/js/JSBufferPrototype.js index a81634f69..689dc2dde 100644 --- a/src/bun.js/builtins/js/JSBufferPrototype.js +++ b/src/bun.js/builtins/js/JSBufferPrototype.js @@ -277,27 +277,27 @@ function toJSON() { return { type, data }; } -function subarray(start, end) { - "use strict"; - - Buffer[Symbol.species] ??= Buffer; - return new Buffer(this.buffer, this.byteOffset + (start || 0), (end || this.byteLength) - (start || 0)); -} - function slice(start, end) { "use strict"; - if (start === undefined && end === undefined) { - return this; - } - - Buffer[Symbol.species] ||= Buffer; + var { buffer, byteOffset, byteLength } = this; - start = start || 0; - if (end !== 0) { - end = end || this.byteLength; + function adjustOffset(offset, length) { + // Use Math.trunc() to convert offset to an integer value that can be larger + // than an Int32. Hence, don't use offset | 0 or similar techniques. + offset = @trunc(offset); + if (offset === 0 || @isNaN(offset)) { + return 0; + } else if (offset < 0) { + offset += length; + return offset > 0 ? offset : 0; + } else { + return offset < length ? offset : length; + } } - return new Buffer(this.buffer, this.byteOffset + start, end - start); + var start_ = adjustOffset(start, byteLength); + var end_ = end !== undefined ? adjustOffset(end, byteLength) : byteLength; + return new Buffer(buffer, byteOffset + start_, end_ > start_ ? (end_ - start_) : 0); } |
