Skip to content

Commit

Permalink
fix(repl): detect cycles when trying to stringify the value
Browse files Browse the repository at this point in the history
Closes #123
  • Loading branch information
michaelfig committed Nov 14, 2019
1 parent 651f9ec commit 0d6fa9e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/ag-solo/vats/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeEvaluators } from '@agoric/evaluate';
import harden from '@agoric/harden';

// A REPL-specific JSON stringify.
export function stringify(value, spaces) {
export function stringify(value, spaces, already = new WeakSet()) {
if (Object(value) !== value) {
return JSON.stringify(value, spaces);
}
Expand All @@ -17,11 +17,17 @@ export function stringify(value, spaces) {
return '[Promise]';
}

// Detect cycles.
if (already.has(value)) {
return '[Circular]';
}
already.add(value);

if (Array.isArray(value)) {
let ret = '[';
let sep = '';
for (let i = 0; i < value.length; i++) {
ret += sep + stringify(value[i]);
ret += sep + stringify(value[i], spaces, already);
sep = ',';
}
ret += ']';
Expand All @@ -37,6 +43,7 @@ export function stringify(value, spaces) {
ret += `${sep}${JSON.stringify(key, undefined, spaces)}:${stringify(
value[key],
spaces,
already,
)}`;
sep = ',';
}
Expand Down

0 comments on commit 0d6fa9e

Please sign in to comment.