예제 #1
0
        private InternalLambdaDescription CompileInterfaceConversion(RedwoodType type)
        {
            int closureId = This.ClosureID;

            int[] slots = RuntimeUtil.GetSlotMapsToInterface(Type, type);
            List <Instruction> instructions = new List <Instruction>();

            for (int i = 0; i < slots.Length; i++)
            {
                // Get the member on our class
                instructions.Add(new LookupClosureInstruction(closureId, slots[i]));
                // Assign it as an argument for the closure
                instructions.Add(new AssignLocalInstruction(i));
            }

            if (type.CSharpType != null)
            {
                instructions.Add(
                    new BuildArrayInstruction(
                        Enumerable
                        .Range(0, slots.Length)
                        .ToArray(),
                        typeof(object)
                        )
                    );
                // Save it right past the arguments for the creation of
                // the array
                instructions.Add(new AssignLocalInstruction(slots.Length));
            }

            instructions.Add(new LoadConstantInstruction(type));
            instructions.Add(new LookupExternalMemberLambdaInstruction(
                                 "Constructor",
                                 RedwoodType.GetForCSharpType(typeof(RedwoodType))
                                 ));

            if (type.CSharpType == null)
            {
                // We already arranged all of the arguments in order
                instructions.Add(
                    new InternalCallInstruction(
                        Enumerable
                        .Range(0, slots.Length)
                        .ToArray()
                        )
                    );
            }
            else
            {
                instructions.Add(
                    new ExternalCallInstruction(
                        new int[] { slots.Length }
                        )
                    );
            }


            instructions.Add(new ReturnInstruction());

            return(new InternalLambdaDescription
            {
                argTypes = new RedwoodType[0],
                closureSize = 0,
                stackSize = slots.Length + 1,
                returnType = type,
                instructions = instructions.ToArray()
            });
        }