TypeReference.cs 7.3 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(_engine, 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. if (v.IsObject())
  84. {
  85. var wrapper = v.AsObject() as IObjectWrapper;
  86. if (wrapper != null)
  87. return wrapper.Target.GetType() == ReferenceType;
  88. }
  89. return base.HasInstance(v);
  90. }
  91. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  92. {
  93. if (throwOnError)
  94. {
  95. ExceptionHelper.ThrowTypeError(_engine, "Can't define a property of a TypeReference");
  96. }
  97. return false;
  98. }
  99. public override bool Delete(string propertyName, bool throwOnError)
  100. {
  101. if (throwOnError)
  102. {
  103. ExceptionHelper.ThrowTypeError(_engine, "Can't delete a property of a TypeReference");
  104. }
  105. return false;
  106. }
  107. public override void Put(string propertyName, JsValue value, bool throwOnError)
  108. {
  109. if (!CanPut(propertyName))
  110. {
  111. if (throwOnError)
  112. {
  113. ExceptionHelper.ThrowTypeError(Engine);
  114. }
  115. return;
  116. }
  117. var ownDesc = GetOwnProperty(propertyName);
  118. if (ownDesc == null)
  119. {
  120. if (throwOnError)
  121. {
  122. ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
  123. }
  124. else
  125. {
  126. return;
  127. }
  128. }
  129. ownDesc.Value = value;
  130. }
  131. public override PropertyDescriptor GetOwnProperty(string propertyName)
  132. {
  133. // todo: cache members locally
  134. if (ReferenceType.IsEnum)
  135. {
  136. Array enumValues = Enum.GetValues(ReferenceType);
  137. Array enumNames = Enum.GetNames(ReferenceType);
  138. for (int i = 0; i < enumValues.Length; i++)
  139. {
  140. if (enumNames.GetValue(i) as string == propertyName)
  141. {
  142. return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
  143. }
  144. }
  145. return PropertyDescriptor.Undefined;
  146. }
  147. var propertyInfo = ReferenceType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
  148. if (propertyInfo != null)
  149. {
  150. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  151. }
  152. var fieldInfo = ReferenceType.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
  153. if (fieldInfo != null)
  154. {
  155. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  156. }
  157. List<MethodInfo> methodInfo = null;
  158. foreach (var mi in ReferenceType.GetMethods(BindingFlags.Public | BindingFlags.Static))
  159. {
  160. if (mi.Name == propertyName)
  161. {
  162. methodInfo = methodInfo ?? new List<MethodInfo>();
  163. methodInfo.Add(mi);
  164. }
  165. }
  166. if (methodInfo == null || methodInfo.Count == 0)
  167. {
  168. return PropertyDescriptor.Undefined;
  169. }
  170. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo.ToArray()), PropertyFlag.AllForbidden);
  171. }
  172. public object Target => ReferenceType;
  173. }
  174. }