Skip to content

Commit

Permalink
buffer: remove support for Symbol.toPrimitive
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BridgeAR committed Apr 15, 2019
1 parent c87d807 commit 4284339
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 27 deletions.
27 changes: 8 additions & 19 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,18 @@ appropriate for `Buffer.from()` variants.

### Class Method: Buffer.from(object[, offsetOrEncoding[, length]])
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/27051
description: Dropped support for `Symbol.toPrimitive`.
added: v8.2.0
-->

* `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)`.
Expand All @@ -887,20 +890,6 @@ const buf = Buffer.from(new String('this is a test'));
// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

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: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

A `TypeError` will be thrown if `object` has not mentioned methods or is not of
other type appropriate for `Buffer.from()` variants.

Expand Down
6 changes: 0 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'],
Expand Down

0 comments on commit 4284339

Please sign in to comment.