From 42843395b07e2f0fb82f3f12c7aad3dc95308d50 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 2 Apr 2019 04:43:51 +0200 Subject: [PATCH] buffer: remove support for Symbol.toPrimitive This is a special casing explicitly for `Buffer.from()` and we do not have any other cases where we handle these. To remove ambiguity from the input, remove support for it. --- doc/api/buffer.md | 27 ++++++++------------------- lib/buffer.js | 6 ------ test/parallel/test-buffer-from.js | 4 ++-- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 13b6ce589c60ff..2012742c467ce4 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -869,15 +869,18 @@ appropriate for `Buffer.from()` variants. ### Class Method: Buffer.from(object[, offsetOrEncoding[, length]]) -* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`. +* `object` {Object} An object supporting `valueOf()`. * `offsetOrEncoding` {integer|string} A byte-offset or encoding, depending on - the value returned either by `object.valueOf()` or - `object[Symbol.toPrimitive]()`. -* `length` {integer} A length, depending on the value returned either by - `object.valueOf()` or `object[Symbol.toPrimitive]()`. + the value returned by `object.valueOf()`. +* `length` {integer} A length, depending on the value returned by + `object.valueOf()`. For objects whose `valueOf()` function returns a value not strictly equal to `object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`. @@ -887,20 +890,6 @@ const buf = Buffer.from(new String('this is a test')); // Prints: ``` -For objects that support `Symbol.toPrimitive`, returns -`Buffer.from(object[Symbol.toPrimitive](), offsetOrEncoding, length)`. - -```js -class Foo { - [Symbol.toPrimitive]() { - return 'this is a test'; - } -} - -const buf = Buffer.from(new Foo(), 'utf8'); -// Prints: -``` - A `TypeError` will be thrown if `object` has not mentioned methods or is not of other type appropriate for `Buffer.from()` variants. diff --git a/lib/buffer.js b/lib/buffer.js index 466dc67e22e14b..16fb17545a4795 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -208,12 +208,6 @@ Buffer.from = function from(value, encodingOrOffset, length) { const b = fromObject(value); if (b) return b; - - if (typeof value[Symbol.toPrimitive] === 'function') { - return Buffer.from(value[Symbol.toPrimitive]('string'), - encodingOrOffset, - length); - } } throw new ERR_INVALID_ARG_TYPE( diff --git a/test/parallel/test-buffer-from.js b/test/parallel/test-buffer-from.js index ec798381696e71..6f3ba9683fc4a7 100644 --- a/test/parallel/test-buffer-from.js +++ b/test/parallel/test-buffer-from.js @@ -28,7 +28,6 @@ class MyBadPrimitive { deepStrictEqual(Buffer.from(new String(checkString)), check); deepStrictEqual(Buffer.from(new MyString()), check); -deepStrictEqual(Buffer.from(new MyPrimitive()), check); deepStrictEqual( Buffer.from(runInNewContext('new String(checkString)', { checkString })), check @@ -42,7 +41,8 @@ deepStrictEqual( [{ valueOf: null }, 'object'], [Object.create(null), 'object'], [new Number(true), 'number'], - [new MyBadPrimitive(), 'number'], + [new MyBadPrimitive(), 'object'], + [new MyPrimitive(), 'object'], [Symbol(), 'symbol'], [5n, 'bigint'], [(one, two, three) => {}, 'function'],