NamespaceReference.cs 7.3 KB

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