TypeReference.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.PreventExtensions();
  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 = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  27. // The initial value of Boolean.prototype is the Boolean prototype object
  28. obj._prototypeDescriptor = 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, thisObject);
  35. }
  36. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  37. {
  38. if (arguments.Length == 0 && ReferenceType.IsValueType)
  39. {
  40. var instance = Activator.CreateInstance(ReferenceType);
  41. var result = TypeConverter.ToObject(Engine, 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(in Key propertyName, PropertyDescriptor desc)
  92. {
  93. return false;
  94. }
  95. public override bool Delete(in Key propertyName)
  96. {
  97. return false;
  98. }
  99. public override bool Set(in Key propertyName, JsValue value, JsValue receiver)
  100. {
  101. if (!CanPut(propertyName))
  102. {
  103. return false;
  104. }
  105. var ownDesc = GetOwnProperty(propertyName);
  106. if (ownDesc == null)
  107. {
  108. return false;
  109. }
  110. ownDesc.Value = value;
  111. return true;
  112. }
  113. public override PropertyDescriptor GetOwnProperty(in Key key)
  114. {
  115. // todo: cache members locally
  116. if (ReferenceType.IsEnum)
  117. {
  118. Array enumValues = Enum.GetValues(ReferenceType);
  119. Array enumNames = Enum.GetNames(ReferenceType);
  120. for (int i = 0; i < enumValues.Length; i++)
  121. {
  122. if (enumNames.GetValue(i) as string == key.Name)
  123. {
  124. return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
  125. }
  126. }
  127. return PropertyDescriptor.Undefined;
  128. }
  129. var propertyInfo = ReferenceType.GetProperty(key.Name, BindingFlags.Public | BindingFlags.Static);
  130. if (propertyInfo != null)
  131. {
  132. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  133. }
  134. var fieldInfo = ReferenceType.GetField(key.Name, BindingFlags.Public | BindingFlags.Static);
  135. if (fieldInfo != null)
  136. {
  137. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  138. }
  139. List<MethodInfo> methodInfo = null;
  140. foreach (var mi in ReferenceType.GetMethods(BindingFlags.Public | BindingFlags.Static))
  141. {
  142. if (mi.Name == key.Name)
  143. {
  144. methodInfo = methodInfo ?? new List<MethodInfo>();
  145. methodInfo.Add(mi);
  146. }
  147. }
  148. if (methodInfo == null || methodInfo.Count == 0)
  149. {
  150. return PropertyDescriptor.Undefined;
  151. }
  152. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo.ToArray()), PropertyFlag.AllForbidden);
  153. }
  154. public object Target => ReferenceType;
  155. }
  156. }