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.codegen; 27 28 import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex; 29 import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import jdk.nashorn.internal.ir.Symbol; 34 import jdk.nashorn.internal.runtime.AccessorProperty; 35 import jdk.nashorn.internal.runtime.Property; 36 import jdk.nashorn.internal.runtime.PropertyMap; 37 38 /** 39 * Class that creates PropertyMap sent to script object constructors. 40 */ 41 public class MapCreator { 42 /** Object structure for objects associated with this map */ 43 private final Class<?> structure; 44 45 /** key set for object map */ 46 final List<String> keys; 47 48 /** corresponding symbol set for object map */ 49 final List<Symbol> symbols; 50 51 /** 52 * Constructor 53 * 54 * @param structure structure to generate map for (a JO subclass) 55 * @param keys list of keys for map 56 * @param symbols list of symbols for map 57 */ 58 MapCreator(final Class<?> structure, final List<String> keys, final List<Symbol> symbols) { 59 this.structure = structure; 60 this.keys = keys; 61 this.symbols = symbols; 62 } 63 64 /** 65 * Constructs a property map based on a set of fields. 66 * 67 * @param hasArguments does the created object have an "arguments" property 68 * @param fieldCount Number of fields in use. 69 * @param fieldMaximum Number of fields available. 70 * 71 * @return New map populated with accessor properties. 72 */ 73 PropertyMap makeFieldMap(final boolean hasArguments, final int fieldCount, final int fieldMaximum) { 74 final List<Property> properties = new ArrayList<>(); 75 assert keys != null; 76 77 for (int i = 0, length = keys.size(); i < length; i++) { 78 final String key = keys.get(i); 79 final Symbol symbol = symbols.get(i); 80 81 if (symbol != null && !isValidArrayIndex(getArrayIndex(key))) { 82 properties.add(new AccessorProperty(key, getPropertyFlags(symbol, hasArguments), structure, symbol.getFieldIndex())); 83 } 84 } 85 86 return PropertyMap.newMap(properties, fieldCount, fieldMaximum, 0); 87 } 88 89 PropertyMap makeSpillMap(final boolean hasArguments) { 90 final List<Property> properties = new ArrayList<>(); 91 int spillIndex = 0; 92 assert keys != null; 93 94 for (int i = 0, length = keys.size(); i < length; i++) { 95 final String key = keys.get(i); 96 final Symbol symbol = symbols.get(i); 97 98 if (symbol != null && !isValidArrayIndex(getArrayIndex(key))) { 99 properties.add(new AccessorProperty(key, getPropertyFlags(symbol, hasArguments), spillIndex++)); 100 } 101 } 102 103 return PropertyMap.newMap(properties, 0, 0, spillIndex); 104 } 105 106 /** 107 * Compute property flags given local state of a field. May be overridden and extended, 108 * 109 * @param symbol symbol to check 110 * @param hasArguments does the created object have an "arguments" property 111 * 112 * @return flags to use for fields 113 */ 114 protected int getPropertyFlags(final Symbol symbol, final boolean hasArguments) { 115 int flags = 0; 116 117 if (symbol.isParam()) { 118 flags |= Property.IS_ALWAYS_OBJECT | Property.IS_PARAMETER; 119 } 120 121 if (hasArguments) { 122 flags |= Property.IS_ALWAYS_OBJECT | Property.HAS_ARGUMENTS; 123 } 124 125 if (symbol.isScope()) { 126 flags |= Property.NOT_CONFIGURABLE; 127 } 128 129 if (symbol.canBePrimitive()) { 130 flags |= Property.CAN_BE_PRIMITIVE; 131 } 132 133 if (symbol.canBeUndefined()) { 134 flags |= Property.CAN_BE_UNDEFINED; 135 } 136 137 if (symbol.isFunctionDeclaration()) { 138 flags |= Property.IS_FUNCTION_DECLARATION; 139 } 140 141 return flags; 142 } 143 144 }