15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
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.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
36
37 /**
38 * A container for data needed to instantiate a specific {@link ScriptFunction} at runtime.
39 * Instances of this class are created during codegen and stored in script classes'
40 * constants array to reduce function instantiation overhead during runtime.
41 */
42 public abstract class ScriptFunctionData {
43
44 /** Name of the function or "" for anonynous functions */
45 protected final String name;
46
47 /** All versions of this function that have been generated to code */
48 protected final CompiledFunctions code;
49
50 /** Function flags */
51 protected int flags;
52
53 private int arity;
54
355 final MethodType newType = type.changeParameterType(0, Object.class).changeParameterType(1, ScriptFunction.class);
356 final int[] reorder = new int[type.parameterCount()];
357 reorder[0] = 1;
358 assert reorder[1] == 0;
359 for (int i = 2; i < reorder.length; ++i) {
360 reorder[i] = i;
361 }
362 return MethodHandles.permuteArguments(mh, newType, reorder);
363 }
364
365 /**
366 * Convert this argument for non-strict functions according to ES 10.4.3
367 *
368 * @param thiz the this argument
369 *
370 * @return the converted this object
371 */
372 private Object convertThisObject(final Object thiz) {
373 if (!(thiz instanceof ScriptObject) && needsWrappedThis()) {
374 if (JSType.nullOrUndefined(thiz)) {
375 return Context.getGlobalTrusted();
376 }
377
378 if (isPrimitiveThis(thiz)) {
379 return ((GlobalObject)Context.getGlobalTrusted()).wrapAsObject(thiz);
380 }
381 }
382
383 return thiz;
384 }
385
386 static boolean isPrimitiveThis(final Object obj) {
387 return obj instanceof String || obj instanceof ConsString ||
388 obj instanceof Number || obj instanceof Boolean;
389 }
390
391 /**
392 * Creates an invoker method handle for a bound function.
393 *
394 * @param targetFn the function being bound
395 * @param originalInvoker an original invoker method handle for the function. This can be its generic invoker or
396 * any of its specializations.
397 * @param self the "this" value being bound
398 * @param args additional arguments being bound
399 *
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
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.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import jdk.nashorn.internal.objects.Global;
36 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
37
38 /**
39 * A container for data needed to instantiate a specific {@link ScriptFunction} at runtime.
40 * Instances of this class are created during codegen and stored in script classes'
41 * constants array to reduce function instantiation overhead during runtime.
42 */
43 public abstract class ScriptFunctionData {
44
45 /** Name of the function or "" for anonynous functions */
46 protected final String name;
47
48 /** All versions of this function that have been generated to code */
49 protected final CompiledFunctions code;
50
51 /** Function flags */
52 protected int flags;
53
54 private int arity;
55
356 final MethodType newType = type.changeParameterType(0, Object.class).changeParameterType(1, ScriptFunction.class);
357 final int[] reorder = new int[type.parameterCount()];
358 reorder[0] = 1;
359 assert reorder[1] == 0;
360 for (int i = 2; i < reorder.length; ++i) {
361 reorder[i] = i;
362 }
363 return MethodHandles.permuteArguments(mh, newType, reorder);
364 }
365
366 /**
367 * Convert this argument for non-strict functions according to ES 10.4.3
368 *
369 * @param thiz the this argument
370 *
371 * @return the converted this object
372 */
373 private Object convertThisObject(final Object thiz) {
374 if (!(thiz instanceof ScriptObject) && needsWrappedThis()) {
375 if (JSType.nullOrUndefined(thiz)) {
376 return Context.getGlobal();
377 }
378
379 if (isPrimitiveThis(thiz)) {
380 return Context.getGlobal().wrapAsObject(thiz);
381 }
382 }
383
384 return thiz;
385 }
386
387 static boolean isPrimitiveThis(final Object obj) {
388 return obj instanceof String || obj instanceof ConsString ||
389 obj instanceof Number || obj instanceof Boolean;
390 }
391
392 /**
393 * Creates an invoker method handle for a bound function.
394 *
395 * @param targetFn the function being bound
396 * @param originalInvoker an original invoker method handle for the function. This can be its generic invoker or
397 * any of its specializations.
398 * @param self the "this" value being bound
399 * @param args additional arguments being bound
400 *
|