X Tutup
Skip to content

fix Set iterator not reflecting mutations after creation#1691

Open
RealColdFry wants to merge 3 commits intoTypeScriptToLua:masterfrom
RealColdFry:fix/set-iterator-mutation
Open

fix Set iterator not reflecting mutations after creation#1691
RealColdFry wants to merge 3 commits intoTypeScriptToLua:masterfrom
RealColdFry:fix/set-iterator-mutation

Conversation

@RealColdFry
Copy link

Fixes #1671

Set.values(), keys(), and entries() eagerly captured firstKey at iterator creation time. If the Set was mutated (e.g. via delete) before the iterator was consumed, the iterator still saw the stale firstKey. Read firstKey lazily on the first next() call instead.

…oLua#1671)

Set.values(), keys(), and entries() eagerly captured firstKey at
iterator creation time. If the Set was mutated (e.g. via delete)
before the iterator was consumed, the iterator still saw the stale
firstKey. Read firstKey lazily on the first next() call instead.
const nextKey = this.nextKey;
let key: T = this.firstKey!;
let key: T | undefined;
let started = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add another bool instead of simply checking if key is undefined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key can also become undefined if the iterator is exhausted. if the iterator is then next'ed again, it will reset and get the first key

@RealColdFry
Copy link
Author

also added tests and code for Map.ts as it had the same pattern

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set.prototype.values() should change if origin Set changes.

2 participants

X Tutup