overlookmotel / livepack

Serialize live running code to Javascript
MIT License
45 stars 1 forks source link

`Set`, `Map`, `WeakSet` and `WeakMap` incorrect output where circular entries and prototype or some properties altered #521

Open overlookmotel opened 1 year ago

overlookmotel commented 1 year ago

Example 1:

// Input
const set = new Set();
set.add(set);
Object.setPrototypeOf(set, null);
export default set;
// Output
const set = Object.setPrototypeOf(new Set(), null);
set.add(set);
export default set;

Example 2:

// Input
const set = new Set();
set.add(set);
set.add = 123;
export default set;
// Output
const set = Object.assign(new Set(), {add: 123});
set.add(set);
export default set;

In both cases, set.add() fails because set.add is no longer Set.prototype.add.

Solution would be that if there are circular entries, apply prototype and any .add property after the set.add() calls.

Same problem applies with WeakSet and with the .set() method of Maps and WeakMaps.

overlookmotel commented 12 months ago

Similar to #126.