Options.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jint.Native;
  7. using Jint.Native.Object;
  8. using Jint.Runtime.Interop;
  9. namespace Jint
  10. {
  11. public sealed class Options
  12. {
  13. private readonly List<IConstraint> _constraints = new List<IConstraint>();
  14. private bool _discardGlobal;
  15. private bool _strict;
  16. private bool _allowDebuggerStatement;
  17. private bool _allowClr;
  18. private bool _allowClrWrite = true;
  19. private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
  20. private Func<Engine, object, ObjectInstance> _wrapObjectHandler;
  21. private int _maxRecursionDepth = -1;
  22. private TimeSpan _regexTimeoutInterval = TimeSpan.FromSeconds(10);
  23. private CultureInfo _culture = CultureInfo.CurrentCulture;
  24. private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local;
  25. private List<Assembly> _lookupAssemblies = new List<Assembly>();
  26. private Predicate<Exception> _clrExceptionsHandler;
  27. private IReferenceResolver _referenceResolver;
  28. /// <summary>
  29. /// When called, doesn't initialize the global scope.
  30. /// Can be useful in lightweight scripts for performance reason.
  31. /// </summary>
  32. public Options DiscardGlobal(bool discard = true)
  33. {
  34. _discardGlobal = discard;
  35. return this;
  36. }
  37. /// <summary>
  38. /// Run the script in strict mode.
  39. /// </summary>
  40. public Options Strict(bool strict = true)
  41. {
  42. _strict = strict;
  43. return this;
  44. }
  45. /// <summary>
  46. /// Allow the <code>debugger</code> statement to be called in a script.
  47. /// </summary>
  48. /// <remarks>
  49. /// Because the <code>debugger</code> statement can start the
  50. /// Visual Studio debugger, is it disabled by default
  51. /// </remarks>
  52. public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
  53. {
  54. _allowDebuggerStatement = allowDebuggerStatement;
  55. return this;
  56. }
  57. /// <summary>
  58. /// Allow to run the script in debug mode.
  59. /// </summary>
  60. public Options DebugMode(bool debugMode = true)
  61. {
  62. IsDebugMode = debugMode;
  63. return this;
  64. }
  65. /// <summary>
  66. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  67. /// </summary>
  68. public Options AddObjectConverter(IObjectConverter objectConverter)
  69. {
  70. _objectConverters.Add(objectConverter);
  71. return this;
  72. }
  73. /// <summary>
  74. /// If no known type could be guessed, objects are normally wrapped as an
  75. /// ObjectInstance using class ObjectWrapper. This function can be used to
  76. /// register a handler for a customized handling.
  77. /// </summary>
  78. public Options SetWrapObjectHandler(Func<Engine, object, ObjectInstance> wrapObjectHandler)
  79. {
  80. _wrapObjectHandler = wrapObjectHandler;
  81. return this;
  82. }
  83. /// <summary>
  84. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  85. /// </summary>
  86. public Options AllowClr(params Assembly[] assemblies)
  87. {
  88. _allowClr = true;
  89. _lookupAssemblies.AddRange(assemblies);
  90. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  91. return this;
  92. }
  93. public Options AllowClrWrite(bool allow = true)
  94. {
  95. _allowClrWrite = allow;
  96. return this;
  97. }
  98. /// <summary>
  99. /// Exceptions thrown from CLR code are converted to JavaScript errors and
  100. /// can be used in at try/catch statement. By default these exceptions are bubbled
  101. /// to the CLR host and interrupt the script execution.
  102. /// </summary>
  103. public Options CatchClrExceptions()
  104. {
  105. CatchClrExceptions(_ => true);
  106. return this;
  107. }
  108. /// <summary>
  109. /// Exceptions that thrown from CLR code are converted to JavaScript errors and
  110. /// can be used in at try/catch statement. By default these exceptions are bubbled
  111. /// to the CLR host and interrupt the script execution.
  112. /// </summary>
  113. public Options CatchClrExceptions(Predicate<Exception> handler)
  114. {
  115. _clrExceptionsHandler = handler;
  116. return this;
  117. }
  118. public Options Constraint(IConstraint constraint)
  119. {
  120. if (constraint != null)
  121. {
  122. _constraints.Add(constraint);
  123. }
  124. return this;
  125. }
  126. public Options WithoutConstraint(Predicate<IConstraint> predicate)
  127. {
  128. _constraints.RemoveAll(predicate);
  129. return this;
  130. }
  131. public Options RegexTimeoutInterval(TimeSpan regexTimeoutInterval)
  132. {
  133. _regexTimeoutInterval = regexTimeoutInterval;
  134. return this;
  135. }
  136. /// <summary>
  137. /// Sets maximum allowed depth of recursion.
  138. /// </summary>
  139. /// <param name="maxRecursionDepth">
  140. /// The allowed depth.
  141. /// a) In case max depth is zero no recursion is allowed.
  142. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  143. /// </param>
  144. /// <returns>Options instance for fluent syntax</returns>
  145. public Options LimitRecursion(int maxRecursionDepth = 0)
  146. {
  147. _maxRecursionDepth = maxRecursionDepth;
  148. return this;
  149. }
  150. public Options Culture(CultureInfo cultureInfo)
  151. {
  152. _culture = cultureInfo;
  153. return this;
  154. }
  155. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  156. {
  157. _localTimeZone = timeZoneInfo;
  158. return this;
  159. }
  160. public Options SetReferencesResolver(IReferenceResolver resolver)
  161. {
  162. _referenceResolver = resolver;
  163. return this;
  164. }
  165. internal bool _IsGlobalDiscarded => _discardGlobal;
  166. internal bool IsStrict => _strict;
  167. internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement;
  168. internal bool IsDebugMode { get; private set; }
  169. internal bool _IsClrAllowed => _allowClr;
  170. internal bool _IsClrWriteAllowed => _allowClrWrite;
  171. internal Predicate<Exception> _ClrExceptionsHandler => _clrExceptionsHandler;
  172. internal List<Assembly> _LookupAssemblies => _lookupAssemblies;
  173. internal List<IObjectConverter> _ObjectConverters => _objectConverters;
  174. internal List<IConstraint> _Constraints => _constraints;
  175. internal Func<Engine, object, ObjectInstance> _WrapObjectHandler => _wrapObjectHandler;
  176. internal int MaxRecursionDepth => _maxRecursionDepth;
  177. internal TimeSpan _RegexTimeoutInterval => _regexTimeoutInterval;
  178. internal CultureInfo _Culture => _culture;
  179. internal TimeZoneInfo _LocalTimeZone => _localTimeZone;
  180. internal IReferenceResolver ReferenceResolver => _referenceResolver;
  181. }
  182. }