NamespaceReference.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Jint.Native;
  6. using Jint.Native.Object;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Runtime.Interop
  9. {
  10. /// <summary>
  11. /// Any instance on this class represents a reference to a CLR namespace.
  12. /// Accessing its properties will look for a class of the full name, or instantiate
  13. /// a new <see cref="NamespaceReference"/> as it assumes that the property is a deeper
  14. /// level of the current namespace
  15. /// </summary>
  16. public class NamespaceReference : ObjectInstance, ICallable
  17. {
  18. private readonly string _path;
  19. public NamespaceReference(Engine engine, string path) : base(engine)
  20. {
  21. _path = path;
  22. }
  23. public override bool DefineOwnProperty(string propertyName, IPropertyDescriptor desc, bool throwOnError)
  24. {
  25. if (throwOnError)
  26. {
  27. throw new JavaScriptException(Engine.TypeError, "Can't define a property of a NamespaceReference");
  28. }
  29. return false;
  30. }
  31. public override bool Delete(string propertyName, bool throwOnError)
  32. {
  33. if (throwOnError)
  34. {
  35. throw new JavaScriptException(Engine.TypeError, "Can't delete a property of a NamespaceReference");
  36. }
  37. return false;
  38. }
  39. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  40. {
  41. // direct calls on a NamespaceReference constructor object is creating a generic type
  42. var genericTypes = new Type[arguments.Length];
  43. for (int i = 0; i < arguments.Length; i++)
  44. {
  45. var genericTypeReference = arguments.At(i);
  46. if (ReferenceEquals(genericTypeReference, Undefined)
  47. || !genericTypeReference.IsObject()
  48. || genericTypeReference.AsObject().Class != "TypeReference")
  49. {
  50. throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?");
  51. }
  52. genericTypes[i] = arguments.At(i).As<TypeReference>().ReferenceType;
  53. }
  54. var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();
  55. if (typeReference == null)
  56. {
  57. return Undefined;
  58. }
  59. try
  60. {
  61. var genericType = typeReference.ReferenceType.MakeGenericType(genericTypes);
  62. return TypeReference.CreateTypeReference(Engine, genericType);
  63. }
  64. catch (Exception e)
  65. {
  66. throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?", e);
  67. }
  68. }
  69. public override JsValue Get(string propertyName)
  70. {
  71. var newPath = _path + "." + propertyName;
  72. return GetPath(newPath);
  73. }
  74. public JsValue GetPath(string path)
  75. {
  76. Type type;
  77. if (Engine.TypeCache.TryGetValue(path, out type))
  78. {
  79. if (type == null)
  80. {
  81. return new NamespaceReference(Engine, path);
  82. }
  83. return TypeReference.CreateTypeReference(Engine, type);
  84. }
  85. // in CoreCLR, for example, classes that used to be in
  86. // mscorlib were moved away, and only stubs remained, because
  87. // of that, we do the search on the lookup assemblies first,
  88. // and only then in mscorlib. Probelm usage: System.IO.File.CreateText
  89. // search in loaded assemblies
  90. var lookupAssemblies = new[] {Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly()};
  91. foreach (var assembly in lookupAssemblies)
  92. {
  93. type = assembly.GetType(path);
  94. if (type != null)
  95. {
  96. Engine.TypeCache.Add(path, type);
  97. return TypeReference.CreateTypeReference(Engine, type);
  98. }
  99. }
  100. // search in lookup assemblies
  101. foreach (var assembly in Engine.Options._LookupAssemblies)
  102. {
  103. type = assembly.GetType(path);
  104. if (type != null)
  105. {
  106. Engine.TypeCache.Add(path, type);
  107. return TypeReference.CreateTypeReference(Engine, type);
  108. }
  109. var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
  110. var trimPath = path.Substring(0, lastPeriodPos);
  111. type = GetType(assembly, trimPath);
  112. if (type != null)
  113. foreach (Type nType in GetAllNestedTypes(type))
  114. {
  115. if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
  116. {
  117. Engine.TypeCache.Add(path.Replace("+", "."), nType);
  118. return TypeReference.CreateTypeReference(Engine, nType);
  119. }
  120. }
  121. }
  122. // search for type in mscorlib
  123. type = System.Type.GetType(path);
  124. if (type != null)
  125. {
  126. Engine.TypeCache.Add(path, type);
  127. return TypeReference.CreateTypeReference(Engine, type);
  128. }
  129. // the new path doesn't represent a known class, thus return a new namespace instance
  130. Engine.TypeCache.Add(path, null);
  131. return new NamespaceReference(Engine, path);
  132. }
  133. /// <summary> Gets a type. </summary>
  134. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  135. /// <param name="assembly"> The assembly. </param>
  136. /// <param name="typeName"> Name of the type. </param>
  137. ///
  138. /// <returns> The type. </returns>
  139. private static Type GetType(Assembly assembly, string typeName)
  140. {
  141. Type[] types = assembly.GetTypes();
  142. foreach (Type t in types)
  143. {
  144. if (t.FullName.Replace("+", ".") == typeName.Replace("+", "."))
  145. {
  146. return t;
  147. }
  148. }
  149. return null;
  150. }
  151. private static IEnumerable<Type> GetAllNestedTypes(Type type)
  152. {
  153. var types = new List<Type>();
  154. AddNestedTypesRecursively(types, type);
  155. return types.ToArray();
  156. }
  157. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  158. {
  159. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  160. foreach (Type nestedType in nestedTypes)
  161. {
  162. types.Add(nestedType);
  163. AddNestedTypesRecursively(types, nestedType);
  164. }
  165. }
  166. public override IPropertyDescriptor GetOwnProperty(string propertyName)
  167. {
  168. return PropertyDescriptor.Undefined;
  169. }
  170. public override string ToString()
  171. {
  172. return "[Namespace: " + _path + "]";
  173. }
  174. }
  175. }