using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; using Jint.Runtime.Interop; namespace Jint { public class Options { private bool _discardGlobal; private bool _strict; private bool _allowDebuggerStatement; private bool _debugMode; private bool _allowClr; private readonly List _objectConverters = new List(); private int _maxStatements; private int _maxRecursionDepth = -1; private TimeSpan _timeoutInterval; private CultureInfo _culture = CultureInfo.CurrentCulture; private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local; private List _lookupAssemblies = new List(); private Predicate _clrExceptionsHandler; private IReferenceResolver _referenceResolver; /// /// When called, doesn't initialize the global scope. /// Can be useful in lightweight scripts for performance reason. /// public Options DiscardGlobal(bool discard = true) { _discardGlobal = discard; return this; } /// /// Run the script in strict mode. /// public Options Strict(bool strict = true) { _strict = strict; return this; } /// /// Allow the debugger statement to be called in a script. /// /// /// Because the debugger statement can start the /// Visual Studio debugger, is it disabled by default /// public Options AllowDebuggerStatement(bool allowDebuggerStatement = true) { _allowDebuggerStatement = allowDebuggerStatement; return this; } /// /// Allow to run the script in debug mode. /// public Options DebugMode(bool debugMode = true) { _debugMode = debugMode; return this; } /// /// Adds a instance to convert CLR types to /// public Options AddObjectConverter(IObjectConverter objectConverter) { _objectConverters.Add(objectConverter); return this; } /// /// Allows scripts to call CLR types directly like System.IO.File /// public Options AllowClr(params Assembly[] assemblies) { _allowClr = true; _lookupAssemblies.AddRange(assemblies); _lookupAssemblies = _lookupAssemblies.Distinct().ToList(); return this; } /// /// Exceptions thrown from CLR code are converted to JavaScript errors and /// can be used in at try/catch statement. By default these exceptions are bubbled /// to the CLR host and interrupt the script execution. /// public Options CatchClrExceptions() { CatchClrExceptions(_ => true); return this; } /// /// Exceptions that thrown from CLR code are converted to JavaScript errors and /// can be used in at try/catch statement. By default these exceptions are bubbled /// to the CLR host and interrupt the script execution. /// public Options CatchClrExceptions(Predicate handler) { _clrExceptionsHandler = handler; return this; } public Options MaxStatements(int maxStatements = 0) { _maxStatements = maxStatements; return this; } public Options TimeoutInterval(TimeSpan timeoutInterval) { _timeoutInterval = timeoutInterval; return this; } /// /// Sets maximum allowed depth of recursion. /// /// /// The allowed depth. /// a) In case max depth is zero no recursion is allowed. /// b) In case max depth is equal to n it means that in one scope function can be called no more than n times. /// /// Options instance for fluent syntax public Options LimitRecursion(int maxRecursionDepth = 0) { _maxRecursionDepth = maxRecursionDepth; return this; } public Options Culture(CultureInfo cultureInfo) { _culture = cultureInfo; return this; } public Options LocalTimeZone(TimeZoneInfo timeZoneInfo) { _localTimeZone = timeZoneInfo; return this; } public Options SetReferencesResolver(IReferenceResolver resolver) { _referenceResolver = resolver; return this; } internal bool _IsGlobalDiscarded => _discardGlobal; internal bool _IsStrict => _strict; internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement; internal bool _IsDebugMode => _debugMode; internal bool _IsClrAllowed => _allowClr; internal Predicate _ClrExceptionsHandler => _clrExceptionsHandler; internal IList _LookupAssemblies => _lookupAssemblies; internal IEnumerable _ObjectConverters => _objectConverters; internal int _MaxStatements => _maxStatements; internal int _MaxRecursionDepth => _maxRecursionDepth; internal TimeSpan _TimeoutInterval => _timeoutInterval; internal CultureInfo _Culture => _culture; internal TimeZoneInfo _LocalTimeZone => _localTimeZone; internal IReferenceResolver _ReferenceResolver => _referenceResolver; } }