Example #1
0
        public ProxyConstructor(TypeProxy typeProxy)
        {
            fields = typeProxy.fields;
            proxy = typeProxy;
#if PORTABLE
            if (proxy.hostedType.GetTypeInfo().ContainsGenericParameters)
                throw new JSException((new TypeError(proxy.hostedType.Name + " can't be created because it's generic type.")));
#else
            if (proxy.hostedType.ContainsGenericParameters)
                throw new JSException((new TypeError(proxy.hostedType.Name + " can't be created because it's generic type.")));
#endif
            if (_length == null)
                _length = new Number(0) { attributes = JSObjectAttributesInternal.ReadOnly | JSObjectAttributesInternal.DoNotDelete | JSObjectAttributesInternal.DoNotEnum };

#if PORTABLE
            var ctors = typeProxy.hostedType.GetTypeInfo().DeclaredConstructors.ToArray();
            List<MethodProxy> ctorsL = new List<MethodProxy>(ctors.Length + (typeProxy.hostedType.GetTypeInfo().IsValueType ? 1 : 0));
#else
            var ctors = typeProxy.hostedType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            List<MethodProxy> ctorsL = new List<MethodProxy>(ctors.Length + (typeProxy.hostedType.IsValueType ? 1 : 0));
#endif
            for (int i = 0; i < ctors.Length; i++)
            {
                if (!ctors[i].IsDefined(typeof(HiddenAttribute), false) || ctors[i].IsDefined(typeof(ForceUse), true))
                {
                    ctorsL.Add(new MethodProxy(ctors[i]));
                    length.iValue = System.Math.Max(ctorsL[ctorsL.Count - 1]._length.iValue, _length.iValue);
                }
            }
            ctorsL.Sort((x, y) => x.Parameters.Length == 1 && x.Parameters[0].ParameterType == typeof(Arguments) ? 1 :
                y.Parameters.Length == 1 && y.Parameters[0].ParameterType == typeof(Arguments) ? -1 :
                x.Parameters.Length - y.Parameters.Length);
            constructors = ctorsL.ToArray();

            passCount = 2;
            // На втором проходе будет выбираться первый метод, 
            // для которого получится сгенерировать параметры по-умолчанию.
            // Если нужен более строгий подбор, то количество проходов нужно
            // уменьшить до одного
        }
Example #2
0
        private TypeProxy(Type type)
            : base(true)
        {
            if (dynamicProxies.ContainsKey(type))
                throw new InvalidOperationException("Type \"" + type + "\" already proxied.");
            else
            {
                hostedType = type;
                dynamicProxies[type] = this;
                try
                {
                    valueType = JSObjectType.Object;
                    oValue = this;
                    var pa = type.GetCustomAttributes(typeof(PrototypeAttribute), false);
                    if (pa.Length != 0 && (pa[0] as PrototypeAttribute).PrototypeType != hostedType)
                        __prototype = GetPrototype((pa[0] as PrototypeAttribute).PrototypeType);
#if PORTABLE
                    ictor = hostedType.GetTypeInfo().DeclaredConstructors.FirstOrDefault(x => x.GetParameters().Length == 0 && !x.IsStatic);
#else
                    ictor = hostedType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy, null, System.Type.EmptyTypes, null);
#endif

                    attributes |= JSObjectAttributesInternal.DoNotEnum | JSObjectAttributesInternal.SystemObject;
                    if (hostedType.IsDefined(typeof(ImmutablePrototypeAttribute), false))
                        attributes |= JSObjectAttributesInternal.Immutable;
                    var staticProxy = new TypeProxy()
                    {
                        hostedType = type,
                        InstanceMode = false
                    };
                    InstanceMode = true;

                    if (typeof(JSObject).IsAssignableFrom(hostedType))
                        _prototypeInstance = prototypeInstance;

#if PORTABLE
                    if (hostedType.GetTypeInfo().IsAbstract)
#else
                    if (hostedType.IsAbstract)
#endif
                    {
                        staticProxies[type] = staticProxy;
                    }
                    else
                    {
                        Function ctor = null;
                        if (type == typeof(JSObject))
                            ctor = new ObjectConstructor(staticProxy);
                        else
                            ctor = new ProxyConstructor(staticProxy);
                        ctor.attributes = attributes;
                        attributes |= JSObjectAttributesInternal.DoNotDelete | JSObjectAttributesInternal.DoNotEnum | JSObjectAttributesInternal.NotConfigurable | JSObjectAttributesInternal.ReadOnly;
                        staticProxies[type] = ctor;
                        if (hostedType != typeof(ProxyConstructor))
                            fields["constructor"] = ctor;
                    }
                }
                catch
                {
                    dynamicProxies.Remove(type);
                    throw;
                }
            }
        }
Example #3
0
 public ObjectConstructor(TypeProxy proxy)
     : base(proxy)
 {
     _length = new Number(1);
 }