Options.Extensions.cs 9.9 KB

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