Rhino.FunctionObject.FunctionObject C# (CSharp) Method

FunctionObject() public method

Create a JavaScript function object from a Java method.
Create a JavaScript function object from a Java method.

The member argument must be either a java.lang.reflect.Method or a java.lang.reflect.Constructor and must match one of two forms.

The first form is a member with zero or more parameters of the following types: Object, String, boolean, Scriptable, int, or double. The Long type is not supported because the double representation of a long (which is the EMCA-mandated storage type for Numbers) may lose precision. If the member is a Method, the return value must be void or one of the types allowed for parameters.

The runtime will perform appropriate conversions based upon the type of the parameter. A parameter type of Object specifies that no conversions are to be done. A parameter of type String will use Context.toString to convert arguments. Similarly, parameters of type double, boolean, and Scriptable will cause Context.toNumber, Context.toBoolean, and Context.toObject, respectively, to be called.

If the method is not static, the Java 'this' value will correspond to the JavaScript 'this' value. Any attempt to call the function with a 'this' value that is not of the right Java type will result in an error.

The second form is the variable arguments (or "varargs") form. If the FunctionObject will be used as a constructor, the member must have the following parameters

 (Context cx, Object[] args, Function ctorObj, boolean inNewExpr)
and if it is a Method, be static and return an Object result.

Otherwise, if the FunctionObject will not be used to define a constructor, the member must be a static Method with parameters

 (Context cx, Scriptable thisObj, Object[] args, Function funObj) 
and an Object result.

When the function varargs form is called as part of a function call, the args parameter contains the arguments, with thisObj set to the JavaScript 'this' value. funObj is the function object for the invoked function.

When the constructor varargs form is called or invoked while evaluating a new expression, args contains the arguments, ctorObj refers to this FunctionObject, and inNewExpr is true if and only if a new expression caused the call. This supports defining a function that has different behavior when called as a constructor than when invoked as a normal function call. (For example, the Boolean constructor, when called as a function, will convert to boolean rather than creating a new object.)

public FunctionObject ( string name, MemberInfo methodOrConstructor, Scriptable scope ) : System
name string the name of the function
methodOrConstructor MemberInfo /// a java.lang.reflect.Method or a java.lang.reflect.Constructor /// that defines the object ///
scope Scriptable enclosing scope of function
return System
		public FunctionObject(string name, MemberInfo methodOrConstructor, Scriptable scope)
		{
			// API class
			if (methodOrConstructor is ConstructorInfo)
			{
				member = new MemberBox((ConstructorInfo<object>)methodOrConstructor);
				isStatic = true;
			}
			else
			{
				// well, doesn't take a 'this'
				member = new MemberBox((MethodInfo)methodOrConstructor);
				isStatic = member.IsStatic();
			}
			string methodName = member.GetName();
			this.functionName = name;
			Type[] types = member.argTypes;
			int arity = types.Length;
			if (arity == 4 && (types[1].IsArray || types[2].IsArray))
			{
				// Either variable args or an error.
				if (types[1].IsArray)
				{
					if (!isStatic || types[0] != ScriptRuntime.ContextClass || types[1].GetElementType() != ScriptRuntime.ObjectClass || types[2] != ScriptRuntime.FunctionClass || types[3] != typeof(bool))
					{
						throw Context.ReportRuntimeError1("msg.varargs.ctor", methodName);
					}
					parmsLength = VARARGS_CTOR;
				}
				else
				{
					if (!isStatic || types[0] != ScriptRuntime.ContextClass || types[1] != ScriptRuntime.ScriptableClass || types[2].GetElementType() != ScriptRuntime.ObjectClass || types[3] != ScriptRuntime.FunctionClass)
					{
						throw Context.ReportRuntimeError1("msg.varargs.fun", methodName);
					}
					parmsLength = VARARGS_METHOD;
				}
			}
			else
			{
				parmsLength = arity;
				if (arity > 0)
				{
					typeTags = new byte[arity];
					for (int i = 0; i != arity; ++i)
					{
						int tag = GetTypeTag(types[i]);
						if (tag == JAVA_UNSUPPORTED_TYPE)
						{
							throw Context.ReportRuntimeError2("msg.bad.parms", types[i].FullName, methodName);
						}
						typeTags[i] = unchecked((byte)tag);
					}
				}
			}
			if (member.IsMethod())
			{
				MethodInfo method = member.Method();
				Type returnType = method.ReturnType;
				if (returnType == typeof(void))
				{
					hasVoidReturn = true;
				}
				else
				{
					returnTypeTag = GetTypeTag(returnType);
				}
			}
			else
			{
				Type ctorType = member.GetDeclaringClass();
				if (!ScriptRuntime.ScriptableClass.IsAssignableFrom(ctorType))
				{
					throw Context.ReportRuntimeError1("msg.bad.ctor.return", ctorType.FullName);
				}
			}
			ScriptRuntime.SetFunctionProtoAndParent(this, scope);
		}