Engine.Ast.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Runtime.Environments;
  4. using Jint.Runtime.Interpreter;
  5. using Jint.Runtime.Interpreter.Expressions;
  6. namespace Jint;
  7. public partial class Engine
  8. {
  9. /// <summary>
  10. /// Prepares a script for the engine that includes static analysis data to speed up execution during run-time.
  11. /// </summary>
  12. /// <remarks>
  13. /// Returned instance is reusable and thread-safe. You should prepare scripts only once and then reuse them.
  14. /// </remarks>
  15. public static Script PrepareScript(string script, string? source = null)
  16. {
  17. var astAnalyzer = new AstAnalyzer();
  18. var options = ParserOptions.Default with { OnNodeCreated = astAnalyzer.NodeVisitor };
  19. return new JavaScriptParser(options).ParseScript(script, source);
  20. }
  21. /// <summary>
  22. /// Prepares a module for the engine that includes static analysis data to speed up execution during run-time.
  23. /// </summary>
  24. /// <remarks>
  25. /// Returned instance is reusable and thread-safe. You should prepare modules only once and then reuse them.
  26. /// </remarks>
  27. public static Module PrepareModule(string script, string? source = null)
  28. {
  29. var astAnalyzer = new AstAnalyzer();
  30. var options = ParserOptions.Default with { OnNodeCreated = astAnalyzer.NodeVisitor };
  31. return new JavaScriptParser(options).ParseModule(script, source);
  32. }
  33. private sealed class AstAnalyzer
  34. {
  35. private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new();
  36. public void NodeVisitor(Node node)
  37. {
  38. switch (node.Type)
  39. {
  40. case Nodes.Identifier:
  41. {
  42. var name = ((Identifier) node).Name;
  43. if (!_bindingNames.TryGetValue(name, out var bindingName))
  44. {
  45. _bindingNames[name] = bindingName = new EnvironmentRecord.BindingName(name);
  46. }
  47. node.AssociatedData = bindingName;
  48. break;
  49. }
  50. case Nodes.Literal:
  51. node.AssociatedData = JintLiteralExpression.ConvertToJsValue((Literal) node);
  52. break;
  53. case Nodes.ArrowFunctionExpression:
  54. case Nodes.FunctionDeclaration:
  55. case Nodes.FunctionExpression:
  56. var function = (IFunction) node;
  57. node.AssociatedData = JintFunctionDefinition.BuildState(function);
  58. break;
  59. }
  60. }
  61. }
  62. }