Options.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Jint.Runtime.Interop;
  6. namespace Jint
  7. {
  8. public class Options
  9. {
  10. private bool _discardGlobal;
  11. private bool _strict;
  12. private bool _allowDebuggerStatement;
  13. private bool _allowClr;
  14. private ITypeConverter _typeConverter = new DefaultTypeConverter();
  15. private int _maxStatements;
  16. private CultureInfo _culture = CultureInfo.CurrentCulture;
  17. private List<Assembly> _lookupAssemblies = new List<Assembly>();
  18. /// <summary>
  19. /// When called, doesn't initialize the global scope.
  20. /// Can be useful in lightweight scripts for performance reason.
  21. /// </summary>
  22. public Options DiscardGlobal(bool discard = true)
  23. {
  24. _discardGlobal = discard;
  25. return this;
  26. }
  27. /// <summary>
  28. /// Run the script in strict mode.
  29. /// </summary>
  30. public Options Strict(bool strict = true)
  31. {
  32. _strict = strict;
  33. return this;
  34. }
  35. /// <summary>
  36. /// Allow the <code>debugger</code> statement to be called in a script.
  37. /// </summary>
  38. /// <remarks>
  39. /// Because the <code>debugger</code> statement can start the
  40. /// Visual Studio debugger, is it disabled by default
  41. /// </remarks>
  42. public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
  43. {
  44. _allowDebuggerStatement = allowDebuggerStatement;
  45. return this;
  46. }
  47. /// <summary>
  48. /// Sets a <see cref="ITypeConverter"/> instance to use when converting CLR types
  49. /// </summary>
  50. public Options SetTypeConverter(ITypeConverter typeConverter)
  51. {
  52. _typeConverter = typeConverter;
  53. return this;
  54. }
  55. /// <summary>
  56. /// Allows scripts to call CLR types directly like <example>System.IO.File</example>
  57. /// </summary>
  58. public Options AllowClr(params Assembly[] assemblies)
  59. {
  60. _allowClr = true;
  61. _lookupAssemblies.AddRange(assemblies);
  62. _lookupAssemblies = _lookupAssemblies.Distinct().ToList();
  63. return this;
  64. }
  65. public Options MaxStatements(int maxStatements = 0)
  66. {
  67. _maxStatements = maxStatements;
  68. return this;
  69. }
  70. public Options Culture(CultureInfo cultureInfo)
  71. {
  72. _culture = cultureInfo;
  73. return this;
  74. }
  75. internal bool GetDiscardGlobal()
  76. {
  77. return _discardGlobal;
  78. }
  79. internal bool IsStrict()
  80. {
  81. return _strict;
  82. }
  83. internal bool IsDebuggerStatementAllowed()
  84. {
  85. return _allowDebuggerStatement;
  86. }
  87. internal bool IsClrAllowed()
  88. {
  89. return _allowClr;
  90. }
  91. internal IList<Assembly> GetLookupAssemblies()
  92. {
  93. return _lookupAssemblies;
  94. }
  95. internal ITypeConverter GetTypeConverter()
  96. {
  97. return _typeConverter;
  98. }
  99. internal int GetMaxStatements()
  100. {
  101. return _maxStatements;
  102. }
  103. internal CultureInfo GetCulture()
  104. {
  105. return _culture;
  106. }
  107. }
  108. }