Options.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 zero no recursion is allowed.
  86. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  87. /// </param>
  88. /// <returns>Options instance for fluent syntax</returns>
  89. public Options LimitRecursion(int maxRecursionDepth = 0)
  90. {
  91. _maxRecursionDepth = maxRecursionDepth;
  92. return this;
  93. }
  94. public Options Culture(CultureInfo cultureInfo)
  95. {
  96. _culture = cultureInfo;
  97. return this;
  98. }
  99. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  100. {
  101. _localTimeZone = timeZoneInfo;
  102. return this;
  103. }
  104. internal bool GetDiscardGlobal()
  105. {
  106. return _discardGlobal;
  107. }
  108. internal bool IsStrict()
  109. {
  110. return _strict;
  111. }
  112. internal bool IsDebuggerStatementAllowed()
  113. {
  114. return _allowDebuggerStatement;
  115. }
  116. internal bool IsClrAllowed()
  117. {
  118. return _allowClr;
  119. }
  120. internal IList<Assembly> GetLookupAssemblies()
  121. {
  122. return _lookupAssemblies;
  123. }
  124. internal IEnumerable<IObjectConverter> GetObjectConverters()
  125. {
  126. return _objectConverters;
  127. }
  128. internal int GetMaxStatements()
  129. {
  130. return _maxStatements;
  131. }
  132. internal int GetMaxRecursionDepth()
  133. {
  134. return _maxRecursionDepth;
  135. }
  136. internal TimeSpan GetTimeoutInterval()
  137. {
  138. return _timeoutInterval;
  139. }
  140. internal CultureInfo GetCulture()
  141. {
  142. return _culture;
  143. }
  144. internal TimeZoneInfo GetLocalTimeZone()
  145. {
  146. return _localTimeZone;
  147. }
  148. }
  149. }