diff --git a/lib/buffer.js b/lib/buffer.js index ccd899da5f32ae..07e533b1ce0f6d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -335,6 +335,14 @@ Buffer.byteLength = byteLength; // toString(encoding, start=0, end=buffer.length) Buffer.prototype.toString = function(encoding, start, end) { + // .toString() can be called by JS on prototype (Buffer.prototype + ''). + if (!(this instanceof Buffer)) { + if (arguments.length === 0) + return Object.prototype.toString.call(this); + else + throw new TypeError('this must be instance of Buffer'); + } + var loweredCase = false; start = start >>> 0; diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 1d02148734e38a..b770c0a3da7df5 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1172,3 +1172,7 @@ Buffer.poolSize = ps; assert.throws(function() { Buffer(10).copy(); }); + +// https://github.com/nodejs/io.js/issues/1485 +// C++ bindings fail assertions (die) when called on non-buffers. +assert.equal(Buffer.prototype.toString(), '[object Object]');