2
0

Options.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Runtime.Interop;
  8. namespace Jint
  9. {
  10. public class Options
  11. {
  12. private bool _discardGlobal;
  13. private bool _strict;
  14. private bool _allowDebuggerStatement;
  15. private bool _debugMode;
  16. private bool _allowClr;
  17. private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
  18. private int _maxStatements;
  19. private int _maxRecursionDepth = -1;
  20. private TimeSpan _timeoutInterval;
  21. private CultureInfo _culture = CultureInfo.CurrentCulture;
  22. private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local;
  23. private List<Assembly> _lookupAssemblies = new List<Assembly>();
  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. public Options MaxStatements(int maxStatements = 0)
  80. {
  81. _maxStatements = maxStatements;
  82. return this;
  83. }
  84. public Options TimeoutInterval(TimeSpan timeoutInterval)
  85. {
  86. _timeoutInterval = timeoutInterval;
  87. return this;
  88. }
  89. /// <summary>
  90. /// Sets maximum allowed depth of recursion.
  91. /// </summary>
  92. /// <param name="maxRecursionDepth">
  93. /// The allowed depth.
  94. /// a) In case max depth is zero no recursion is allowed.
  95. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  96. /// </param>
  97. /// <returns>Options instance for fluent syntax</returns>
  98. public Options LimitRecursion(int maxRecursionDepth = 0)
  99. {
  100. _maxRecursionDepth = maxRecursionDepth;
  101. return this;
  102. }
  103. public Options Culture(CultureInfo cultureInfo)
  104. {
  105. _culture = cultureInfo;
  106. return this;
  107. }
  108. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  109. {
  110. _localTimeZone = timeZoneInfo;
  111. return this;
  112. }
  113. internal bool GetDiscardGlobal()
  114. {
  115. return _discardGlobal;
  116. }
  117. internal bool IsStrict()
  118. {
  119. return _strict;
  120. }
  121. internal bool IsDebuggerStatementAllowed()
  122. {
  123. return _allowDebuggerStatement;
  124. }
  125. internal bool IsDebugMode()
  126. {
  127. return _debugMode;
  128. }
  129. internal bool IsClrAllowed()
  130. {
  131. return _allowClr;
  132. }
  133. internal IList<Assembly> GetLookupAssemblies()
  134. {
  135. return _lookupAssemblies;
  136. }
  137. internal IEnumerable<IObjectConverter> GetObjectConverters()
  138. {
  139. return _objectConverters;
  140. }
  141. internal int GetMaxStatements()
  142. {
  143. return _maxStatements;
  144. }
  145. internal int GetMaxRecursionDepth()
  146. {
  147. return _maxRecursionDepth;
  148. }
  149. internal TimeSpan GetTimeoutInterval()
  150. {
  151. return _timeoutInterval;
  152. }
  153. internal CultureInfo GetCulture()
  154. {
  155. return _culture;
  156. }
  157. internal TimeZoneInfo GetLocalTimeZone()
  158. {
  159. return _localTimeZone;
  160. }
  161. }
  162. }