123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Reflection;
- using Jint.Native;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- namespace Jint.Runtime.Interop
- {
- /// <summary>
- /// Wraps a CLR instance
- /// </summary>
- public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
- {
- public ObjectWrapper(Engine engine, object obj)
- : base(engine)
- {
- Target = obj;
- var type = obj.GetType();
- if (ObjectIsArrayLikeClrCollection(type))
- {
- // create a forwarder to produce length from Count or Length if one of them is present
- var lengthProperty = type.GetProperty("Count") ?? type.GetProperty("Length");
- if (lengthProperty is null)
- {
- return;
- }
- IsArrayLike = true;
- var functionInstance = new ClrFunctionInstance(engine, "length", (thisObj, arguments) => JsNumber.Create((int) lengthProperty.GetValue(obj)));
- var descriptor = new GetSetPropertyDescriptor(functionInstance, Undefined, PropertyFlag.Configurable);
- SetProperty(KnownKeys.Length, descriptor);
- }
- }
- private static bool ObjectIsArrayLikeClrCollection(Type type)
- {
- if (typeof(ICollection).IsAssignableFrom(type))
- {
- return true;
- }
-
- foreach (var interfaceType in type.GetInterfaces())
- {
- if (!interfaceType.IsGenericType)
- {
- continue;
- }
-
- if (interfaceType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>)
- || interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
- {
- return true;
- }
- }
- return false;
- }
- public object Target { get; }
- public override bool IsArrayLike { get; }
- public override bool Set(JsValue property, JsValue value, JsValue receiver)
- {
- if (!CanPut(property))
- {
- return false;
- }
- var ownDesc = GetOwnProperty(property);
- if (ownDesc == null)
- {
- return false;
- }
- ownDesc.Value = value;
- return true;
- }
- public override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
- {
- var propertyKeys = base.GetOwnPropertyKeys(types);
-
- if (Target is IDictionary dictionary)
- {
- // 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))
- {
- propertyKeys.Add(JsString.Create((string) stringKey));
- }
- }
- }
- else
- {
- // 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)
- {
- propertyKeys.Add(p.Name);
- }
- }
-
- foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- propertyKeys.Add(f.Name);
- }
- }
- return propertyKeys;
- }
- 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 type = Target.GetType();
- var key = new Engine.ClrPropertyDescriptorFactoriesKey(type, property.ToString());
- if (!_engine.ClrPropertyDescriptorFactories.TryGetValue(key, out var factory))
- {
- factory = ResolveProperty(type, property.ToString());
- _engine.ClrPropertyDescriptorFactories[key] = factory;
- }
- var descriptor = factory(_engine, Target);
- AddProperty(property, descriptor);
- return descriptor;
- }
-
- private Func<Engine, object, PropertyDescriptor> ResolveProperty(Type type, string propertyName)
- {
- var isNumber = uint.TryParse(propertyName, out _);
- // properties and fields cannot be numbers
- if (!isNumber)
- {
- // look for a property
- PropertyInfo property = null;
- foreach (var p in type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- if (EqualsIgnoreCasing(p.Name, propertyName))
- {
- property = p;
- break;
- }
- }
- if (property != null)
- {
- return (engine, target) => new PropertyInfoDescriptor(engine, property, target);
- }
- // look for a field
- FieldInfo field = null;
- foreach (var f in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
- {
- if (EqualsIgnoreCasing(f.Name, propertyName))
- {
- field = f;
- break;
- }
- }
- if (field != null)
- {
- return (engine, target) => new FieldInfoDescriptor(engine, field, target);
- }
-
- // 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, propertyName))
- {
- methods ??= new List<MethodInfo>();
- methods.Add(m);
- }
- }
- if (methods?.Count > 0)
- {
- return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, methods.ToArray()), PropertyFlag.OnlyEnumerable);
- }
- }
- // if no methods are found check if target implemented indexing
- if (IndexDescriptor.TryFindIndexer(_engine, type, propertyName, out _, out _, out _))
- {
- return (engine, target) => new IndexDescriptor(engine, propertyName, target);
- }
- // 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, propertyName))
- {
- list ??= new List<PropertyInfo>();
- list.Add(iprop);
- }
- }
- }
- if (list?.Count == 1)
- {
- return (engine, target) => new PropertyInfoDescriptor(engine, list[0], target);
- }
- // 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, propertyName))
- {
- explicitMethods ??= new List<MethodInfo>();
- explicitMethods.Add(imethod);
- }
- }
- }
- if (explicitMethods?.Count > 0)
- {
- return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, explicitMethods.ToArray()), PropertyFlag.OnlyEnumerable);
- }
- // try to find explicit indexer implementations
- foreach (Type interfaceType in type.GetInterfaces())
- {
- if (IndexDescriptor.TryFindIndexer(_engine, interfaceType, propertyName, out _, out _, out _))
- {
- return (engine, target) => new IndexDescriptor(engine, interfaceType, propertyName, target);
- }
- }
- return (engine, target) => PropertyDescriptor.Undefined;
- }
- private static bool EqualsIgnoreCasing(string s1, string s2)
- {
- bool equals = false;
- if (s1.Length == s2.Length)
- {
- if (s1.Length > 0)
- {
- equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
- }
- if (equals && s1.Length > 1)
- {
- equals = s1.Substring(1) == s2.Substring(1);
- }
- }
- return equals;
- }
- }
- }
|