/Runtime/Microsoft.Scripting/Hosting/ScriptRuntimeSetup.cs

https://github.com/kumaryu/IronLanguages-main · C# · 208 lines · 120 code · 26 blank · 62 comment · 1 complexity · 51f154231b3b01c8731b23d3d0154c5d MD5 · raw file

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