NamespaceReference.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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");
  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. var genericType = typeReference.Type.MakeGenericType(genericTypes);
  58. return TypeReference.CreateTypeReference(Engine, genericType);
  59. }
  60. public override JsValue Get(string propertyName)
  61. {
  62. var newPath = _path + "." + propertyName;
  63. return GetPath(newPath);
  64. }
  65. public JsValue GetPath(string path)
  66. {
  67. Type type;
  68. if (Engine.TypeCache.TryGetValue(path, out type))
  69. {
  70. if (type == null)
  71. {
  72. return new NamespaceReference(Engine, path);
  73. }
  74. return TypeReference.CreateTypeReference(Engine, type);
  75. }
  76. // search for type in mscorlib
  77. type = Type.GetType(path);
  78. if (type != null)
  79. {
  80. Engine.TypeCache.Add(path, type);
  81. return TypeReference.CreateTypeReference(Engine, type);
  82. }
  83. // search in loaded assemblies
  84. #if NETSTANDARD1_3
  85. var lookupAssemblies = new[] { typeof(NamespaceReference).GetTypeInfo().Assembly };
  86. #else
  87. var lookupAssemblies = new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() };
  88. #endif
  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. // the new path doesn't represent a known class, thus return a new namespace instance
  121. Engine.TypeCache.Add(path, null);
  122. return new NamespaceReference(Engine, path);
  123. }
  124. /// <summary> Gets a type. </summary>
  125. ///<remarks>Nested type separators are converted to '.' instead of '+' </remarks>
  126. /// <param name="assembly"> The assembly. </param>
  127. /// <param name="typeName"> Name of the type. </param>
  128. ///
  129. /// <returns> The type. </returns>
  130. private static Type GetType(Assembly assembly, string typeName)
  131. {
  132. Type[] types = assembly.GetTypes();
  133. foreach (Type t in types)
  134. {
  135. if (t.FullName.Replace("+", ".") == typeName.Replace("+", "."))
  136. {
  137. return t;
  138. }
  139. }
  140. return null;
  141. }
  142. private static IEnumerable<Type> GetAllNestedTypes(Type type)
  143. {
  144. var types = new List<Type>();
  145. AddNestedTypesRecursively(types, type);
  146. return types.ToArray();
  147. }
  148. private static void AddNestedTypesRecursively(List<Type> types, Type type)
  149. {
  150. Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public);
  151. foreach (Type nestedType in nestedTypes)
  152. {
  153. types.Add(nestedType);
  154. AddNestedTypesRecursively(types, nestedType);
  155. }
  156. }
  157. public override PropertyDescriptor GetOwnProperty(string propertyName)
  158. {
  159. return PropertyDescriptor.Undefined;
  160. }
  161. public override string ToString()
  162. {
  163. return "[Namespace: " + _path + "]";
  164. }
  165. }
  166. }