0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 03:52:22 +02:00

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.
This commit is contained in:
Avi Halachmi (:avih) 2018-01-27 20:20:00 +02:00 committed by Kevin Mitchell
parent 9a47023c44
commit 84aa9e7146
No known key found for this signature in database
GPG Key ID: 559A34B46A917232

View File

@ -537,7 +537,7 @@ function replacer(k, v) {
function obj2str(v) {
try { // can process objects more than once, but throws on cycles
return JSON.stringify(v, replacer, 2);
return JSON.stringify(v, replacer.bind(null), 2);
} catch (e) { // simple safe: exclude visited objects, even if not cyclic
return JSON.stringify(v, replacer.bind([]), 2);
}