NamespaceReference.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jint.Native;
  7. using Jint.Native.Object;
  8. using Jint.Runtime.Descriptors;
  9. namespace Jint.Runtime.Interop
  10. {
  11. /// <summary>
  12. /// Any instance on this class represents a reference to a CLR namespace.
  13. /// Accessing its properties will look for a class of the full name, or instantiate
  14. /// a new <see cref="NamespaceReference"/> as it assumes that the property is a deeper
  15. /// level of the current namespace
  16. /// </summary>
  17. public class NamespaceReference : ObjectInstance, ICallable
  18. {
  19. private readonly string _path;
  20. public NamespaceReference(Engine engine, string path) : base(engine)
  21. {
  22. _path = path;
  23. }
  24. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  25. {
  26. if (throwOnError)
  27. {
  28. throw new JavaScriptException(Engine.TypeError, "Can't define a property of a NamespaceReference");
  29. }
  30. return false;
  31. }
  32. public override bool Delete(string propertyName, bool throwOnError)
  33. {
  34. if (throwOnError)
  35. {
  36. throw new JavaScriptException(Engine.TypeError, "Can't delete a property of a NamespaceReference");
  37. }
  38. return false;
  39. }
  40. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  41. {
  42. // direct calls on a NamespaceReference constructor object is creating a generic type
  43. var genericTypes = new Type[arguments.Length];
  44. for (int i = 0; i < arguments.Length; i++)
  45. {
  46. var genericTypeReference = arguments.At(i);
  47. if (genericTypeReference == Undefined.Instance || !genericTypeReference.IsObject() || genericTypeReference.AsObject().Class != "TypeReference")
  48. {
  49. throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter");
  50. }
  51. genericTypes[i] = arguments.At(i).As<TypeReference>().Type;
  52. }
  53. var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();
  54. if (typeReference == null)
  55. {
  56. return Undefined.Instance;
  57. }
  58. var genericType = typeReference.Type.MakeGenericType(genericTypes);
  59. return TypeReference.CreateTypeReference(Engine, genericType);
  60. }
  61. public override JsValue Get(string propertyName)
  62. {
  63. var newPath = _path + "." + propertyName;
  64. return GetPath(newPath);
  65. }
  66. public JsValue GetPath(string path)
  67. {
  68. Type type;
  69. if (Engine.TypeCache.TryGetValue(path, out type))
  70. {
  71. if (type == null)
  72. {
  73. return new NamespaceReference(Engine, path);
  74. }
  75. return TypeReference.CreateTypeReference(Engine, type);
  76. }
  77. // search for type in mscorlib
  78. type = Type.GetType(path);
  79. if (type != null)
  80. {
  81. Engine.TypeCache.Add(path, type);
  82. return TypeReference.CreateTypeReference(Engine, type);
  83. }
  84. // search in loaded assemblies
  85. foreach (var assembly in new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }.Distinct())
  86. {
  87. type = assembly.GetType(path);
  88. if (type != null)
  89. {
  90. Engine.TypeCache.Add(path, type);
  91. return TypeReference.CreateTypeReference(Engine, type);
  92. }
  93. }
  94. // search in lookup assemblies
  95. foreach (var assembly in Engine.Options.GetLookupAssemblies())
  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. foreach (Type nType in GetAllNestedTypes(type))
  108. {
  109. if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
  110. {
  111. Engine.TypeCache.Add(path.Replace("+", "."), nType);
  112. return TypeReference.CreateTypeReference(Engine, nType);
  113. }
  114. }
  115. }
  116. // the new path doesn't represent a known class, thus return a new namespace instance
  117. Engine.TypeCache.Add(path, null);
  118. return new NamespaceReference(Engine, path);
  119. }
  120. /// <summary> Gets a type. </summary>
  121. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  122. /// <param name="assembly"> The assembly. </param>
  123. /// <param name="typeName"> Name of the type. </param>
  124. ///
  125. /// <returns> The type. </returns>
  126. private static Type GetType(Assembly assembly, string typeName)
  127. {
  128. Type[] types = assembly.GetTypes();
  129. foreach (Type t in types)
  130. {
  131. if (t.FullName.Replace("+", ".") == typeName.Replace("+", "."))
  132. {
  133. return t;
  134. }
  135. }
  136. return null;
  137. }
  138. private static IEnumerable<Type> GetAllNestedTypes(Type type)
  139. {
  140. var types = new List<Type>();
  141. AddNestedTypesRecursively(types, type);
  142. return types.ToArray();
  143. }
  144. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  145. {
  146. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  147. foreach (Type nestedType in nestedTypes)
  148. {
  149. types.Add(nestedType);
  150. AddNestedTypesRecursively(types, nestedType);
  151. }
  152. }
  153. public override PropertyDescriptor GetOwnProperty(string propertyName)
  154. {
  155. return PropertyDescriptor.Undefined;
  156. }
  157. public override string ToString()
  158. {
  159. return "[Namespace: " + _path + "]";
  160. }
  161. }
  162. }