Skip to content

JSON.stringify throws cyclic value! with cycle-avoiding replacer #254

Description

@dckc

I'm trying port the serialize ibid cycle from @agoric/SwingSet. xs throws cyclic value! at https://github.com/Agoric/marshal/blob/v0.0.1/marshal.js#L398

I reduced it to this function; if I run this in node, it works as expected. If I call it from a main.js under xs, it throws:

export function cycleFun() {
  const cycle = ['a', 'x', 'c'];
  cycle[1] = cycle;

  const seen = new Set();

  function skipSeen(k, v) {
    if (seen.has(v)) {
      return undefined;
    }
    seen.add(v);
    return v;
  }

  return JSON.stringify(cycle, skipSeen);
}

console.log(cycleFun());

I think the JSON.stringify spec says this should work.

I looked at xsJSON.c... is it checking for cycles before calling replacer or something? I'm struggling to see if the logic is quite the same as the spec.

cc @warner

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions