ParsingOptions.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Text.RegularExpressions;
  2. namespace Jint
  3. {
  4. public interface IParsingOptions
  5. {
  6. /// <summary>
  7. /// Gets or sets whether to create compiled <see cref="Regex"/> instances when adapting regular expressions.
  8. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  9. /// regular expressions will be interpreted, otherwise they will be compiled.
  10. /// </summary>
  11. bool? CompileRegex { get; init; }
  12. /// <summary>
  13. /// Gets or sets the default timeout for created <see cref="Regex"/> instances.
  14. /// Defaults to <see langword="null"/>, which means that in the case of non-prepared scripts and modules
  15. /// the <see cref="Options.ConstraintOptions.RegexTimeout"/> setting should apply,
  16. /// otherwise the default of the <see cref="ParserOptions.RegexTimeout"/> setting (10 seconds).
  17. /// </summary>
  18. /// <remarks>
  19. /// Please note that <see cref="Options.ConstraintOptions.RegexTimeout"/> setting will be ignored
  20. /// if this option is set to a value other than <see langword="null"/>.
  21. /// </remarks>
  22. TimeSpan? RegexTimeout { get; init; }
  23. /// <summary>
  24. /// Gets or sets whether to parse the source code in tolerant mode.
  25. /// Defaults to <see langword="true"/>.
  26. /// </summary>
  27. /// <remarks>
  28. /// Please note that there is a plan to change the default to <see langword="false"/> in the next major version.
  29. /// </remarks>
  30. bool Tolerant { get; init; }
  31. }
  32. public sealed record class ScriptParsingOptions : IParsingOptions
  33. {
  34. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  35. {
  36. AllowReturnOutsideFunction = true,
  37. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  38. };
  39. public static readonly ScriptParsingOptions Default = new();
  40. /// <summary>
  41. /// Gets or sets whether to allow return statements at the top level.
  42. /// Defaults to <see langword="true"/>.
  43. /// </summary>
  44. /// <remarks>
  45. /// Please note that there is a plan to change the default to <see langword="false"/> in the next major version.
  46. /// </remarks>
  47. public bool AllowReturnOutsideFunction { get; init; } = _defaultParserOptions.AllowReturnOutsideFunction;
  48. /// <inheritdoc/>
  49. public bool? CompileRegex { get; init; }
  50. /// <inheritdoc/>
  51. public TimeSpan? RegexTimeout { get; init; }
  52. /// <inheritdoc/>
  53. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  54. internal ParserOptions ApplyTo(ParserOptions parserOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => parserOptions with
  55. {
  56. AllowReturnOutsideFunction = AllowReturnOutsideFunction,
  57. RegExpParseMode = CompileRegex is null
  58. ? defaultRegExpParseMode
  59. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  60. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  61. Tolerant = Tolerant,
  62. };
  63. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  64. ? _defaultParserOptions
  65. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  66. internal ParserOptions GetParserOptions(Options engineOptions)
  67. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  68. }
  69. public sealed record class ModuleParsingOptions : IParsingOptions
  70. {
  71. private static readonly ParserOptions _defaultParserOptions = Engine.BaseParserOptions with
  72. {
  73. RegExpParseMode = RegExpParseMode.AdaptToInterpreted,
  74. };
  75. public static readonly ModuleParsingOptions Default = new();
  76. /// <inheritdoc/>
  77. public bool? CompileRegex { get; init; }
  78. /// <inheritdoc/>
  79. public TimeSpan? RegexTimeout { get; init; }
  80. /// <inheritdoc/>
  81. public bool Tolerant { get; init; } = _defaultParserOptions.Tolerant;
  82. internal ParserOptions ApplyTo(ParserOptions baseOptions, RegExpParseMode defaultRegExpParseMode, TimeSpan defaultRegexTimeout) => baseOptions with
  83. {
  84. RegExpParseMode = CompileRegex is null
  85. ? defaultRegExpParseMode
  86. : (CompileRegex.Value ? RegExpParseMode.AdaptToCompiled : RegExpParseMode.AdaptToInterpreted),
  87. RegexTimeout = RegexTimeout ?? defaultRegexTimeout,
  88. Tolerant = Tolerant,
  89. };
  90. internal ParserOptions GetParserOptions() => ReferenceEquals(this, Default)
  91. ? _defaultParserOptions
  92. : ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, _defaultParserOptions.RegexTimeout);
  93. internal ParserOptions GetParserOptions(Options engineOptions)
  94. => ApplyTo(_defaultParserOptions, _defaultParserOptions.RegExpParseMode, engineOptions.Constraints.RegexTimeout);
  95. }
  96. }