Options.cs 5.9 KB

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