Skip to content

Commit

Permalink
Printing with [@@toStringTag]
Browse files Browse the repository at this point in the history
Summary:
For any built-in objects whose printing is not specialized, we can
prefix its 19.4.2.15 Symbol.toStringTag value to help with
disambiguating those objects.

This covers cases including Generator, {String, RegExp, Map, Set,
Array}Iterator, Math, JSON, etc.  They were all printed as an
meaningless `{  }` before.

Reviewed By: avp

Differential Revision: D22631553

fbshipit-source-id: b4536965ff97672e855e02c6afbb9e83054d55ed
  • Loading branch information
Huxpro authored and facebook-github-bot committed Jul 28, 2020
1 parent 9706e9a commit a3a5b92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/repl/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,22 @@ a = new Float64Array([]);
// CHECK-NEXT: Float64Array [ ]
a = new ArrayBuffer();
// CHECK-NEXT: ArrayBuffer { }

g = (function*(){})();
// CHECK-NEXT: Generator { }

it = new Map().entries()
// CHECK-NEXT: Map Iterator { }
it = new Set().entries()
// CHECK-NEXT: Set Iterator { }
it = [][Symbol.iterator]();
// CHECK-NEXT: Array Iterator { }
it = 'abc'[Symbol.iterator]();
// CHECK-NEXT: String Iterator { }
it = 'abc'.matchAll(/1/g)
// CHECK-NEXT: RegExp String Iterator { }

Math
// CHECK-NEXT: Math {{{.*}}}
JSON
// CHECK-NEXT: JSON {{{.*}}}
6 changes: 5 additions & 1 deletion tools/hermes/evaluate-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ C_STRING((function() {
}
if (value.constructor && value.constructor.name && value.constructor.name !== "Object") {
return value.constructor.name + ' { ' + elements.join(', ') + ' }';
} else if (value[Symbol.toStringTag]) {
// For any built-in objects whose printing is not specialized, we prefix its
// 19.4.2.15 Symbol.toStringTag value to help with disambiguation. This covers cases
// including Generator, {String, RegExp, Map, Set, Array}Iterator, Math, JSON, etc.
return value[Symbol.toStringTag] + ' { ' + elements.join(', ') + ' }';
} else {
return '{ ' + elements.join(', ') + ' }';
}

}

function prettyPrint(value, isColored) {
Expand Down

0 comments on commit a3a5b92

Please sign in to comment.