Пример #1
0
        private static void DumpTrait(TextWriter writer, AbcTrait t)
        {
            switch (t.Kind)
            {
            case AbcTraitKind.Const:
            case AbcTraitKind.Slot:
                writer.WriteLine("\tslot {0}: {1}", t.Name.FullName, t.SlotType.FullName);
                break;

            case AbcTraitKind.Getter:
                writer.WriteLine("\tgetter {0}", t.Name.FullName);
                break;

            case AbcTraitKind.Setter:
                writer.WriteLine("\tsetter {0}", t.Name.FullName);
                break;

            case AbcTraitKind.Method:
            case AbcTraitKind.Function:
                writer.WriteLine("\t{0}", t.Name.FullName);
                break;

            case AbcTraitKind.Class:
                writer.WriteLine("\tclass {0}", t.Name.FullName);
                break;
            }
        }
Пример #2
0
        private Embed ImportEmbedAsset(AbcTrait from)
        {
            if (from == null)
            {
                return(null);
            }
            var embed = from.Embed;

            if (embed == null)
            {
                return(null);
            }
            if (embed.Asset == null)
            {
                return(null);
            }

            if (!IsSwf)
            {
                throw Errors.NotSwf.CreateException();
            }

            var sfc   = SwfCompiler;
            var asset = sfc.Assets.Import(embed);

            return(new Embed(embed)
            {
                Asset = asset, Movie = sfc.Swf
            });
        }
Пример #3
0
        public AbcTrait CreateSlot(object type, object name)
        {
            var typeName = DefineTypeNameSafe(type);
            var mn       = DefineName(name);

            return(AbcTrait.CreateSlot(typeName, mn));
        }
Пример #4
0
        public AbcTrait DefineSlot(object name, object type, bool isStatic)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var traitName = Abc.DefineName(name);

            var traits = isStatic ? Class.Traits : Traits;
            var trait  = traits.Find(traitName, AbcTraitKind.Slot);

            if (trait != null)
            {
                return(trait);
            }

            var typeName = Abc.DefineTypeNameSafe(type);

            trait = AbcTrait.CreateSlot(typeName, traitName);
            traits.Add(trait);

            return(trait);
        }
Пример #5
0
        public void Add(AbcTrait trait)
        {
            if (trait == null)
            {
                throw new ArgumentNullException("trait");
            }

            string key = KeyOf(trait);

            _cache.Add(key, trait);
        }
Пример #6
0
        private void ImportAssets(AbcTrait trait)
        {
            ImportEmbedAsset(trait);

            if (trait.HasMetadata)
            {
                foreach (var e in trait.Metadata)
                {
                    ProcessMetaEntry(trait.Name.ABC, trait, e);
                }
            }
        }
Пример #7
0
        private void ImportEmbedAsset(AbcTrait trait, AbcTrait from)
        {
            var newEmbed = ImportEmbedAsset(from);

            if (newEmbed == null)
            {
                return;
            }

            var instance = trait.Class.Instance;

            trait.Embed         = newEmbed;
            trait.AssetInstance = instance;
            instance.Embed      = newEmbed;
        }
Пример #8
0
 /// <summary>
 /// Adds trait to instance or class trait collection.
 /// </summary>
 /// <param name="trait">trait to add</param>
 /// <param name="isStatic">true to add to class trais; false - to instance traits</param>
 public void AddTrait(AbcTrait trait, bool isStatic)
 {
     if (trait == null)
     {
         throw new ArgumentNullException("trait");
     }
     //Note: traits for static members are contained within ABC file in array of AbcClass
     if (isStatic)
     {
         _class.Traits.Add(trait);
     }
     else
     {
         _traits.Add(trait);
     }
 }
Пример #9
0
        public AbcTrait CreateSlot(object name, object type, bool isStatic)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var traitName = Abc.DefineName(name);
            var typeName  = Abc.DefineTypeNameSafe(type);

            var trait = AbcTrait.CreateSlot(typeName, traitName);

            AddTrait(trait, isStatic);

            return(trait);
        }
Пример #10
0
        public bool IsDefined(AbcTrait trait)
        {
            var instance = trait.Owner as AbcInstance;

            if (instance != null)
            {
                return(IsDefined(instance));
            }
            var klass = trait.Owner as AbcClass;

            if (klass != null)
            {
                return(IsDefined(klass));
            }
            var script = trait.Owner as AbcScript;

            if (script != null)
            {
                return(IsDefined(script));
            }
            return(false);
        }
Пример #11
0
        private AbcTrait ImportTrait(AbcTrait from)
        {
            var trait = new AbcTrait
            {
                Kind       = from.Kind,
                Name       = ImportConst(from.Name),
                Attributes = from.Attributes,
                Property   = from.Property
            };

            switch (from.Kind)
            {
            case AbcTraitKind.Slot:
            case AbcTraitKind.Const:
            {
                trait.SlotId   = from.SlotId;
                trait.HasValue = from.HasValue;
                trait.SlotType = ImportType(from.SlotType);
                if (from.HasValue)
                {
                    trait.SlotValue = ImportValue(from.SlotValue);
                }
            }
            break;

            case AbcTraitKind.Method:
            case AbcTraitKind.Getter:
            case AbcTraitKind.Setter:
            {
                trait.SlotId = from.SlotId;
                trait.Method = ImportMethod(from.Method);
            }
            break;

            case AbcTraitKind.Class:
            {
                var instance = Instances.Find(from.Name) ?? ImportInstance(from.Class.Instance);

                trait.Class = instance.Class;
                //trait.SlotID = trait.SlotID;
            }
            break;

            case AbcTraitKind.Function:
            {
                trait.SlotId = from.SlotId;
                trait.Method = ImportMethod(from.Method);
            }
            break;
            }

            ImportEmbedAsset(trait, from);

            if (from.HasMetadata)
            {
                var md = from.Metadata;
                if (md != null)
                {
                    var abc = from.Name.ABC;
                    foreach (var e in md)
                    {
                        if (ProcessMetaEntry(abc, trait, e))
                        {
                            continue;
                        }

                        var e2 = ImportMetaEntry(abc, e);
                        if (e2 != null)
                        {
                            trait.HasMetadata = true;
                            trait.Metadata.Add(e2);
                        }
                    }
                }
            }

            return(trait);
        }
Пример #12
0
        private bool ProcessMetaEntry(AbcFile abc, AbcTrait trait, AbcMetaEntry e)
        {
            if (ImportResourceBundle(abc, e))
            {
                return(true);
            }

            string name = e.NameString;

            if (name == MetadataTags.Mixin)
            {
                var klass = trait.Class;
                if (klass != null)
                {
                    var instance = klass.Instance;
                    instance.IsMixin = true;

                    var sfc = SwfCompiler;
                    if (sfc != null)
                    {
                        sfc.Mixins.RegisterMixinClass(instance);
                    }
                }
                return(true);
            }

            if (name == MetadataTags.RemoteClass)
            {
                var sfc = SwfCompiler;
                if (sfc != null)
                {
                    var klass = trait.Class;
                    if (klass != null)
                    {
                        string alias = e["alias"];
                        sfc.FlexInit.RegisterRemoteClass(alias, klass.Instance);
                    }
                }
                return(true);
            }

            if (name == MetadataTags.Effect)
            {
                var sfc = SwfCompiler;
                if (sfc != null)
                {
                    string effectName  = e["name"];
                    string effectEvent = e["event"];
                    sfc.FlexInit.RegisterEffectTrigger(effectName, effectEvent);
                }
                return(true);
            }

            if (name == MetadataTags.Style)
            {
                //TODO: Can be also used to collect inheriting styles
                var klass = trait.Class;
                if (klass != null)
                {
                    klass.Instance.HasStyles = true;

                    var sfc = SwfCompiler;
                    if (sfc != null)
                    {
                        sfc.Mixins.AddStyleClient(klass.Instance);
                    }
                }
            }

            return(false);
        }
Пример #13
0
        internal AbcMethod DefineMethod(Sig sig, AbcCoder coder, Action <AbcMethod> complete)
        {
            if (sig == null)
            {
                throw new ArgumentNullException("sig");
            }

            if (!sig.IsInitilizer && sig.Name == null)
            {
                throw new InvalidOperationException();
            }

            var klass = Class;

            if (klass == null)
            {
                throw new InvalidOperationException(string.Format("Class is not defined yet for Instance {0}", FullName));
            }

            AbcMultiname traitName = null;
            AbcTrait     trait;

            bool isStatic = (sig.Semantics & MethodSemantics.Static) != 0;
            var  traits   = isStatic ? klass.Traits : Traits;

            if (sig.IsInitilizer)
            {
                if (Initializer != null)
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                traitName = Abc.DefineName(sig.Name);
                trait     = traits.Find(traitName, sig.Kind);
                if (trait != null)
                {
                    return(trait.Method);
                }
            }

            var method = new AbcMethod
            {
                Method = sig.Source
            };

            var generator = Abc.Generator;

            if (sig.Source != null)
            {
                generator.SetData(sig.Source, method);
            }

            AbcMethodBody body = null;

            if (sig.IsAbstract)
            {
                if (coder != null)
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                body = new AbcMethodBody(method);
            }

            Abc.AddMethod(method);

            if (sig.IsInitilizer)
            {
                Initializer = method;
            }
            else
            {
                //for non initializer method we must define trait and return type
                method.ReturnType = BuildReturnType(sig.ReturnType, method);

                trait      = AbcTrait.CreateMethod(method, traitName);
                trait.Kind = sig.Kind;

                if (!isStatic)
                {
                    trait.IsVirtual  = (sig.Semantics & MethodSemantics.Virtual) != 0;
                    trait.IsOverride = (sig.Semantics & MethodSemantics.Override) != 0;
                }

                traits.Add(trait);
            }

            if (sig.Args != null)
            {
                if (sig.Args.Length == 1 && sig.Args[0] is IMethod)
                {
                    var m = (IMethod)sig.Args[0];
                    if (generator == null)
                    {
                        throw new InvalidOperationException();
                    }
                    generator.MethodBuilder.BuildParameters(method, m);
                }
                else
                {
                    Abc.AddParameters(method.Parameters, sig.Args);
                }
            }

            if (body != null && coder != null)
            {
                var code = new AbcCode(Abc);
                coder(code);
                body.Finish(code);
            }

            if (complete != null)
            {
                complete(method);
            }

            return(method);
        }
Пример #14
0
            public void Add(AbcTrait trait)
            {
                string key = MemberKey.BuildKey(trait.Name);

                _cache.Add(key, trait.Method);
            }
Пример #15
0
 private static string KeyOf(AbcTrait trait)
 {
     return(Prefix(trait.Kind) + MemberKey.BuildKey(trait.Name));
 }