PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs

https://bitbucket.org/stefanrusek/xronos
C# | 212 lines | 124 code | 26 blank | 62 comment | 1 complexity | 219fe5c5030622cc051c253f5b3ffcc0 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 Microsoft.Scripting.Runtime;
  22. using Microsoft.Scripting.Utils;
  23. using System.Reflection;
  24. using System.IO;
  25. using System.Collections.ObjectModel;
  26. namespace Microsoft.Scripting.Hosting {
  27. /// <summary>
  28. /// Stores information needed to setup a ScriptRuntime
  29. /// </summary>
  30. [Serializable]
  31. public sealed class ScriptRuntimeSetup {
  32. // host specification:
  33. private Type _hostType;
  34. private IList<object> _hostArguments;
  35. // languages available in the runtime:
  36. private IList<LanguageSetup> _languageSetups;
  37. // DLR options:
  38. private bool _debugMode;
  39. private bool _privateBinding;
  40. // common language options:
  41. private IDictionary<string, object> _options;
  42. // true if the ScriptRuntimeSetup is no longer mutable because it's been
  43. // used to start a ScriptRuntime
  44. private bool _frozen;
  45. public ScriptRuntimeSetup() {
  46. _languageSetups = new List<LanguageSetup>();
  47. _options = new Dictionary<string, object>();
  48. _hostType = typeof(ScriptHost);
  49. _hostArguments = ArrayUtils.EmptyObjects;
  50. }
  51. /// <summary>
  52. /// The list of language setup information for languages to load into
  53. /// the runtime
  54. /// </summary>
  55. public IList<LanguageSetup> LanguageSetups {
  56. get { return _languageSetups; }
  57. }
  58. /// <summary>
  59. /// Indicates that the script runtime is in debug mode.
  60. /// This means:
  61. ///
  62. /// 1) Symbols are emitted for debuggable methods (methods associated with SourceUnit).
  63. /// 2) Debuggable methods are emitted to non-collectable types (this is due to CLR limitations on dynamic method debugging).
  64. /// 3) JIT optimization is disabled for all methods
  65. /// 4) Languages may disable optimizations based on this value.
  66. /// </summary>
  67. public bool DebugMode {
  68. get { return _debugMode; }
  69. set {
  70. CheckFrozen();
  71. _debugMode = value;
  72. }
  73. }
  74. /// <summary>
  75. /// Ignore CLR visibility checks
  76. /// </summary>
  77. public bool PrivateBinding {
  78. get { return _privateBinding; }
  79. set {
  80. CheckFrozen();
  81. _privateBinding = value;
  82. }
  83. }
  84. /// <summary>
  85. /// Can be any derived class of ScriptHost. When set, it allows the
  86. /// host to override certain methods to control behavior of the runtime
  87. /// </summary>
  88. public Type HostType {
  89. get { return _hostType; }
  90. set {
  91. ContractUtils.RequiresNotNull(value, "value");
  92. ContractUtils.Requires(typeof(ScriptHost).IsAssignableFrom(value), "value", "Must be ScriptHost or a derived type of ScriptHost");
  93. CheckFrozen();
  94. _hostType = value;
  95. }
  96. }
  97. /// <remarks>
  98. /// Option names are case-sensitive.
  99. /// </remarks>
  100. public IDictionary<string, object> Options {
  101. get { return _options; }
  102. }
  103. /// <summary>
  104. /// Arguments passed to the host type when it is constructed
  105. /// </summary>
  106. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  107. public IList<object> HostArguments {
  108. get {
  109. return _hostArguments;
  110. }
  111. set {
  112. ContractUtils.RequiresNotNull(value, "value");
  113. CheckFrozen();
  114. _hostArguments = value;
  115. }
  116. }
  117. internal DlrConfiguration ToConfiguration() {
  118. ContractUtils.Requires(_languageSetups.Count > 0, "ScriptRuntimeSetup must have at least one LanguageSetup");
  119. // prepare
  120. ReadOnlyCollection<LanguageSetup> setups = new ReadOnlyCollection<LanguageSetup>(ArrayUtils.MakeArray(_languageSetups));
  121. var hostArguments = new ReadOnlyCollection<object>(ArrayUtils.MakeArray(_hostArguments));
  122. var options = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>(_options));
  123. var config = new DlrConfiguration(_debugMode, _privateBinding, options);
  124. // validate
  125. foreach (var language in setups) {
  126. config.AddLanguage(
  127. language.TypeName,
  128. language.DisplayName,
  129. language.Names,
  130. language.FileExtensions,
  131. language.Options
  132. );
  133. }
  134. // commit
  135. _languageSetups = setups;
  136. _options = options;
  137. _hostArguments = hostArguments;
  138. Freeze(setups);
  139. return config;
  140. }
  141. private void Freeze(ReadOnlyCollection<LanguageSetup> setups) {
  142. foreach (var language in setups) {
  143. language.Freeze();
  144. }
  145. _frozen = true;
  146. }
  147. private void CheckFrozen() {
  148. if (_frozen) {
  149. throw new InvalidOperationException("Cannot modify ScriptRuntimeSetup after it has been used to create a ScriptRuntime");
  150. }
  151. }
  152. /// <summary>
  153. /// Reads setup from .NET configuration system (.config files).
  154. /// If there is no configuration available returns an empty setup.
  155. /// </summary>
  156. public static ScriptRuntimeSetup ReadConfiguration() {
  157. #if SILVERLIGHT
  158. return new ScriptRuntimeSetup();
  159. #else
  160. var setup = new ScriptRuntimeSetup();
  161. Configuration.Section.LoadRuntimeSetup(setup, null);
  162. return setup;
  163. #endif
  164. }
  165. #if !SILVERLIGHT
  166. /// <summary>
  167. /// Reads setup from a specified XML stream.
  168. /// </summary>
  169. public static ScriptRuntimeSetup ReadConfiguration(Stream configFileStream) {
  170. ContractUtils.RequiresNotNull(configFileStream, "configFileStream");
  171. var setup = new ScriptRuntimeSetup();
  172. Configuration.Section.LoadRuntimeSetup(setup, configFileStream);
  173. return setup;
  174. }
  175. /// <summary>
  176. /// Reads setup from a specified XML file.
  177. /// </summary>
  178. public static ScriptRuntimeSetup ReadConfiguration(string configFilePath) {
  179. ContractUtils.RequiresNotNull(configFilePath, "configFilePath");
  180. using (var stream = File.OpenRead(configFilePath)) {
  181. return ReadConfiguration(stream);
  182. }
  183. }
  184. #endif
  185. }
  186. }