示例#1
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);
        }
示例#2
0
        /// <summary>
        /// Returns instance that stores reflection data for API builtin in target player (AVM, Flash, AIR).
        /// </summary>
        /// <returns></returns>
        private AbcInstance DefineReflectionInstance()
        {
            if (_instanceReflection != null)
            {
                return(_instanceReflection);
            }
            var name = Abc.DefineName(QName.PfxPackage("PlayerReflectionData"));

            _instanceReflection = Abc.DefineEmptyInstance(name, true);
            Abc.DefineScript(_instanceReflection);
            return(_instanceReflection);
        }
示例#3
0
        private AbcInstance BuildInstance(IAssembly assembly)
        {
            var name     = Abc.DefineName(QName.PfxPackage(assembly.Name + "$runtime"));
            var instance = Abc.Instances[name];

            if (instance != null)
            {
                return(instance);
            }
            instance = Abc.DefineEmptyInstance(name, true);
            Abc.DefineScript(instance);
            return(instance);
        }
示例#4
0
        private AbcMultiname GetCastMethodName(IType type, bool me)
        {
            string name = "cast_to_";

            if (me)
            {
                name += "me";
            }
            else
            {
                name += type.GetSigName();
            }
            return(Abc.DefineName(QName.PfxPublic(name)));
        }
示例#5
0
        private AbcInstance BuildRecord(object name, params object[] args)
        {
            var mn = Abc.DefineName(name);

            var instance = Abc.DefineEmptyInstance(mn, true);

            int slotID = 1;

            for (int i = 0; i < args.Length; i += 2)
            {
                var value = instance.CreateStaticSlot(args[i], args[i + 1]);
                value.SlotId = slotID;
                ++slotID;
            }

            return(instance);
        }
示例#6
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);
        }
示例#7
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();
            }));
        }
示例#8
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);
        }