Options.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 _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. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  54. /// </summary>
  55. public Options AddObjectConverter(IObjectConverter objectConverter)
  56. {
  57. _objectConverters.Add(objectConverter);
  58. return this;
  59. }
  60. /// <summary>
  61. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  62. /// </summary>
  63. public Options AllowClr(params Assembly[] assemblies)
  64. {
  65. _allowClr = true;
  66. _lookupAssemblies.AddRange(assemblies);
  67. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  68. return this;
  69. }
  70. public Options MaxStatements(int maxStatements = 0)
  71. {
  72. _maxStatements = maxStatements;
  73. return this;
  74. }
  75. public Options TimeoutInterval(TimeSpan timeoutInterval)
  76. {
  77. _timeoutInterval = timeoutInterval;
  78. return this;
  79. }
  80. /// <summary>
  81. /// Sets maximum allowed depth of recursion.
  82. /// </summary>
  83. /// <param name="maxRecursionDepth">
  84. /// The allowed depth.
  85. /// a) In case max depth is negative engine ignores stack overflow protection logic;
  86. /// b) In case max depth is zero no recursion is allowed.
  87. /// c) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  88. /// </param>
  89. /// <returns>Options instance for fluent syntax</returns>
  90. public Options MaxRecursionDepth(int maxRecursionDepth = -1)
  91. {
  92. _maxRecursionDepth = maxRecursionDepth;
  93. return this;
  94. }
  95. public Options Culture(CultureInfo cultureInfo)
  96. {
  97. _culture = cultureInfo;
  98. return this;
  99. }
  100. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  101. {
  102. _localTimeZone = timeZoneInfo;
  103. return this;
  104. }
  105. internal bool GetDiscardGlobal()
  106. {
  107. return _discardGlobal;
  108. }
  109. internal bool IsStrict()
  110. {
  111. return _strict;
  112. }
  113. internal bool IsDebuggerStatementAllowed()
  114. {
  115. return _allowDebuggerStatement;
  116. }
  117. internal bool IsClrAllowed()
  118. {
  119. return _allowClr;
  120. }
  121. internal IList<Assembly> GetLookupAssemblies()
  122. {
  123. return _lookupAssemblies;
  124. }
  125. internal IEnumerable<IObjectConverter> GetObjectConverters()
  126. {
  127. return _objectConverters;
  128. }
  129. internal int GetMaxStatements()
  130. {
  131. return _maxStatements;
  132. }
  133. internal int GetMaxRecursionDepth()
  134. {
  135. return _maxRecursionDepth;
  136. }
  137. internal TimeSpan GetTimeoutInterval()
  138. {
  139. return _timeoutInterval;
  140. }
  141. internal CultureInfo GetCulture()
  142. {
  143. return _culture;
  144. }
  145. internal TimeZoneInfo GetLocalTimeZone()
  146. {
  147. return _localTimeZone;
  148. }
  149. }
  150. }