TypeReference.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  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, 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.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
  27. // The initial value of Boolean.prototype is the Boolean prototype object
  28. obj.SetOwnProperty("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. var methods = TypeConverter.FindBestMatch(Engine, constructors, arguments).ToList();
  46. foreach (var method in methods)
  47. {
  48. var parameters = new object[arguments.Length];
  49. try
  50. {
  51. for (var i = 0; i < arguments.Length; i++)
  52. {
  53. var parameterType = method.GetParameters()[i].ParameterType;
  54. if (typeof(JsValue).IsAssignableFrom(parameterType))
  55. {
  56. parameters[i] = arguments[i];
  57. }
  58. else
  59. {
  60. parameters[i] = Engine.ClrTypeConverter.Convert(
  61. arguments[i].ToObject(),
  62. parameterType,
  63. CultureInfo.InvariantCulture);
  64. }
  65. }
  66. var constructor = (ConstructorInfo)method;
  67. var instance = constructor.Invoke(parameters.ToArray());
  68. var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
  69. // todo: cache method info
  70. return result;
  71. }
  72. catch
  73. {
  74. // ignore method
  75. }
  76. }
  77. ExceptionHelper.ThrowTypeError(_engine, "No public methods with the specified arguments were found.");
  78. return null;
  79. }
  80. public override bool HasInstance(JsValue v)
  81. {
  82. ObjectWrapper wrapper = v.As<ObjectWrapper>();
  83. if (ReferenceEquals(wrapper, null))
  84. {
  85. return base.HasInstance(v);
  86. }
  87. return wrapper.Target.GetType() == ReferenceType;
  88. }
  89. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  90. {
  91. if (throwOnError)
  92. {
  93. ExceptionHelper.ThrowTypeError(_engine, "Can't define a property of a TypeReference");
  94. }
  95. return false;
  96. }
  97. public override bool Delete(string propertyName, bool throwOnError)
  98. {
  99. if (throwOnError)
  100. {
  101. ExceptionHelper.ThrowTypeError(_engine, "Can't delete a property of a TypeReference");
  102. }
  103. return false;
  104. }
  105. public override void Put(string propertyName, JsValue value, bool throwOnError)
  106. {
  107. if (!CanPut(propertyName))
  108. {
  109. if (throwOnError)
  110. {
  111. ExceptionHelper.ThrowTypeError(Engine);
  112. }
  113. return;
  114. }
  115. var ownDesc = GetOwnProperty(propertyName);
  116. if (ownDesc == null)
  117. {
  118. if (throwOnError)
  119. {
  120. ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
  121. }
  122. else
  123. {
  124. return;
  125. }
  126. }
  127. ownDesc.Value = value;
  128. }
  129. public override PropertyDescriptor GetOwnProperty(string propertyName)
  130. {
  131. // todo: cache members locally
  132. if (ReferenceType.IsEnum())
  133. {
  134. Array enumValues = Enum.GetValues(ReferenceType);
  135. Array enumNames = Enum.GetNames(ReferenceType);
  136. for (int i = 0; i < enumValues.Length; i++)
  137. {
  138. if (enumNames.GetValue(i) as string == propertyName)
  139. {
  140. return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
  141. }
  142. }
  143. return PropertyDescriptor.Undefined;
  144. }
  145. var propertyInfo = ReferenceType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
  146. if (propertyInfo != null)
  147. {
  148. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  149. }
  150. var fieldInfo = ReferenceType.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
  151. if (fieldInfo != null)
  152. {
  153. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  154. }
  155. var methodInfo = ReferenceType
  156. .GetMethods(BindingFlags.Public | BindingFlags.Static)
  157. .Where(mi => mi.Name == propertyName)
  158. .ToArray();
  159. if (methodInfo.Length == 0)
  160. {
  161. return PropertyDescriptor.Undefined;
  162. }
  163. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo), PropertyFlag.AllForbidden);
  164. }
  165. public object Target => ReferenceType;
  166. }
  167. }