Options.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. private Predicate<Exception> _clrExceptionsHandler;
  24. /// <summary>
  25. /// When called, doesn't initialize the global scope.
  26. /// Can be useful in lightweight scripts for performance reason.
  27. /// </summary>
  28. public Options DiscardGlobal(bool discard = true)
  29. {
  30. _discardGlobal = discard;
  31. return this;
  32. }
  33. /// <summary>
  34. /// Run the script in strict mode.
  35. /// </summary>
  36. public Options Strict(bool strict = true)
  37. {
  38. _strict = strict;
  39. return this;
  40. }
  41. /// <summary>
  42. /// Allow the <code>debugger</code> statement to be called in a script.
  43. /// </summary>
  44. /// <remarks>
  45. /// Because the <code>debugger</code> statement can start the
  46. /// Visual Studio debugger, is it disabled by default
  47. /// </remarks>
  48. public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
  49. {
  50. _allowDebuggerStatement = allowDebuggerStatement;
  51. return this;
  52. }
  53. /// <summary>
  54. /// Allow to run the script in debug mode.
  55. /// </summary>
  56. public Options DebugMode(bool debugMode = true)
  57. {
  58. _debugMode = debugMode;
  59. return this;
  60. }
  61. /// <summary>
  62. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  63. /// </summary>
  64. public Options AddObjectConverter(IObjectConverter objectConverter)
  65. {
  66. _objectConverters.Add(objectConverter);
  67. return this;
  68. }
  69. /// <summary>
  70. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  71. /// </summary>
  72. public Options AllowClr(params Assembly[] assemblies)
  73. {
  74. _allowClr = true;
  75. _lookupAssemblies.AddRange(assemblies);
  76. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  77. return this;
  78. }
  79. /// <summary>
  80. /// Exceptions thrown from CLR code are converted to JavaScript errors and
  81. /// can be used in at try/catch statement. By default these exceptions are bubbled
  82. /// to the CLR host and interrupt the script execution.
  83. /// </summary>
  84. public Options CatchClrExceptions()
  85. {
  86. CatchClrExceptions(_ => true);
  87. return this;
  88. }
  89. /// <summary>
  90. /// Exceptions that thrown from CLR code are converted to JavaScript errors and
  91. /// can be used in at try/catch statement. By default these exceptions are bubbled
  92. /// to the CLR host and interrupt the script execution.
  93. /// </summary>
  94. public Options CatchClrExceptions(Predicate<Exception> handler)
  95. {
  96. _clrExceptionsHandler = handler;
  97. return this;
  98. }
  99. public Options MaxStatements(int maxStatements = 0)
  100. {
  101. _maxStatements = maxStatements;
  102. return this;
  103. }
  104. public Options TimeoutInterval(TimeSpan timeoutInterval)
  105. {
  106. _timeoutInterval = timeoutInterval;
  107. return this;
  108. }
  109. /// <summary>
  110. /// Sets maximum allowed depth of recursion.
  111. /// </summary>
  112. /// <param name="maxRecursionDepth">
  113. /// The allowed depth.
  114. /// a) In case max depth is zero no recursion is allowed.
  115. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  116. /// </param>
  117. /// <returns>Options instance for fluent syntax</returns>
  118. public Options LimitRecursion(int maxRecursionDepth = 0)
  119. {
  120. _maxRecursionDepth = maxRecursionDepth;
  121. return this;
  122. }
  123. public Options Culture(CultureInfo cultureInfo)
  124. {
  125. _culture = cultureInfo;
  126. return this;
  127. }
  128. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  129. {
  130. _localTimeZone = timeZoneInfo;
  131. return this;
  132. }
  133. internal bool _IsGlobalDiscarded => _discardGlobal;
  134. internal bool _IsStrict => _strict;
  135. internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement;
  136. internal bool _IsDebugMode => _debugMode;
  137. internal bool _IsClrAllowed => _allowClr;
  138. internal Predicate<Exception> _ClrExceptionsHandler => _clrExceptionsHandler;
  139. internal IList<Assembly> _LookupAssemblies => _lookupAssemblies;
  140. internal IEnumerable<IObjectConverter> _ObjectConverters => _objectConverters;
  141. internal int _MaxStatements => _maxStatements;
  142. internal int _MaxRecursionDepth => _maxRecursionDepth;
  143. internal TimeSpan _TimeoutInterval => _timeoutInterval;
  144. internal CultureInfo _Culture => _culture;
  145. internal TimeZoneInfo _LocalTimeZone => _localTimeZone;
  146. }
  147. }