TypeReference.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, 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(_realm, 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 (method, _, _) in TypeConverter.FindBestMatch(_engine, constructors, _ => arguments))
  52. {
  53. var retVal = method.Call(Engine, null, arguments);
  54. var result = TypeConverter.ToObject(_realm, retVal);
  55. // todo: cache method info
  56. return result;
  57. }
  58. ExceptionHelper.ThrowTypeError(_engine.Realm, "No public methods with the specified arguments were found.");
  59. return null;
  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 key = new Tuple<Type, string>(ReferenceType, name);
  110. var accessor = _memberAccessors.GetOrAdd(key, x => ResolveMemberAccessor(x.Item1, x.Item2));
  111. return accessor.CreatePropertyDescriptor(_engine, ReferenceType);
  112. }
  113. private ReflectionAccessor ResolveMemberAccessor(Type type, string name)
  114. {
  115. var typeResolver = _engine.Options.Interop.TypeResolver;
  116. if (type.IsEnum)
  117. {
  118. var memberNameComparer = typeResolver.MemberNameComparer;
  119. var typeResolverMemberNameCreator = typeResolver.MemberNameCreator;
  120. var enumValues = Enum.GetValues(type);
  121. var enumNames = Enum.GetNames(type);
  122. for (var i = 0; i < enumValues.Length; i++)
  123. {
  124. var enumOriginalName = enumNames.GetValue(i).ToString();
  125. var member = type.GetMember(enumOriginalName)[0];
  126. foreach (var exposedName in typeResolverMemberNameCreator(member))
  127. {
  128. if (memberNameComparer.Equals(name, exposedName))
  129. {
  130. var value = enumValues.GetValue(i);
  131. return new ConstantValueAccessor(JsNumber.Create(value));
  132. }
  133. }
  134. }
  135. return ConstantValueAccessor.NullAccessor;
  136. }
  137. const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
  138. return typeResolver.TryFindMemberAccessor(_engine, type, name, bindingFlags, indexerToTry: null, out var accessor)
  139. ? accessor
  140. : ConstantValueAccessor.NullAccessor;
  141. }
  142. public object Target => ReferenceType;
  143. }
  144. }