NamespaceReference.cs 7.6 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, PropertyDescriptor 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 (genericTypeReference.IsUndefined()
  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 (ReferenceEquals(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. if (Engine.TypeCache.TryGetValue(path, out var type))
  77. {
  78. if (type == null)
  79. {
  80. return new NamespaceReference(Engine, path);
  81. }
  82. return TypeReference.CreateTypeReference(Engine, type);
  83. }
  84. // in CoreCLR, for example, classes that used to be in
  85. // mscorlib were moved away, and only stubs remained, because
  86. // of that, we do the search on the lookup assemblies first,
  87. // and only then in mscorlib. Probelm usage: System.IO.File.CreateText
  88. // search in loaded assemblies
  89. var lookupAssemblies = new[] {Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly()};
  90. var lookupAssembliesLength = lookupAssemblies.Length;
  91. for (var i = 0; i < lookupAssembliesLength; i++)
  92. {
  93. var assembly = lookupAssemblies[i];
  94. type = assembly.GetType(path);
  95. if (type != null)
  96. {
  97. Engine.TypeCache.Add(path, type);
  98. return TypeReference.CreateTypeReference(Engine, type);
  99. }
  100. }
  101. // search in lookup assemblies
  102. foreach (var assembly in Engine.Options._LookupAssemblies)
  103. {
  104. type = assembly.GetType(path);
  105. if (type != null)
  106. {
  107. Engine.TypeCache.Add(path, type);
  108. return TypeReference.CreateTypeReference(Engine, type);
  109. }
  110. var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
  111. var trimPath = path.Substring(0, lastPeriodPos);
  112. type = GetType(assembly, trimPath);
  113. if (type != null)
  114. foreach (Type nType in GetAllNestedTypes(type))
  115. {
  116. if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
  117. {
  118. Engine.TypeCache.Add(path.Replace("+", "."), nType);
  119. return TypeReference.CreateTypeReference(Engine, nType);
  120. }
  121. }
  122. }
  123. // search for type in mscorlib
  124. type = System.Type.GetType(path);
  125. if (type != null)
  126. {
  127. Engine.TypeCache.Add(path, type);
  128. return TypeReference.CreateTypeReference(Engine, type);
  129. }
  130. // the new path doesn't represent a known class, thus return a new namespace instance
  131. Engine.TypeCache.Add(path, null);
  132. return new NamespaceReference(Engine, path);
  133. }
  134. /// <summary> Gets a type. </summary>
  135. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  136. /// <param name="assembly"> The assembly. </param>
  137. /// <param name="typeName"> Name of the type. </param>
  138. ///
  139. /// <returns> The type. </returns>
  140. private static Type GetType(Assembly assembly, string typeName)
  141. {
  142. Type[] types = assembly.GetTypes();
  143. foreach (Type t in types)
  144. {
  145. if (t.FullName.Replace("+", ".") == typeName.Replace("+", "."))
  146. {
  147. return t;
  148. }
  149. }
  150. return null;
  151. }
  152. private static IEnumerable<Type> GetAllNestedTypes(Type type)
  153. {
  154. var types = new List<Type>();
  155. AddNestedTypesRecursively(types, type);
  156. return types.ToArray();
  157. }
  158. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  159. {
  160. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  161. foreach (Type nestedType in nestedTypes)
  162. {
  163. types.Add(nestedType);
  164. AddNestedTypesRecursively(types, nestedType);
  165. }
  166. }
  167. public override PropertyDescriptor GetOwnProperty(string propertyName)
  168. {
  169. return PropertyDescriptor.Undefined;
  170. }
  171. public override string ToString()
  172. {
  173. return "[Namespace: " + _path + "]";
  174. }
  175. }
  176. }