Exemple #1
0
        //returns true if passed object is null
        public AbcMethod IsNullImpl()
        {
            var instance = _generator.Corlib.SystemType.Instance;

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

                code.GetLocal(value);
                var notNull = code.IfNotNull();
                code.PushNativeBool(true);
                code.ReturnValue();

                notNull.BranchTarget = code.Label();
                code.Try();

                code.GetLocal(value);
                code.Nullable_HasValue(true);
                code.Add(InstructionCode.Not);
                code.ReturnValue();

                code.BeginCatch();
                code.Pop();
                code.PushNativeBool(false);
                code.ReturnValue();
                code.EndCatch(true);
            }));
        }
Exemple #2
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();
            }));
        }
Exemple #3
0
        public AbcMethod ToStringImpl()
        {
            var instance = _generator.TypeBuilder.BuildInstance(SystemTypes.String);

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

                code.IfNullReturnNull(value);

                code.If(
                    () =>
                {
                    code.GetLocal(value);
                    code.Is(AvmTypeCode.String);
                    return code.IfTrue();
                },
                    () =>
                {
                    code.GetLocal(value);
                    code.CoerceString();
                    code.ReturnValue();
                },
                    () => code.ThrowInvalidCastException()
                    );
            }));
        }
Exemple #4
0
        private AbcMethod CreateSystemArraySZ()
        {
            return(Instance.DefineMethod(
                       Sig.@static("__create_sz_array__", Instance.Name, AvmTypeCode.Int32, "size"),
                       code =>
            {
                const int varSize = 1;
                const int varArray = 2;

                code.CreateInstance(Instance);
                code.SetLocal(varArray);

                code.GetLocal(varArray);
                code.CreateArrayVarSize(varSize);
                code.SetProperty(Const.Array.Value);

                //NOTE: explicitly set rank because we did not call default ctor for System.Array
                code.GetLocal(varArray);
                code.PushInt(1);
                code.SetProperty(Const.Array.Rank);

                code.GetLocal(varArray);
                code.ReturnValue();
            }));
        }
Exemple #5
0
        private static AbcMethod GetHashCodeDic(AbcGenerator generator)
        {
            var instance = generator.RuntimeImpl.Instance;

            var dicType = generator.Abc.DefineName(QName.Package("flash.utils", "Dictionary"));
            var dic     = instance.DefineStaticSlot("hcdic$", dicType);

            return(instance.DefineMethod(
                       Sig.@static("get_hashcode_dic", dicType),
                       code =>
            {
                code.LoadThis();
                code.GetProperty(dic);
                var br = code.IfNotNull();

                code.LoadThis();
                code.CreateInstance(dicType);
                code.SetProperty(dic);

                br.BranchTarget = code.Label();
                code.LoadThis();
                code.GetProperty(dic);
                code.ReturnValue();
            }));
        }
Exemple #6
0
        private AbcMethod CastToDefaultImpl(IType type, bool cast)
        {
            const bool me = true;

            if (cast)
            {
                var AS = CastToDefaultImpl(type, false);
                return(Impl(type, AS, GetCastMethodName(type, me)));
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);
            var typeName = _generator.TypeBuilder.BuildMemberType(type);
            var name     = GetAsMethodName(type, me);

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

                code.Try();
                code.GetLocal(value);
                code.Coerce(typeName);
                code.ReturnValue();

                code.CatchReturnNull();
            }));
        }
Exemple #7
0
        public AbcMethod InitImpl(IType elemType)
        {
            if (!InternalTypeExtensions.IsInitArray(elemType))
            {
                return(null);
            }

            if (elemType.IsEnum)
            {
                elemType = elemType.ValueType;
            }

            string name = "init_" + elemType.GetSigName();

            return(Instance.DefineMethod(
                       Sig.@static(name, AvmTypeCode.Void,
                                   AvmTypeCode.Array, "arr",
                                   AvmTypeCode.Int32, "size"),
                       code =>
            {
                var init = InitImpl();
                var f = DefineInitObjectMethod(elemType);

                code.LoadThis();
                code.GetLocal(1);
                code.GetLocal(2);
                code.Getlex(f.Instance);
                code.GetProperty(f.TraitName);
                code.Call(init);
                code.ReturnVoid();
            }));
        }
Exemple #8
0
        private AbcMethod DefineCustomAttributesInitializer(AbcInstance instance, ICustomAttributeProvider provider)
        {
            instance = FixInstance(instance);

            var provname = provider.GetQName();

            if (provname == null)
            {
                provname = NameDummyCounter.ToString();
                ++NameDummyCounter;
            }
            var name = _generator.Abc.DefineName(QName.PfxPublic("init_custom_attrs_" + provname));

            return(instance.DefineMethod(
                       Sig.@static(name, _generator.Corlib.Array.Instance),
                       code =>
            {
                const int arr = 1;
                const int varAttr = 2;

                code.NewArray(arr, SystemTypes.Object, provider.CustomAttributes,
                              attr => NewAttribute(code, attr, varAttr));

                code.ReturnValue();
            }));
        }
Exemple #9
0
        /// <summary>
        /// Defines static method to call this ctor.
        /// </summary>
        /// <param name="ctor">this ctor</param>
        /// <returns></returns>
        public AbcMethod DefineCtorStaticCall(IMethod ctor)
        {
            Debug.Assert(ctor.IsConstructor);
            Debug.Assert(!ctor.IsStatic);

            var thisCtor = _generator.MethodBuilder.BuildAbcMethod(ctor);

            if (thisCtor.IsInitializer)
            {
                throw new InvalidOperationException("ctor is initializer");
            }

            var declType = ctor.DeclaringType;
            var instance = BuildInstance(declType);

            return(instance.DefineMethod(
                       Sig.@static(thisCtor.TraitName.NameString + "__static", instance.Name, thisCtor),
                       code =>
            {
                code.PushThisScope();
                code.CreateInstance(instance);
                code.Dup();
                code.LoadArguments(ctor);
                code.Call(thisCtor);
                code.ReturnValue();
            }));
        }
Exemple #10
0
        public AbcMethod Exit()
        {
            var instance = _generator.Corlib.Environment.Instance;

            return(instance.DefineMethod(
                       Sig.@static("exit_impl", AvmTypeCode.Void, AvmTypeCode.Int32, "exitCode"),
                       code =>
            {
                var isFlash = IsFlashPlayer();
                code.Getlex(isFlash);
                code.Call(isFlash);
                var ifNotFlash = code.IfFalse();

                var ns = Abc.DefinePackage("avmplus");
                var mn = Abc.DefineQName(ns, "System");
                code.Getlex(mn);
                mn = Abc.DefineQName(ns, "exit");
                code.GetLocal(1);                                 //exitCode
                code.Call(mn, 1);
                code.ReturnVoid();

                ifNotFlash.BranchTarget = code.Label();

                ns = Abc.DefinePackage("flash.System");
                mn = Abc.DefineQName(ns, "System");
                code.Getlex(mn);
                mn = Abc.DefineQName(ns, "exit");
                code.GetLocal(1);                                 //exitCode
                code.Add(InstructionCode.Coerce_u);               //???
                code.Call(mn, 1);
                code.ReturnVoid();
            }));
        }
Exemple #11
0
        private AbcMethod BoxNullable(IType type)
        {
            if (!type.IsNullableInstance())
            {
                return(null);
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);

            var arg         = type.GetTypeArgument(0);
            var argInstance = _generator.TypeBuilder.BuildInstance(arg);

            return(instance.DefineMethod(
                       Sig.@static(BoxName, argInstance.Name, type, "value"),
                       code =>
            {
                const int value = 1;
                code.If(
                    //hasValue?
                    () =>
                {
                    code.GetLocal(value);
                    code.Nullable_HasValue(true);
                    return code.IfTrue();
                },
                    () =>
                {
                    code.Box(arg, () => code.GetBoxedValue(value));
                    code.ReturnValue();
                },
                    () => code.ReturnNull()
                    );
            }));
        }
Exemple #12
0
        public AbcMethod FindClass()
        {
            var instance = _generator.Corlib.SystemType.Instance;

            return(instance.DefineMethod(
                       Sig.@static(QName.PfxPackage("FindClass"), AvmTypeCode.Class,
                                   AvmTypeCode.Namespace, "ns",
                                   AvmTypeCode.String, "name"),
                       code =>
            {
                const int ns = 1;
                const int name = 2;

                code.GetLocal(ns);
                code.GetLocal(name);
                code.FindPropertyStrict(code.Abc.RuntimeQName);

                code.GetLocal(ns);
                code.GetLocal(name);
                code.GetRuntimeProperty();

                code.CoerceClass();

                code.ReturnValue();
            }));
        }
Exemple #13
0
        private AbcMethod DefineEnumInfoInitializer(IType type)
        {
            var enumInstance = type.AbcInstance();

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

            var EnumInfo = _generator.Corlib.GetInstance(CorlibTypeId.EnumInfo);

            var name = _generator.Abc.DefineName(QName.PfxPublic("init_enum_info_" + type.GetSigName()));

            return(enumInstance.DefineMethod(
                       Sig.@static(name, EnumInfo),
                       code =>
            {
                const int varEnumInfo = 1;
                const int varArr = 2;
                code.CreateInstance(EnumInfo);
                code.SetLocal(varEnumInfo);

                if (type.HasAttribute("System.FlagsAttribute"))
                {
                    code.GetLocal(varEnumInfo);
                    code.PushBool(true);
                    code.SetProperty(Const.EnumInfo.Flags);
                }

                var fields = type.GetEnumFields();
                var utype = type.ValueType;

                code.GetLocal(varEnumInfo);

                code.NewArray(varArr, SystemTypes.Object, fields,
                              f =>
                {
                    var val = f.Value;
                    if (val == null)
                    {
                        throw new InvalidOperationException();
                    }
                    val = ToIntegralType(utype, val);
                    code.Box(type, () => code.LoadConstant(val));
                });

                code.SetProperty(Const.EnumInfo.Values);

                code.GetLocal(varEnumInfo);
                code.NewArray(varArr, SystemTypes.String, fields,
                              field => code.PushString(field.Name));
                code.SetProperty(Const.EnumInfo.Names);

                code.GetLocal(varEnumInfo);
                code.ReturnValue();
            }));
        }
Exemple #14
0
        private AbcMethod ToNullableImpl(IType type, bool cast)
        {
            if (!type.IsNullableInstance())
            {
                return(null);
            }

            const bool me = true;

            if (cast)
            {
                var AS = ToNullableImpl(type, false);
                return(Impl(type, AS, GetCastMethodName(type, me)));
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);
            var name     = GetAsMethodName(type, me);

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

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

                code.Try();

                code.If(
                    //not me?
                    () =>
                {
                    code.GetLocal(value);
                    code.As(typeName);
                    return code.IfNull();
                },
                    //not me
                    () =>
                {
                    code.NewNullable(type, value);
                    code.ReturnValue();
                },
                    //me
                    () =>
                {
                    code.GetLocal(value);
                    code.Coerce(typeName);
                    code.ReturnValue();
                }
                    );

                code.CatchReturnNull();
            }));
        }
Exemple #15
0
        private AbcMethod UnboxNullable(IType type)
        {
            if (!type.IsNullableInstance())
            {
                return(null);
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);

            var name = UnboxName;

            //Rules for operation (T?)obj
            //1. null => new T?();
            //2. if obj is T? => coerce (T?)
            //3. try unbox T

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

                code.If(
                    () =>
                {
                    code.GetLocal(value);
                    return code.IfNullable();
                },
                    () =>
                {
                    code.CreateInstance(instance);
                    code.ReturnValue();
                },
                    () => code.If(
                        () =>
                {
                    code.GetLocal(value);
                    code.As(instance.Name);
                    return code.IfNotNull();
                },
                        () =>
                {
                    code.GetLocal(value);
                    code.Coerce(instance.Name);
                    code.ReturnValue();
                },
                        () =>
                {
                    code.NewNullable(type, value);
                    code.ReturnValue();
                })
                    );
            }));
        }
Exemple #16
0
        private AbcMethod UnboxStruct(IType type)
        {
            if (type == null)
            {
                return(null);
            }
            if (type.TypeKind != TypeKind.Struct)
            {
                return(null);
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);

            var name = UnboxName;

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

                code.GetLocal(value);
                code.ThrowNullReferenceException();

                var MyNullable = _generator.TypeBuilder.BuildInstance(_generator.Corlib.MakeNullable(type));

                code.If(
                    () =>
                {
                    code.GetLocal(value);
                    code.As(MyNullable.Name);
                    return code.IfNull();
                },
                    () =>
                {
                    code.GetLocal(value);
                    code.Coerce(instance.Name);
                    code.ReturnValue();
                },
                    () =>
                {
                    code.GetBoxedValue(value);
                    code.Coerce(instance.Name);
                    code.ReturnValue();
                }
                    );
            }));
        }
Exemple #17
0
        private AbcMethod BoxPrimitive(IType type)
        {
            if (!type.IsBoxableType())
            {
                return(null);
            }

            var instance = _generator.TypeBuilder.BuildInstance(type);

            return(instance.DefineMethod(
                       Sig.@static(BoxName, instance.Name, type, "value"),
                       code =>
            {
                code.BoxVariable(instance, 1);
                code.ReturnValue();
            }));
        }
Exemple #18
0
        public AbcMethod ToArrayImpl(IType type, bool cast)
        {
            if (type == null)
            {
                return(null);
            }
            if (!type.IsArray)
            {
                return(null);
            }

            const bool me = false;

            if (cast)
            {
                var AS = ToArrayImpl(type, false);
                return(Impl(type, AS, GetCastMethodName(type, me)));
            }

            var instance = _generator.Corlib.Array.Instance;
            var typeName = _generator.TypeBuilder.BuildMemberType(type);
            var name     = GetAsMethodName(type, me);

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

                code.BeginAsMethod();

                code.Try();

                code.GetLocal(value);
                code.Coerce(typeName);

                code.PushTypeId(type);
                code.Call(ArrayMethodId.CastTo);

                code.ReturnValue();

                code.CatchReturnNull();
            }));
        }
Exemple #19
0
        public static AbcMethod CalcHashCode(AbcGenerator generator)
        {
            var instance = generator.RuntimeImpl.Instance;

            return(instance.DefineMethod(
                       Sig.@static("get_hash_code", AvmTypeCode.Int32, AvmTypeCode.Object),
                       code =>
            {
                var getDic = GetHashCodeDic(generator);

                const int varKey = 1;
                const int varDic = 2;
                const int varHC = 3;

                code.PushInt(0);
                code.SetLocal(varHC);

                code.LoadThis();
                code.Call(getDic);
                code.SetLocal(varDic);

                code.GetLocal(varDic);
                code.GetLocal(varKey);
                code.GetNativeArrayItem();                         //[]
                code.CoerceInt32();
                code.SetLocal(varHC);

                code.GetLocal(varHC);
                code.PushInt(0);
                var br = code.IfNotEquals();

                code.GetLocal(varDic);
                code.GetLocal(varKey);
                code.CallStatic(generator.Corlib.GetMethod(ObjectMethodId.NewHashCode));
                code.SetLocal(varHC);
                code.GetLocal(varHC);
                code.SetNativeArrayItem();                         //dic[key] = value

                br.BranchTarget = code.Label();
                code.GetLocal(varHC);
                code.ReturnValue();
            }));
        }
Exemple #20
0
        private AbcMethod BuildCctorCaller(AbcInstance instance)
        {
            var type = instance.Type;

            if (type == null)
            {
                return(null);
            }
            var ctor = DefineStaticCtor(instance, type);

            if (ctor == null)
            {
                return(null);
            }

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

            var I = _generator.RuntimeImpl.Instance;

            instance.StaticCtorCaller =
                I.DefineMethod(
                    Sig.@static("call_cctor." + instance.FullName, AvmTypeCode.Void),
                    code =>
            {
                //NOTE: we must init class before setting cctor flag to true
                code.Getlex(ctor);

                GetCalledFlag(code, instance);
                var br = code.IfFalse();
                code.ReturnVoid();

                br.BranchTarget = code.Label();
                SetCalledFlag(code, instance, true);

                code.Call(ctor);
                code.ReturnVoid();
            });

            return(instance.StaticCtorCaller);
        }
Exemple #21
0
        public static AbcMethod StaticCopy(AbcInstance instance)
        {
            var copy = Copy(instance);

            if (copy == null)
            {
                return(null);
            }

            var name = instance.Abc.DefineName(QName.PfxPublic("__static_copy__"));

            return(instance.DefineMethod(
                       Sig.@static(name, instance.Name, instance.Name, "value"),
                       code =>
            {
                code.GetLocal(1);                         //value
                code.Call(copy);
                code.ReturnValue();
            }));
        }
Exemple #22
0
        private AbcMethod DefineStaticCtor(AbcInstance instance, IType type)
        {
            if (type == null)
            {
                return(null);
            }
            if (instance.IsForeign)
            {
                return(null);
            }

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

            var ctor = type.GetStaticCtor();

            if (ctor != null)
            {
                return(instance.StaticCtor = _generator.MethodBuilder.BuildAbcMethod(ctor));
            }

            if (type.HasInitFields(true))
            {
                string name = type.GetStaticCtorName();
                instance.StaticCtor = instance.DefineMethod(
                    Sig.@static(name, AvmTypeCode.Void),
                    code =>
                {
                    code.PushThisScope();
                    code.InitFields(type, true, false);
                    code.ReturnVoid();
                });
                return(instance.StaticCtor);
            }

            return(null);
        }
Exemple #23
0
        private AbcMethod DefineInitObjectMethod(IType type)
        {
            var          instance = _generator.TypeBuilder.BuildInstance(type);
            AbcMultiname name;

            if (instance.IsNative)
            {
                instance = _generator.Corlib.Object.Instance;
                name     = _generator.Abc.DefineName(QName.PfxPublic("initobj_" + type.GetSigName()));
            }
            else
            {
                name = _generator.Abc.DefineName(QName.PfxPublic("initobj"));
            }
            return(instance.DefineMethod(
                       Sig.@static(name, AvmTypeCode.Object),
                       code =>
            {
                code.InitObject(type);
                code.ReturnValue();
            }));
        }
Exemple #24
0
        private AbcMethod DefineMethodsInitializer(AbcInstance instance, IType type, bool ctor)
        {
            var    provname = type.GetSigName();
            string prefix   = "init_methods_";

            if (ctor)
            {
                prefix = "init_ctors_";
            }

            var name = _generator.Abc.DefineName(QName.PfxPublic(prefix + provname));

            var methods =
                new List <IMethod>(type.Methods.Where(m => m.IsConstructor == ctor && !IsUnsupportedMethod(m)));

            var mtype = ctor
                                                        ? _generator.Corlib.GetType(CorlibTypeId.ConstructorInfo)
                                                        : _generator.Corlib.GetType(CorlibTypeId.MethodInfo);

            instance = FixInstance(instance);

            return(instance.DefineMethod(
                       Sig.@static(name, _generator.Corlib.Array.Instance),
                       code =>
            {
                const int arr = 1;
                const int varObj = 2;
                const int varParams = 3;
                const int varParam = 4;

                code.NewArray(arr, mtype, methods,
                              (method, index) => NewMethodInfo(code, instance, method,
                                                               varObj, varParams, varParam,
                                                               mtype, index));
                code.ReturnValue();
            }));
        }
Exemple #25
0
        private AbcMethod InitImpl()
        {
            return(Instance.DefineMethod(
                       Sig.@static("init_core", AvmTypeCode.Void,
                                   AvmTypeCode.Array, "arr",
                                   AvmTypeCode.Int32, "size",
                                   AvmTypeCode.Function, "f"),
                       code =>
            {
                const int arr = 1;
                const int size = 2;
                const int func = 3;

                code.While(
                    () =>
                {
                    code.GetLocal(size);
                    code.Add(InstructionCode.Decrement_i);
                    code.SetLocal(size);
                    code.GetLocal(size);
                    code.Add(InstructionCode.Pushbyte, 0);
                    return code.If(BranchOperator.LessThan);
                },
                    () =>
                {
                    code.GetLocal(arr);
                    code.GetLocal(size);
                    code.GetLocal(func);
                    code.PushNull();
                    code.CallClosure(0);
                    code.SetNativeArrayItem();
                }
                    );

                code.ReturnVoid();
            }));
        }
Exemple #26
0
        private AbcMethod DefineMyFieldsInitializer(AbcInstance instance, IType type)
        {
            var myfields = new List <IField>(type.Fields.Where(f => !f.IsStatic));

            if (myfields.Count == 0)
            {
                return(null);
            }

            instance = FixInstance(instance);

            var name = _generator.Abc.DefineName(QName.PfxPublic("init_myfields_" + GetMethodSuffix(type)));

            return(instance.DefineMethod(
                       Sig.@static(name, _generator.Corlib.Array.Instance),
                       code =>
            {
                var typeFieldInfo = _generator.Corlib.GetType(CorlibTypeId.FieldInfo);
                const int arr = 1;
                code.NewArray(arr, typeFieldInfo, myfields,
                              f => NewFieldInfo(code, instance, f, 2));
                code.ReturnValue();
            }));
        }
Exemple #27
0
        /// <summary>
        /// m_properties initializer
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private AbcMethod DefinePropertiesInitializer(AbcInstance instance, IType type)
        {
            var provname = type.GetSigName();

            if (provname == null)
            {
                provname = NameDummyCounter.ToString();
                ++NameDummyCounter;
            }
            var name = _generator.Abc.DefineName(QName.PfxPublic("init_properties_" + provname));

            instance = FixInstance(instance);

            return(instance.DefineMethod(
                       Sig.@static(name, _generator.Corlib.Array.Instance),
                       code =>
            {
                var mtype = _generator.Corlib.GetType(CorlibTypeId.PropertyInfo);
                const int arr = 1;
                code.NewArray(arr, mtype, type.Properties,
                              property => NewPropertyInfo(code, instance, property, 2));
                code.ReturnValue();
            }));
        }
Exemple #28
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 #29
0
        private AbcMethod DefineTestRunner(IMethod test)
        {
            var method = _generator.MethodBuilder.BuildAbcMethod(test);

            var    instance = method.Instance;
            string name     = "run_test_" + test.GetMonoTestCaseName();

            name = name.Replace('.', '_');

            return(instance.DefineMethod(
                       Sig.@static(name, AvmTypeCode.Void, GetInstance(NUnitTypeId.Test)),
                       code =>
            {
                var testFixture = test.DeclaringType;
                const int varTF = 2;
                const int varErr = 3;

                //TODO: Redirect Console, Debug output

                var ee = test.GetExpectedExceptionType();
                var setup = testFixture.GetUnitTestSetup();
                AbcMethod setupAM = null;
                if (setup != null)
                {
                    setupAM = _generator.MethodBuilder.BuildAbcMethod(setup);
                }

                Test_Success(code, true);
                Test_Executed(code, true);

                code.ConsoleOpenSW();

                code.Try();

                #region setup & call
                if (test.IsStatic)
                {
                    if (setup != null && setup.IsStatic)
                    {
                        code.Getlex(setupAM);
                        code.Call(setupAM);
                    }

                    code.Getlex(method);
                    code.Call(method);
                }
                else
                {
                    code.CreateInstance(testFixture, true);
                    code.CoerceAnyType();
                    code.SetLocal(varTF);

                    if (setup != null)
                    {
                        code.GetLocal(varTF);
                        code.Call(setupAM);
                    }

                    code.GetLocal(varTF);
                    code.Call(method);
                }
                #endregion

                if (ee != null)
                {
                    code.ConsoleCloseSW(true);
                    Test_Success(code, false);
                    Test_Output(code, "No expected exception: {0}", ee.FullName);
                }
                else
                {
                    Test_Output(code, () => code.ConsoleCloseSW(false));
                }

                code.ReturnVoid();

                if (ee != null)
                {
                    code.BeginCatch(_generator.TypeBuilder.BuildInstance(ee), false);
                    code.Pop();
                    Test_Success(code, true);
                    Test_Output(code, () => code.ConsoleCloseSW(false));
                    code.ReturnVoid();
                    code.EndCatch(false);
                }

                code.BeginCatch();
                code.CoerceAnyType();
                code.SetLocal(varErr);
                code.ConsoleCloseSW(true);
                Test_Success(code, false);
                Test_Output(
                    code,
                    () =>
                {
                    code.PushString("Unexpected exception: ");
                    code.GetLocal(varErr);
                    code.GetErrorMessage();
                    code.Add(InstructionCode.Add);
                });
                Test_StackTrace(
                    code,
                    () =>
                {
                    code.GetLocal(varErr);
                    code.GetErrorStackTrace();
                });
                code.EndCatch(true);

                code.ReturnVoid();
            }));
        }
Exemple #30
0
        internal AbcInstance DefineFlexInitMixin(AbcFile app)
        {
            var flexModuleFactoryInterface = FlexTypes.GetFlexModuleFactoryInterface(app);
            var childManagerInstance       = FlexTypes.GetChildManagerInstance(app);
            var flex4 = childManagerInstance != null;

            string name = "_FlexInit_" + _compiler.FlexAppPrefix;
            string ns   = _compiler.RootNamespace;

            var instance = new AbcInstance(true)
            {
                Name            = app.DefineName(QName.Package(ns, name)),
                BaseTypeName    = app.BuiltinTypes.Object,
                IsMixin         = true,
                IsFlexInitMixin = true,
                Initializer     = app.DefineMethod(
                    Sig.@this(null, AvmTypeCode.Void),
                    code =>
                {
                    code.ConstructSuper();
                    code.ReturnVoid();
                }),
                Class = { Initializer = app.DefineEmptyMethod() }
            };

            app.AddInstance(instance);

            instance.DefineMethod(
                Sig.@static("init", AvmTypeCode.Void, flexModuleFactoryInterface, "f"),
                code =>
            {
                code.PushThisScope();

                const int moduleFactoryArg = 1;
                const int styleManagerVar  = 2;

                if (flex4)
                {
                    CreateInstance(code, childManagerInstance, moduleFactoryArg);
                    code.Pop();

                    var styleManager2    = FlexTypes.GetStyleManager2Interface(app);
                    var styleManagerImpl = FlexTypes.GetStyleManagerImpl(app);
                    CreateInstance(code, styleManagerImpl, moduleFactoryArg);
                    code.Coerce(styleManager2);
                    code.SetLocal(styleManagerVar);
                }

                RegisterEffectTriggers(app, code);
                RegisterRemoteClasses(app, code);

                Action pushStyleManager;
                if (flex4)
                {
                    pushStyleManager = () => code.GetLocal(styleManagerVar);
                }
                else
                {
                    pushStyleManager = () => code.Getlex(FlexTypes.GetStyleManagerInstance(app));
                }

                RegisterInheritStyles(app, code, pushStyleManager, flex4);

                //NOTE: Uncomment to add forward refernce to Flex Application
                //var appInstance = app.generator.MainInstance;
                //code.Trace(string.Format("PFC: forward reference to FlexApp class {0}", appInstance.FullName));
                //code.Getlex(appInstance);
                //code.Pop();

                code.ReturnVoid();
            });

            return(instance);
        }