ArgumentSetter.java revision 953:221a84ef44c0
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
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 */
25package jdk.nashorn.internal.runtime;
26
27import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
28
29import jdk.nashorn.internal.codegen.CompilerConstants.Call;
30
31/**
32 * A class with static helper methods invoked from generated bytecode for setting values of parameters of variable-arity
33 * functions.
34 */
35public final class ArgumentSetter {
36    private ArgumentSetter() {}
37
38    /** Method handle for setting a function argument at a given index in an arguments object. Used from generated bytecode */
39    public static final Call SET_ARGUMENT      = staticCallNoLookup(ArgumentSetter.class, "setArgument", void.class, Object.class, ScriptObject.class, int.class);
40
41    /** Method handle for setting a function argument at a given index in an arguments array. Used from generated bytecode */
42    public static final Call SET_ARRAY_ELEMENT = staticCallNoLookup(ArgumentSetter.class, "setArrayElement", void.class, Object.class, Object[].class, int.class);
43
44
45    /**
46     * Used from generated bytecode to invoke {@link ScriptObject#setArgument(int, Object)} without having to reorder
47     * the arguments on the stack. When we're generating a store into the argument, we first have the value on the
48     * stack, and only afterwards load the target object and the index.
49     * @param value the value to write at the given argument index.
50     * @param arguments the arguments object that we're writing the value to
51     * @param key the index of the argument
52     */
53    public static void setArgument(final Object value, final ScriptObject arguments, final int key) {
54        arguments.setArgument(key, value);
55    }
56
57    /**
58     * Used from generated bytecode to set a variable arity parameter - an array element - without having to reorder
59     * the arguments on the stack. When we're generating a store into the array, we first have the value on the
60     * stack, and only afterwards load the target array and the index.
61     * @param value the value to write at the given argument index.
62     * @param arguments the arguments array that we're writing the value to
63     * @param key the index of the argument
64     */
65    public static void setArrayElement(final Object value, final Object[] arguments, final int key) {
66        arguments[key] = value;
67    }
68}
69