diff options
author | 2022-11-08 00:07:25 -0800 | |
---|---|---|
committer | 2022-11-08 00:07:25 -0800 | |
commit | 38db5f1c83c40546420ee5379876456a6bd125ac (patch) | |
tree | 8eda6b35076e483cc1edcf9271051ca13927a22c /src/bun.js/builtins/js | |
parent | 9f10724eda493222bb895c8be67a7934f173ad93 (diff) | |
download | bun-38db5f1c83c40546420ee5379876456a6bd125ac.tar.gz bun-38db5f1c83c40546420ee5379876456a6bd125ac.tar.zst bun-38db5f1c83c40546420ee5379876456a6bd125ac.zip |
Fix Buffer.slice and Buffer.subarray
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); } |