NativeStrictArguments.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 */
25
26package jdk.nashorn.internal.objects;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
30
31import java.lang.invoke.MethodHandle;
32import java.lang.invoke.MethodHandles;
33import java.util.ArrayList;
34import java.util.Arrays;
35import jdk.nashorn.internal.runtime.AccessorProperty;
36import jdk.nashorn.internal.runtime.Property;
37import jdk.nashorn.internal.runtime.PropertyMap;
38import jdk.nashorn.internal.runtime.ScriptFunction;
39import jdk.nashorn.internal.runtime.ScriptObject;
40import jdk.nashorn.internal.runtime.arrays.ArrayData;
41
42/**
43 * ECMA 10.6 Arguments Object.
44 *
45 * Arguments object for strict mode functions.
46 */
47public final class NativeStrictArguments extends ScriptObject {
48
49    private static final MethodHandle G$LENGTH = findOwnMH("G$length", Object.class, Object.class);
50
51    private static final MethodHandle S$LENGTH = findOwnMH("S$length", void.class, Object.class, Object.class);
52
53    // property map for strict mode arguments object
54    private static final PropertyMap map$;
55
56    static {
57        final ArrayList<Property> properties = new ArrayList<>(1);
58        properties.add(AccessorProperty.create("length", Property.NOT_ENUMERABLE, G$LENGTH, S$LENGTH));
59        PropertyMap map = PropertyMap.newMap(properties);
60        // In strict mode, the caller and callee properties should throw TypeError
61        // Need to add properties directly to map since slots are assigned speculatively by newUserAccessors.
62        final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE;
63        map = map.addPropertyNoHistory(map.newUserAccessors("caller", flags));
64        map = map.addPropertyNoHistory(map.newUserAccessors("callee", flags));
65        map$ = map;
66    }
67
68    static PropertyMap getInitialMap() {
69        return map$;
70    }
71
72    private Object   length;
73    private final Object[] namedArgs;
74
75    NativeStrictArguments(final Object[] values, final int numParams,final ScriptObject proto, final PropertyMap map) {
76        super(proto, map);
77        setIsArguments();
78
79        final ScriptFunction func = Global.instance().getTypeErrorThrower();
80        // We have to fill user accessor functions late as these are stored
81        // in this object rather than in the PropertyMap of this object.
82        final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE;
83        initUserAccessors("caller", flags, func, func);
84        initUserAccessors("callee", flags, func, func);
85
86        setArray(ArrayData.allocate(values));
87        this.length = values.length;
88
89        // extend/truncate named arg array as needed and copy values
90        this.namedArgs = new Object[numParams];
91        if (numParams > values.length) {
92            Arrays.fill(namedArgs, UNDEFINED);
93        }
94        System.arraycopy(values, 0, namedArgs, 0, Math.min(namedArgs.length, values.length));
95    }
96
97    @Override
98    public String getClassName() {
99        return "Arguments";
100    }
101
102    /**
103     * getArgument is used for named argument access.
104     */
105    @Override
106    public Object getArgument(final int key) {
107        return (key >=0 && key < namedArgs.length) ? namedArgs[key] : UNDEFINED;
108    }
109
110    /**
111     * setArgument is used for named argument set.
112     */
113    @Override
114    public void setArgument(final int key, final Object value) {
115        if (key >= 0 && key < namedArgs.length) {
116            namedArgs[key] = value;
117        }
118    }
119
120    /**
121     * Length getter
122     * @param self self reference
123     * @return length property value
124     */
125    public static Object G$length(final Object self) {
126        if (self instanceof NativeStrictArguments) {
127            return ((NativeStrictArguments)self).getArgumentsLength();
128        }
129        return 0;
130    }
131
132    /**
133     * Length setter
134     * @param self self reference
135     * @param value value for length property
136     */
137    public static void S$length(final Object self, final Object value) {
138        if (self instanceof NativeStrictArguments) {
139            ((NativeStrictArguments)self).setArgumentsLength(value);
140        }
141    }
142
143    private Object getArgumentsLength() {
144        return length;
145    }
146
147    private void setArgumentsLength(final Object length) {
148        this.length = length;
149    }
150
151    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
152        return MH.findStatic(MethodHandles.lookup(), NativeStrictArguments.class, name, MH.type(rtype, types));
153    }
154}
155