Options.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jint.Runtime.Interop;
  7. namespace Jint
  8. {
  9. public class Options
  10. {
  11. private bool _discardGlobal;
  12. private bool _strict;
  13. private bool _allowDebuggerStatement;
  14. private bool _debugMode;
  15. private bool _allowClr;
  16. private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
  17. private int _maxStatements;
  18. private int _maxRecursionDepth = -1;
  19. private TimeSpan _timeoutInterval;
  20. private CultureInfo _culture = CultureInfo.CurrentCulture;
  21. private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local;
  22. private List<Assembly> _lookupAssemblies = new List<Assembly>();
  23. /// <summary>
  24. /// When called, doesn't initialize the global scope.
  25. /// Can be useful in lightweight scripts for performance reason.
  26. /// </summary>
  27. public Options DiscardGlobal(bool discard = true)
  28. {
  29. _discardGlobal = discard;
  30. return this;
  31. }
  32. /// <summary>
  33. /// Run the script in strict mode.
  34. /// </summary>
  35. public Options Strict(bool strict = true)
  36. {
  37. _strict = strict;
  38. return this;
  39. }
  40. /// <summary>
  41. /// Allow the <code>debugger</code> statement to be called in a script.
  42. /// </summary>
  43. /// <remarks>
  44. /// Because the <code>debugger</code> statement can start the
  45. /// Visual Studio debugger, is it disabled by default
  46. /// </remarks>
  47. public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
  48. {
  49. _allowDebuggerStatement = allowDebuggerStatement;
  50. return this;
  51. }
  52. /// <summary>
  53. /// Allow to run the script in debug mode.
  54. /// </summary>
  55. public Options DebugMode(bool debugMode = true)
  56. {
  57. _debugMode = debugMode;
  58. return this;
  59. }
  60. /// <summary>
  61. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  62. /// </summary>
  63. public Options AddObjectConverter(IObjectConverter objectConverter)
  64. {
  65. _objectConverters.Add(objectConverter);
  66. return this;
  67. }
  68. /// <summary>
  69. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  70. /// </summary>
  71. public Options AllowClr(params Assembly[] assemblies)
  72. {
  73. _allowClr = true;
  74. _lookupAssemblies.AddRange(assemblies);
  75. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  76. return this;
  77. }
  78. public Options MaxStatements(int maxStatements = 0)
  79. {
  80. _maxStatements = maxStatements;
  81. return this;
  82. }
  83. public Options TimeoutInterval(TimeSpan timeoutInterval)
  84. {
  85. _timeoutInterval = timeoutInterval;
  86. return this;
  87. }
  88. /// <summary>
  89. /// Sets maximum allowed depth of recursion.
  90. /// </summary>
  91. /// <param name="maxRecursionDepth">
  92. /// The allowed depth.
  93. /// a) In case max depth is zero no recursion is allowed.
  94. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  95. /// </param>
  96. /// <returns>Options instance for fluent syntax</returns>
  97. public Options LimitRecursion(int maxRecursionDepth = 0)
  98. {
  99. _maxRecursionDepth = maxRecursionDepth;
  100. return this;
  101. }
  102. public Options Culture(CultureInfo cultureInfo)
  103. {
  104. _culture = cultureInfo;
  105. return this;
  106. }
  107. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  108. {
  109. _localTimeZone = timeZoneInfo;
  110. return this;
  111. }
  112. internal bool _IsGlobalDiscarded => _discardGlobal;
  113. internal bool _IsStrict => _strict;
  114. internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement;
  115. internal bool _IsDebugMode => _debugMode;
  116. internal bool _IsClrAllowed => _allowClr;
  117. internal IList<Assembly> _LookupAssemblies => _lookupAssemblies;
  118. internal IEnumerable<IObjectConverter> _ObjectConverters => _objectConverters;
  119. internal int _MaxStatements => _maxStatements;
  120. internal int _MaxRecursionDepth => _maxRecursionDepth;
  121. internal TimeSpan _TimeoutInterval => _timeoutInterval;
  122. internal CultureInfo _Culture => _culture;
  123. internal TimeZoneInfo _LocalTimeZone => _localTimeZone;
  124. }
  125. }