Exemplo n.º 1
0
        private void DeclareMember(Property member)
        {
            var name = member.Name;

            CheckName(name, member);
            var modifier = member.Modifier;
            var g        = member.Getter;
            var s        = member.Setter;

            CheckModifier(modifier, g, s);
            ClassMethod gmtd = null, smtd = null;
            Modifier    gmod = 0, smod = 0;

            if (g == null && s == null)
            {
                throw new RuntimeException(member.LinePragma,
                                           ExceptionResource.AccessorStatementsExpected);
            }
            else if (g != null && s != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                smod = s.Modifier ?? member.Modifier;
                if (g.Statements == null && s.Statements == null)
                {
                    var id = "#autoproperty_" + member.Name;
                    // CheckName(id, member);
                    if (member.IsStatic)
                    {
                        Class.StaticFields.Add(id, ScriptNull.Instance);
                    }
                    else
                    {
                        Class.FieldInitializers.Add(id, null);
                    }
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(),
                                            new StatementCollection(
                                                new ReturnStatement(g.LinePragma,
                                                                    new MemberReferenceExpression(g.LinePragma,
                                                                                                  member.IsStatic ?
                                                                                                  new VariableReferenceExpression(g.LinePragma, Class.Name) as Expression :
                                                                                                  new ThisReferenceExpression(g.LinePragma),
                                                                                                  id))),
                                            Context);
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")),
                                            new StatementCollection(
                                                new ExpressionStatement(s.LinePragma,
                                                                        new AssignExpression(s.LinePragma,
                                                                                             new MemberReferenceExpression(s.LinePragma,
                                                                                                                           member.IsStatic ?
                                                                                                                           new VariableReferenceExpression(s.LinePragma, Class.Name) as Expression :
                                                                                                                           new ThisReferenceExpression(s.LinePragma),
                                                                                                                           id),
                                                                                             new VariableReferenceExpression(s.LinePragma, "value")))),
                                            Context);
                }
                else if (g.Statements != null && s.Statements != null)
                {
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(),
                                            g.Statements, Context);
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")),
                                            s.Statements, Context);
                }
                else
                {
                    throw new RuntimeException((g.Statements == null ? g : s).LinePragma,
                                               ExceptionResource.AccessorStatementsExpected);
                }
            }
            else if (g != null)
            {
                gmod = g.Modifier ?? member.Modifier;
                if (g.Statements == null)
                {
                    throw new RuntimeException(g.LinePragma,
                                               ExceptionResource.AutoPropertyMustHasGetSet);
                }
                else
                {
                    gmtd = new ScriptMethod(Class,
                                            new ParameterCollection(), g.Statements,
                                            Context);
                }
            }
            else
            {
                smod = s.Modifier ?? member.Modifier;
                if (s.Statements == null)
                {
                    throw new RuntimeException(s.LinePragma,
                                               ExceptionResource.AutoPropertyMustHasGetSet);
                }
                else
                {
                    smtd = new ScriptMethod(Class,
                                            new ParameterCollection(new Parameter(s.LinePragma, "value")), s.Statements,
                                            Context);
                }
            }
            if (gmtd != null)
            {
                var propget = ClassHelper.RenamePropertyGetter(name);
                propget = RenameModifier(gmod, propget);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(propget, new PropertyGetter(gmtd));
                }
                else
                {
                    Class.InstanceMembers.Add(propget, new PropertyGetter(gmtd));
                }
            }
            if (smtd != null)
            {
                var propset = ClassHelper.RenamePropertySetter(name);
                propset = RenameModifier(smod, propset);
                if (member.IsStatic)
                {
                    Class.StaticMembers.Add(propset, new PropertySetter(smtd));
                }
                else
                {
                    Class.InstanceMembers.Add(propset, new PropertySetter(smtd));
                }
            }
        }
Exemplo n.º 2
0
        internal ClassMember SetMember(RuntimeContexts.RuntimeContext context, string name, bool isStatic)
        {
            var         members = isStatic ? StaticMembers : InstanceMembers;
            ClassMember value;
            string      rename;
            string      propget = ClassHelper.RenamePropertyGetter(name);
            string      propset = ClassHelper.RenamePropertySetter(name);
            var         cls     = context.Class as ClassObject;

            if (cls != null)
            {
                rename = ClassHelper.RenamePrivate(name, cls.Name);
                if (members.TryGetValue(rename, out value) && value is ClassMethod)
                {
                    return(ClassHelper.ThrowCannotAssignMethod(name));
                }
                rename = ClassHelper.RenamePrivate(propset, cls.Name);
                if (members.TryGetValue(rename, out value) && value is PropertySetter)
                {
                    return(value);
                }
                rename = ClassHelper.RenamePrivate(propget, cls.Name);
                if (members.TryGetValue(rename, out value) && value is PropertyGetter)
                {
                    return(ClassHelper.ThrowPropertyIsReadonly(name));
                }
            }
            {
                rename = name;
                if (members.TryGetValue(rename, out value) && value is ClassMethod)
                {
                    return(ClassHelper.ThrowCannotAssignMethod(name));
                }
                rename = propset;
                if (members.TryGetValue(rename, out value) && value is PropertySetter)
                {
                    return(value);
                }
                rename = propget;
                if (members.TryGetValue(rename, out value) && value is PropertyGetter)
                {
                    return(ClassHelper.ThrowPropertyIsReadonly(name));
                }
            }
            if (cls == this || ClassHelper.IsExtendedFrom(this, cls) || ClassHelper.IsExtendedFrom(cls, this))
            {
                rename = ClassHelper.RenameProtected(name);
                if (members.TryGetValue(rename, out value) && value is ClassMethod)
                {
                    return(value);
                }
                rename = ClassHelper.RenameProtected(propset);
                if (members.TryGetValue(rename, out value) && value is PropertySetter)
                {
                    return(value);
                }
                rename = ClassHelper.RenameProtected(propget);
                if (members.TryGetValue(rename, out value) && value is PropertyGetter)
                {
                    return(ClassHelper.ThrowPropertyIsReadonly(name));
                }
            }
            if (Super != null)
            {
                return(Super.SetMember(context, name, isStatic));
            }
            return(ClassHelper.ThrowMemberNotFound(name));
        }