xronos /Microsoft.Scripting/Runtime/DlrConfiguration.cs

Language C# Lines 329
MD5 Hash b43131bc005610a38554788b41e9ce07 Estimated Cost $5,747 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Runtime {
    /// <summary>
    /// Singleton for each language.
    /// </summary>
    internal sealed class LanguageConfiguration {
        private readonly AssemblyQualifiedTypeName _providerName;
        private readonly string _displayName;
        private readonly IDictionary<string, object> _options;
        private LanguageContext _context;
        
        public LanguageContext LanguageContext {
            get { return _context; }
        }

        public AssemblyQualifiedTypeName ProviderName {
            get { return _providerName; }
        }

        public string DisplayName {
            get { return _displayName; }
        }
        
        public LanguageConfiguration(AssemblyQualifiedTypeName providerName, string displayName, IDictionary<string, object> options) {
            _providerName = providerName;
            _displayName = displayName;
            _options = options;
        }

        /// <summary>
        /// Must not be called under a lock as it can potentially call a user code.
        /// </summary>
        /// <exception cref="MissingTypeException"></exception>
        /// <exception cref="Microsoft.Scripting.InvalidImplementationException">The language context's implementation failed to instantiate.</exception>
        internal LanguageContext LoadLanguageContext(ScriptDomainManager domainManager, out bool alreadyLoaded) {
            if (_context == null) {

                // Let assembly load errors bubble out
                var assembly = domainManager.Platform.LoadAssembly(_providerName.AssemblyName.FullName);

                Type type = assembly.GetType(_providerName.TypeName);
                if (type == null) {
                    throw new InvalidOperationException(
                        string.Format(
                            "Failed to load language '{0}': assembly '{1}' does not contain type '{2}'",
                            _displayName, assembly.Location, _providerName.TypeName
                    ));
                }

                if (!type.IsSubclassOf(typeof(LanguageContext))) {
                    throw new InvalidOperationException(
                        string.Format(
                            "Failed to load language '{0}': type '{1}' is not a valid language provider because it does not inherit from LanguageContext",
                            _displayName, type
                    )); 
                }

                var context = ReflectionUtils.CreateInstance<LanguageContext>(type, domainManager, _options);
                alreadyLoaded = Interlocked.CompareExchange(ref _context, context, null) != null;
            } else {
                alreadyLoaded = true;
            }

            return _context;
        }
    }

    public sealed class DlrConfiguration {
        private bool _frozen;

        private readonly bool _debugMode;
        private readonly bool _privateBinding;
        private readonly IDictionary<string, object> _options;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly StringComparer FileExtensionComparer = StringComparer.OrdinalIgnoreCase;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly StringComparer LanguageNameComparer = StringComparer.OrdinalIgnoreCase;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly StringComparer OptionNameComparer = StringComparer.Ordinal;

        private readonly Dictionary<string, LanguageConfiguration> _languageNames;
        private readonly Dictionary<string, LanguageConfiguration> _languageExtensions;
        private readonly Dictionary<AssemblyQualifiedTypeName, LanguageConfiguration> _languageConfigurations;
        private readonly Dictionary<Type, LanguageConfiguration> _loadedProviderTypes;

        public DlrConfiguration(bool debugMode, bool privateBinding, IDictionary<string, object> options) {
            ContractUtils.RequiresNotNull(options, "options");
            _debugMode = debugMode;
            _privateBinding = privateBinding;
            _options = options;

            _languageNames = new Dictionary<string, LanguageConfiguration>(LanguageNameComparer);
            _languageExtensions = new Dictionary<string, LanguageConfiguration>(FileExtensionComparer);
            _languageConfigurations = new Dictionary<AssemblyQualifiedTypeName, LanguageConfiguration>();
            _loadedProviderTypes = new Dictionary<Type, LanguageConfiguration>();
        }

        /// <summary>
        /// Whether the application is in debug mode.
        /// This means:
        /// 
        /// 1) Symbols are emitted for debuggable methods (methods associated with SourceUnit).
        /// 2) Debuggable methods are emitted to non-collectable types (this is due to CLR limitations on dynamic method debugging).
        /// 3) JIT optimization is disabled for all methods
        /// 4) Languages may disable optimizations based on this value.
        /// </summary>
        public bool DebugMode {
            get { return _debugMode; }
        }

        /// <summary>
        /// Ignore CLR visibility checks.
        /// </summary>
        public bool PrivateBinding {
            get { return _privateBinding; }
        }

        internal IDictionary<string, object> Options {
            get { return _options; }
        }

        internal IDictionary<AssemblyQualifiedTypeName, LanguageConfiguration> Languages {
            get { return _languageConfigurations; }
        }

        public void AddLanguage(string languageTypeName, string displayName, IList<string> names, IList<string> fileExtensions,
            IDictionary<string, object> options) {
            AddLanguage(languageTypeName, displayName, names, fileExtensions, options, null);
        }

        internal void AddLanguage(string languageTypeName, string displayName, IList<string> names, IList<string> fileExtensions, 
            IDictionary<string, object> options, string paramName) {
            ContractUtils.Requires(!_frozen, "Configuration cannot be modified once the runtime is initialized");
            ContractUtils.Requires(
                CollectionUtils.TrueForAll(names, (id) => !String.IsNullOrEmpty(id) && !_languageNames.ContainsKey(id)),
                paramName ?? "names",
                "Language name should not be null, empty or duplicated between languages"
            );
            ContractUtils.Requires(
                CollectionUtils.TrueForAll(fileExtensions, (ext) => !String.IsNullOrEmpty(ext) && !_languageExtensions.ContainsKey(ext)),
                paramName ?? "fileExtensions",
                "File extension should not be null, empty or duplicated between languages"
            );
            ContractUtils.RequiresNotNull(displayName, paramName ?? "displayName");

            if (string.IsNullOrEmpty(displayName)) {
                ContractUtils.Requires(names.Count > 0, paramName ?? "displayName", "Must have a non-empty display name or a a non-empty list of language names");
                displayName = names[0];
            }

            var aqtn = AssemblyQualifiedTypeName.ParseArgument(languageTypeName, paramName ?? "languageTypeName");
            if (_languageConfigurations.ContainsKey(aqtn)) {
                throw new ArgumentException(string.Format("Duplicate language with type name '{0}'", aqtn), "languageTypeName");
            }

            // Add global language options first, they can be rewritten by language specific ones:
            var mergedOptions = new Dictionary<string, object>(_options);

            // Replace global options with language-specific options
            foreach (var option in options) {
                mergedOptions[option.Key] = option.Value;
            }

            var config = new LanguageConfiguration(aqtn, displayName, mergedOptions);

            _languageConfigurations.Add(aqtn, config);

            // allow duplicate ids in identifiers and extensions lists:
            foreach (var name in names) {
                _languageNames[name] = config;
            }

            foreach (var ext in fileExtensions) {
                _languageExtensions[NormalizeExtension(ext)] = config;
            }
        }

        internal static string NormalizeExtension(string extension) {
            return extension[0] == '.' ? extension : "." + extension;
        }

        internal void Freeze() {
            Debug.Assert(!_frozen);
            _frozen = true;
        }

        internal bool TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName providerName, out LanguageContext language) {
            Assert.NotNull(manager);
            LanguageConfiguration config;

            if (_languageConfigurations.TryGetValue(providerName, out config)) {
                language = LoadLanguageContext(manager, config);
                return true;
            }

            language = null;
            return false;
        }

        internal bool TryLoadLanguage(ScriptDomainManager manager, string str, bool isExtension, out LanguageContext language) {
            Assert.NotNull(manager, str);

            var dict = (isExtension) ? _languageExtensions : _languageNames;

            LanguageConfiguration config;
            if (dict.TryGetValue(str, out config)) {
                language = LoadLanguageContext(manager, config);
                return true;
            }

            language = null;
            return false;
        }

        private LanguageContext LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config) {
            bool alreadyLoaded;
            var language = config.LoadLanguageContext(manager, out alreadyLoaded);

            if (!alreadyLoaded) {
                // Checks whether a single language is not registered under two different AQTNs.
                // We can only do it now because there is no way how to ensure that two AQTNs don't refer to the same type w/o loading the type.
                // The check takes place after config.LoadLanguageContext is called to avoid calling user code while holding a lock.
                lock (_loadedProviderTypes) {
                    LanguageConfiguration existingConfig;
                    Type type = language.GetType();
                    if (_loadedProviderTypes.TryGetValue(type, out existingConfig)) {
                        throw new InvalidOperationException(String.Format("Language implemented by type '{0}' has already been loaded using name '{1}'",
                            config.ProviderName, existingConfig.ProviderName));
                    }

                    _loadedProviderTypes.Add(type, config);
                }
            }
            return language;
        }

        public string[] GetLanguageNames(LanguageContext context) {
            ContractUtils.RequiresNotNull(context, "context");

            List<string> result = new List<string>();
            
            foreach (var entry in _languageNames) {
                if (entry.Value.LanguageContext == context) {
                    result.Add(entry.Key);
                }
            }

            return result.ToArray();
        }

        internal string[] GetLanguageNames(LanguageConfiguration config) {
            List<string> result = new List<string>();

            foreach (var entry in _languageNames) {
                if (entry.Value == config) {
                    result.Add(entry.Key);
                }
            }

            return result.ToArray();
        }

        public string[] GetLanguageNames() {
            return ArrayUtils.MakeArray<string>(_languageNames.Keys);
        }

        public string[] GetFileExtensions(LanguageContext context) {
            var result = new List<string>();
            foreach (var entry in _languageExtensions) {
                if (entry.Value.LanguageContext == context) {
                    result.Add(entry.Key);
                }
            }

            return result.ToArray();
        }

        internal string[] GetFileExtensions(LanguageConfiguration config) {
            var result = new List<string>();
            foreach (var entry in _languageExtensions) {
                if (entry.Value == config) {
                    result.Add(entry.Key);
                }
            }

            return result.ToArray();
        }

        public string[] GetFileExtensions() {
            return ArrayUtils.MakeArray<string>(_languageExtensions.Keys);
        }

        internal LanguageConfiguration GetLanguageConfig(LanguageContext context) {
            foreach (var config in _languageConfigurations.Values) {
                if (config.LanguageContext == context) {
                    return config;
                }
            }
            return null;
        }
    }
}
Back to Top