PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting/Runtime/ScriptDomainManager.cs

https://bitbucket.org/stefanrusek/xronos
C# | 339 lines | 251 code | 68 blank | 20 comment | 17 complexity | 5352c31db316317c353692de0acc196d 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;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Reflection;
  24. using System.Runtime.Serialization;
  25. #if CODEPLEX_40
  26. using System.Dynamic;
  27. #else
  28. using Microsoft.Scripting;
  29. #endif
  30. using Microsoft.Scripting.Actions;
  31. using Microsoft.Scripting.Utils;
  32. using System.Threading;
  33. namespace Microsoft.Scripting.Runtime {
  34. [Serializable]
  35. public class MissingTypeException : Exception {
  36. public MissingTypeException() {
  37. }
  38. public MissingTypeException(string name)
  39. : this(name, null) {
  40. }
  41. public MissingTypeException(string name, Exception e) :
  42. base(Strings.MissingType(name), e) {
  43. }
  44. #if !SILVERLIGHT // SerializationInfo
  45. protected MissingTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { }
  46. #endif
  47. }
  48. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")] // TODO: fix
  49. public sealed class ScriptDomainManager {
  50. private readonly DynamicRuntimeHostingProvider _hostingProvider;
  51. private readonly SharedIO _sharedIO;
  52. // last id assigned to a language context:
  53. private int _lastContextId;
  54. private ScopeAttributesWrapper _scopeWrapper;
  55. private Scope _globals;
  56. private readonly DlrConfiguration _configuration;
  57. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
  58. public PlatformAdaptationLayer Platform {
  59. get {
  60. PlatformAdaptationLayer result = _hostingProvider.PlatformAdaptationLayer;
  61. if (result == null) {
  62. throw new InvalidImplementationException();
  63. }
  64. return result;
  65. }
  66. }
  67. public SharedIO SharedIO {
  68. get { return _sharedIO; }
  69. }
  70. public DynamicRuntimeHostingProvider Host {
  71. get { return _hostingProvider; }
  72. }
  73. public DlrConfiguration Configuration {
  74. get { return _configuration; }
  75. }
  76. public ScriptDomainManager(DynamicRuntimeHostingProvider hostingProvider, DlrConfiguration configuration) {
  77. ContractUtils.RequiresNotNull(hostingProvider, "hostingProvider");
  78. ContractUtils.RequiresNotNull(configuration, "configuration");
  79. configuration.Freeze();
  80. _hostingProvider = hostingProvider;
  81. _configuration = configuration;
  82. _sharedIO = new SharedIO();
  83. // create the initial default scope
  84. _scopeWrapper = new ScopeAttributesWrapper(this);
  85. _globals = new Scope(_scopeWrapper);
  86. }
  87. #region Language Registration
  88. internal ContextId GenerateContextId() {
  89. return new ContextId(Interlocked.Increment(ref _lastContextId));
  90. }
  91. public LanguageContext GetLanguage(Type providerType) {
  92. ContractUtils.RequiresNotNull(providerType, "providerType");
  93. return GetLanguageByTypeName(providerType.AssemblyQualifiedName);
  94. }
  95. public LanguageContext GetLanguageByTypeName(string providerAssemblyQualifiedTypeName) {
  96. ContractUtils.RequiresNotNull(providerAssemblyQualifiedTypeName, "providerAssemblyQualifiedTypeName");
  97. var aqtn = AssemblyQualifiedTypeName.ParseArgument(providerAssemblyQualifiedTypeName, "providerAssemblyQualifiedTypeName");
  98. LanguageContext language;
  99. if (!_configuration.TryLoadLanguage(this, aqtn, out language)) {
  100. throw Error.UnknownLanguageProviderType();
  101. }
  102. return language;
  103. }
  104. public bool TryGetLanguage(string languageName, out LanguageContext language) {
  105. ContractUtils.RequiresNotNull(languageName, "languageName");
  106. return _configuration.TryLoadLanguage(this, languageName, false, out language);
  107. }
  108. public LanguageContext GetLanguageByName(string languageName) {
  109. LanguageContext language;
  110. if (!TryGetLanguage(languageName, out language)) {
  111. throw new ArgumentException(String.Format("Unknown language name: '{0}'", languageName));
  112. }
  113. return language;
  114. }
  115. public bool TryGetLanguageByFileExtension(string fileExtension, out LanguageContext language) {
  116. ContractUtils.RequiresNotEmpty(fileExtension, "fileExtension");
  117. return _configuration.TryLoadLanguage(this, DlrConfiguration.NormalizeExtension(fileExtension), true, out language);
  118. }
  119. public LanguageContext GetLanguageByExtension(string fileExtension) {
  120. LanguageContext language;
  121. if (!TryGetLanguageByFileExtension(fileExtension, out language)) {
  122. throw new ArgumentException(String.Format("Unknown file extension: '{0}'", fileExtension));
  123. }
  124. return language;
  125. }
  126. #endregion
  127. /// <summary>
  128. /// A collection of environment variables.
  129. /// </summary>
  130. public Scope Globals {
  131. get {
  132. return _globals;
  133. }
  134. }
  135. public void SetGlobalsDictionary(IAttributesCollection dictionary) {
  136. ContractUtils.RequiresNotNull(dictionary, "dictionary");
  137. _scopeWrapper.Dict = dictionary;
  138. }
  139. public event EventHandler<AssemblyLoadedEventArgs> AssemblyLoaded;
  140. public bool LoadAssembly(Assembly assembly) {
  141. ContractUtils.RequiresNotNull(assembly, "assembly");
  142. if (_scopeWrapper.LoadAssembly(assembly)) {
  143. // only deliver the event if we've never added the assembly before
  144. EventHandler<AssemblyLoadedEventArgs> assmLoaded = AssemblyLoaded;
  145. if (assmLoaded != null) {
  146. assmLoaded(this, new AssemblyLoadedEventArgs(assembly));
  147. }
  148. return true;
  149. }
  150. return false;
  151. }
  152. #region ScopeAttributesWrapper
  153. private class ScopeAttributesWrapper : IAttributesCollection {
  154. private IAttributesCollection _dict = new SymbolDictionary();
  155. private readonly TopNamespaceTracker _tracker;
  156. public ScopeAttributesWrapper(ScriptDomainManager manager) {
  157. _tracker = new TopNamespaceTracker(manager);
  158. }
  159. public IAttributesCollection Dict {
  160. set {
  161. Assert.NotNull(_dict);
  162. _dict = value;
  163. }
  164. }
  165. public bool LoadAssembly(Assembly asm) {
  166. return _tracker.LoadAssembly(asm);
  167. }
  168. public List<Assembly> GetLoadedAssemblies() {
  169. return _tracker._packageAssemblies;
  170. }
  171. #region IAttributesCollection Members
  172. public void Add(SymbolId name, object value) {
  173. _dict[name] = value;
  174. }
  175. public bool TryGetValue(SymbolId name, out object value) {
  176. if (_dict.TryGetValue(name, out value)) {
  177. return true;
  178. }
  179. value = _tracker.TryGetPackageAny(name);
  180. return value != null;
  181. }
  182. public bool Remove(SymbolId name) {
  183. return _dict.Remove(name);
  184. }
  185. public bool ContainsKey(SymbolId name) {
  186. return _dict.ContainsKey(name) || _tracker.TryGetPackageAny(name) != null;
  187. }
  188. public object this[SymbolId name] {
  189. get {
  190. object value;
  191. if (TryGetValue(name, out value)) {
  192. return value;
  193. }
  194. throw new KeyNotFoundException();
  195. }
  196. set {
  197. Add(name, value);
  198. }
  199. }
  200. public IDictionary<SymbolId, object> SymbolAttributes {
  201. get { return _dict.SymbolAttributes; }
  202. }
  203. public void AddObjectKey(object name, object value) {
  204. _dict.AddObjectKey(name, value);
  205. }
  206. public bool TryGetObjectValue(object name, out object value) {
  207. return _dict.TryGetObjectValue(name, out value);
  208. }
  209. public bool RemoveObjectKey(object name) {
  210. return _dict.RemoveObjectKey(name);
  211. }
  212. public bool ContainsObjectKey(object name) {
  213. return _dict.ContainsObjectKey(name);
  214. }
  215. public IDictionary<object, object> AsObjectKeyedDictionary() {
  216. return _dict.AsObjectKeyedDictionary();
  217. }
  218. public int Count {
  219. get {
  220. int count = _dict.Count + _tracker.Count;
  221. foreach (object o in _tracker.Keys) {
  222. if (ContainsObjectKey(o)) {
  223. count--;
  224. }
  225. }
  226. return count;
  227. }
  228. }
  229. public ICollection<object> Keys {
  230. get {
  231. List<object> keys = new List<object>(_dict.Keys);
  232. foreach (object o in _tracker.Keys) {
  233. if (!_dict.ContainsObjectKey(o)) {
  234. keys.Add(o);
  235. }
  236. }
  237. return keys;
  238. }
  239. }
  240. #endregion
  241. #region IEnumerable<KeyValuePair<object,object>> Members
  242. public IEnumerator<KeyValuePair<object, object>> GetEnumerator() {
  243. foreach (KeyValuePair<object, object> kvp in _dict) {
  244. yield return kvp;
  245. }
  246. foreach (KeyValuePair<object, object> kvp in _tracker) {
  247. if (!_dict.ContainsObjectKey(kvp.Key)) {
  248. yield return kvp;
  249. }
  250. }
  251. }
  252. #endregion
  253. #region IEnumerable Members
  254. IEnumerator IEnumerable.GetEnumerator() {
  255. foreach (KeyValuePair<object, object> kvp in _dict) {
  256. yield return kvp.Key;
  257. }
  258. foreach (KeyValuePair<object, object> kvp in _tracker) {
  259. if (!_dict.ContainsObjectKey(kvp.Key)) {
  260. yield return kvp.Key;
  261. }
  262. }
  263. }
  264. #endregion
  265. }
  266. public Assembly[] GetLoadedAssemblyList() {
  267. return _scopeWrapper.GetLoadedAssemblies().ToArray();
  268. }
  269. #endregion
  270. }
  271. }