TypeReference.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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(Engine engine, Type type)
  28. {
  29. return new TypeReference(engine, type);
  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, thisObject);
  35. }
  36. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  37. {
  38. if (arguments.Length == 0 && ReferenceType.IsValueType)
  39. {
  40. var instance = Activator.CreateInstance(ReferenceType);
  41. var result = TypeConverter.ToObject(_realm, FromObject(Engine, instance));
  42. return result;
  43. }
  44. var constructors = _constructorCache.GetOrAdd(
  45. ReferenceType,
  46. t => MethodDescriptor.Build(t.GetConstructors(BindingFlags.Public | BindingFlags.Instance)));
  47. foreach (var tuple in TypeConverter.FindBestMatch(constructors, _ => arguments))
  48. {
  49. var method = tuple.Item1;
  50. var retVal = method.Call(Engine, null, arguments);
  51. var result = TypeConverter.ToObject(_realm, retVal);
  52. // todo: cache method info
  53. return result;
  54. }
  55. ExceptionHelper.ThrowTypeError(_engine.Realm, "No public methods with the specified arguments were found.");
  56. return null;
  57. }
  58. internal override bool OrdinaryHasInstance(JsValue v)
  59. {
  60. if (v is IObjectWrapper wrapper)
  61. {
  62. return wrapper.Target.GetType() == ReferenceType;
  63. }
  64. return base.OrdinaryHasInstance(v);
  65. }
  66. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  67. {
  68. return false;
  69. }
  70. public override bool Delete(JsValue property)
  71. {
  72. return false;
  73. }
  74. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  75. {
  76. if (!CanPut(property))
  77. {
  78. return false;
  79. }
  80. var ownDesc = GetOwnProperty(property);
  81. if (ownDesc == null)
  82. {
  83. return false;
  84. }
  85. ownDesc.Value = value;
  86. return true;
  87. }
  88. public override PropertyDescriptor GetOwnProperty(JsValue property)
  89. {
  90. if (property is not JsString jsString)
  91. {
  92. return PropertyDescriptor.Undefined;
  93. }
  94. var key = jsString._value;
  95. var descriptor = PropertyDescriptor.Undefined;
  96. if (_properties?.TryGetValue(key, out descriptor) != true)
  97. {
  98. descriptor = CreatePropertyDescriptor(key);
  99. _properties ??= new PropertyDictionary();
  100. _properties[key] = descriptor;
  101. }
  102. return descriptor;
  103. }
  104. private PropertyDescriptor CreatePropertyDescriptor(string name)
  105. {
  106. var key = new Tuple<Type, string>(ReferenceType, name);
  107. var accessor = _memberAccessors.GetOrAdd(key, x => ResolveMemberAccessor(x.Item1, x.Item2));
  108. return accessor.CreatePropertyDescriptor(_engine, ReferenceType);
  109. }
  110. private ReflectionAccessor ResolveMemberAccessor(Type type, string name)
  111. {
  112. var typeResolver = _engine.Options.Interop.TypeResolver;
  113. if (type.IsEnum)
  114. {
  115. var memberNameComparer = typeResolver.MemberNameComparer;
  116. var typeResolverMemberNameCreator = typeResolver.MemberNameCreator;
  117. var enumValues = Enum.GetValues(type);
  118. var enumNames = Enum.GetNames(type);
  119. for (var i = 0; i < enumValues.Length; i++)
  120. {
  121. var enumOriginalName = enumNames.GetValue(i).ToString();
  122. var member = type.GetMember(enumOriginalName)[0];
  123. foreach (var exposedName in typeResolverMemberNameCreator(member))
  124. {
  125. if (memberNameComparer.Equals(name, exposedName))
  126. {
  127. var value = enumValues.GetValue(i);
  128. return new ConstantValueAccessor(JsNumber.Create(value));
  129. }
  130. }
  131. }
  132. return ConstantValueAccessor.NullAccessor;
  133. }
  134. const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
  135. return typeResolver.TryFindMemberAccessor(_engine, type, name, bindingFlags, indexerToTry: null, out var accessor)
  136. ? accessor
  137. : ConstantValueAccessor.NullAccessor;
  138. }
  139. public object Target => ReferenceType;
  140. }
  141. }