Options.Extensions.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #nullable enable
  2. using System;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jint.Native;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Debugger;
  9. using Jint.Runtime.Interop;
  10. using Jint.Runtime.Modules;
  11. namespace Jint
  12. {
  13. /// <summary>
  14. /// Compatibility layer to allow fluent syntax against options object.
  15. /// </summary>
  16. public static class OptionsExtensions
  17. {
  18. /// <summary>
  19. /// Run the script in strict mode.
  20. /// </summary>
  21. public static Options Strict(this Options options, bool strict = true)
  22. {
  23. options.Strict = strict;
  24. return options;
  25. }
  26. /// <summary>
  27. /// Selects the handling for script <code>debugger</code> statements.
  28. /// </summary>
  29. /// <remarks>
  30. /// The <c>debugger</c> statement can either be ignored (default) trigger debugging at CLR level (e.g. Visual Studio),
  31. /// or trigger a break in Jint's DebugHandler.
  32. /// </remarks>
  33. public static Options DebuggerStatementHandling(this Options options,
  34. DebuggerStatementHandling debuggerStatementHandling)
  35. {
  36. options.Debugger.StatementHandling = debuggerStatementHandling;
  37. return options;
  38. }
  39. /// <summary>
  40. /// Allow to run the script in debug mode.
  41. /// </summary>
  42. public static Options DebugMode(this Options options, bool debugMode = true)
  43. {
  44. options.Debugger.Enabled = debugMode;
  45. return options;
  46. }
  47. /// <summary>
  48. /// Set initial step mode.
  49. /// </summary>
  50. public static Options InitialStepMode(this Options options, StepMode initialStepMode = StepMode.None)
  51. {
  52. options.Debugger.InitialStepMode = initialStepMode;
  53. return options;
  54. }
  55. /// <summary>
  56. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  57. /// </summary>
  58. public static Options AddObjectConverter<T>(this Options options) where T : IObjectConverter, new()
  59. {
  60. return AddObjectConverter(options, new T());
  61. }
  62. /// <summary>
  63. /// Adds a <see cref="IObjectConverter"/> instance to convert CLR types to <see cref="JsValue"/>
  64. /// </summary>
  65. public static Options AddObjectConverter(this Options options, IObjectConverter objectConverter)
  66. {
  67. options.Interop.ObjectConverters.Add(objectConverter);
  68. return options;
  69. }
  70. /// <summary>
  71. /// Sets maximum allowed depth of recursion.
  72. /// </summary>
  73. /// <param name="maxRecursionDepth">
  74. /// The allowed depth.
  75. /// a) In case max depth is zero no recursion is allowed.
  76. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times.
  77. /// </param>
  78. /// <returns>Options instance for fluent syntax</returns>
  79. public static Options LimitRecursion(this Options options, int maxRecursionDepth = 0)
  80. {
  81. options.Constraints.MaxRecursionDepth = maxRecursionDepth;
  82. return options;
  83. }
  84. public static Options Culture(this Options options, CultureInfo cultureInfo)
  85. {
  86. options.Culture = cultureInfo;
  87. return options;
  88. }
  89. public static Options LocalTimeZone(this Options options, TimeZoneInfo timeZoneInfo)
  90. {
  91. options.TimeZone = timeZoneInfo;
  92. return options;
  93. }
  94. public static Options AddExtensionMethods(this Options options, params Type[] types)
  95. {
  96. options.Interop.ExtensionMethodTypes.AddRange(types);
  97. return options;
  98. }
  99. /// <summary>
  100. /// If no known type could be guessed, objects are normally wrapped as an
  101. /// ObjectInstance using class ObjectWrapper. This function can be used to
  102. /// register a handler for a customized handling.
  103. /// </summary>
  104. public static Options SetWrapObjectHandler(this Options options, WrapObjectDelegate wrapObjectHandler)
  105. {
  106. options.Interop.WrapObjectHandler = wrapObjectHandler;
  107. return options;
  108. }
  109. /// <summary>
  110. /// Sets the type converter to use.
  111. /// </summary>
  112. public static Options SetTypeConverter(this Options options, Func<Engine, ITypeConverter> typeConverterFactory)
  113. {
  114. options._configurations.Add(engine => engine.ClrTypeConverter = typeConverterFactory(engine));
  115. return options;
  116. }
  117. /// <summary>
  118. /// Registers a delegate that is called when CLR members are invoked. This allows
  119. /// to change what values are returned for specific CLR objects, or if any value
  120. /// is returned at all.
  121. /// </summary>
  122. /// <param name="accessor">
  123. /// The delegate to invoke for each CLR member. If the delegate
  124. /// returns <c>null</c>, the standard evaluation is performed.
  125. /// </param>
  126. public static Options SetMemberAccessor(this Options options, MemberAccessorDelegate accessor)
  127. {
  128. options.Interop.MemberAccessor = accessor;
  129. return options;
  130. }
  131. /// <summary>
  132. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  133. /// </summary>
  134. public static Options AllowClr(this Options options, params Assembly[] assemblies)
  135. {
  136. options.Interop.Enabled = true;
  137. options.Interop.AllowedAssemblies.AddRange(assemblies);
  138. options.Interop.AllowedAssemblies = options.Interop.AllowedAssemblies.Distinct().ToList();
  139. return options;
  140. }
  141. public static Options AllowClrWrite(this Options options, bool allow = true)
  142. {
  143. options.Interop.AllowWrite = allow;
  144. return options;
  145. }
  146. public static Options AllowOperatorOverloading(this Options options, bool allow = true)
  147. {
  148. options.Interop.AllowOperatorOverloading = allow;
  149. return options;
  150. }
  151. /// <summary>
  152. /// Exceptions thrown from CLR code are converted to JavaScript errors and
  153. /// can be used in at try/catch statement. By default these exceptions are bubbled
  154. /// to the CLR host and interrupt the script execution.
  155. /// </summary>
  156. public static Options CatchClrExceptions(this Options options)
  157. {
  158. CatchClrExceptions(options, _ => true);
  159. return options;
  160. }
  161. /// <summary>
  162. /// Exceptions that thrown from CLR code are converted to JavaScript errors and
  163. /// can be used in at try/catch statement. By default these exceptions are bubbled
  164. /// to the CLR host and interrupt the script execution.
  165. /// </summary>
  166. public static Options CatchClrExceptions(this Options options, ExceptionHandlerDelegate handler)
  167. {
  168. options.Interop.ExceptionHandler = handler;
  169. return options;
  170. }
  171. public static Options Constraint(this Options options, IConstraint constraint)
  172. {
  173. if (constraint != null)
  174. {
  175. options.Constraints.Constraints.Add(constraint);
  176. }
  177. return options;
  178. }
  179. public static Options WithoutConstraint(this Options options, Predicate<IConstraint> predicate)
  180. {
  181. options.Constraints.Constraints.RemoveAll(predicate);
  182. return options;
  183. }
  184. public static Options RegexTimeoutInterval(this Options options, TimeSpan regexTimeoutInterval)
  185. {
  186. options.Constraints.RegexTimeout = regexTimeoutInterval;
  187. return options;
  188. }
  189. public static Options MaxArraySize(this Options options, uint maxSize)
  190. {
  191. options.Constraints.MaxArraySize = maxSize;
  192. return options;
  193. }
  194. public static Options SetReferencesResolver(this Options options, IReferenceResolver resolver)
  195. {
  196. options.ReferenceResolver = resolver;
  197. return options;
  198. }
  199. public static Options SetTypeResolver(this Options options, TypeResolver resolver)
  200. {
  201. options.Interop.TypeResolver = resolver;
  202. return options;
  203. }
  204. /// <summary>
  205. /// Registers some custom logic to apply on an <see cref="Engine"/> instance when the options
  206. /// are loaded.
  207. /// </summary>
  208. /// <param name="configuration">The action to register.</param>
  209. public static Options Configure(this Options options, Action<Engine> configuration)
  210. {
  211. options._configurations.Add(configuration);
  212. return options;
  213. }
  214. /// <summary>
  215. /// Allows to configure how the host is constructed.
  216. /// </summary>
  217. /// <remarks>
  218. /// Passed Engine instance is still in construction and should not be used during call stage.
  219. /// </remarks>
  220. public static Options UseHostFactory<T>(this Options options, Func<Engine, T> factory) where T : Host
  221. {
  222. options.Host.Factory = factory;
  223. return options;
  224. }
  225. /// <summary>
  226. /// Enables module loading in the engine via the 'require' function. By default there's no sand-boxing and
  227. /// you need to trust the script loading the modules not doing bad things.
  228. /// </summary>
  229. public static Options EnableModules(this Options options, string basePath, bool restrictToBasePath = true)
  230. {
  231. return EnableModules(options, new DefaultModuleLoader(basePath, restrictToBasePath));
  232. }
  233. /// <summary>
  234. /// Enables module loading using a custom loader implementation.
  235. /// </summary>
  236. public static Options EnableModules(this Options options, IModuleLoader moduleLoader)
  237. {
  238. options.Modules.ModuleLoader = moduleLoader;
  239. return options;
  240. }
  241. }
  242. }