PageRenderTime 46ms CodeModel.GetById 33ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Microsoft.Scripting/Hosting/LanguageSetup.cs

https://bitbucket.org/stefanrusek/xronos
C# | 193 lines | 122 code | 23 blank | 48 comment | 7 complexity | d14a7c0d04b8b455affb9d116fa96832 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Microsoft Public License. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Microsoft Public License, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Microsoft Public License.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16#if CODEPLEX_40
 17using System;
 18#else
 19using System; using Microsoft;
 20#endif
 21using System.Collections.Generic;
 22using System.Collections.ObjectModel;
 23using System.Threading;
 24
 25using Microsoft.Scripting.Utils;
 26
 27namespace Microsoft.Scripting.Hosting {
 28    /// <summary>
 29    /// Stores information needed to setup a language
 30    /// </summary>
 31    [Serializable]
 32    public sealed class LanguageSetup {
 33        private string _typeName;
 34        private string _displayName;
 35        private IList<string> _names;
 36        private IList<string> _fileExtensions;
 37        private IDictionary<string, object> _options;
 38        private bool _frozen;
 39        private bool? _interpretedMode, _exceptionDetail, _perfStats, _adaptiveCompilation;
 40
 41        /// <summary>
 42        /// Creates a new LanguageSetup
 43        /// </summary>
 44        /// <param name="typeName">assembly qualified type name of the language
 45        /// provider</param>
 46        public LanguageSetup(string typeName)
 47            : this(typeName, "", ArrayUtils.EmptyStrings, ArrayUtils.EmptyStrings) {
 48        }
 49
 50        /// <summary>
 51        /// Creates a new LanguageSetup with the provided options
 52        /// TODO: remove this overload?
 53        /// </summary>
 54        public LanguageSetup(string typeName, string displayName)
 55            : this(typeName, displayName, ArrayUtils.EmptyStrings, ArrayUtils.EmptyStrings) {
 56        }
 57
 58        /// <summary>
 59        /// Creates a new LanguageSetup with the provided options
 60        /// </summary>
 61        public LanguageSetup(string typeName, string displayName, IEnumerable<string> names, IEnumerable<string> fileExtensions) {
 62            ContractUtils.RequiresNotEmpty(typeName, "typeName");
 63            ContractUtils.RequiresNotNull(displayName, "displayName");
 64            ContractUtils.RequiresNotNull(names, "names");
 65            ContractUtils.RequiresNotNull(fileExtensions, "fileExtensions");
 66
 67            _typeName = typeName;
 68            _displayName = displayName;
 69            _names = new List<string>(names);
 70            _fileExtensions = new List<string>(fileExtensions);
 71            _options = new Dictionary<string, object>();
 72        }
 73
 74        /// <summary>
 75        /// Gets an option as a strongly typed value.
 76        /// </summary>
 77        public T GetOption<T>(string name, T defaultValue) {
 78            object value;
 79            if (_options != null && _options.TryGetValue(name, out value)) {
 80                if (value is T) {
 81                    return (T)value;
 82                }
 83                return (T)Convert.ChangeType(value, typeof(T), Thread.CurrentThread.CurrentCulture);
 84            }
 85            return defaultValue;
 86        }
 87
 88        /// <summary>
 89        /// The assembly qualified type name of the language provider
 90        /// </summary>
 91        public string TypeName {
 92            get { return _typeName; }
 93            set {
 94                ContractUtils.RequiresNotEmpty(value, "value");
 95                CheckFrozen();
 96                _typeName = value;
 97            }
 98        }
 99
100        /// <summary>
101        /// Display name of the language. If empty, it will be set to the first
102        /// name in the Names list.
103        /// </summary>
104        public string DisplayName {
105            get { return _displayName; }
106            set {
107                ContractUtils.RequiresNotNull(value, "value");
108                CheckFrozen();
109                _displayName = value;
110            }
111        }
112
113        /// <remarks>
114        /// Case-insensitive language names.
115        /// </remarks>
116        public IList<string> Names {
117            get { return _names; }
118        }
119
120        /// <remarks>
121        /// Case-insensitive file extension, optionally starts with a dot.
122        /// </remarks>
123        public IList<string> FileExtensions {
124            get { return _fileExtensions; }
125        }
126
127        /// <remarks>
128        /// Option names are case-sensitive.
129        /// </remarks>
130        public IDictionary<string, object> Options {
131            get { return _options; }
132        }
133
134        public bool InterpretedMode {
135            get { return GetCachedOption("InterpretedMode", ref _interpretedMode); }
136            set { 
137                CheckFrozen();
138                Options["InterpretedMode"] = value; 
139            }
140        }
141
142        public bool AdaptiveCompilation {
143            get { return GetCachedOption("AdaptiveCompilation", ref _adaptiveCompilation); }
144            set {
145                CheckFrozen();
146                Options["AdaptiveCompilation"] = value;
147            }
148        }
149
150        public bool ExceptionDetail {
151            get { return GetCachedOption("ExceptionDetail", ref _exceptionDetail); }
152            set {
153                CheckFrozen();
154                Options["ExceptionDetail"] = value;
155            }
156        }
157
158        public bool PerfStats {
159            get { return GetCachedOption("PerfStats", ref _perfStats); }
160            set {
161                CheckFrozen();
162                Options["PerfStats"] = value;
163            }
164        }
165
166        private bool GetCachedOption(string name, ref bool? storage) {
167            if (storage.HasValue) {
168                return storage.Value;
169            }
170
171            if (_frozen) {
172                storage = GetOption<bool>(name, false);
173                return storage.Value;
174            }
175
176            return GetOption<bool>(name, false);
177        }
178
179        internal void Freeze() {
180            _frozen = true;
181
182            _names = new ReadOnlyCollection<string>(ArrayUtils.MakeArray(_names));
183            _fileExtensions = new ReadOnlyCollection<string>(ArrayUtils.MakeArray(_fileExtensions));
184            _options = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>(_options));
185        }
186
187        private void CheckFrozen() {
188            if (_frozen) {
189                throw new InvalidOperationException("Cannot modify LanguageSetup after it has been used to create a ScriptRuntime");
190            }
191        }        
192    }
193}