Options.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. using Jint.Runtime.References;
  10. namespace Jint
  11. {
  12. public delegate JsValue MemberAccessorDelegate(Engine engine, object target, string member);
  13. public sealed class Options
  14. {
  15. private readonly List<IConstraint> _constraints = new List<IConstraint>();
  16. private bool _strict;
  17. private bool _allowDebuggerStatement;
  18. private bool _allowClr;
  19. private bool _allowClrWrite = true;
  20. private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
  21. private Func<Engine, object, ObjectInstance> _wrapObjectHandler;
  22. private MemberAccessorDelegate _memberAccessor;
  23. private int _maxRecursionDepth = -1;
  24. private TimeSpan _regexTimeoutInterval = TimeSpan.FromSeconds(10);
  25. private CultureInfo _culture = CultureInfo.CurrentCulture;
  26. private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local;
  27. private List<Assembly> _lookupAssemblies = new List<Assembly>();
  28. private Predicate<Exception> _clrExceptionsHandler;
  29. private IReferenceResolver _referenceResolver = DefaultReferenceResolver.Instance;
  30. /// <summary>
  31. /// Run the script in strict mode.
  32. /// </summary>
  33. public Options Strict(bool strict = true)
  34. {
  35. _strict = strict;
  36. return this;
  37. }
  38. /// <summary>
  39. /// Allow the <code>debugger</code> statement to be called in a script.
  40. /// </summary>
  41. /// <remarks>
  42. /// Because the <code>debugger</code> statement can start the
  43. /// Visual Studio debugger, is it disabled by default
  44. /// </remarks>
  45. public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
  46. {
  47. _allowDebuggerStatement = allowDebuggerStatement;
  48. return this;
  49. }
  50. /// <summary>
  51. /// Allow to run the script in debug mode.
  52. /// </summary>
  53. public Options DebugMode(bool debugMode = true)
  54. {
  55. IsDebugMode = debugMode;
  56. return this;
  57. }
  58. /// <summary>
  59. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  60. /// </summary>
  61. public Options AddObjectConverter<T>() where T : IObjectConverter, new()
  62. {
  63. return AddObjectConverter(new T());
  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. /// Registers a delegate that is called when CLR members are invoked. This allows
  85. /// to change what values are returned for specific CLR objects, or if any value
  86. /// is returned at all.
  87. /// </summary>
  88. /// <param name="accessor">
  89. /// The delegate to invoke for each CLR member. If the delegate
  90. /// returns <c>null</c>, the standard evaluation is performed.
  91. /// </param>
  92. public Options SetMemberAccessor(MemberAccessorDelegate accessor)
  93. {
  94. _memberAccessor = accessor;
  95. return this;
  96. }
  97. /// <summary>
  98. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  99. /// </summary>
  100. public Options AllowClr(params Assembly[] assemblies)
  101. {
  102. _allowClr = true;
  103. _lookupAssemblies.AddRange(assemblies);
  104. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  105. return this;
  106. }
  107. public Options AllowClrWrite(bool allow = true)
  108. {
  109. _allowClrWrite = allow;
  110. return this;
  111. }
  112. /// <summary>
  113. /// Exceptions thrown from CLR code are converted to JavaScript errors and
  114. /// can be used in at try/catch statement. By default these exceptions are bubbled
  115. /// to the CLR host and interrupt the script execution.
  116. /// </summary>
  117. public Options CatchClrExceptions()
  118. {
  119. CatchClrExceptions(_ => true);
  120. return this;
  121. }
  122. /// <summary>
  123. /// Exceptions that thrown from CLR code are converted to JavaScript errors and
  124. /// can be used in at try/catch statement. By default these exceptions are bubbled
  125. /// to the CLR host and interrupt the script execution.
  126. /// </summary>
  127. public Options CatchClrExceptions(Predicate<Exception> handler)
  128. {
  129. _clrExceptionsHandler = handler;
  130. return this;
  131. }
  132. public Options Constraint(IConstraint constraint)
  133. {
  134. if (constraint != null)
  135. {
  136. _constraints.Add(constraint);
  137. }
  138. return this;
  139. }
  140. public Options WithoutConstraint(Predicate<IConstraint> predicate)
  141. {
  142. _constraints.RemoveAll(predicate);
  143. return this;
  144. }
  145. public Options RegexTimeoutInterval(TimeSpan regexTimeoutInterval)
  146. {
  147. _regexTimeoutInterval = regexTimeoutInterval;
  148. return this;
  149. }
  150. /// <summary>
  151. /// Sets maximum allowed depth of recursion.
  152. /// </summary>
  153. /// <param name="maxRecursionDepth">
  154. /// The allowed depth.
  155. /// a) In case max depth is zero no recursion is allowed.
  156. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  157. /// </param>
  158. /// <returns>Options instance for fluent syntax</returns>
  159. public Options LimitRecursion(int maxRecursionDepth = 0)
  160. {
  161. _maxRecursionDepth = maxRecursionDepth;
  162. return this;
  163. }
  164. public Options Culture(CultureInfo cultureInfo)
  165. {
  166. _culture = cultureInfo;
  167. return this;
  168. }
  169. public Options LocalTimeZone(TimeZoneInfo timeZoneInfo)
  170. {
  171. _localTimeZone = timeZoneInfo;
  172. return this;
  173. }
  174. public Options SetReferencesResolver(IReferenceResolver resolver)
  175. {
  176. _referenceResolver = resolver;
  177. return this;
  178. }
  179. internal bool IsStrict => _strict;
  180. internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement;
  181. internal bool IsDebugMode { get; private set; }
  182. internal bool _IsClrAllowed => _allowClr;
  183. internal bool _IsClrWriteAllowed => _allowClrWrite;
  184. internal Predicate<Exception> _ClrExceptionsHandler => _clrExceptionsHandler;
  185. internal List<Assembly> _LookupAssemblies => _lookupAssemblies;
  186. internal List<IObjectConverter> _ObjectConverters => _objectConverters;
  187. internal List<IConstraint> _Constraints => _constraints;
  188. internal Func<Engine, object, ObjectInstance> _WrapObjectHandler => _wrapObjectHandler;
  189. internal MemberAccessorDelegate _MemberAccessor => _memberAccessor;
  190. internal int MaxRecursionDepth => _maxRecursionDepth;
  191. internal TimeSpan _RegexTimeoutInterval => _regexTimeoutInterval;
  192. internal CultureInfo _Culture => _culture;
  193. internal TimeZoneInfo _LocalTimeZone => _localTimeZone;
  194. internal IReferenceResolver ReferenceResolver => _referenceResolver;
  195. private sealed class DefaultReferenceResolver : IReferenceResolver
  196. {
  197. public static readonly DefaultReferenceResolver Instance = new DefaultReferenceResolver();
  198. private DefaultReferenceResolver()
  199. {
  200. }
  201. public bool TryUnresolvableReference(Engine engine, Reference reference, out JsValue value)
  202. {
  203. value = JsValue.Undefined;
  204. return false;
  205. }
  206. public bool TryPropertyReference(Engine engine, Reference reference, ref JsValue value)
  207. {
  208. return false;
  209. }
  210. public bool TryGetCallable(Engine engine, object callee, out JsValue value)
  211. {
  212. value = JsValue.Undefined;
  213. return false;
  214. }
  215. public bool CheckCoercible(JsValue value)
  216. {
  217. return false;
  218. }
  219. }
  220. }
  221. }