esprima-dotnet icon indicating copy to clipboard operation
esprima-dotnet copied to clipboard

Add a note README.md stating that the Ast is immutable

Open ChrML opened this issue 2 years ago • 2 comments

I was checking to see if I can redistribute the AST accross multiple threads, such as for caching the AST for multiple Jint that are running the same script. So it's natural to check if the AST is immutable, and hence safe for this, like e.g. the Roslyn syntax tree is.

According to the source code I have looked at so far, the AST is indeed immutable, but it would be nice if the documentation mentioned it somewhere :). So other people later don't have to spend time investigating the source code to figure that out.

ChrML avatar Sep 28 '22 10:09 ChrML

From Jint's perspective using Engine.PrepareScript should guarantee safe instance for multi-threading. But it's always possible to mutate the custom state field if any code decides to do so afterwards. But the AST structure should be static.

lahma avatar Sep 28 '22 15:09 lahma

I'd say the AST is immutable-ish. The tree structure and node-specific data are immutable indeed but there are a few general or optional properties (like SyntaxElement.Range, SyntaxElement.Location, SyntaxElement.AssociatedData, Program.Tokens, Program.Comments, etc.) which are not for performance and/or backward compatibility reasons.

There are some warnings about this in the XML comments of these properties but the documentation could be improved for sure.

adams85 avatar Sep 28 '22 22:09 adams85