| 17 | import java.lang.reflect.Modifier; |
| 18 | |
| 19 | public class FunctionObject extends BaseFunction { |
| 20 | @Serial private static final long serialVersionUID = 8880062939740158370L; |
| 21 | |
| 22 | /** |
| 23 | * Create a JavaScript function object from a Java method. |
| 24 | * |
| 25 | * <p>The {@code member} argument must be either a java.lang.reflect.Method or a |
| 26 | * java.lang.reflect.Constructor and must match one of two forms. |
| 27 | * |
| 28 | * <p>The first form is a member with zero or more parameters of the following types: Object, |
| 29 | * String, boolean, Scriptable, int, or double. The Long type is not supported because the |
| 30 | * double representation of a long (which is the EMCA-mandated storage type for Numbers) may |
| 31 | * lose precision. If the member is a Method, the return value must be void or one of the types |
| 32 | * allowed for parameters. |
| 33 | * |
| 34 | * <p>The runtime will perform appropriate conversions based upon the type of the parameter. A |
| 35 | * parameter type of Object specifies that no conversions are to be done. A parameter of type |
| 36 | * String will use Context.toString to convert arguments. Similarly, parameters of type double, |
| 37 | * boolean, and Scriptable will cause Context.toNumber, Context.toBoolean, and Context.toObject, |
| 38 | * respectively, to be called. |
| 39 | * |
| 40 | * <p>If the method is not static, the Java 'this' value will correspond to the JavaScript |
| 41 | * 'this' value. Any attempt to call the function with a 'this' value that is not of the right |
| 42 | * Java type will result in an error. |
| 43 | * |
| 44 | * <p>The second form is the variable arguments (or "varargs") form. If the FunctionObject will |
| 45 | * be used as a constructor, the member must have the following parameters |
| 46 | * |
| 47 | * <pre> |
| 48 | * (Context cx, Object[] args, Function ctorObj, |
| 49 | * boolean inNewExpr)</pre> |
| 50 | * |
| 51 | * and if it is a Method, be static and return an Object result. |
| 52 | * |
| 53 | * <p>Otherwise, if the FunctionObject will <i>not</i> be used to define a constructor, the |
| 54 | * member must be a static Method with parameters |
| 55 | * |
| 56 | * <pre> |
| 57 | * (Context cx, Scriptable thisObj, Object[] args, |
| 58 | * Function funObj) </pre> |
| 59 | * |
| 60 | * and an Object result. |
| 61 | * |
| 62 | * <p>When the function varargs form is called as part of a function call, the {@code args} |
| 63 | * parameter contains the arguments, with {@code thisObj} set to the JavaScript 'this' value. |
| 64 | * {@code funObj} is the function object for the invoked function. |
| 65 | * |
| 66 | * <p>When the constructor varargs form is called or invoked while evaluating a {@code new} |
| 67 | * expression, {@code args} contains the arguments, {@code ctorObj} refers to this |
| 68 | * FunctionObject, and {@code inNewExpr} is true if and only if a {@code new} expression caused |
| 69 | * the call. This supports defining a function that has different behavior when called as a |
| 70 | * constructor than when invoked as a normal function call. (For example, the Boolean |
| 71 | * constructor, when called as a function, will convert to boolean rather than creating a new |
| 72 | * object.) |
| 73 | * |
| 74 | * @param name the name of the function |
| 75 | * @param methodOrConstructor a java.lang.reflect.Method or a java.lang.reflect.Constructor that |
| 76 | * defines the object |
nothing calls this directly
no outgoing calls
no test coverage detected