Skip to content

Commit

Permalink
use Object.setPrototypeOf instead of __proto__
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Aug 11, 2019
1 parent 2a00ecb commit db5572c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function typedArraySupport () {
// Can typed array instances can be augmented?
try {
var arr = new Uint8Array(1)
arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } }
var proto = { foo: function () { return 42 } }
Object.setPrototypeOf(proto, Uint8Array.prototype)
Object.setPrototypeOf(arr, proto)
return arr.foo() === 42
} catch (e) {
return false
Expand Down Expand Up @@ -75,7 +77,7 @@ function createBuffer (length) {
}
// Return an augmented `Uint8Array` instance
var buf = new Uint8Array(length)
buf.__proto__ = Buffer.prototype
Object.setPrototypeOf(buf, Buffer.prototype)
return buf
}

Expand Down Expand Up @@ -177,8 +179,8 @@ Buffer.from = function (value, encodingOrOffset, length) {

// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
// https://github.com/feross/buffer/pull/148
Buffer.prototype.__proto__ = Uint8Array.prototype
Buffer.__proto__ = Uint8Array
Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype)
Object.setPrototypeOf(Buffer, Uint8Array)

function assertSize (size) {
if (typeof size !== 'number') {
Expand Down Expand Up @@ -282,7 +284,8 @@ function fromArrayBuffer (array, byteOffset, length) {
}

// Return an augmented `Uint8Array` instance
buf.__proto__ = Buffer.prototype
Object.setPrototypeOf(buf, Buffer.prototype)

return buf
}

Expand Down Expand Up @@ -1095,7 +1098,8 @@ Buffer.prototype.slice = function slice (start, end) {

var newBuf = this.subarray(start, end)
// Return an augmented `Uint8Array` instance
newBuf.__proto__ = Buffer.prototype
Object.setPrototypeOf(newBuf, Buffer.prototype)

return newBuf
}

Expand Down

1 comment on commit db5572c

@ljharb
Copy link
Contributor

@ljharb ljharb commented on db5572c Dec 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this is a breaking change in a minor. Can it be reverted?

Please sign in to comment.