TypeReference.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
  13. {
  14. private TypeReference(Engine engine)
  15. : base(engine, null, null, false)
  16. {
  17. }
  18. public Type Type { get; set; }
  19. public static TypeReference CreateTypeReference(Engine engine, Type type)
  20. {
  21. var obj = new TypeReference(engine);
  22. obj.Extensible = false;
  23. obj.Type = 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.FastAddProperty("length", 0, false, false, false);
  27. // The initial value of Boolean.prototype is the Boolean prototype object
  28. obj.FastAddProperty("prototype", engine.Object.PrototypeObject, false, false, false);
  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 && Type.IsValueType())
  39. {
  40. var instance = Activator.CreateInstance(Type);
  41. var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
  42. return result;
  43. }
  44. var constructors = Type.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 (parameterType == typeof(JsValue))
  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. throw new JavaScriptException(Engine.TypeError, "No public methods with the specified arguments were found.");
  78. }
  79. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  80. {
  81. if (throwOnError)
  82. {
  83. throw new JavaScriptException(Engine.TypeError, "Can't define a property of a TypeReference");
  84. }
  85. return false;
  86. }
  87. public override bool Delete(string propertyName, bool throwOnError)
  88. {
  89. if (throwOnError)
  90. {
  91. throw new JavaScriptException(Engine.TypeError, "Can't delete a property of a TypeReference");
  92. }
  93. return false;
  94. }
  95. public override void Put(string propertyName, JsValue value, bool throwOnError)
  96. {
  97. if (!CanPut(propertyName))
  98. {
  99. if (throwOnError)
  100. {
  101. throw new JavaScriptException(Engine.TypeError);
  102. }
  103. return;
  104. }
  105. var ownDesc = GetOwnProperty(propertyName);
  106. if (ownDesc == null)
  107. {
  108. if (throwOnError)
  109. {
  110. throw new JavaScriptException(Engine.TypeError, "Unknown member: " + propertyName);
  111. }
  112. else
  113. {
  114. return;
  115. }
  116. }
  117. ownDesc.Value = value;
  118. }
  119. public override PropertyDescriptor GetOwnProperty(string propertyName)
  120. {
  121. // todo: cache members locally
  122. if (Type.IsEnum())
  123. {
  124. Array enumValues = Enum.GetValues(Type);
  125. Array enumNames = Enum.GetNames(Type);
  126. for (int i = 0; i < enumValues.Length; i++)
  127. {
  128. if (enumNames.GetValue(i) as string == propertyName)
  129. {
  130. return new PropertyDescriptor((int)enumValues.GetValue(i), false, false, false);
  131. }
  132. }
  133. return PropertyDescriptor.Undefined;
  134. }
  135. var propertyInfo = Type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
  136. if (propertyInfo != null)
  137. {
  138. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  139. }
  140. var fieldInfo = Type.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
  141. if (fieldInfo != null)
  142. {
  143. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  144. }
  145. var methodInfo = Type
  146. .GetMethods(BindingFlags.Public | BindingFlags.Static)
  147. .Where(mi => mi.Name == propertyName)
  148. .ToArray();
  149. if (methodInfo.Length == 0)
  150. {
  151. return PropertyDescriptor.Undefined;
  152. }
  153. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo), false, false, false);
  154. }
  155. public object Target
  156. {
  157. get
  158. {
  159. return Type;
  160. }
  161. }
  162. public override string Class
  163. {
  164. get { return "TypeReference"; }
  165. }
  166. }
  167. }