Exemple #1
0
 private AbcMultiname DefineSlotName(string name)
 {
     if (AbcGenConfig.UseActivationTraits)
     {
         return(_abc.DefineName(QName.BodyTrait(name)));
     }
     return(_abc.DefineName(QName.Global(name)));
 }
Exemple #2
0
        private static void DefineDebuggerDisplay(AbcGenerator generator, IType type, AbcInstance instance)
        {
            if (type.IsInterface)
            {
                return;
            }
            if (type.Is(SystemTypeCode.Object))
            {
                return;
            }
            if (type.Is(SystemTypeCode.String))
            {
                return;
            }
            if (type.Is(SystemTypeCode.Array))
            {
                return;
            }

            //MSDN: DebuggerDisplay attribute takes precedence over the ToString() override
            var attr = type.FindAttribute(Attrs.DebuggerDisplay);

            if (attr != null)
            {
                if (DefineDebuggerDisplay(generator, type, instance, attr))
                {
                    return;
                }
            }

            //Use ToString
            var toString = type.Methods.Find(Const.Object.MethodToString, 0);

            if (toString != null)
            {
                var tostr = generator.MethodBuilder.BuildAbcMethod(toString);

                var name = generator.Abc.DefineName(QName.Global(DebugPropertyPrefix + "display"));

                var m = instance.DefineMethod(
                    Sig.get(name, AvmTypeCode.String),
                    code =>
                {
                    code.LoadThis();                         // this might be redundant --olegz
                    code.Call(tostr);
                    code.ReturnValue();
                });

                // as soon the method will be overridden in descendants we have to mark it as virtual (case 147084)
                m.Trait.IsVirtual  = !type.IsSealed;
                m.Trait.IsOverride = instance.FindSuperTrait(name, AbcTraitKind.Getter) != null;
            }
        }
Exemple #3
0
        private AbcInstance DefineEnumSuperType(IType type)
        {
            if (!type.IsEnum)
            {
                throw new InvalidOperationException("type is not enum");
            }
            var vtype = type.ValueType;
            var st    = vtype.SystemType();

            if (st == null)
            {
                throw new InvalidOperationException(string.Format("invalid enum type {0}", type.FullName));
            }
            int index = GetEnumIndex(st);

            var instance = _enumSuperTypes[index];

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

            var super = BuildInstance(type.BaseType);

            instance = _enumSuperTypes[index];
            if (instance != null)
            {
                return(instance);
            }

            instance = new AbcInstance(true)
            {
                Name         = Abc.DefineName(QName.PfxPackage(GetEnumName(st))),
                BaseTypeName = super.Name,
                BaseInstance = super,
                Initializer  = Initializers.BuildDefaultInitializer(Abc, null)
            };
            Abc.AddInstance(instance);

            instance.Class.Initializer = Abc.DefineEmptyMethod();

            _enumSuperTypes[index] = instance;

            string name = Const.Boxing.Value;

            instance.CreateSlot(Abc.DefineName(QName.Global(name)), BuildMemberType(vtype));

            //SetFlags(instance, type);

            return(instance);
        }
Exemple #4
0
        private void OverrideFlexAppModuleFactorySetter(AbcInstance instance)
        {
            Debug.Assert(IsFlex4);

            var moduleFactoryInitialized   = instance.DefineSlot("__moduleFactoryInitialized", AvmTypeCode.Boolean);
            var flexModuleFactoryInterface = _generator.ImportType(MX.IFlexModuleFactory);

            var propertyName = Abc.DefineName(QName.Global("moduleFactory"));

            instance.DefineMethod(
                Sig.set(propertyName, flexModuleFactoryInterface).@virtual().@override(),
                code =>
            {
                code.Trace("PFC: setting FlexModuleFactory to application");

                // super.moduleFactory = value
                code.LoadThis();
                code.GetLocal(1);
                code.SetSuper(propertyName);

                code.LoadThis();
                code.GetProperty(moduleFactoryInitialized);
                var br = code.IfFalse();
                code.ReturnVoid();

                br.BranchTarget = code.Label();

                code.LoadThis();
                code.Add(InstructionCode.Pushtrue);
                code.SetProperty(moduleFactoryInitialized);

                CallInitStyles(code, instance);

                // init application after styles are initialized
                var ctor = instance.FindParameterlessConstructor();
                if (ctor != null)
                {
                    var ctorMethod = _generator.MethodBuilder.BuildAbcMethod(ctor);
                    code.LoadThis();
                    if (AbcGenConfig.FlexAppCtorAsHandler)
                    {
                        code.PushNull();
                    }
                    code.Call(ctorMethod);
                }

                code.ReturnVoid();
            });
        }
Exemple #5
0
        void DefineInitTypeMethod(IType type, int typeId)
        {
            Debug.Assert(typeId >= 0);

            var instance = _generator.Corlib.Assembly.Instance;

            instance.DefineMethod(
                Sig.@this(QName.Global(Const.InitTypePrefix + typeId),
                          AvmTypeCode.Void,
                          SystemTypes.Type, "type"),
                code =>
            {
                InitTypeInfo(code, type, typeId);
                code.ReturnVoid();
            });
        }
Exemple #6
0
        private static void RegisterInheritStyles(AbcFile app, AbcCode code, Action pushStyleManager, bool flex4)
        {
            var registerInheritingStyle = app.DefineName(QName.Global("registerInheritingStyle"));

            var styles = new List <string>(CommonInheritStyles);

            styles.AddRange(flex4 ? Flex4InheritStyles : Flex3InheritStyles);
            styles.Sort(StringComparer.Ordinal);

            foreach (var style in styles)
            {
                pushStyleManager();
                code.PushString(style);
                code.CallVoid(registerInheritingStyle, 1);
            }
        }
Exemple #7
0
        public void TestQNames()
        {
            var abc = new AbcFile();

            for (int i = 0; i < 10; ++i)
            {
                string name  = "name" + i;
                var    c1    = abc.DefineName(QName.Global(name));
                string name1 = "name" + i;
                var    c2    = abc.DefineName(QName.Global(name1));
                Assert.IsTrue(ReferenceEquals(c1, c2));

                c1 = abc.ImportConst(c1);
                c2 = abc.ImportConst(c2);
                Assert.IsTrue(ReferenceEquals(c1, c2));
            }
        }
Exemple #8
0
        private void OverrideFlexAppInitialize(AbcInstance instance)
        {
            var name = Abc.DefineName(QName.Global("initialize"));

            instance.DefineMethod(
                Sig.@virtual(name, AvmTypeCode.Void).@override(),
                code =>
            {
                code.LoadThis();
                code.Add(InstructionCode.Callsupervoid, name, 0);

                code.Trace("PFC: calling App.initialize");

                //Alert(code, "App Initialize!!!");

                code.ReturnVoid();
            });
        }
Exemple #9
0
        private void LoadGlobalReceiver(AbcCode code, IMethod method)
        {
            var type = method.DeclaringType;

            if (type.Data is GlobalFunctionsContainer)
            {
                var mn = GetMethodName(method);
                code.FindPropertyStrict(mn);
                return;
            }

            if (type.IsInternalType())
            {
                string name = method.Name;
                var    mn   = _abc.DefineName(QName.Global(name));
                code.FindPropertyStrict(mn);
                return;
            }

            if (type.Is(SystemTypeCode.String))
            {
                code.Getlex(AvmTypeCode.String);
                return;
            }

            if (type.Is(AvmTypeCode.Class))
            {
                if (method.Name == "Find")
                {
                    var m = _generator.RuntimeImpl.FindClass();
                    code.Getlex(m);
                    return;
                }
            }

            throw new InvalidOperationException();
        }
Exemple #10
0
        /// <summary>
        /// Creates single-dimensional array with given element type.
        /// </summary>
        /// <param name="elemType"></param>
        /// <returns></returns>
        public AbcMethod NewArray(IType elemType)
        {
            _generator.TypeBuilder.Build(elemType);

            string name1 = "newarr_" + elemType.GetSigName();
            var    name  = Abc.DefineName(QName.Global(name1));

            return(Instance.DefineMethod(
                       Sig.@static(name, Instance.Name, AvmTypeCode.Int32, "size"),
                       code =>
            {
                const int varSize = 1;                                 //size argument
                const int varArray = 2;

                var m = CreateSystemArraySZ();
                code.LoadThis();
                code.GetLocal(varSize);
                code.Call(m);
                code.SetLocal(varArray);

                InitFields(code, elemType, varArray);

                if (InternalTypeExtensions.IsInitArray(elemType))
                {
                    code.InitArray(elemType,
                                   () =>
                    {
                        code.GetLocal(varArray);
                        code.GetProperty(Const.Array.Value);
                    }, varSize);
                }

                code.GetLocal(varArray);
                code.ReturnValue();
            }));
        }
Exemple #11
0
        //Called when GetTypeId method is used.
        public AbcMethod DefineGetTypeIdMethod(IType type, AbcInstance instance)
        {
            if (type == null)
            {
                return(null);
            }
            if (instance == null)
            {
                return(null);
            }
            if (instance.IsNative)
            {
                return(null);
            }
            if (instance.IsInterface)
            {
                return(null);
            }
            if (instance.IsForeign)
            {
                return(null);
            }

            var abc = instance.Abc;

            if (IsSwf)
            {
                if (!((IEnumerable <AbcFile>)SwfCompiler.AbcFrames).Contains(abc))
                {
                    return(null);
                }
            }
            else
            {
                if (abc != Abc)
                {
                    return(null);
                }
            }

            string name1 = Const.GetTypeId;
            var    name  = abc.DefineName(QName.Global(name1));
            var    trait = instance.Traits.FindMethod(name);

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

            var method = instance.DefineMethod(
                Sig.@virtual(name, AvmTypeCode.Int32),
                code => code.ReturnTypeId(type));

            method.Trait.IsOverride = IsOverrideGetTypeId(type, instance);

            //File.AppendAllText("c:\\GetTypeId.txt", type.FullName + "\n");

            if (type.Is(SystemTypeCode.Exception))
            {
                //DefinePrototype_GetType(instance, type);

                var getTypeId = _generator.Corlib.GetMethod(ObjectMethodId.GetTypeId);
                instance.DefineMethod(Sig.@from(getTypeId).@override(false), code => code.ReturnTypeId(type));

                var prototype = _generator.Corlib.GetMethod(ObjectMethodId.GetType);
                instance.DefineMethod(Sig.@from(prototype).@override(false), code =>
                {
                    code.CallAssemblyGetType(
                        () =>
                    {
                        code.LoadThis();
                        code.Call(getTypeId);
                    }
                        );
                    code.ReturnValue();
                });
            }

            return(method);
        }
Exemple #12
0
        public void TestBuiltinTypes()
        {
            var abc = new AbcFile();

            foreach (AvmTypeCode type in Enum.GetValues(typeof(AvmTypeCode)))
            {
                TestBuiltinType(abc, type);
            }

            Assert.IsTrue(ReferenceEquals(abc.BuiltinTypes[AvmTypeCode.Void], abc.DefineName(QName.Global("void"))));
            Assert.IsTrue(ReferenceEquals(abc.BuiltinTypes[AvmTypeCode.Int32], abc.DefineName(QName.Global("int"))));
            Assert.IsTrue(ReferenceEquals(abc.BuiltinTypes[AvmTypeCode.UInt32], abc.DefineName(QName.Global("uint"))));
            Assert.IsTrue(ReferenceEquals(abc.BuiltinTypes[AvmTypeCode.String], abc.DefineName(QName.Global("String"))));
        }
Exemple #13
0
        private AbcMethod BuildCtorImpl(IMethod method, AbcInstance instance)
        {
            if (!method.IsConstructor)
            {
                return(null);
            }
            if (method.IsStatic)
            {
                return(null);
            }
            var type = method.DeclaringType;

            if (!type.IsArray)
            {
                return(null);
            }

            var ctor = new AbcMethod
            {
                ReturnType = Abc.BuiltinTypes.Void
            };

            _generator.MethodBuilder.BuildParameters(ctor, method);

            string name1 = "arrctor_" + type.GetSigName();
            var    name  = Abc.DefineName(QName.Global(name1));
            var    trait = AbcTrait.CreateMethod(ctor, name);

            instance.Traits.Add(trait);

            var body = new AbcMethodBody(ctor);

            Abc.AddMethod(ctor);

            var code = new AbcCode(Abc);

            code.PushThisScope();
            code.ConstructSuper();

            //check arguments
            int n = method.Parameters.Count;

            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
                code.PushInt(0);
                var br            = code.If(BranchOperator.GreaterThanOrEqual);
                var exceptionType = _generator.Corlib.GetType(CorlibTypeId.ArgumentOutOfRangeException);
                code.ThrowException(exceptionType);
                br.BranchTarget = code.Label();
            }

            //m_rank = n
            code.LoadThis();
            code.PushInt(n);
            code.SetProperty(Const.Array.Rank);

            int varSize = n + 1;

            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
            }
            for (int i = 1; i < n; ++i)
            {
                code.Add(InstructionCode.Multiply_i);
            }
            code.SetLocal(varSize);

            //init m_value
            code.LoadThis();
            code.CreateArrayVarSize(varSize);
            code.SetProperty(Const.Array.Value);

            //init m_lengths
            code.LoadThis();
            for (int i = 0; i < n; ++i)
            {
                code.GetLocal(i + 1);
            }
            code.Add(InstructionCode.Newarray, n);
            code.SetProperty(Const.Array.Lengths);

            int varDimArr = varSize + 1;

            //init m_dims
            code.CreateArray(n - 1);
            code.SetLocal(varDimArr);

            //1, n, n * (n-1), ..., n * (n-1) * ... * n0
            for (int i = n - 2; i >= 0; --i)
            {
                int leni = i + 2;
                code.GetLocal(varDimArr);
                code.PushInt(i);

                if (i != n - 2)
                {
                    code.GetLocal(varDimArr);
                    code.PushInt(i + 1);
                    code.GetNativeArrayItem();
                    code.CoerceInt32();                     //prev

                    code.GetLocal(leni);
                    code.Add(InstructionCode.Multiply_i);                     //prev * leni
                }
                else
                {
                    code.GetLocal(leni);
                }

                code.SetNativeArrayItem();
            }

            code.LoadThis();
            code.GetLocal(varDimArr);
            code.SetProperty(Const.Array.Dims);

            var elemType = type.GetElementType();

            InitFields(code, type, elemType, 0);

            if (InternalTypeExtensions.IsInitArray(elemType))
            {
                code.InitArray(elemType,
                               () =>
                {
                    code.LoadThis();
                    code.GetProperty(Const.Array.Value);
                }, varSize);
            }

            code.ReturnVoid();

            body.Finish(code);

            return(ctor);
        }
Exemple #14
0
        /* Base Method Code
         *  public function create(... params):Object
         *      {
         *          var mainClassName:String = info()["mainClassName"];
         *
         *              if (mainClassName == null)
         *          {
         *          var url:String = loaderInfo.loaderURL;
         *          var dot:int = url.lastIndexOf(".");
         *          var slash:int = url.lastIndexOf("/");
         *          mainClassName = url.substring(slash + 1, dot);
         *          }
         *
         *              var mainClass:Class = Class(getDefinitionByName(mainClassName));
         *
         *              return mainClass ? new mainClass() : null;
         *      }
         */

        /* Flex Auto Generated Code
         *  public override function create(... params):Object
         *  {
         *      if (params.length > 0 && !(params[0] is String))
         *          return super.create.apply(this, params);
         *
         *      var mainClassName:String = params.length == 0 ? "AppName" : String(params[0]);
         *      var mainClass:Class = Class(getDefinitionByName(mainClassName));
         *      if (!mainClass)
         *          return null;
         *
         *      var instance:Object = new mainClass();
         *      if (instance is IFlexModule)
         *          (IFlexModule(instance)).moduleFactory = this;
         *      return instance;
         *  }
         */
        void BuildSystemManagerCreate(AbcFile abc, AbcInstance instance)
        {
            var method = instance.DefineMethod(
                Sig.@virtual("create", AvmTypeCode.Object).@override(),
                code =>
            {
                var typeString = abc.DefineName(QName.Global("String"));
                var typeClass  = abc.DefineName(QName.Global("Class"));
                //NOTE: getDefinitionByName is method of mx.core.SystemManager
                var getDefByName = abc.DefineName(QName.Global("getDefinitionByName"));

                const int argParams    = 1;
                const int varClassName = 2;
                const int varClass     = 3;
                const int varInstance  = 4;

                code.PushThisScope();
                code.GetLocal(argParams);
                code.GetArrayLengthInt();
                code.PushInt(0);
                var gotoCheckString = code.If(BranchOperator.GreaterThan);

                code.PushString(MainClassName);
                code.SetLocal(varClassName);
                var gotoCreate1 = code.Goto();

                //check string block
                var checkString = code.Label();
                gotoCheckString.BranchTarget = checkString;

                //params[0]
                code.GetNativeArrayItem(argParams, 0);
                code.Getlex(typeString);
                code.Add(InstructionCode.Istypelate);
                //if not str goto calling of super.create
                var ifNotStr = code.IfFalse();

                code.FindPropertyStrict(typeString);
                code.GetNativeArrayItem(argParams, 0);
                code.Call(typeString, 1);
                code.CoerceString();
                code.SetLocal(varClassName);
                var gotoCreate2 = code.Goto();

                //super.create.apply(this, params) block
                var superCreate       = code.Label();
                ifNotStr.BranchTarget = superCreate;

                code.LoadThis();
                code.GetSuper("create");
                code.LoadThis();
                code.GetLocal(argParams);
                code.ApplyFunction(2);
                code.ReturnValue();

                //instance creation block
                gotoCreate1.BranchTarget     =
                    gotoCreate2.BranchTarget = code.Label();

                // resolve class by name using SystemManager.getDefinitionByName
                //code.Trace("PFX_SYSMGR: try to resolve class using SystemManager.getDefinitionByName");
                code.FindPropertyStrict(typeClass);
                code.LoadThis();
                code.GetLocal(varClassName);
                code.Call(getDefByName, 1);
                code.Call(typeClass, 1);
                code.Coerce(typeClass);
                code.SetLocal(varClass);

                code.GetLocal(varClass);
                var ifClassNotNull1 = code.IfTrue();

                // try to resolve class using flash.system.ApplicationDomain.currentDomain
                //code.Trace("PFX_SYSMGR: try to resolve class using flash.system.ApplicationDomain.currentDomain");
                code.FindPropertyStrict(typeClass);
                GetCurrentAppDomain(code);
                code.GetLocal(varClassName);
                code.Call(abc.DefineName(QName.Global("getDefinition")), 1);
                code.Call(typeClass, 1);
                code.Coerce(typeClass);
                code.SetLocal(varClass);

                code.GetLocal(varClass);
                var ifClassNotNull2 = code.IfTrue();
                code.Trace(string.Format("PFX_SYSMGR: unable to resolve class '{0}'", MainClassName));
                code.ReturnNull();

                ifClassNotNull1.BranchTarget     =
                    ifClassNotNull2.BranchTarget = code.Label();

                // create instance of class was resolved succesfully
                code.GetLocal(varClass);
                code.Add(InstructionCode.Construct, 0);
                code.Add(InstructionCode.Coerce_o);
                code.SetLocal(varInstance);

                //if (instance is mx.core.IFlexModule)
                code.GetLocal(varInstance);
                var flexModule = _compiler.ImportType(abc, MX.IFlexModule);
                code.Getlex(flexModule);
                code.Add(InstructionCode.Istypelate);
                var gotoReturn = code.IfFalse();

                //((IFlexModule)instance).moduleFactory = this
                code.FindPropertyStrict(flexModule.Name);
                code.GetLocal(varInstance);
                code.Call(flexModule.Name, 1);
                code.LoadThis();
                code.SetPublicProperty("mx.core:IFlexModule", "moduleFactory");

                gotoReturn.BranchTarget = code.Label();
                code.GetLocal(varInstance);
                code.ReturnValue();
            });

            method.Flags |= AbcMethodFlags.NeedRest;
        }