PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/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. #if CODEPLEX_40
  16. using System;
  17. #else
  18. using System; using Microsoft;
  19. #endif
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Threading;
  23. using Microsoft.Scripting.Utils;
  24. namespace Microsoft.Scripting.Hosting {
  25. /// <summary>
  26. /// Stores information needed to setup a language
  27. /// </summary>
  28. [Serializable]
  29. public sealed class LanguageSetup {
  30. private string _typeName;
  31. private string _displayName;
  32. private IList<string> _names;
  33. private IList<string> _fileExtensions;
  34. private IDictionary<string, object> _options;
  35. private bool _frozen;
  36. private bool? _interpretedMode, _exceptionDetail, _perfStats, _adaptiveCompilation;
  37. /// <summary>
  38. /// Creates a new LanguageSetup
  39. /// </summary>
  40. /// <param name="typeName">assembly qualified type name of the language
  41. /// provider</param>
  42. public LanguageSetup(string typeName)
  43. : this(typeName, "", ArrayUtils.EmptyStrings, ArrayUtils.EmptyStrings) {
  44. }
  45. /// <summary>
  46. /// Creates a new LanguageSetup with the provided options
  47. /// TODO: remove this overload?
  48. /// </summary>
  49. public LanguageSetup(string typeName, string displayName)
  50. : this(typeName, displayName, ArrayUtils.EmptyStrings, ArrayUtils.EmptyStrings) {
  51. }
  52. /// <summary>
  53. /// Creates a new LanguageSetup with the provided options
  54. /// </summary>
  55. public LanguageSetup(string typeName, string displayName, IEnumerable<string> names, IEnumerable<string> fileExtensions) {
  56. ContractUtils.RequiresNotEmpty(typeName, "typeName");
  57. ContractUtils.RequiresNotNull(displayName, "displayName");
  58. ContractUtils.RequiresNotNull(names, "names");
  59. ContractUtils.RequiresNotNull(fileExtensions, "fileExtensions");
  60. _typeName = typeName;
  61. _displayName = displayName;
  62. _names = new List<string>(names);
  63. _fileExtensions = new List<string>(fileExtensions);
  64. _options = new Dictionary<string, object>();
  65. }
  66. /// <summary>
  67. /// Gets an option as a strongly typed value.
  68. /// </summary>
  69. public T GetOption<T>(string name, T defaultValue) {
  70. object value;
  71. if (_options != null && _options.TryGetValue(name, out value)) {
  72. if (value is T) {
  73. return (T)value;
  74. }
  75. return (T)Convert.ChangeType(value, typeof(T), Thread.CurrentThread.CurrentCulture);
  76. }
  77. return defaultValue;
  78. }
  79. /// <summary>
  80. /// The assembly qualified type name of the language provider
  81. /// </summary>
  82. public string TypeName {
  83. get { return _typeName; }
  84. set {
  85. ContractUtils.RequiresNotEmpty(value, "value");
  86. CheckFrozen();
  87. _typeName = value;
  88. }
  89. }
  90. /// <summary>
  91. /// Display name of the language. If empty, it will be set to the first
  92. /// name in the Names list.
  93. /// </summary>
  94. public string DisplayName {
  95. get { return _displayName; }
  96. set {
  97. ContractUtils.RequiresNotNull(value, "value");
  98. CheckFrozen();
  99. _displayName = value;
  100. }
  101. }
  102. /// <remarks>
  103. /// Case-insensitive language names.
  104. /// </remarks>
  105. public IList<string> Names {
  106. get { return _names; }
  107. }
  108. /// <remarks>
  109. /// Case-insensitive file extension, optionally starts with a dot.
  110. /// </remarks>
  111. public IList<string> FileExtensions {
  112. get { return _fileExtensions; }
  113. }
  114. /// <remarks>
  115. /// Option names are case-sensitive.
  116. /// </remarks>
  117. public IDictionary<string, object> Options {
  118. get { return _options; }
  119. }
  120. public bool InterpretedMode {
  121. get { return GetCachedOption("InterpretedMode", ref _interpretedMode); }
  122. set {
  123. CheckFrozen();
  124. Options["InterpretedMode"] = value;
  125. }
  126. }
  127. public bool AdaptiveCompilation {
  128. get { return GetCachedOption("AdaptiveCompilation", ref _adaptiveCompilation); }
  129. set {
  130. CheckFrozen();
  131. Options["AdaptiveCompilation"] = value;
  132. }
  133. }
  134. public bool ExceptionDetail {
  135. get { return GetCachedOption("ExceptionDetail", ref _exceptionDetail); }
  136. set {
  137. CheckFrozen();
  138. Options["ExceptionDetail"] = value;
  139. }
  140. }
  141. public bool PerfStats {
  142. get { return GetCachedOption("PerfStats", ref _perfStats); }
  143. set {
  144. CheckFrozen();
  145. Options["PerfStats"] = value;
  146. }
  147. }
  148. private bool GetCachedOption(string name, ref bool? storage) {
  149. if (storage.HasValue) {
  150. return storage.Value;
  151. }
  152. if (_frozen) {
  153. storage = GetOption<bool>(name, false);
  154. return storage.Value;
  155. }
  156. return GetOption<bool>(name, false);
  157. }
  158. internal void Freeze() {
  159. _frozen = true;
  160. _names = new ReadOnlyCollection<string>(ArrayUtils.MakeArray(_names));
  161. _fileExtensions = new ReadOnlyCollection<string>(ArrayUtils.MakeArray(_fileExtensions));
  162. _options = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>(_options));
  163. }
  164. private void CheckFrozen() {
  165. if (_frozen) {
  166. throw new InvalidOperationException("Cannot modify LanguageSetup after it has been used to create a ScriptRuntime");
  167. }
  168. }
  169. }
  170. }