TypeReference.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Jint.Collections;
  6. using Jint.Native;
  7. using Jint.Native.Function;
  8. using Jint.Native.Object;
  9. using Jint.Runtime.Descriptors;
  10. using Jint.Runtime.Interop.Reflection;
  11. namespace Jint.Runtime.Interop
  12. {
  13. public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
  14. {
  15. private static readonly JsString _name = new JsString("typereference");
  16. private static readonly ConcurrentDictionary<Type, MethodDescriptor[]> _constructorCache = new();
  17. private static readonly ConcurrentDictionary<Tuple<Type, string>, ReflectionAccessor> _memberAccessors = new();
  18. private TypeReference(Engine engine)
  19. : base(engine, _name, FunctionThisMode.Global, ObjectClass.TypeReference)
  20. {
  21. }
  22. public Type ReferenceType { get; set; }
  23. public static TypeReference CreateTypeReference(Engine engine, Type type)
  24. {
  25. var obj = new TypeReference(engine);
  26. obj.PreventExtensions();
  27. obj.ReferenceType = type;
  28. // The value of the [[Prototype]] internal property of the TypeReference constructor is the Function prototype object
  29. obj._prototype = engine.Function.PrototypeObject;
  30. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  31. // The initial value of Boolean.prototype is the Boolean prototype object
  32. obj._prototypeDescriptor = new PropertyDescriptor(engine.Object.PrototypeObject, PropertyFlag.AllForbidden);
  33. return obj;
  34. }
  35. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  36. {
  37. // direct calls on a TypeReference constructor object is equivalent to the new operator
  38. return Construct(arguments, thisObject);
  39. }
  40. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  41. {
  42. if (arguments.Length == 0 && ReferenceType.IsValueType)
  43. {
  44. var instance = Activator.CreateInstance(ReferenceType);
  45. var result = TypeConverter.ToObject(Engine, FromObject(Engine, instance));
  46. return result;
  47. }
  48. var constructors = _constructorCache.GetOrAdd(
  49. ReferenceType,
  50. t => MethodDescriptor.Build(t.GetConstructors(BindingFlags.Public | BindingFlags.Instance)));
  51. foreach (var tuple in TypeConverter.FindBestMatch(_engine, constructors, _ => arguments))
  52. {
  53. var method = tuple.Item1;
  54. var retVal = method.Call(Engine, null, arguments);
  55. var result = TypeConverter.ToObject(Engine, retVal);
  56. // todo: cache method info
  57. return result;
  58. }
  59. return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine, "No public methods with the specified arguments were found.");
  60. }
  61. internal override bool OrdinaryHasInstance(JsValue v)
  62. {
  63. if (v is IObjectWrapper wrapper)
  64. {
  65. return wrapper.Target.GetType() == ReferenceType;
  66. }
  67. return base.OrdinaryHasInstance(v);
  68. }
  69. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  70. {
  71. return false;
  72. }
  73. public override bool Delete(JsValue property)
  74. {
  75. return false;
  76. }
  77. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  78. {
  79. if (!CanPut(property))
  80. {
  81. return false;
  82. }
  83. var ownDesc = GetOwnProperty(property);
  84. if (ownDesc == null)
  85. {
  86. return false;
  87. }
  88. ownDesc.Value = value;
  89. return true;
  90. }
  91. public override PropertyDescriptor GetOwnProperty(JsValue property)
  92. {
  93. if (property is not JsString jsString)
  94. {
  95. return PropertyDescriptor.Undefined;
  96. }
  97. var key = jsString._value;
  98. var descriptor = PropertyDescriptor.Undefined;
  99. if (_properties?.TryGetValue(key, out descriptor) != true)
  100. {
  101. descriptor = CreatePropertyDescriptor(key);
  102. _properties ??= new PropertyDictionary();
  103. _properties[key] = descriptor;
  104. }
  105. return descriptor;
  106. }
  107. private PropertyDescriptor CreatePropertyDescriptor(string name)
  108. {
  109. var accessor = _memberAccessors.GetOrAdd(
  110. new Tuple<Type, string>(ReferenceType, name),
  111. key => ResolveMemberAccessor(key.Item1, key.Item2)
  112. );
  113. return accessor.CreatePropertyDescriptor(_engine, ReferenceType);
  114. }
  115. private static ReflectionAccessor ResolveMemberAccessor(Type type, string name)
  116. {
  117. if (type.IsEnum)
  118. {
  119. var enumValues = Enum.GetValues(type);
  120. var enumNames = Enum.GetNames(type);
  121. for (var i = 0; i < enumValues.Length; i++)
  122. {
  123. if (enumNames.GetValue(i) as string == name)
  124. {
  125. var value = enumValues.GetValue(i);
  126. return new ConstantValueAccessor(JsNumber.Create(value));
  127. }
  128. }
  129. return ConstantValueAccessor.NullAccessor;
  130. }
  131. var propertyInfo = type.GetProperty(name, BindingFlags.Public | BindingFlags.Static);
  132. if (propertyInfo != null)
  133. {
  134. return new PropertyAccessor(name, propertyInfo);
  135. }
  136. var fieldInfo = type.GetField(name, BindingFlags.Public | BindingFlags.Static);
  137. if (fieldInfo != null)
  138. {
  139. return new FieldAccessor(fieldInfo, name);
  140. }
  141. List<MethodInfo> methods = null;
  142. foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
  143. {
  144. if (mi.Name != name)
  145. {
  146. continue;
  147. }
  148. methods ??= new List<MethodInfo>();
  149. methods.Add(mi);
  150. }
  151. if (methods == null || methods.Count == 0)
  152. {
  153. return ConstantValueAccessor.NullAccessor;
  154. }
  155. return new MethodAccessor(MethodDescriptor.Build(methods));
  156. }
  157. public object Target => ReferenceType;
  158. }
  159. }