NamespaceReference.cs 7.4 KB

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