bytecodealliance / javy

JS to WebAssembly toolchain
Apache License 2.0
2.16k stars 103 forks source link

Improve cycle checks in `JSON.stringify` #700

Closed saulecabrera closed 1 month ago

saulecabrera commented 2 months ago

Prior to this commit, the following program will erroneously fail, with a circular dependency error:

const template = {
  foo: "foo",
  bar: "bar",
};
const x = [];

x.push(template);
x.push(template);
x.push(template);
x.push(template);
x.push(template);
x.push(template);

console.log(JSON.stringify(x));

The logic for testing for circular dependencies didn't correctly manage the stack state and therefore pushing the same object multiple times to an array resulted in a false-positive cycle.

Cycles can only really happen in Arrays and Objects. This commit solidifies the approach to checking for cycles, by scoping cycle checks and cycle stack management to the MapAccess and SeqAccess structs.

Checklist