예제 #1
0
        /// <summary>
        ///     定义一个匿名函数
        /// </summary>
        /// <param name="info">
        ///     TupleStruct<Int32,bool>
        ///       @First 函数参数个数
        ///       @Second 是否是不定参数
        /// </param>
        /// <param name="body">函数体</param>
        /// <param name="label">结束标记</param>
        /// <param name="upValues">闭包变量集合</param>
        /// <returns></returns>
        internal Expression MethodDef(TupleStruct <Int32, bool> info, BlockExpression body, LabelTarget label, List <KeyValuePair <string, Int32> > upValues)
        {
            //函数体
            Expression funcBody = Expression.Block(body, Expression.Call(typeof(Instructor).GetMethod("SetReturnVoid")), Expression.Label(label));

            Action func = Expression.Lambda <Action>(funcBody).Compile();   //编译方法体

            Expression o = ScriptObject.CreateFunction(func, info.First, info.Second).GetConstantExpression();

            if (upValues == null || upValues.Count == 0)
            {
                return(o);
            }

            List <Expression> steps = new List <Expression>();

            Expression e = Expression.Call(typeof(Instructor).GetMethod("SetUpvaluesLength"), o, upValues.Count.GetConstantExpression());

            steps.Add(e);

            for (int i = 0; i < upValues.Count; i++)
            {
                KeyValuePair <string, Int32> upvalue = upValues[i];

                e = Expression.Call(typeof(Instructor).GetMethod("SetUpvalue"), o, i.GetConstantExpression(), upvalue.Value.GetConstantExpression());

                steps.Add(e);
            }

            steps.Add(o);

            return(Expression.Block(steps));
        }