TypeReference.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Jint.Native;
  6. using Jint.Native.Function;
  7. using Jint.Native.Object;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Descriptors.Specialized;
  10. namespace Jint.Runtime.Interop
  11. {
  12. public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
  13. {
  14. private TypeReference(Engine engine)
  15. : base(engine, "typereference", null, null, false, "TypeReference")
  16. {
  17. }
  18. public Type ReferenceType { get; set; }
  19. public static TypeReference CreateTypeReference(Engine engine, Type type)
  20. {
  21. var obj = new TypeReference(engine);
  22. obj.Extensible = false;
  23. obj.ReferenceType = type;
  24. // The value of the [[Prototype]] internal property of the TypeReference constructor is the Function prototype object
  25. obj.Prototype = engine.Function.PrototypeObject;
  26. obj._length = new PropertyDescriptor(0, PropertyFlag.AllForbidden);
  27. // The initial value of Boolean.prototype is the Boolean prototype object
  28. obj._prototype = new PropertyDescriptor(engine.Object.PrototypeObject, PropertyFlag.AllForbidden);
  29. return obj;
  30. }
  31. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  32. {
  33. // direct calls on a TypeReference constructor object is equivalent to the new operator
  34. return Construct(arguments);
  35. }
  36. public ObjectInstance Construct(JsValue[] arguments)
  37. {
  38. if (arguments.Length == 0 && ReferenceType.IsValueType)
  39. {
  40. var instance = Activator.CreateInstance(ReferenceType);
  41. var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
  42. return result;
  43. }
  44. var constructors = ReferenceType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
  45. foreach (var tuple in TypeConverter.FindBestMatch(constructors, (info, b) => arguments))
  46. {
  47. var method = tuple.Item1;
  48. var parameters = new object[arguments.Length];
  49. var methodParameters = method.GetParameters();
  50. try
  51. {
  52. for (var i = 0; i < arguments.Length; i++)
  53. {
  54. var parameterType = methodParameters[i].ParameterType;
  55. if (typeof(JsValue).IsAssignableFrom(parameterType))
  56. {
  57. parameters[i] = arguments[i];
  58. }
  59. else
  60. {
  61. parameters[i] = Engine.ClrTypeConverter.Convert(
  62. arguments[i].ToObject(),
  63. parameterType,
  64. CultureInfo.InvariantCulture);
  65. }
  66. }
  67. var constructor = (ConstructorInfo) method;
  68. var instance = constructor.Invoke(parameters);
  69. var result = TypeConverter.ToObject(Engine, FromObject(Engine, instance));
  70. // todo: cache method info
  71. return result;
  72. }
  73. catch
  74. {
  75. // ignore method
  76. }
  77. }
  78. ExceptionHelper.ThrowTypeError(_engine, "No public methods with the specified arguments were found.");
  79. return null;
  80. }
  81. public override bool HasInstance(JsValue v)
  82. {
  83. ObjectWrapper wrapper = v.As<ObjectWrapper>();
  84. if (ReferenceEquals(wrapper, null))
  85. {
  86. return base.HasInstance(v);
  87. }
  88. return wrapper.Target.GetType() == ReferenceType;
  89. }
  90. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  91. {
  92. if (throwOnError)
  93. {
  94. ExceptionHelper.ThrowTypeError(_engine, "Can't define a property of a TypeReference");
  95. }
  96. return false;
  97. }
  98. public override bool Delete(string propertyName, bool throwOnError)
  99. {
  100. if (throwOnError)
  101. {
  102. ExceptionHelper.ThrowTypeError(_engine, "Can't delete a property of a TypeReference");
  103. }
  104. return false;
  105. }
  106. public override void Put(string propertyName, JsValue value, bool throwOnError)
  107. {
  108. if (!CanPut(propertyName))
  109. {
  110. if (throwOnError)
  111. {
  112. ExceptionHelper.ThrowTypeError(Engine);
  113. }
  114. return;
  115. }
  116. var ownDesc = GetOwnProperty(propertyName);
  117. if (ownDesc == null)
  118. {
  119. if (throwOnError)
  120. {
  121. ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
  122. }
  123. else
  124. {
  125. return;
  126. }
  127. }
  128. ownDesc.Value = value;
  129. }
  130. public override PropertyDescriptor GetOwnProperty(string propertyName)
  131. {
  132. // todo: cache members locally
  133. if (ReferenceType.IsEnum)
  134. {
  135. Array enumValues = Enum.GetValues(ReferenceType);
  136. Array enumNames = Enum.GetNames(ReferenceType);
  137. for (int i = 0; i < enumValues.Length; i++)
  138. {
  139. if (enumNames.GetValue(i) as string == propertyName)
  140. {
  141. return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
  142. }
  143. }
  144. return PropertyDescriptor.Undefined;
  145. }
  146. var propertyInfo = ReferenceType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
  147. if (propertyInfo != null)
  148. {
  149. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  150. }
  151. var fieldInfo = ReferenceType.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
  152. if (fieldInfo != null)
  153. {
  154. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  155. }
  156. List<MethodInfo> methodInfo = null;
  157. foreach (var mi in ReferenceType.GetMethods(BindingFlags.Public | BindingFlags.Static))
  158. {
  159. if (mi.Name == propertyName)
  160. {
  161. methodInfo = methodInfo ?? new List<MethodInfo>();
  162. methodInfo.Add(mi);
  163. }
  164. }
  165. if (methodInfo == null || methodInfo.Count == 0)
  166. {
  167. return PropertyDescriptor.Undefined;
  168. }
  169. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo.ToArray()), PropertyFlag.AllForbidden);
  170. }
  171. public object Target => ReferenceType;
  172. }
  173. }