TypeReference.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 PropertyDescriptor GetOwnProperty(string propertyName)
  89. {
  90. // todo: cache members locally
  91. if (Type.IsEnum)
  92. {
  93. Array enumValues = Enum.GetValues(Type);
  94. Array enumNames = Enum.GetNames(Type);
  95. for (int i = 0; i < enumValues.Length; i++)
  96. {
  97. if (enumNames.GetValue(i) as string == propertyName)
  98. {
  99. return new PropertyDescriptor((int)enumValues.GetValue(i), false, false, false);
  100. }
  101. }
  102. return PropertyDescriptor.Undefined;
  103. }
  104. var propertyInfo = Type.GetProperty(propertyName);
  105. if (propertyInfo != null)
  106. {
  107. return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
  108. }
  109. var methodInfo = Type
  110. .GetMethods(BindingFlags.Public | BindingFlags.Static)
  111. .Where(mi => mi.Name == propertyName)
  112. .ToArray();
  113. if (methodInfo.Length == 0)
  114. {
  115. return PropertyDescriptor.Undefined;
  116. }
  117. return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo), false, false, false);
  118. }
  119. public object Target
  120. {
  121. get
  122. {
  123. return Type;
  124. }
  125. }
  126. public override string Class
  127. {
  128. get { return "TypeReference"; }
  129. }
  130. }
  131. }