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 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  29 import static jdk.nashorn.internal.lookup.Lookup.MH;
  30 
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import jdk.nashorn.internal.runtime.AccessorProperty;
  36 import jdk.nashorn.internal.runtime.Property;
  37 import jdk.nashorn.internal.runtime.PropertyMap;
  38 import jdk.nashorn.internal.runtime.ScriptFunction;
  39 import jdk.nashorn.internal.runtime.ScriptObject;
  40 import jdk.nashorn.internal.runtime.arrays.ArrayData;
  41 
  42 /**
  43  * ECMA 10.6 Arguments Object.
  44  *
  45  * Arguments object for strict mode functions.
  46  */
  47 public 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         setUserAccessors("caller", func, func);
  83         setUserAccessors("callee", func, func);
  84 
  85         setArray(ArrayData.allocate(values));
  86         this.length = values.length;
  87 
  88         // extend/truncate named arg array as needed and copy values
  89         this.namedArgs = new Object[numParams];
  90         if (numParams > values.length) {
  91             Arrays.fill(namedArgs, UNDEFINED);
  92         }
  93         System.arraycopy(values, 0, namedArgs, 0, Math.min(namedArgs.length, values.length));
  94     }
  95 
  96     @Override
  97     public String getClassName() {
  98         return "Arguments";
  99     }
 100 
 101     /**
 102      * getArgument is used for named argument access.
 103      */
 104     @Override
 105     public Object getArgument(final int key) {
 106         return (key >=0 && key < namedArgs.length) ? namedArgs[key] : UNDEFINED;
 107     }
 108 
 109     /**
 110      * setArgument is used for named argument set.
 111      */
 112     @Override
 113     public void setArgument(final int key, final Object value) {
 114         if (key >= 0 && key < namedArgs.length) {
 115             namedArgs[key] = value;
 116         }
 117     }
 118 
 119     /**
 120      * Length getter
 121      * @param self self reference
 122      * @return length property value
 123      */
 124     public static Object G$length(final Object self) {
 125         if (self instanceof NativeStrictArguments) {
 126             return ((NativeStrictArguments)self).getArgumentsLength();
 127         }
 128         return 0;
 129     }
 130 
 131     /**
 132      * Length setter
 133      * @param self self reference
 134      * @param value value for length property
 135      */
 136     public static void S$length(final Object self, final Object value) {
 137         if (self instanceof NativeStrictArguments) {
 138             ((NativeStrictArguments)self).setArgumentsLength(value);
 139         }
 140     }
 141 
 142     private Object getArgumentsLength() {
 143         return length;
 144     }
 145 
 146     private void setArgumentsLength(final Object length) {
 147         this.length = length;
 148     }
 149 
 150     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 151         return MH.findStatic(MethodHandles.lookup(), NativeStrictArguments.class, name, MH.type(rtype, types));
 152     }
 153 }