Пример #1
0
        private static WrappedIndexerSetter WrapIndexerSetterCore(MethodInfo method)
        {
            var builder = new Builder();

            builder.AddGlobalParameter();
            builder.AddThisParameter(typeof(JsObject));
            var indexParameter = builder.AddParameter(typeof(object), "index");
            var argument = builder.AddParameter(typeof(object), "value");

            var parameters = method.GetParameters();

            builder.AddStatement(Expression.Call(
                builder.MarshalThis(method.DeclaringType, method.IsStatic),
                method,
                builder.Marshal(indexParameter, parameters[0].ParameterType),
                builder.Marshal(argument, parameters[1].ParameterType)
            ));

            return builder.Compile<WrappedIndexerSetter>();
        }
Пример #2
0
        private static WrappedConstructor WrapConstructorCore(ConstructorInfo constructor)
        {
            var builder = new Builder();

            builder.AddGlobalParameter();
            var argumentsParameter = builder.AddParameter(typeof(object[]), "arguments");

            var parameters = constructor.GetParameters();
            var arguments = new List<Expression>();

            for (int i = 0; i < parameters.Length; i++)
            {
                // Get the argument from the array, or undefined there is none,
                // and marshal it.

                arguments.Add(builder.Marshal(
                    Expression.Condition(
                        Expression.GreaterThan(
                            Expression.ArrayLength(argumentsParameter),
                            Expression.Constant(i)
                        ),
                        Expression.ArrayAccess(
                            argumentsParameter,
                            Expression.Constant(i)
                        ),
                        Expression.Field(null, typeof(JsUndefined).GetField("Instance")),
                        typeof(object)
                    ),
                    parameters[i].ParameterType
                ));
            }

            // Construct the new object. This does **NOT** un-marshal the
            // result; this is done at the call site.

            builder.AddStatement(Expression.Convert(
                Expression.New(
                    constructor,
                    arguments
                ),
                typeof(object)
            ));

            return builder.Compile<WrappedConstructor>();
        }