Options.Extensions.cs 9.9 KB

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