boa icon indicating copy to clipboard operation
boa copied to clipboard

Pretty print `Arguments` objects

Open davimiku opened this issue 4 years ago • 3 comments

Describe the bug Using console.log on the arguments object inside a function produces a type error

Uncaught "TypeError": "cannot convert object to primitive value"

To Reproduce This JavaScript code reproduces the issue:

function test() {
   console.log(arguments)
}
test('first', 'second')

Expected behavior

Running this code should log the arguments object to the console.

In Firefox the output looks like this:

Arguments { 0: "first", 1: "second", … }

In Node the output looks like this:

[Arguments] { '0': 'first', '1': 'second' }

Build environment (please complete the following information):

  • OS: Ubuntu
  • Version: 20.04
  • Target triple: x86_64-unknown-linux-gnu
  • Rustc version: rustc 1.53.0 (53cb7b09b 2021-06-17)

Additional context I believe that the arguments object is missing the Object prototype, so it's missing Object.prototype.valueOf which console.log is trying to invoke.

davimiku avatar Jul 28 '21 01:07 davimiku

After further investigation, the issue may not be specific to arguments, but to a missing implementation of Object.prototype.valueOf (spec). I'm not entirely sure in what scenarios console.log invokes valueOf vs. toString.

davimiku avatar Jul 28 '21 03:07 davimiku

The error comes from possibly here:

https://github.com/boa-dev/boa/blob/91f0fe62bb9b9d5faf0d181871e99c2ea4464e2b/boa/src/object/gcobject.rs#L396-L423

either valueOf is the only function that is able to convert arguments to primitive values or toString is failing to convert arguments to a string of value names. Maybe it's both.

jedel1043 avatar Jul 30 '21 20:07 jedel1043

Fixed the error on df836f1e2a9fdabcae94e38193347c69e7363934, but we'll keep open the issue to improve the display of Arguments objects. It currently prints [object Arguments].

jedel1043 avatar Oct 07 '21 03:10 jedel1043