NamespaceReference.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(in Key propertyName, PropertyDescriptor desc, bool throwOnError)
  24. {
  25. if (throwOnError)
  26. {
  27. ExceptionHelper.ThrowTypeError(_engine, "Can't define a property of a NamespaceReference");
  28. }
  29. return false;
  30. }
  31. public override bool Delete(in Key propertyName, bool throwOnError)
  32. {
  33. if (throwOnError)
  34. {
  35. ExceptionHelper.ThrowTypeError(_engine, "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[i];
  46. if (genericTypeReference.IsUndefined()
  47. || !genericTypeReference.IsObject()
  48. || genericTypeReference.AsObject().Class != "TypeReference")
  49. {
  50. ExceptionHelper.ThrowTypeError(_engine, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?");
  51. }
  52. genericTypes[i] = ((TypeReference) genericTypeReference).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. ExceptionHelper.ThrowTypeError(_engine, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?", e);
  67. return null;
  68. }
  69. }
  70. public override JsValue Get(in Key propertyName)
  71. {
  72. var newPath = _path + "." + propertyName;
  73. return GetPath(newPath);
  74. }
  75. public JsValue GetPath(string path)
  76. {
  77. if (_engine.TypeCache.TryGetValue(path, out var 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. var comparedPath = path.Replace("+", ".");
  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. {
  115. foreach (Type nType in GetAllNestedTypes(type))
  116. {
  117. if (nType.FullName.Replace("+", ".").Equals(comparedPath))
  118. {
  119. _engine.TypeCache.Add(comparedPath, nType);
  120. return TypeReference.CreateTypeReference(_engine, nType);
  121. }
  122. }
  123. }
  124. }
  125. // search for type in mscorlib
  126. type = System.Type.GetType(path);
  127. if (type != null)
  128. {
  129. _engine.TypeCache.Add(path, type);
  130. return TypeReference.CreateTypeReference(_engine, type);
  131. }
  132. // the new path doesn't represent a known class, thus return a new namespace instance
  133. _engine.TypeCache.Add(path, null);
  134. return new NamespaceReference(_engine, path);
  135. }
  136. /// <summary> Gets a type. </summary>
  137. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  138. /// <param name="assembly"> The assembly. </param>
  139. /// <param name="typeName"> Name of the type. </param>
  140. ///
  141. /// <returns> The type. </returns>
  142. private static Type GetType(Assembly assembly, string typeName)
  143. {
  144. var compared = typeName.Replace("+", ".");
  145. Type[] types = assembly.GetTypes();
  146. foreach (Type t in types)
  147. {
  148. if (t.FullName.Replace("+", ".") == compared)
  149. {
  150. return t;
  151. }
  152. }
  153. return null;
  154. }
  155. private static Type[] GetAllNestedTypes(Type type)
  156. {
  157. var types = new List<Type>();
  158. AddNestedTypesRecursively(types, type);
  159. return types.ToArray();
  160. }
  161. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  162. {
  163. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  164. foreach (Type nestedType in nestedTypes)
  165. {
  166. types.Add(nestedType);
  167. AddNestedTypesRecursively(types, nestedType);
  168. }
  169. }
  170. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  171. {
  172. return PropertyDescriptor.Undefined;
  173. }
  174. public override string ToString()
  175. {
  176. return "[Namespace: " + _path + "]";
  177. }
  178. }
  179. }