PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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