TypeReference.cs 6.2 KB

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