Beispiel #1
0
        internal JSValue proxyMember(bool forWrite, IList <MemberInfo> m)
        {
            JSValue r = null;

            if (m.Count > 1)
            {
                for (int i = 0; i < m.Count; i++)
                {
                    if (!(m[i] is MethodBase))
                    {
                        ExceptionHelper.Throw(_context.ProxyValue(new TypeError("Incompatible fields types.")));
                    }
                }

                var cache = new MethodProxy[m.Count];
                for (int i = 0; i < m.Count; i++)
                {
                    cache[i] = new MethodProxy(_context, m[i] as MethodBase);
                }

                r = new MethodGroup(cache);
            }
            else
            {
#if PORTABLE || NETCORE
                switch (m[0].GetMemberType())
#else
                switch (m[0].MemberType)
#endif
                {
                case MemberTypes.Method:
                {
                    var method = (MethodInfo)m[0];
                    r              = new MethodProxy(_context, method);
                    r._attributes &= ~(JSValueAttributesInternal.ReadOnly | JSValueAttributesInternal.DoNotDelete | JSValueAttributesInternal.NonConfigurable | JSValueAttributesInternal.DoNotEnumerate);
                    break;
                }

                case MemberTypes.Field:
                {
                    var field = (m[0] as FieldInfo);
                    if ((field.Attributes & (FieldAttributes.Literal | FieldAttributes.InitOnly)) != 0 &&
                        (field.Attributes & FieldAttributes.Static) != 0)
                    {
                        r              = _context.ProxyValue(field.GetValue(null));
                        r._attributes |= JSValueAttributesInternal.ReadOnly;
                    }
                    else
                    {
                        r = new JSValue()
                        {
                            _valueType = JSValueType.Property,
                            _oValue    = new PropertyPair
                                         (
                                new ExternalFunction((thisBind, a) => _context.ProxyValue(field.GetValue(field.IsStatic ? null : thisBind.Value))),
                                !m[0].IsDefined(typeof(ReadOnlyAttribute), false) ? new ExternalFunction((thisBind, a) =>
                                {
                                    field.SetValue(field.IsStatic ? null : thisBind.Value, a[0].Value);
                                    return(null);
                                }) : null
                                         )
                        };

                        r._attributes = JSValueAttributesInternal.Immutable | JSValueAttributesInternal.Field;
                        if ((r._oValue as PropertyPair).setter == null)
                        {
                            r._attributes |= JSValueAttributesInternal.ReadOnly;
                        }
                    }
                    break;
                }

                case MemberTypes.Property:
                {
                    var pinfo = (PropertyInfo)m[0];
                    r = new JSValue()
                    {
                        _valueType = JSValueType.Property,
                        _oValue    = new PropertyPair
                                     (
#if (PORTABLE || NETCORE)
                            pinfo.CanRead && pinfo.GetMethod != null ? new MethodProxy(_context, pinfo.GetMethod) : null,
                            pinfo.CanWrite && pinfo.SetMethod != null && !pinfo.IsDefined(typeof(ReadOnlyAttribute), false) ? new MethodProxy(_context, pinfo.SetMethod) : null
#else
                            pinfo.CanRead&& pinfo.GetGetMethod(false) != null ? new MethodProxy(_context, pinfo.GetGetMethod(false)) : null,
                            pinfo.CanWrite && pinfo.GetSetMethod(false) != null && !pinfo.IsDefined(typeof(ReadOnlyAttribute), false) ? new MethodProxy(_context, pinfo.GetSetMethod(false)) : null
#endif
                                     )
                    };

                    r._attributes = JSValueAttributesInternal.Immutable;

                    if ((r._oValue as PropertyPair).setter == null)
                    {
                        r._attributes |= JSValueAttributesInternal.ReadOnly;
                    }

                    if (pinfo.IsDefined(typeof(FieldAttribute), false))
                    {
                        r._attributes |= JSValueAttributesInternal.Field;
                    }

                    break;
                }

                case MemberTypes.Event:
                {
                    var pinfo = (EventInfo)m[0];
                    r = new JSValue()
                    {
                        _valueType = JSValueType.Property,
                        _oValue    = new PropertyPair
                                     (
                            null,
#if (PORTABLE || NETCORE)
                            new MethodProxy(_context, pinfo.AddMethod)
#else
                            new MethodProxy(_context, pinfo.GetAddMethod())
#endif
                                     )
                    };
                    break;
                }

                case MemberTypes.TypeInfo:
#if (PORTABLE || NETCORE)
                {
                    r = GetConstructor((m[0] as TypeInfo).AsType());
                    break;
                }
#else
                case MemberTypes.NestedType:
                {
                    r = _context.GetConstructor((Type)m[0]);
                    break;
                }

                default:
                    throw new NotImplementedException("Convertion from " + m[0].MemberType + " not implemented");
#endif
                }
            }

            if (m[0].IsDefined(typeof(DoNotEnumerateAttribute), false))
            {
                r._attributes |= JSValueAttributesInternal.DoNotEnumerate;
            }

            if (forWrite && r.NeedClone)
            {
                r = r.CloneImpl(false);
            }

            for (var i = m.Count; i-- > 0;)
            {
                if (!m[i].IsDefined(typeof(DoNotEnumerateAttribute), false))
                {
                    r._attributes &= ~JSValueAttributesInternal.DoNotEnumerate;
                }
                if (m[i].IsDefined(typeof(ReadOnlyAttribute), false))
                {
                    r._attributes |= JSValueAttributesInternal.ReadOnly;
                }
                if (m[i].IsDefined(typeof(NotConfigurable), false))
                {
                    r._attributes |= JSValueAttributesInternal.NonConfigurable;
                }
                if (m[i].IsDefined(typeof(DoNotDeleteAttribute), false))
                {
                    r._attributes |= JSValueAttributesInternal.DoNotDelete;
                }
            }

            return(r);
        }