TypeReference.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 HasInstance(JsValue v)
  80. {
  81. ObjectWrapper wrapper = v.As<ObjectWrapper>();
  82. if (wrapper == null)
  83. {
  84. return base.HasInstance(v);
  85. }
  86. return wrapper.Target.GetType() == this.Type;
  87. }
  88. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  89. {
  90. if (throwOnError)
  91. {
  92. throw new JavaScriptException(Engine.TypeError, "Can't define a property of a TypeReference");
  93. }
  94. return false;
  95. }
  96. public override bool Delete(string propertyName, bool throwOnError)
  97. {
  98. if (throwOnError)
  99. {
  100. throw new JavaScriptException(Engine.TypeError, "Can't delete a property of a TypeReference");
  101. }
  102. return false;
  103. }
  104. public override void Put(string propertyName, JsValue value, bool throwOnError)
  105. {
  106. if (!CanPut(propertyName))
  107. {
  108. if (throwOnError)
  109. {
  110. throw new JavaScriptException(Engine.TypeError);
  111. }
  112. return;
  113. }
  114. var ownDesc = GetOwnProperty(propertyName);
  115. if (ownDesc == null)
  116. {
  117. if (throwOnError)
  118. {
  119. throw new JavaScriptException(Engine.TypeError, "Unknown member: " + propertyName);
  120. }
  121. else
  122. {
  123. return;
  124. }
  125. }
  126. ownDesc.Value = value;
  127. }
  128. public override PropertyDescriptor GetOwnProperty(string propertyName)
  129. {
  130. // todo: cache members locally
  131. if (Type.IsEnum())
  132. {
  133. Array enumValues = Enum.GetValues(Type);
  134. Array enumNames = Enum.GetNames(Type);
  135. for (int i = 0; i < enumValues.Length; i++)
  136. {
  137. if (enumNames.GetValue(i) as string == propertyName)
  138. {
  139. return new PropertyDescriptor((int)enumValues.GetValue(i), false, false, false);
  140. }
  141. }
  142. return PropertyDescriptor.Undefined;
  143. }
  144. var propertyInfo = Type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
  145. if (propertyInfo != null)
  146. {
  147. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  148. }
  149. var fieldInfo = Type.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
  150. if (fieldInfo != null)
  151. {
  152. return new FieldInfoDescriptor(Engine, fieldInfo, Type);
  153. }
  154. var methodInfo = Type
  155. .GetMethods(BindingFlags.Public | BindingFlags.Static)
  156. .Where(mi => mi.Name == propertyName)
  157. .ToArray();
  158. if (methodInfo.Length == 0)
  159. {
  160. return PropertyDescriptor.Undefined;
  161. }
  162. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo), false, false, false);
  163. }
  164. public object Target
  165. {
  166. get
  167. {
  168. return Type;
  169. }
  170. }
  171. public override string Class
  172. {
  173. get { return "TypeReference"; }
  174. }
  175. }
  176. }