NamespaceReference.cs 7.2 KB

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