History

js: dump(..): fix incorrect <VISITED> with array argument

When dump's argument is an array, it was displaying <VISITED> for all the array's object elements (objects, arrays, etc), regardless if they're actually visited or not. The reason is that we try to stringify twice: once normally which may throw (on cycles), and a second time while excluding visited items which is indicated by binding the replacer to an empty array - in which we hold the visited items, where the replacer tests if its 'this' is an array or not and acts accordingly. However, its "this" may also be an array even if we don't bind it to one, because its "normal" this is the main stringified object, so the test of Array.isArray(this) is true when the top object is an array, and the object items are indeed are in it - so the replacer considers them visited. Fix by binding to null on the first attempt such that "this" is an array only when we want it to test for visited items and not when the argument itself is an array.

Avi Halachmi (:avih) committed 8 years ago

Commit 84aa9e71 parent 9a470231 file · +1 −1Browse files

Showing the changes to player/javascript (1 of 1 file). Show all

Unified · Split

player/javascript/defaults.js+1−1
@@ -537,7 +537,7 @@ function replacer(k, v) { · 536 unmodified lines
537537
538538 function obj2str(v) {
539539 try { // can process objects more than once, but throws on cycles
540− return JSON.stringify(v, replacer, 2);
540+ return JSON.stringify(v, replacer.bind(null), 2);
541541 } catch (e) { // simple safe: exclude visited objects, even if not cyclic
542542 return JSON.stringify(v, replacer.bind([]), 2);
543543 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t