Example #1
0
        /// <summary>
        /// Creates a new FunctionBinderMethod instance.
        /// </summary>
        /// <param name="method"> The method to call. </param>
        /// <param name="flags"> Flags that modify the binding process. </param>
        private void Init(JSFunctionFlags flags)
        {
            this.Flags = flags;
            this.HasEngineParameter = (flags & JSFunctionFlags.HasEngineParameter) != 0;
            if (this.HasEngineParameter == true && this.Method.IsStatic == false)
                throw new InvalidOperationException(string.Format("The {0} flag cannot be used on the instance method '{1}'.", JSFunctionFlags.HasEngineParameter, this.Name));
            this.HasExplicitThisParameter = (flags & JSFunctionFlags.HasThisObject) != 0;
            if (this.HasExplicitThisParameter == true && this.Method.IsStatic == false)
                throw new InvalidOperationException(string.Format("The {0} flag cannot be used on the instance method '{1}'.", JSFunctionFlags.HasThisObject, this.Name));

            var parameters = this.Method.GetParameters();

            // If HasEngineParameter is specified, the first parameter must be of type ScriptEngine.
            if (this.HasEngineParameter == true)
            {
                if (parameters.Length == 0)
                    throw new InvalidOperationException(string.Format("The method '{0}' does not have enough parameters.", this.Name));
                if (parameters[0].ParameterType != typeof(ScriptEngine))
                    throw new InvalidOperationException(string.Format("The first parameter of the method '{0}' must be of type ScriptEngine.", this.Name));
            }

            // If there is a "this" parameter, it must be of type ObjectInstance (or derived from it).
            if (this.HasExplicitThisParameter == true)
            {
                if (parameters.Length <= (this.HasEngineParameter ? 1 : 0))
                    throw new InvalidOperationException(string.Format("The method '{0}' does not have enough parameters.", this.Name));
                this.ExplicitThisType = parameters[this.HasEngineParameter ? 1 : 0].ParameterType;
            }
            /*else if (method.IsStatic == false)
            {
                this.ThisType = method.DeclaringType;
            }*/

            // Calculate the min and max parameter count.

            // Check the parameter types (the this parameter has already been checked).
            // Only certain types are supported.
            int start = (this.HasExplicitThisParameter ? 1 : 0) + (this.HasEngineParameter ? 1 : 0);
            for (int i = start; i < parameters.Length; i++)
            {
                Type type = parameters[i].ParameterType;

                // ParamArray types must be an array of a supported type.
                if (this.HasParamArray == true && i == parameters.Length - 1)
                {
                    if (type.IsArray == false)
                        throw new NotImplementedException(string.Format("Unsupported varargs parameter type '{0}'.", type));
                    type = type.GetElementType();
                }

                if (type != typeof(bool) &&
                    type != typeof(int) &&
                    type != typeof(double) &&
                    type != typeof(string) &&
                    type != typeof(object) &&
                    typeof(ObjectInstance).IsAssignableFrom(type) == false)
                    throw new NotImplementedException(string.Format("Unsupported parameter type '{0}'.", type));
            }
        }
Example #2
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new FunctionBinderMethod instance.
        /// </summary>
        /// <param name="method"> The method to call. </param>
        /// <param name="flags"> Flags that modify the binding process. </param>
        public JSBinderMethod(MethodInfo method, JSFunctionFlags flags = JSFunctionFlags.None)
            : base(method)
        {
            Init(flags);
        }
Example #3
0
        /// <summary>
        /// Creates a new FunctionBinderMethod instance.
        /// </summary>
        /// <param name="flags"> Flags that modify the binding process. </param>
        private void Init(JSFunctionFlags flags)
        {
            this.Flags = flags;
            this.HasEngineParameter = (flags & JSFunctionFlags.HasEngineParameter) != 0;
            if (this.HasEngineParameter == true && this.Method.IsStatic == false)
            {
                throw new InvalidOperationException(string.Format("The {0} flag cannot be used on the instance method '{1}'.", JSFunctionFlags.HasEngineParameter, this.Name));
            }
            this.HasExplicitThisParameter = (flags & JSFunctionFlags.HasThisObject) != 0;
            if (this.HasExplicitThisParameter == true && this.Method.IsStatic == false)
            {
                throw new InvalidOperationException(string.Format("The {0} flag cannot be used on the instance method '{1}'.", JSFunctionFlags.HasThisObject, this.Name));
            }

            var parameters = this.Method.GetParameters();

            // If HasEngineParameter is specified, the first parameter must be of type ScriptEngine.
            if (this.HasEngineParameter == true)
            {
                if (parameters.Length == 0)
                {
                    throw new InvalidOperationException(string.Format("The method '{0}' does not have enough parameters.", this.Name));
                }
                if (parameters[0].ParameterType != typeof(ScriptEngine))
                {
                    throw new InvalidOperationException(string.Format("The first parameter of the method '{0}' must be of type ScriptEngine.", this.Name));
                }
            }

            // If there is a "this" parameter, it must be of type ObjectInstance (or derived from it).
            if (this.HasExplicitThisParameter == true)
            {
                if (parameters.Length <= (this.HasEngineParameter ? 1 : 0))
                {
                    throw new InvalidOperationException(string.Format("The method '{0}' does not have enough parameters.", this.Name));
                }
                this.ExplicitThisType = parameters[this.HasEngineParameter ? 1 : 0].ParameterType;
            }

            /*else if (method.IsStatic == false)
             * {
             *  this.ThisType = method.DeclaringType;
             * }*/

            // Calculate the min and max parameter count.

            // Check the parameter types (the this parameter has already been checked).
            // Only certain types are supported.
            int start = (this.HasExplicitThisParameter ? 1 : 0) + (this.HasEngineParameter ? 1 : 0);

            for (int i = start; i < parameters.Length; i++)
            {
                Type type = parameters[i].ParameterType;

                // ParamArray types must be an array of a supported type.
                if (this.HasParamArray == true && i == parameters.Length - 1)
                {
                    if (type.IsArray == false)
                    {
                        throw new NotImplementedException(string.Format("Unsupported varargs parameter type '{0}'.", type));
                    }
                    type = type.GetElementType();
                }

                if (type != typeof(bool) &&
                    type != typeof(int) &&
                    type != typeof(double) &&
                    type != typeof(string) &&
                    type != typeof(object) &&
                    typeof(ObjectInstance).IsAssignableFrom(type) == false)
                {
                    throw new NotImplementedException(string.Format("Unsupported parameter type '{0}'.", type));
                }
            }
        }
Example #4
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new FunctionBinderMethod instance.
        /// </summary>
        /// <param name="method"> The method to call. </param>
        /// <param name="flags"> Flags that modify the binding process. </param>
        public JSBinderMethod(MethodInfo method, JSFunctionFlags flags = JSFunctionFlags.None)
            : base(method)
        {
            Init(flags);
        }
Example #5
0
 /// <summary>
 /// Creates a new BaseJSFunctionAttribute instance.
 /// </summary>
 /// <param name="flags"> One or more flags. </param>
 protected BaseJSFunctionAttribute(JSFunctionFlags flags)
 {
     this.Flags = flags;
 }
Example #6
0
 /// <summary>
 /// Creates a new BaseJSFunctionAttribute instance.
 /// </summary>
 /// <param name="flags"> One or more flags. </param>
 protected BaseJSFunctionAttribute(JSFunctionFlags flags)
 {
     this.Flags = flags;
 }