18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31 import static jdk.nashorn.internal.runtime.Source.sourceFor;
32
33 import java.lang.invoke.MethodHandle;
34 import java.lang.invoke.MethodHandles;
35 import java.util.List;
36 import jdk.dynalink.linker.support.Lookup;
37 import jdk.nashorn.api.scripting.JSObject;
38 import jdk.nashorn.internal.objects.annotations.Attribute;
39 import jdk.nashorn.internal.objects.annotations.Constructor;
40 import jdk.nashorn.internal.objects.annotations.Function;
41 import jdk.nashorn.internal.objects.annotations.ScriptClass;
42 import jdk.nashorn.internal.parser.Parser;
43 import jdk.nashorn.internal.runtime.Context;
44 import jdk.nashorn.internal.runtime.JSType;
45 import jdk.nashorn.internal.runtime.ParserException;
46 import jdk.nashorn.internal.runtime.PropertyMap;
47 import jdk.nashorn.internal.runtime.ScriptEnvironment;
48 import jdk.nashorn.internal.runtime.ScriptFunction;
49 import jdk.nashorn.internal.runtime.ScriptObject;
50 import jdk.nashorn.internal.runtime.ScriptRuntime;
51 import jdk.nashorn.internal.runtime.linker.Bootstrap;
52
53 /**
54 * ECMA 15.3 Function Objects
55 *
56 * Note: instances of this class are never created. This class is not even a
57 * subclass of ScriptObject. But, we use this class to generate prototype and
84 throw typeError("not.a.function", ScriptRuntime.safeToString(self));
85 }
86 return ((ScriptFunction)self).toSource();
87 }
88
89 /**
90 * ECMA 15.3.4.3 Function.prototype.apply (thisArg, argArray)
91 *
92 * @param self self reference
93 * @param thiz {@code this} arg for apply
94 * @param array array of argument for apply
95 * @return result of apply
96 */
97 @Function(attributes = Attribute.NOT_ENUMERABLE)
98 public static Object apply(final Object self, final Object thiz, final Object array) {
99 checkCallable(self);
100 final Object[] args = toApplyArgs(array);
101
102 if (self instanceof ScriptFunction) {
103 return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
104 } else if (self instanceof JSObject) {
105 return ((JSObject)self).call(thiz, args);
106 }
107 throw new AssertionError("Should not reach here");
108 }
109
110 /**
111 * Given an array-like object, converts it into a Java object array suitable for invocation of ScriptRuntime.apply
112 * or for direct invocation of the applied function.
113 * @param array the array-like object. Can be null in which case a zero-length array is created.
114 * @return the Java array
115 */
116 public static Object[] toApplyArgs(final Object array) {
117 if (array instanceof NativeArguments) {
118 return ((NativeArguments)array).getArray().asObjectArray();
119 } else if (array instanceof ScriptObject) {
120 // look for array-like object
121 final ScriptObject sobj = (ScriptObject)array;
122 final int n = lengthToInt(sobj.getLength());
123
124 final Object[] args = new Object[n];
125 for (int i = 0; i < args.length; i++) {
|
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31 import static jdk.nashorn.internal.runtime.Source.sourceFor;
32
33 import java.lang.invoke.MethodHandle;
34 import java.lang.invoke.MethodHandles;
35 import java.util.List;
36 import jdk.dynalink.linker.support.Lookup;
37 import jdk.nashorn.api.scripting.JSObject;
38 import jdk.nashorn.api.scripting.ScriptObjectMirror;
39 import jdk.nashorn.internal.objects.annotations.Attribute;
40 import jdk.nashorn.internal.objects.annotations.Constructor;
41 import jdk.nashorn.internal.objects.annotations.Function;
42 import jdk.nashorn.internal.objects.annotations.ScriptClass;
43 import jdk.nashorn.internal.parser.Parser;
44 import jdk.nashorn.internal.runtime.Context;
45 import jdk.nashorn.internal.runtime.JSType;
46 import jdk.nashorn.internal.runtime.ParserException;
47 import jdk.nashorn.internal.runtime.PropertyMap;
48 import jdk.nashorn.internal.runtime.ScriptEnvironment;
49 import jdk.nashorn.internal.runtime.ScriptFunction;
50 import jdk.nashorn.internal.runtime.ScriptObject;
51 import jdk.nashorn.internal.runtime.ScriptRuntime;
52 import jdk.nashorn.internal.runtime.linker.Bootstrap;
53
54 /**
55 * ECMA 15.3 Function Objects
56 *
57 * Note: instances of this class are never created. This class is not even a
58 * subclass of ScriptObject. But, we use this class to generate prototype and
85 throw typeError("not.a.function", ScriptRuntime.safeToString(self));
86 }
87 return ((ScriptFunction)self).toSource();
88 }
89
90 /**
91 * ECMA 15.3.4.3 Function.prototype.apply (thisArg, argArray)
92 *
93 * @param self self reference
94 * @param thiz {@code this} arg for apply
95 * @param array array of argument for apply
96 * @return result of apply
97 */
98 @Function(attributes = Attribute.NOT_ENUMERABLE)
99 public static Object apply(final Object self, final Object thiz, final Object array) {
100 checkCallable(self);
101 final Object[] args = toApplyArgs(array);
102
103 if (self instanceof ScriptFunction) {
104 return ScriptRuntime.apply((ScriptFunction)self, thiz, args);
105 } else if (self instanceof ScriptObjectMirror) {
106 return ((JSObject)self).call(thiz, args);
107 } else if (self instanceof JSObject) {
108 final Global global = Global.instance();
109 final Object result = ((JSObject) self).call(ScriptObjectMirror.wrap(thiz, global),
110 ScriptObjectMirror.wrapArray(args, global));
111 return ScriptObjectMirror.unwrap(result, global);
112 }
113 throw new AssertionError("Should not reach here");
114 }
115
116 /**
117 * Given an array-like object, converts it into a Java object array suitable for invocation of ScriptRuntime.apply
118 * or for direct invocation of the applied function.
119 * @param array the array-like object. Can be null in which case a zero-length array is created.
120 * @return the Java array
121 */
122 public static Object[] toApplyArgs(final Object array) {
123 if (array instanceof NativeArguments) {
124 return ((NativeArguments)array).getArray().asObjectArray();
125 } else if (array instanceof ScriptObject) {
126 // look for array-like object
127 final ScriptObject sobj = (ScriptObject)array;
128 final int n = lengthToInt(sobj.getLength());
129
130 final Object[] args = new Object[n];
131 for (int i = 0; i < args.length; i++) {
|