NamespaceReference.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  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(0).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. foreach (var assembly in new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }.Distinct())
  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 asemblies
  94. foreach (var assembly in Engine.Options.GetLookupAssemblies())
  95. {
  96. type = assembly.GetType(path);
  97. if (type != null)
  98. {
  99. Engine.TypeCache.Add(path, type);
  100. return TypeReference.CreateTypeReference(Engine, type);
  101. }
  102. }
  103. // the new path doesn't represent a known class, thus return a new namespace instance
  104. Engine.TypeCache.Add(path, null);
  105. return new NamespaceReference(Engine, path);
  106. }
  107. public override PropertyDescriptor GetOwnProperty(string propertyName)
  108. {
  109. return PropertyDescriptor.Undefined;
  110. }
  111. public override string ToString()
  112. {
  113. return "[Namespace: " + _path + "]";
  114. }
  115. }
  116. }