TypeReference.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 static readonly JsString _name = new JsString("typereference");
  15. private TypeReference(Engine engine)
  16. : base(engine, _name, FunctionThisMode.Global, ObjectClass.TypeReference)
  17. {
  18. }
  19. public Type ReferenceType { get; set; }
  20. public static TypeReference CreateTypeReference(Engine engine, Type type)
  21. {
  22. var obj = new TypeReference(engine);
  23. obj.PreventExtensions();
  24. obj.ReferenceType = type;
  25. // The value of the [[Prototype]] internal property of the TypeReference constructor is the Function prototype object
  26. obj._prototype = engine.Function.PrototypeObject;
  27. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  28. // The initial value of Boolean.prototype is the Boolean prototype object
  29. obj._prototypeDescriptor = new PropertyDescriptor(engine.Object.PrototypeObject, PropertyFlag.AllForbidden);
  30. return obj;
  31. }
  32. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  33. {
  34. // direct calls on a TypeReference constructor object is equivalent to the new operator
  35. return Construct(arguments, thisObject);
  36. }
  37. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  38. {
  39. if (arguments.Length == 0 && ReferenceType.IsValueType)
  40. {
  41. var instance = Activator.CreateInstance(ReferenceType);
  42. var result = TypeConverter.ToObject(Engine, FromObject(Engine, instance));
  43. return result;
  44. }
  45. var constructors = ReferenceType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
  46. foreach (var tuple in TypeConverter.FindBestMatch(_engine, constructors, (info, b) => arguments))
  47. {
  48. var method = tuple.Item1;
  49. var parameters = new object[arguments.Length];
  50. var methodParameters = method.GetParameters();
  51. try
  52. {
  53. for (var i = 0; i < arguments.Length; i++)
  54. {
  55. var parameterType = methodParameters[i].ParameterType;
  56. if (typeof(JsValue).IsAssignableFrom(parameterType))
  57. {
  58. parameters[i] = arguments[i];
  59. }
  60. else
  61. {
  62. parameters[i] = Engine.ClrTypeConverter.Convert(
  63. arguments[i].ToObject(),
  64. parameterType,
  65. CultureInfo.InvariantCulture);
  66. }
  67. }
  68. var constructor = (ConstructorInfo) method;
  69. var instance = constructor.Invoke(parameters);
  70. var result = TypeConverter.ToObject(Engine, FromObject(Engine, instance));
  71. // todo: cache method info
  72. return result;
  73. }
  74. catch
  75. {
  76. // ignore method
  77. }
  78. }
  79. return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine, "No public methods with the specified arguments were found.");
  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(JsValue property, PropertyDescriptor desc)
  92. {
  93. return false;
  94. }
  95. public override bool Delete(JsValue property)
  96. {
  97. return false;
  98. }
  99. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  100. {
  101. if (!CanPut(property))
  102. {
  103. return false;
  104. }
  105. var ownDesc = GetOwnProperty(property);
  106. if (ownDesc == null)
  107. {
  108. return false;
  109. }
  110. ownDesc.Value = value;
  111. return true;
  112. }
  113. public override PropertyDescriptor GetOwnProperty(JsValue property)
  114. {
  115. // todo: cache members locally
  116. var name = property.ToString();
  117. if (ReferenceType.IsEnum)
  118. {
  119. Array enumValues = Enum.GetValues(ReferenceType);
  120. Array enumNames = Enum.GetNames(ReferenceType);
  121. for (int i = 0; i < enumValues.Length; i++)
  122. {
  123. if (enumNames.GetValue(i) as string == name)
  124. {
  125. return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
  126. }
  127. }
  128. return PropertyDescriptor.Undefined;
  129. }
  130. var propertyInfo = ReferenceType.GetProperty(name, BindingFlags.Public | BindingFlags.Static);
  131. if (propertyInfo != null)
  132. {
  133. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  134. }
  135. var fieldInfo = ReferenceType.GetField(name, BindingFlags.Public | BindingFlags.Static);
  136. if (fieldInfo != null)
  137. {
  138. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  139. }
  140. List<MethodInfo> methodInfo = null;
  141. foreach (var mi in ReferenceType.GetMethods(BindingFlags.Public | BindingFlags.Static))
  142. {
  143. if (mi.Name == name)
  144. {
  145. methodInfo = methodInfo ?? new List<MethodInfo>();
  146. methodInfo.Add(mi);
  147. }
  148. }
  149. if (methodInfo == null || methodInfo.Count == 0)
  150. {
  151. return PropertyDescriptor.Undefined;
  152. }
  153. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo.ToArray()), PropertyFlag.AllForbidden);
  154. }
  155. public object Target => ReferenceType;
  156. }
  157. }