示例#1
0
        /// <summary>
        /// Creates cast_to_type method via given AS method
        /// </summary>
        /// <param name="type"></param>
        /// <param name="AS"></param>
        /// <returns></returns>
        private AbcMethod Impl(IType type, AbcMethod AS, AbcMultiname name)
        {
            var instance = AS.Instance;

            var typeName = _generator.TypeBuilder.BuildMemberType(type);

            return(instance.DefineMethod(
                       Sig.@static(name, typeName, AvmTypeCode.Object, "value"),
                       code =>
            {
                const int value = 1;

                code.GetLocal(value);
                code.IfNullReturnNull(1);

                code.Getlex(instance);
                code.GetLocal(value);
                code.Call(AS);
                code.SetLocal(value);

                code.GetLocal(value);
                var notNull = code.IfNotNull(false);
                code.ThrowInvalidCastException(type);

                notNull.BranchTarget = code.Label();
                code.GetLocal(value);
                code.ReturnValue();
            }));
        }
示例#2
0
        public static AbcInstance FindInstance(IAssembly assembly, AbcMultiname name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            //TODO: decide whether to return Object for any (*) type
            if (name.IsRuntime || name.IsAny)
            {
                return(null);
            }

// TODO: fix importing of global types like void
//			if (name.IsGlobalType)
//			{
//				var fullName = GlobalTypes.GetCorlibTypeName(name.NameString);
//				return FindInstance(assembly, fullName);
//			}

            if (name.NamespaceSet != null)
            {
                return(name.NamespaceSet
                       .Select(ns => ns.NameString.MakeFullName(name.NameString))
                       .Select(fullName => FindInstance(assembly, fullName))
                       .FirstOrDefault(instance => instance != null));
            }

            return(FindInstance(assembly, name.FullName));
        }
示例#3
0
        public AbcInstance BuildBitmapAsset(AbcMultiname name, Image image, bool jpeg)
        {
            var instance = BuildAssetInstance(name, GetBitmapAssetSuperName());

            _generator.SwfCompiler.Assets.DefineBitmapAsset(image, instance, jpeg);
            return(instance);
        }
示例#4
0
 public void RegisterRemoteClass(string alias, AbcMultiname name)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     _remoteClasses.Add(new KeyValuePair <string, AbcMultiname>(alias, name));
 }
示例#5
0
 void Add(AbcMultiname name, string type)
 {
     if (name == null)
     {
         return;
     }
     Add(name.ToSwcId(), type);
 }
示例#6
0
        private VarPtr CreateVarPtr(AbcMultiname type, string name)
        {
            var ptr = new VarPtr {
                Slot = CreateSlot(type, name)
            };

            ptr.Ptr = CreateSlotPtr(ptr.Slot);
            return(ptr);
        }
示例#7
0
        private void CallCore(AbcCode code, IMethod method, AbcMultiname prop, CallFlags flags)
        {
            prop = _abc.ImportConst(prop);

            //determines whether method belongs to base type
            bool super = IsSuperCall(method, flags);

            CallCore(code, method, prop, super);
        }
示例#8
0
        internal static string ToSwcId(this AbcMultiname mn)
        {
            string ns   = mn.NamespaceString;
            string name = mn.NameString;

            if (string.IsNullOrEmpty(ns))
            {
                return(name);
            }
            return(ns + ":" + name);
        }
示例#9
0
        private static void WriteTraitName(TextWriter writer, AbcMultiname mn)
        {
            string ns = mn.NamespaceString;

            writer.Write("[");
            writer.Write(mn.Namespace.Kind);
            writer.Write(" = ");
            writer.Write(ns);
            writer.Write("] ");
            writer.Write(mn.NameString);
        }
示例#10
0
 private static void WriteTypeName(TextWriter writer, AbcMultiname mn)
 {
     if (mn == null || mn.Index == 0)
     {
         writer.Write("*");
     }
     else
     {
         WriteQName(writer, mn);
     }
 }
示例#11
0
        private static void WriteQName(TextWriter writer, AbcMultiname mn)
        {
            string ns = mn.NamespaceString;

            if (!string.IsNullOrEmpty(ns))
            {
                writer.Write(ns);
                writer.Write(".");
            }
            writer.Write(mn.NameString);
        }
示例#12
0
        private AbcInstance BuildAssetInstance(AbcTrait trait, AbcMultiname superName)
        {
            var name = GetAssetInstanceName(trait);
            //TODO: Check existance of instance

            var instance = BuildAssetInstance(name, superName);

            instance.Embed      = trait.Embed;
            trait.AssetInstance = instance;

            return(instance);
        }
示例#13
0
 public static string ToXml(this AbcMultiname multiname)
 {
     if (multiname is AbcMultinameVoid)
     {
         return("");
     }
     if (multiname is AbcMultinameAny)
     {
         return("*");
     }
     return(multiname.ToString());
 }
示例#14
0
        public ExternalCall(IMethod method, AbcMultiname name)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            Method = method;
            Name   = name;
        }
示例#15
0
 void AddSigRef(AbcMultiname name)
 {
     if (name == null)
     {
         return;
     }
     if (name.IsAny)
     {
         return;
     }
     //if (name.IsGlobalType) return;
     //var type = _app.FindInstance(name.FullName);
     //if (type != null && type.IsNative) return;
     Add(name, SwcCatalog.DepTypes.Signature);
 }
示例#16
0
        public InlineCall(IMethod method, AbcMultiname targetType, AbcMultiname name, AbcCode inlineCode)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (inlineCode == null)
            {
                throw new ArgumentNullException("inlineCode");
            }

            Method     = method;
            TargetType = targetType;
            Name       = name;
            InlineCode = inlineCode;
        }
示例#17
0
        private void Call(AbcCode code, IMethod method, AbcMultiname prop, CallFlags flags)
        {
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }

            CallCore(code, method, prop, flags);

            if (MustCoerceReturnType(method))
            {
                var type = method.Type;
                EnsureType(type);
                code.Coerce(type, true);
            }
        }
示例#18
0
        //TODO: avoid usage of out parameters
        private void DefineSuperType(IType type, out AbcMultiname superName, out AbcInstance superType)
        {
            superName = null;
            superType = null;

            if (type.IsInterface)
            {
                return;
            }

            if (type.IsEnum)
            {
                superType = DefineEnumSuperType(type);
                superName = superType.Name;
                return;
            }

            if (type.Is(SystemTypeCode.Exception))
            {
                // TODO: use Native.Error
                // superType = typeof(Error);
                superName = Abc.BuiltinTypes.Error;
                return;
            }

            var baseType = type.BaseType;

            if (baseType != null)
            {
                //NOTE: This fix explicit usage of Avm.Object as base class.
                //In fact .NET developer will never use this class, or no need to use this class.
                if (baseType.IsAvmObject())
                {
                    baseType = SystemTypes.Object;
                }

                superName = DefineTypeName(baseType);
                superType = baseType.AbcInstance();
                return;
            }

            if (type.Is(SystemTypeCode.Object))
            {
                superName = Abc.BuiltinTypes.Object;
            }
        }
示例#19
0
        private AbcTrait CreateSlot(AbcMultiname type, string name)
        {
            var      traitName = DefineSlotName(name);
            AbcTrait t;

            if (AbcGenConfig.UseActivationTraits)
            {
                t        = _body.Traits.AddSlot(type, traitName);
                t.SlotId = _body.Traits.Count;
            }
            else
            {
                t        = _activation.CreateSlot(traitName, type);
                t.SlotId = _activation.Traits.Count;
            }
            return(t);
        }
示例#20
0
        private AbcInstance BuildAssetInstance(AbcMultiname name, AbcMultiname superName)
        {
            var superType = FindAssetSuperType(superName);

            if (superType == null)
            {
                throw Errors.Type.UnableToFind.CreateException(superName.FullName);
            }

            //TODO: Check existance of instance

            var instance = new AbcInstance(true)
            {
                Name               = name,
                BaseTypeName       = superName,
                Flags              = (AbcClassFlags.Sealed | AbcClassFlags.ProtectedNamespace),
                ProtectedNamespace = Abc.DefineProtectedNamespace(name.NameString),
                Initializer        = Abc.DefineMethod(
                    Sig.@void(),
                    code =>
                {
                    code.PushThisScope();
                    code.ConstructSuper();
                    code.ReturnVoid();
                }),
                BaseInstance = superType,
                Class        = { Initializer = Abc.DefineMethod(
                                     Sig.global(null),
                                     code =>
                    {
                        if (true)
                        {
                            code.PushThisScope();
                        }
                        code.ReturnVoid();
                    }) }
            };

            Abc.AddInstance(instance);

            return(instance);
        }
示例#21
0
        private AbcInstance FindAssetSuperType(AbcMultiname superName)
        {
            var type = _generator.FindTypeDefOrRef(superName.FullName);

            if (type == null)
            {
                throw new InvalidOperationException();
            }

            var instance = type.AbcInstance();

            if (instance == null)
            {
                throw new InvalidOperationException();
            }

            instance = Abc.ImportInstance(instance);

            return(instance);
        }
示例#22
0
        public AbcMethod GetProperty(AbcMultiname prop)
        {
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }

            var m = _abc.DefineMethod(
                Sig.@global(AvmTypeCode.Object),
                code =>
            {
                code.Getlex(prop);
                code.ReturnValue();
            });

#if DEBUG
            m.Name = _abc.DefineString("get_" + prop.NameString);
#endif

            return(m);
        }
示例#23
0
        public AbcMethod SetProperty(AbcMultiname prop)
        {
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }

            var m = _abc.DefineMethod(
                Sig.@void(AvmTypeCode.Object, "value"),
                code =>
            {
                code.FindProperty(prop);
                code.GetLocal(1);                                 //value
                code.SetProperty(prop);
                code.ReturnVoid();
            });

#if DEBUG
            m.Name = _abc.DefineString("set_" + prop.NameString);
#endif

            return(m);
        }
示例#24
0
 public InstructionCode GetCoercionInstructionCode(AbcMultiname type)
 {
     if (ReferenceEquals(type, Int32))
     {
         return(InstructionCode.Coerce_i);
     }
     if (ReferenceEquals(type, UInt32))
     {
         return(InstructionCode.Coerce_u);
     }
     if (ReferenceEquals(type, String))
     {
         return(InstructionCode.Coerce_s);
     }
     if (ReferenceEquals(type, Boolean))
     {
         return(InstructionCode.Coerce_b);
     }
     if (ReferenceEquals(type, Object))
     {
         return(InstructionCode.Coerce_o);
     }
     return(InstructionCode.Coerce);
 }
示例#25
0
        private void CallCore(AbcCode code, IMethod method, AbcMultiname prop, bool super)
        {
            var abcMethod = method.AbcMethod();

            if (abcMethod != null)
            {
                if (abcMethod.IsGetter)
                {
                    code.Add(super ? InstructionCode.Getsuper : InstructionCode.Getproperty, prop);
                    return;
                }

                if (abcMethod.IsSetter)
                {
                    code.Add(super ? InstructionCode.Setsuper : InstructionCode.Setproperty, prop);
                    return;
                }
            }

            int n = method.Parameters.Count;

            if (method.AsStaticCall())
            {
                ++n;
            }

            bool isVoid = method.IsVoid();

            if (CanUseCallStatic)
            {
                if (method.IsStaticCall() &&
                    method.IsManaged() &&
                    abcMethod != null && _abc.IsDefined(abcMethod))
                {
                    code.Add(InstructionCode.Callstatic, abcMethod, n);
                    if (isVoid)
                    {
                        code.Pop();
                    }
                    return;
                }
            }

            if (super)
            {
                if (isVoid)
                {
                    code.Add(InstructionCode.Callsupervoid, prop, n);
                    return;
                }
                code.Add(InstructionCode.Callsuper, prop, n);
                return;
            }

            if (isVoid)
            {
                code.Add(InstructionCode.Callpropvoid, prop, n);
                return;
            }

            code.Add(InstructionCode.Callproperty, prop, n);
        }
示例#26
0
 public static XElement AddName(this XElement node, AbcMultiname multiname)
 {
     node.Add(new XAttribute("name", multiname.ToXml()));
     return(node);
 }
示例#27
0
 public AbcInstance FindInstanceRef(AbcMultiname name)
 {
     return(AssemblyIndex.FindInstance(AppAssembly, name));
 }
示例#28
0
 public static IType FindType(IAssembly assembly, AbcMultiname name)
 {
     return(name.GetFullNames().Select(fullName => FindType(assembly, fullName)).FirstOrDefault(type => type != null));
 }
示例#29
0
 public AbcInstance PropertyPtr(AbcMultiname name)
 {
     return(name.Namespace.IsGlobalPackage
                                ? GlobalPropertyPtr.Instance
                                : PropPtr.Instance);
 }
示例#30
0
 void AddInheritanceRef(AbcMultiname name)
 {
     Add(name, SwcCatalog.DepTypes.Inheritance);
 }