|
@@ -1,5 +1,5 @@
|
|
|
using System;
|
|
|
-using System.Linq;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.Reflection;
|
|
|
using Jint.Native;
|
|
|
using Jint.Native.Object;
|
|
@@ -13,14 +13,14 @@ namespace Jint.Runtime.Interop
|
|
|
/// </summary>
|
|
|
public sealed class ObjectWrapper : ObjectInstance, IObjectWrapper
|
|
|
{
|
|
|
- public Object Target { get; set; }
|
|
|
-
|
|
|
- public ObjectWrapper(Engine engine, Object obj)
|
|
|
+ public ObjectWrapper(Engine engine, object obj)
|
|
|
: base(engine)
|
|
|
{
|
|
|
Target = obj;
|
|
|
}
|
|
|
|
|
|
+ public object Target { get; }
|
|
|
+
|
|
|
public override void Put(string propertyName, JsValue value, bool throwOnError)
|
|
|
{
|
|
|
if (!CanPut(propertyName))
|
|
@@ -41,103 +41,158 @@ namespace Jint.Runtime.Interop
|
|
|
{
|
|
|
ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
- ownDesc.Value = value;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ownDesc.Value = value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public override PropertyDescriptor GetOwnProperty(string propertyName)
|
|
|
{
|
|
|
if (TryGetProperty(propertyName, out var x))
|
|
|
+ {
|
|
|
return x;
|
|
|
+ }
|
|
|
|
|
|
var type = Target.GetType();
|
|
|
+ var key = (type, propertyName);
|
|
|
+
|
|
|
+ if (!_engine.ClrPropertyDescriptorFactories.TryGetValue(key, out var factory))
|
|
|
+ {
|
|
|
+ factory = ResolveProperty(type, propertyName);
|
|
|
+ _engine.ClrPropertyDescriptorFactories[key] = factory;
|
|
|
+ }
|
|
|
|
|
|
+ var descriptor = factory(_engine, Target);
|
|
|
+ AddProperty(propertyName, descriptor);
|
|
|
+ return descriptor;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Func<Engine, object, PropertyDescriptor> ResolveProperty(Type type, string propertyName)
|
|
|
+ {
|
|
|
// look for a property
|
|
|
- var property = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
|
|
|
- .Where(p => EqualsIgnoreCasing(p.Name, propertyName))
|
|
|
- .FirstOrDefault();
|
|
|
+ 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)
|
|
|
{
|
|
|
- var descriptor = new PropertyInfoDescriptor(Engine, property, Target);
|
|
|
- AddProperty(propertyName, descriptor);
|
|
|
- return descriptor;
|
|
|
+ return (engine, target) => new PropertyInfoDescriptor(engine, property, target);
|
|
|
}
|
|
|
|
|
|
// look for a field
|
|
|
- var field = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
|
|
|
- .Where(f => EqualsIgnoreCasing(f.Name, propertyName))
|
|
|
- .FirstOrDefault();
|
|
|
+ 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)
|
|
|
{
|
|
|
- var descriptor = new FieldInfoDescriptor(Engine, field, Target);
|
|
|
- AddProperty(propertyName, descriptor);
|
|
|
- return descriptor;
|
|
|
+ return (engine, target) => new FieldInfoDescriptor(engine, field, target);
|
|
|
}
|
|
|
|
|
|
// if no properties were found then look for a method
|
|
|
- var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public)
|
|
|
- .Where(m => EqualsIgnoreCasing(m.Name, propertyName))
|
|
|
- .ToArray();
|
|
|
+ List<MethodInfo> methods = null;
|
|
|
+ foreach (var m in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
|
|
|
+ {
|
|
|
+ if (EqualsIgnoreCasing(m.Name, propertyName))
|
|
|
+ {
|
|
|
+ methods = methods ?? new List<MethodInfo>();
|
|
|
+ methods.Add(m);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (methods.Any())
|
|
|
+ if (methods?.Count > 0)
|
|
|
{
|
|
|
- var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methods), PropertyFlag.OnlyEnumerable);
|
|
|
- AddProperty(propertyName, descriptor);
|
|
|
- return descriptor;
|
|
|
+ return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, methods.ToArray()), PropertyFlag.OnlyEnumerable);
|
|
|
}
|
|
|
|
|
|
// if no methods are found check if target implemented indexing
|
|
|
- if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
|
|
|
+ PropertyInfo first = null;
|
|
|
+ foreach (var p in type.GetProperties())
|
|
|
{
|
|
|
- return new IndexDescriptor(Engine, propertyName, Target);
|
|
|
+ if (p.GetIndexParameters().Length != 0)
|
|
|
+ {
|
|
|
+ first = p;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- var interfaces = type.GetInterfaces();
|
|
|
+ if (first != null)
|
|
|
+ {
|
|
|
+ return (engine, target) => new IndexDescriptor(engine, propertyName, target);
|
|
|
+ }
|
|
|
|
|
|
// try to find a single explicit property implementation
|
|
|
- var explicitProperties = (from iface in interfaces
|
|
|
- from iprop in iface.GetProperties()
|
|
|
- where EqualsIgnoreCasing(iprop.Name, propertyName)
|
|
|
- select iprop).ToArray();
|
|
|
+ List<PropertyInfo> list = null;
|
|
|
+ foreach (Type iface in type.GetInterfaces())
|
|
|
+ {
|
|
|
+ foreach (var iprop in iface.GetProperties())
|
|
|
+ {
|
|
|
+ if (EqualsIgnoreCasing(iprop.Name, propertyName))
|
|
|
+ {
|
|
|
+ list = list ?? new List<PropertyInfo>();
|
|
|
+ list.Add(iprop);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (explicitProperties.Length == 1)
|
|
|
+ if (list?.Count == 1)
|
|
|
{
|
|
|
- var descriptor = new PropertyInfoDescriptor(Engine, explicitProperties[0], Target);
|
|
|
- AddProperty(propertyName, descriptor);
|
|
|
- return descriptor;
|
|
|
+ return (engine, target) => new PropertyInfoDescriptor(engine, list[0], target);
|
|
|
}
|
|
|
|
|
|
// try to find explicit method implementations
|
|
|
- var explicitMethods = (from iface in interfaces
|
|
|
- from imethod in iface.GetMethods()
|
|
|
- where EqualsIgnoreCasing(imethod.Name, propertyName)
|
|
|
- select imethod).ToArray();
|
|
|
+ List<MethodInfo> explicitMethods = null;
|
|
|
+ foreach (Type iface in type.GetInterfaces())
|
|
|
+ {
|
|
|
+ foreach (var imethod in iface.GetMethods())
|
|
|
+ {
|
|
|
+ if (EqualsIgnoreCasing(imethod.Name, propertyName))
|
|
|
+ {
|
|
|
+ explicitMethods = explicitMethods ?? new List<MethodInfo>();
|
|
|
+ explicitMethods.Add(imethod);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (explicitMethods.Length > 0)
|
|
|
+ if (explicitMethods?.Count > 0)
|
|
|
{
|
|
|
- var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitMethods), PropertyFlag.OnlyEnumerable);
|
|
|
- AddProperty(propertyName, descriptor);
|
|
|
- return descriptor;
|
|
|
+ return (engine, target) => new PropertyDescriptor(new MethodInfoFunctionInstance(engine, explicitMethods.ToArray()), PropertyFlag.OnlyEnumerable);
|
|
|
}
|
|
|
|
|
|
// try to find explicit indexer implementations
|
|
|
- var explicitIndexers =
|
|
|
- (from iface in interfaces
|
|
|
- from iprop in iface.GetProperties()
|
|
|
- where iprop.GetIndexParameters().Length != 0
|
|
|
- select iprop).ToArray();
|
|
|
+ List<PropertyInfo> explicitIndexers = null;
|
|
|
+ foreach (Type iface in type.GetInterfaces())
|
|
|
+ {
|
|
|
+ foreach (var iprop in iface.GetProperties())
|
|
|
+ {
|
|
|
+ if (iprop.GetIndexParameters().Length != 0)
|
|
|
+ {
|
|
|
+ explicitIndexers = explicitIndexers ?? new List<PropertyInfo>();
|
|
|
+ explicitIndexers.Add(iprop);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (explicitIndexers.Length == 1)
|
|
|
+ if (explicitIndexers?.Count == 1)
|
|
|
{
|
|
|
- return new IndexDescriptor(Engine, explicitIndexers[0].DeclaringType, propertyName, Target);
|
|
|
+ return (engine, target) => new IndexDescriptor(engine, explicitIndexers[0].DeclaringType, propertyName, target);
|
|
|
}
|
|
|
|
|
|
- return PropertyDescriptor.Undefined;
|
|
|
+ return (engine, target) => PropertyDescriptor.Undefined;
|
|
|
}
|
|
|
|
|
|
private static bool EqualsIgnoreCasing(string s1, string s2)
|
|
@@ -145,13 +200,13 @@ namespace Jint.Runtime.Interop
|
|
|
bool equals = false;
|
|
|
if (s1.Length == s2.Length)
|
|
|
{
|
|
|
- if (s1.Length > 0 && s2.Length > 0)
|
|
|
+ if (s1.Length > 0)
|
|
|
{
|
|
|
- equals = (s1.ToLower()[0] == s2.ToLower()[0]);
|
|
|
+ equals = char.ToLowerInvariant(s1[0]) == char.ToLowerInvariant(s2[0]);
|
|
|
}
|
|
|
- if (s1.Length > 1 && s2.Length > 1)
|
|
|
+ if (equals && s1.Length > 1)
|
|
|
{
|
|
|
- equals = equals && (s1.Substring(1) == s2.Substring(1));
|
|
|
+ equals = s1.Substring(1) == s2.Substring(1);
|
|
|
}
|
|
|
}
|
|
|
return equals;
|