Engine.Ast.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, bool strict = false)
  16. {
  17. var astAnalyzer = new AstAnalyzer();
  18. var options = ParserOptions.Default with
  19. {
  20. AllowReturnOutsideFunction = true,
  21. OnNodeCreated = astAnalyzer.NodeVisitor
  22. };
  23. return new JavaScriptParser(options).ParseScript(script, source, strict);
  24. }
  25. /// <summary>
  26. /// Prepares a module for the engine that includes static analysis data to speed up execution during run-time.
  27. /// </summary>
  28. /// <remarks>
  29. /// Returned instance is reusable and thread-safe. You should prepare modules only once and then reuse them.
  30. /// </remarks>
  31. public static Module PrepareModule(string script, string? source = null)
  32. {
  33. var astAnalyzer = new AstAnalyzer();
  34. var options = ParserOptions.Default with { OnNodeCreated = astAnalyzer.NodeVisitor };
  35. return new JavaScriptParser(options).ParseModule(script, source);
  36. }
  37. private sealed class AstAnalyzer
  38. {
  39. private readonly Dictionary<string, EnvironmentRecord.BindingName> _bindingNames = new();
  40. public void NodeVisitor(Node node)
  41. {
  42. switch (node.Type)
  43. {
  44. case Nodes.Identifier:
  45. {
  46. var name = ((Identifier) node).Name;
  47. if (!_bindingNames.TryGetValue(name, out var bindingName))
  48. {
  49. _bindingNames[name] = bindingName = new EnvironmentRecord.BindingName(name);
  50. }
  51. node.AssociatedData = bindingName;
  52. break;
  53. }
  54. case Nodes.Literal:
  55. node.AssociatedData = JintLiteralExpression.ConvertToJsValue((Literal) node);
  56. break;
  57. case Nodes.ArrowFunctionExpression:
  58. case Nodes.FunctionDeclaration:
  59. case Nodes.FunctionExpression:
  60. var function = (IFunction) node;
  61. node.AssociatedData = JintFunctionDefinition.BuildState(function);
  62. break;
  63. }
  64. }
  65. }
  66. }