NamespaceReference.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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)
  24. {
  25. return false;
  26. }
  27. public override bool Delete(in Key propertyName)
  28. {
  29. return false;
  30. }
  31. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  32. {
  33. // direct calls on a NamespaceReference constructor object is creating a generic type
  34. var genericTypes = new Type[arguments.Length];
  35. for (int i = 0; i < arguments.Length; i++)
  36. {
  37. var genericTypeReference = arguments[i];
  38. if (genericTypeReference.IsUndefined()
  39. || !genericTypeReference.IsObject()
  40. || genericTypeReference.AsObject().Class != "TypeReference")
  41. {
  42. ExceptionHelper.ThrowTypeError(_engine, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?");
  43. }
  44. genericTypes[i] = ((TypeReference) genericTypeReference).ReferenceType;
  45. }
  46. var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();
  47. if (ReferenceEquals(typeReference, null))
  48. {
  49. return Undefined;
  50. }
  51. try
  52. {
  53. var genericType = typeReference.ReferenceType.MakeGenericType(genericTypes);
  54. return TypeReference.CreateTypeReference(Engine, genericType);
  55. }
  56. catch (Exception e)
  57. {
  58. ExceptionHelper.ThrowTypeError(_engine, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?", e);
  59. return null;
  60. }
  61. }
  62. public override JsValue Get(in Key propertyName, JsValue receiver)
  63. {
  64. var newPath = _path + "." + propertyName;
  65. return GetPath(newPath);
  66. }
  67. public JsValue GetPath(string path)
  68. {
  69. if (_engine.TypeCache.TryGetValue(path, out var type))
  70. {
  71. if (type == null)
  72. {
  73. return new NamespaceReference(_engine, path);
  74. }
  75. return TypeReference.CreateTypeReference(_engine, type);
  76. }
  77. // in CoreCLR, for example, classes that used to be in
  78. // mscorlib were moved away, and only stubs remained, because
  79. // of that, we do the search on the lookup assemblies first,
  80. // and only then in mscorlib. Probelm usage: System.IO.File.CreateText
  81. // search in loaded assemblies
  82. var lookupAssemblies = new[] {Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly()};
  83. foreach (var assembly in lookupAssemblies)
  84. {
  85. type = assembly.GetType(path);
  86. if (type != null)
  87. {
  88. _engine.TypeCache.Add(path, type);
  89. return TypeReference.CreateTypeReference(_engine, type);
  90. }
  91. }
  92. // search in lookup assemblies
  93. var comparedPath = path.Replace("+", ".");
  94. foreach (var assembly in _engine.Options._LookupAssemblies)
  95. {
  96. type = assembly.GetType(path);
  97. if (type != null)
  98. {
  99. _engine.TypeCache.Add(path, type);
  100. return TypeReference.CreateTypeReference(_engine, type);
  101. }
  102. var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
  103. var trimPath = path.Substring(0, lastPeriodPos);
  104. type = GetType(assembly, trimPath);
  105. if (type != null)
  106. {
  107. foreach (Type nType in GetAllNestedTypes(type))
  108. {
  109. if (nType.FullName.Replace("+", ".").Equals(comparedPath))
  110. {
  111. _engine.TypeCache.Add(comparedPath, nType);
  112. return TypeReference.CreateTypeReference(_engine, nType);
  113. }
  114. }
  115. }
  116. }
  117. // search for type in mscorlib
  118. type = System.Type.GetType(path);
  119. if (type != null)
  120. {
  121. _engine.TypeCache.Add(path, type);
  122. return TypeReference.CreateTypeReference(_engine, type);
  123. }
  124. // the new path doesn't represent a known class, thus return a new namespace instance
  125. _engine.TypeCache.Add(path, null);
  126. return new NamespaceReference(_engine, path);
  127. }
  128. /// <summary> Gets a type. </summary>
  129. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  130. /// <param name="assembly"> The assembly. </param>
  131. /// <param name="typeName"> Name of the type. </param>
  132. ///
  133. /// <returns> The type. </returns>
  134. private static Type GetType(Assembly assembly, string typeName)
  135. {
  136. var compared = typeName.Replace("+", ".");
  137. Type[] types = assembly.GetTypes();
  138. foreach (Type t in types)
  139. {
  140. if (t.FullName.Replace("+", ".") == compared)
  141. {
  142. return t;
  143. }
  144. }
  145. return null;
  146. }
  147. private static Type[] GetAllNestedTypes(Type type)
  148. {
  149. var types = new List<Type>();
  150. AddNestedTypesRecursively(types, type);
  151. return types.ToArray();
  152. }
  153. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  154. {
  155. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  156. foreach (Type nestedType in nestedTypes)
  157. {
  158. types.Add(nestedType);
  159. AddNestedTypesRecursively(types, nestedType);
  160. }
  161. }
  162. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  163. {
  164. return PropertyDescriptor.Undefined;
  165. }
  166. public override string ToString()
  167. {
  168. return "[Namespace: " + _path + "]";
  169. }
  170. }
  171. }