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(
  18. Engine engine,
  19. Realm realm)
  20. : base(engine, realm, _name, FunctionThisMode.Global, ObjectClass.TypeReference)
  21. {
  22. }
  23. public Type ReferenceType { get; private set; }
  24. public static TypeReference CreateTypeReference(Engine engine, Type type)
  25. {
  26. var obj = new TypeReference(engine, engine.Realm);
  27. obj.PreventExtensions();
  28. obj.ReferenceType = type;
  29. // The value of the [[Prototype]] internal property of the TypeReference constructor is the Function prototype object
  30. obj._prototype = engine.Realm.Intrinsics.Function.PrototypeObject;
  31. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  32. // The initial value of Boolean.prototype is the Boolean prototype object
  33. obj._prototypeDescriptor = new PropertyDescriptor(engine.Realm.Intrinsics.Object.PrototypeObject, PropertyFlag.AllForbidden);
  34. return obj;
  35. }
  36. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  37. {
  38. // direct calls on a TypeReference constructor object is equivalent to the new operator
  39. return Construct(arguments, thisObject);
  40. }
  41. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  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 tuple in TypeConverter.FindBestMatch(_engine, constructors, _ => arguments))
  53. {
  54. var method = tuple.Item1;
  55. var retVal = method.Call(Engine, null, arguments);
  56. var result = TypeConverter.ToObject(_realm, retVal);
  57. // todo: cache method info
  58. return result;
  59. }
  60. ExceptionHelper.ThrowTypeError(_engine.Realm, "No public methods with the specified arguments were found.");
  61. return null;
  62. }
  63. internal override bool OrdinaryHasInstance(JsValue v)
  64. {
  65. if (v is IObjectWrapper wrapper)
  66. {
  67. return wrapper.Target.GetType() == ReferenceType;
  68. }
  69. return base.OrdinaryHasInstance(v);
  70. }
  71. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  72. {
  73. return false;
  74. }
  75. public override bool Delete(JsValue property)
  76. {
  77. return false;
  78. }
  79. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  80. {
  81. if (!CanPut(property))
  82. {
  83. return false;
  84. }
  85. var ownDesc = GetOwnProperty(property);
  86. if (ownDesc == null)
  87. {
  88. return false;
  89. }
  90. ownDesc.Value = value;
  91. return true;
  92. }
  93. public override PropertyDescriptor GetOwnProperty(JsValue property)
  94. {
  95. if (property is not JsString jsString)
  96. {
  97. return PropertyDescriptor.Undefined;
  98. }
  99. var key = jsString._value;
  100. var descriptor = PropertyDescriptor.Undefined;
  101. if (_properties?.TryGetValue(key, out descriptor) != true)
  102. {
  103. descriptor = CreatePropertyDescriptor(key);
  104. _properties ??= new PropertyDictionary();
  105. _properties[key] = descriptor;
  106. }
  107. return descriptor;
  108. }
  109. private PropertyDescriptor CreatePropertyDescriptor(string name)
  110. {
  111. var key = new Tuple<Type, string>(ReferenceType, name);
  112. var accessor = _memberAccessors.GetOrAdd(key, x => ResolveMemberAccessor(x.Item1, x.Item2));
  113. return accessor.CreatePropertyDescriptor(_engine, ReferenceType);
  114. }
  115. private ReflectionAccessor ResolveMemberAccessor(Type type, string name)
  116. {
  117. var typeResolver = _engine.Options._TypeResolver;
  118. if (type.IsEnum)
  119. {
  120. var memberNameComparer = typeResolver.MemberNameComparer;
  121. var enumValues = Enum.GetValues(type);
  122. var enumNames = Enum.GetNames(type);
  123. for (var i = 0; i < enumValues.Length; i++)
  124. {
  125. if (memberNameComparer.Equals(enumNames.GetValue(i), name))
  126. {
  127. var value = enumValues.GetValue(i);
  128. return new ConstantValueAccessor(JsNumber.Create(value));
  129. }
  130. }
  131. return ConstantValueAccessor.NullAccessor;
  132. }
  133. const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
  134. return typeResolver.TryFindMemberAccessor(type, name, bindingFlags, indexerToTry: null, out var accessor)
  135. ? accessor
  136. : ConstantValueAccessor.NullAccessor;
  137. }
  138. public object Target => ReferenceType;
  139. }
  140. }