TypeReference.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Reflection;
  4. using Jint.Collections;
  5. using Jint.Native;
  6. using Jint.Native.Function;
  7. using Jint.Native.Object;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Interop.Reflection;
  10. namespace Jint.Runtime.Interop
  11. {
  12. public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
  13. {
  14. private static readonly JsString _name = new JsString("typereference");
  15. private static readonly ConcurrentDictionary<Type, MethodDescriptor[]> _constructorCache = new();
  16. private static readonly ConcurrentDictionary<Tuple<Type, string>, ReflectionAccessor> _memberAccessors = new();
  17. private TypeReference(Engine engine, Type type)
  18. : base(engine, engine.Realm, _name, FunctionThisMode.Global, ObjectClass.TypeReference)
  19. {
  20. ReferenceType = type;
  21. _prototype = engine.Realm.Intrinsics.Function.PrototypeObject;
  22. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  23. _prototypeDescriptor = new PropertyDescriptor(engine.Realm.Intrinsics.Object.PrototypeObject, PropertyFlag.AllForbidden);
  24. PreventExtensions();
  25. }
  26. public Type ReferenceType { get; }
  27. public static TypeReference CreateTypeReference<T>(Engine engine)
  28. {
  29. return CreateTypeReference(engine, typeof(T));
  30. }
  31. public static TypeReference CreateTypeReference(Engine engine, Type type)
  32. {
  33. return new TypeReference(engine, type);
  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);
  39. }
  40. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments);
  41. private ObjectInstance Construct(JsValue[] arguments)
  42. {
  43. if (arguments.Length == 0 && ReferenceType.IsValueType)
  44. {
  45. var instance = Activator.CreateInstance(ReferenceType);
  46. var result = TypeConverter.ToObject(_realm, FromObject(Engine, instance));
  47. return result;
  48. }
  49. var constructors = _constructorCache.GetOrAdd(
  50. ReferenceType,
  51. t => MethodDescriptor.Build(t.GetConstructors(BindingFlags.Public | BindingFlags.Instance)));
  52. foreach (var (method, _, _) in TypeConverter.FindBestMatch(_engine, constructors, _ => arguments))
  53. {
  54. var retVal = method.Call(Engine, null, arguments);
  55. var result = TypeConverter.ToObject(_realm, retVal);
  56. // todo: cache method info
  57. return result;
  58. }
  59. ExceptionHelper.ThrowTypeError(_engine.Realm, "No public methods with the specified arguments were found.");
  60. return null;
  61. }
  62. internal override bool OrdinaryHasInstance(JsValue v)
  63. {
  64. if (v is IObjectWrapper wrapper)
  65. {
  66. return wrapper.Target.GetType() == ReferenceType;
  67. }
  68. return base.OrdinaryHasInstance(v);
  69. }
  70. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  71. {
  72. return false;
  73. }
  74. public override bool Delete(JsValue property)
  75. {
  76. return false;
  77. }
  78. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  79. {
  80. if (!CanPut(property))
  81. {
  82. return false;
  83. }
  84. var ownDesc = GetOwnProperty(property);
  85. if (ownDesc == null)
  86. {
  87. return false;
  88. }
  89. ownDesc.Value = value;
  90. return true;
  91. }
  92. public override PropertyDescriptor GetOwnProperty(JsValue property)
  93. {
  94. if (property is not JsString jsString)
  95. {
  96. return PropertyDescriptor.Undefined;
  97. }
  98. var key = jsString._value;
  99. var descriptor = PropertyDescriptor.Undefined;
  100. if (_properties?.TryGetValue(key, out descriptor) != true)
  101. {
  102. descriptor = CreatePropertyDescriptor(key);
  103. _properties ??= new PropertyDictionary();
  104. _properties[key] = descriptor;
  105. }
  106. return descriptor;
  107. }
  108. private PropertyDescriptor CreatePropertyDescriptor(string name)
  109. {
  110. var key = new Tuple<Type, string>(ReferenceType, name);
  111. var accessor = _memberAccessors.GetOrAdd(key, x => ResolveMemberAccessor(x.Item1, x.Item2));
  112. return accessor.CreatePropertyDescriptor(_engine, ReferenceType);
  113. }
  114. private ReflectionAccessor ResolveMemberAccessor(Type type, string name)
  115. {
  116. var typeResolver = _engine.Options.Interop.TypeResolver;
  117. if (type.IsEnum)
  118. {
  119. var memberNameComparer = typeResolver.MemberNameComparer;
  120. var typeResolverMemberNameCreator = typeResolver.MemberNameCreator;
  121. var enumValues = Enum.GetValues(type);
  122. var enumNames = Enum.GetNames(type);
  123. for (var i = 0; i < enumValues.Length; i++)
  124. {
  125. var enumOriginalName = enumNames.GetValue(i).ToString();
  126. var member = type.GetMember(enumOriginalName)[0];
  127. foreach (var exposedName in typeResolverMemberNameCreator(member))
  128. {
  129. if (memberNameComparer.Equals(name, exposedName))
  130. {
  131. var value = enumValues.GetValue(i);
  132. return new ConstantValueAccessor(JsNumber.Create(value));
  133. }
  134. }
  135. }
  136. return ConstantValueAccessor.NullAccessor;
  137. }
  138. const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
  139. return typeResolver.TryFindMemberAccessor(_engine, type, name, bindingFlags, indexerToTry: null, out var accessor)
  140. ? accessor
  141. : ConstantValueAccessor.NullAccessor;
  142. }
  143. public object Target => ReferenceType;
  144. }
  145. }