Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleStack.clone() doesn't actually clone #228

Open
RedCMD opened this issue Feb 29, 2024 · 1 comment
Open

ruleStack.clone() doesn't actually clone #228

RedCMD opened this issue Feb 29, 2024 · 1 comment

Comments

@RedCMD
Copy link

RedCMD commented Feb 29, 2024

The clone() function on StateStack doesn't actually clone the object, but instead just returns the original object (reference)
meaning, tokenizeLine() will still modify the ruleStack that I was given

clone(): StateStack;

Problem is that the clone implementation doesn't actually do any cloning

public clone(): StateStackImpl {
return this;
}

instead something like structuredClone() should be used or use the clone function provided in the utils.ts file
export function clone<T>(something: T): T {
return doClone(something);
}

Found this out when the _enterPos and _anchorPos kept getting modified externally

@alexdima
Copy link
Member

alexdima commented Jun 11, 2024

This is intentional and is a technique to save memory.

The _enterPos and _anchorPos fields are used for temporary storage and they only make sense during tokenization of a line. No external user of the library should make any assumption about those two fields. Then, clone() returns this because all other fields are immutable. This is to save memory.

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

No branches or pull requests

2 participants