123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Globalization;
- using System.Reflection;
- using System.Threading;
- using Jint.Native;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop.Reflection;
- namespace Jint.Runtime.Interop
- {
- /// <summary>
- /// Wraps a CLR instance
- /// </summary>
- public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
- {
- private readonly TypeDescriptor _typeDescriptor;
- public ObjectWrapper(Engine engine, object obj)
- : base(engine)
- {
- Target = obj;
- _typeDescriptor = TypeDescriptor.Get(obj.GetType());
- if (_typeDescriptor.IsArrayLike)
- {
- // create a forwarder to produce length from Count or Length if one of them is present
- var lengthProperty = obj.GetType().GetProperty("Count") ?? obj.GetType().GetProperty("Length");
- if (lengthProperty is null)
- {
- return;
- }
- var functionInstance = new ClrFunctionInstance(engine, "length", (_, _) => JsNumber.Create((int) lengthProperty.GetValue(obj)));
- var descriptor = new GetSetPropertyDescriptor(functionInstance, Undefined, PropertyFlag.Configurable);
- SetProperty(KnownKeys.Length, descriptor);
- }
- }
- public object Target { get; }
- public override bool IsArrayLike => _typeDescriptor.IsArrayLike;
- internal override bool IsIntegerIndexedArray => _typeDescriptor.IsIntegerIndexedArray;
- public override bool Set(JsValue property, JsValue value, JsValue receiver)
- {
- // check if we can take shortcuts for empty object, no need to generate properties
- if (property is JsString stringKey)
- {
- var member = stringKey.ToString();
- if (_properties is null || !_properties.ContainsKey(member))
- {
- // can try utilize fast path
- var accessor = GetAccessor(_engine, Target.GetType(), member);
-
- // CanPut logic
- if (!accessor.Writable || !_engine.Options._IsClrWriteAllowed)
- {
- return false;
- }
-
- accessor.SetValue(_engine, Target, value);
- return true;
- }
- }
- return SetSlow(property, value);
- }
- private bool SetSlow(JsValue property, JsValue value)
- {
- if (!CanPut(property))
- {
- return false;
- }
- var ownDesc = GetOwnProperty(property);
- if (ownDesc == null)
- {
- return false;
- }
- ownDesc.Value = value;
- return true;
- }
- public override JsValue Get(JsValue property, JsValue receiver)
- {
- if (property.IsInteger() && Target is IList list)
- {
- var index = (int) ((JsNumber) property)._value;
- return (uint) index < list.Count ? FromObject(_engine, list[index]) : Undefined;
- }
-
- if (property.IsSymbol() && property != GlobalSymbolRegistry.Iterator)
- {
- // wrapped objects cannot have symbol properties
- return Undefined;
- }
- if (property is JsString stringKey)
- {
- var member = stringKey.ToString();
- var result = Engine.Options._MemberAccessor?.Invoke(Engine, Target, member);
- if (result is not null)
- {
- return result;
- }
-
- if (_properties is null || !_properties.ContainsKey(member))
- {
- // can try utilize fast path
- var accessor = GetAccessor(_engine, Target.GetType(), member);
- var value = accessor.GetValue(_engine, Target);
- if (value is not null)
- {
- return FromObject(_engine, value);
- }
- }
- }
- return base.Get(property, receiver);
- }
- public override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
- {
- return new List<JsValue>(EnumerateOwnPropertyKeys(types));
- }
- public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
- {
- foreach (var key in EnumerateOwnPropertyKeys(Types.String | Types.Symbol))
- {
- yield return new KeyValuePair<JsValue, PropertyDescriptor>(key, GetOwnProperty(key));
- }
- }
- private IEnumerable<JsValue> EnumerateOwnPropertyKeys(Types types)
- {
- var basePropertyKeys = base.GetOwnPropertyKeys(types);
- // prefer object order, add possible other properties after
- var processed = basePropertyKeys.Count > 0 ? new HashSet<JsValue>() : null;
- var includeStrings = (types & Types.String) != 0;
- if (Target is IDictionary dictionary && includeStrings)
- {
- // we take values exposed as dictionary keys only
- foreach (var key in dictionary.Keys)
- {
- if (_engine.ClrTypeConverter.TryConvert(key, typeof(string), CultureInfo.InvariantCulture, out var stringKey))
- {
- var jsString = JsString.Create((string) stringKey);
- processed?.Add(jsString);
- yield return jsString;
- }
- }
- }
- else if (includeStrings)
- {
- // we take public properties and fields
- var type = Target.GetType();
- foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- var indexParameters = p.GetIndexParameters();
- if (indexParameters.Length == 0)
- {
- var jsString = JsString.Create(p.Name);
- processed?.Add(jsString);
- yield return jsString;
- }
- }
- foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- var jsString = JsString.Create(f.Name);
- processed?.Add(jsString);
- yield return jsString;
- }
- }
- if (processed != null)
- {
- // we have base keys
- for (var i = 0; i < basePropertyKeys.Count; i++)
- {
- var key = basePropertyKeys[i];
- if (processed.Add(key))
- {
- yield return key;
- }
- }
- }
- }
- public override PropertyDescriptor GetOwnProperty(JsValue property)
- {
- if (TryGetProperty(property, out var x))
- {
- return x;
- }
- if (property.IsSymbol() && property == GlobalSymbolRegistry.Iterator)
- {
- var iteratorFunction = new ClrFunctionInstance(Engine, "iterator", (thisObject, arguments) => _engine.Iterator.Construct(this), 1, PropertyFlag.Configurable);
- var iteratorProperty = new PropertyDescriptor(iteratorFunction, PropertyFlag.Configurable | PropertyFlag.Writable);
- SetProperty(GlobalSymbolRegistry.Iterator, iteratorProperty);
- return iteratorProperty;
- }
- var member = property.ToString();
- var result = Engine.Options._MemberAccessor?.Invoke(Engine, Target, member);
- if (result is not null)
- {
- return new PropertyDescriptor(result, PropertyFlag.OnlyEnumerable);
- }
- var accessor = GetAccessor(_engine, Target.GetType(), member);
- var descriptor = accessor.CreatePropertyDescriptor(_engine, Target);
- SetProperty(member, descriptor);
- return descriptor;
- }
- // need to be public for advanced cases like RavenDB yielding properties from CLR objects
- public static PropertyDescriptor GetPropertyDescriptor(Engine engine, object target, MemberInfo member)
- {
- // fast path which uses slow search if not found for some reason
- ReflectionAccessor Factory()
- {
- return member switch
- {
- PropertyInfo pi => new PropertyAccessor(pi.Name, pi),
- MethodBase mb => new MethodAccessor(MethodDescriptor.Build(new[] {mb})),
- FieldInfo fi => new FieldAccessor(fi),
- _ => null
- };
- }
- return GetAccessor(engine, target.GetType(), member.Name, Factory).CreatePropertyDescriptor(engine, target);
- }
- private static ReflectionAccessor GetAccessor(Engine engine, Type type, string member, Func<ReflectionAccessor> accessorFactory = null)
- {
- var key = new ClrPropertyDescriptorFactoriesKey(type, member);
- var factories = Engine.ReflectionAccessors;
- if (factories.TryGetValue(key, out var accessor))
- {
- return accessor;
- }
- accessor = accessorFactory?.Invoke() ?? ResolvePropertyDescriptorFactory(engine, type, member);
-
- // racy, we don't care, worst case we'll catch up later
- Interlocked.CompareExchange(ref Engine.ReflectionAccessors,
- new Dictionary<ClrPropertyDescriptorFactoriesKey, ReflectionAccessor>(factories)
- {
- [key] = accessor
- }, factories);
- return accessor;
- }
- private static ReflectionAccessor ResolvePropertyDescriptorFactory(Engine engine, Type type, string memberName)
- {
- if (typeof(DynamicObject).IsAssignableFrom(type))
- {
- return new DynamicObjectAccessor(typeof(void), memberName);
- }
-
- var isNumber = uint.TryParse(memberName, out _);
- // we can always check indexer if there's one, and then fall back to properties if indexer returns null
- IndexerAccessor.TryFindIndexer(engine, type, memberName, out var indexerAccessor, out var indexer);
-
- // properties and fields cannot be numbers
- if (!isNumber && TryFindStringPropertyAccessor(type, memberName, indexer, out var temp))
- {
- return temp;
- }
- // if no methods are found check if target implemented indexing
- if (indexerAccessor != null)
- {
- return indexerAccessor;
- }
- // try to find a single explicit property implementation
- List<PropertyInfo> list = null;
- foreach (Type iface in type.GetInterfaces())
- {
- foreach (var iprop in iface.GetProperties())
- {
- if (EqualsIgnoreCasing(iprop.Name, memberName))
- {
- list ??= new List<PropertyInfo>();
- list.Add(iprop);
- }
- }
- }
- if (list?.Count == 1)
- {
- return new PropertyAccessor(memberName, list[0]);
- }
- // try to find explicit method implementations
- List<MethodInfo> explicitMethods = null;
- foreach (Type iface in type.GetInterfaces())
- {
- foreach (var imethod in iface.GetMethods())
- {
- if (EqualsIgnoreCasing(imethod.Name, memberName))
- {
- explicitMethods ??= new List<MethodInfo>();
- explicitMethods.Add(imethod);
- }
- }
- }
- if (explicitMethods?.Count > 0)
- {
- return new MethodAccessor(MethodDescriptor.Build(explicitMethods));
- }
- // try to find explicit indexer implementations
- foreach (var interfaceType in type.GetInterfaces())
- {
- if (IndexerAccessor.TryFindIndexer(engine, interfaceType, memberName, out var accessor, out _))
- {
- return accessor;
- }
- }
- if (engine.Options._extensionMethods.TryGetExtensionMethods(type, out var extensionMethods))
- {
- var matches = new List<MethodInfo>();
- foreach (var method in extensionMethods)
- {
- if (EqualsIgnoreCasing(method.Name, memberName))
- {
- matches.Add(method);
- }
- }
- return new MethodAccessor(MethodDescriptor.Build(matches));
- }
- return ConstantValueAccessor.NullAccessor;
- }
- private static bool TryFindStringPropertyAccessor(
- Type type,
- string memberName,
- PropertyInfo indexerToTry,
- out ReflectionAccessor wrapper)
- {
- // look for a property, bit be wary of indexers, we don't want indexers which have name "Item" to take precedence
- PropertyInfo property = null;
- foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- // only if it's not an indexer, we can do case-ignoring matches
- var isStandardIndexer = p.GetIndexParameters().Length == 1 && p.Name == "Item";
- if (!isStandardIndexer && EqualsIgnoreCasing(p.Name, memberName))
- {
- property = p;
- break;
- }
- }
- if (property != null)
- {
- wrapper = new PropertyAccessor(memberName, property, indexerToTry);
- return true;
- }
- // look for a field
- FieldInfo field = null;
- foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- if (EqualsIgnoreCasing(f.Name, memberName))
- {
- field = f;
- break;
- }
- }
- if (field != null)
- {
- wrapper = new FieldAccessor(field, memberName, indexerToTry);
- return true;
- }
- // if no properties were found then look for a method
- List<MethodInfo> methods = null;
- foreach (var m in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- if (EqualsIgnoreCasing(m.Name, memberName))
- {
- methods ??= new List<MethodInfo>();
- methods.Add(m);
- }
- }
- if (methods?.Count > 0)
- {
- wrapper = new MethodAccessor(MethodDescriptor.Build(methods));
- return true;
- }
- wrapper = default;
- return false;
- }
- private static bool EqualsIgnoreCasing(string s1, string s2)
- {
- if (s1.Length != s2.Length)
- {
- return false;
- }
- var equals = false;
- if (s1.Length > 0)
- {
- equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
- }
- if (@equals && s1.Length > 1)
- {
- #if NETSTANDARD2_1
- equals = s1.AsSpan(1).SequenceEqual(s2.AsSpan(1));
- #else
- equals = s1.Substring(1) == s2.Substring(1);
- #endif
- }
- return equals;
- }
- }
- }
|