FieldObjectCreator.java revision 1261:231d6fd660b8
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.codegen; 27 28import static jdk.nashorn.internal.codegen.CompilerConstants.ARGUMENTS; 29import static jdk.nashorn.internal.codegen.CompilerConstants.constructorNoLookup; 30import static jdk.nashorn.internal.codegen.CompilerConstants.typeDescriptor; 31import static jdk.nashorn.internal.codegen.ObjectClassGenerator.PRIMITIVE_FIELD_TYPE; 32import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getFieldName; 33import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getPaddedFieldCount; 34import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex; 35import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex; 36 37import java.util.Iterator; 38import java.util.List; 39import jdk.nashorn.internal.codegen.types.Type; 40import jdk.nashorn.internal.ir.Symbol; 41import jdk.nashorn.internal.runtime.Context; 42import jdk.nashorn.internal.runtime.JSType; 43import jdk.nashorn.internal.runtime.PropertyMap; 44import jdk.nashorn.internal.runtime.ScriptObject; 45import jdk.nashorn.internal.runtime.arrays.ArrayIndex; 46 47/** 48 * Analyze an object's characteristics for appropriate code generation. This 49 * is used for functions and for objects. A field object take a set of values which 50 * to assign to the various fields in the object. This is done by the generated code 51 * 52 * @param <T> the value type for the fields being written on object creation, e.g. Node 53 * @see jdk.nashorn.internal.ir.Node 54 */ 55public abstract class FieldObjectCreator<T> extends ObjectCreator<T> { 56 57 private String fieldObjectClassName; 58 private Class<? extends ScriptObject> fieldObjectClass; 59 private int fieldCount; 60 private int paddedFieldCount; 61 private int paramCount; 62 63 /** call site flags to be used for invocations */ 64 private final int callSiteFlags; 65 /** are we creating this field object from 'eval' code? */ 66 private final boolean evalCode; 67 68 /** 69 * Constructor 70 * 71 * @param codegen code generator 72 * @param tuples tuples for fields in object 73 */ 74 FieldObjectCreator(final CodeGenerator codegen, final List<MapTuple<T>> tuples) { 75 this(codegen, tuples, false, false); 76 } 77 78 /** 79 * Constructor 80 * 81 * @param codegen code generator 82 * @param tuples tuples for fields in object 83 * @param isScope is this a scope object 84 * @param hasArguments does the created object have an "arguments" property 85 */ 86 FieldObjectCreator(final CodeGenerator codegen, final List<MapTuple<T>> tuples, final boolean isScope, final boolean hasArguments) { 87 super(codegen, tuples, isScope, hasArguments); 88 this.callSiteFlags = codegen.getCallSiteFlags(); 89 this.evalCode = codegen.isEvalCode(); 90 countFields(); 91 findClass(); 92 } 93 94 /** 95 * Construct an object. 96 * 97 * @param method the method emitter 98 */ 99 @Override 100 protected void makeObject(final MethodEmitter method) { 101 makeMap(); 102 final String className = getClassName(); 103 try { 104 // NOTE: we must load the actual structure class here, because the API operates with Nashorn Type objects, 105 // and Type objects need a loaded class, for better or worse. We also have to be specific and use the type 106 // of the actual structure class, we can't generalize it to e.g. Type.typeFor(ScriptObject.class) as the 107 // exact type information is needed for generating continuations in rest-of methods. If we didn't do this, 108 // object initializers like { x: arr[i] } would fail during deoptimizing compilation on arr[i], as the 109 // values restored from the RewriteException would be cast to "ScriptObject" instead of to e.g. "JO4", and 110 // subsequently the "PUTFIELD J04.L0" instruction in the continuation code would fail bytecode verification. 111 method._new(Context.forStructureClass(className.replace('/', '.'))).dup(); 112 } catch (final ClassNotFoundException e) { 113 throw new AssertionError(e); 114 } 115 loadMap(method); //load the map 116 117 if (isScope()) { 118 loadScope(method); 119 120 if (hasArguments()) { 121 method.loadCompilerConstant(ARGUMENTS); 122 method.invoke(constructorNoLookup(className, PropertyMap.class, ScriptObject.class, ARGUMENTS.type())); 123 } else { 124 method.invoke(constructorNoLookup(className, PropertyMap.class, ScriptObject.class)); 125 } 126 } else { 127 method.invoke(constructorNoLookup(className, PropertyMap.class)); 128 } 129 130 helpOptimisticRecognizeDuplicateIdentity(method); 131 132 // Set values. 133 final Iterator<MapTuple<T>> iter = tuples.iterator(); 134 135 while (iter.hasNext()) { 136 final MapTuple<T> tuple = iter.next(); 137 //we only load when we have both symbols and values (which can be == the symbol) 138 //if we didn't load, we need an array property 139 if (tuple.symbol != null && tuple.value != null) { 140 final int index = getArrayIndex(tuple.key); 141 method.dup(); 142 if (!isValidArrayIndex(index)) { 143 putField(method, tuple.key, tuple.symbol.getFieldIndex(), tuple); 144 } else { 145 putSlot(method, ArrayIndex.toLongIndex(index), tuple); 146 } 147 148 //this is a nop of tuple.key isn't e.g. "apply" or another special name 149 method.invalidateSpecialName(tuple.key); 150 } 151 } 152 } 153 154 @Override 155 protected PropertyMap makeMap() { 156 assert propertyMap == null : "property map already initialized"; 157 propertyMap = newMapCreator(fieldObjectClass).makeFieldMap(hasArguments(), codegen.useDualFields(), fieldCount, paddedFieldCount, evalCode); 158 return propertyMap; 159 } 160 161 /** 162 * Store a value in a field of the generated class object. 163 * 164 * @param method Script method. 165 * @param key Property key. 166 * @param fieldIndex Field number. 167 * @param tuple Tuple to store. 168 */ 169 private void putField(final MethodEmitter method, final String key, final int fieldIndex, final MapTuple<T> tuple) { 170 final Type fieldType = codegen.useDualFields() && tuple.isPrimitive() ? PRIMITIVE_FIELD_TYPE : Type.OBJECT; 171 final String fieldClass = getClassName(); 172 final String fieldName = getFieldName(fieldIndex, fieldType); 173 final String fieldDesc = typeDescriptor(fieldType.getTypeClass()); 174 175 assert fieldName.equals(getFieldName(fieldIndex, PRIMITIVE_FIELD_TYPE)) || fieldType.isObject() : key + " object keys must store to L*-fields"; 176 assert fieldName.equals(getFieldName(fieldIndex, Type.OBJECT)) || fieldType.isPrimitive() : key + " primitive keys must store to J*-fields"; 177 178 loadTuple(method, tuple); 179 180 method.putField(fieldClass, fieldName, fieldDesc); 181 } 182 183 /** 184 * Store a value in an indexed slot of a generated class object. 185 * 186 * @param method Script method. 187 * @param index Slot index. 188 * @param tuple Tuple to store. 189 */ 190 private void putSlot(final MethodEmitter method, final long index, final MapTuple<T> tuple) { 191 if (JSType.isRepresentableAsInt(index)) { 192 method.load((int)index); 193 } else { 194 method.load(index); 195 } 196 loadTuple(method, tuple, false); //we don't pack array like objects 197 method.dynamicSetIndex(callSiteFlags); 198 } 199 200 /** 201 * Locate (or indirectly create) the object container class. 202 */ 203 private void findClass() { 204 fieldObjectClassName = isScope() ? 205 ObjectClassGenerator.getClassName(fieldCount, paramCount, codegen.useDualFields()) : 206 ObjectClassGenerator.getClassName(paddedFieldCount, codegen.useDualFields()); 207 208 try { 209 this.fieldObjectClass = Context.forStructureClass(Compiler.binaryName(fieldObjectClassName)); 210 } catch (final ClassNotFoundException e) { 211 throw new AssertionError("Nashorn has encountered an internal error. Structure can not be created."); 212 } 213 } 214 215 /** 216 * Get the class name for the object class, 217 * e.g. {@code com.nashorn.oracle.scripts.JO2P0} 218 * 219 * @return script class name 220 */ 221 String getClassName() { 222 return fieldObjectClassName; 223 } 224 225 /** 226 * Tally the number of fields and parameters. 227 */ 228 private void countFields() { 229 for (final MapTuple<T> tuple : tuples) { 230 final Symbol symbol = tuple.symbol; 231 if (symbol != null) { 232 if (hasArguments() && symbol.isParam()) { 233 symbol.setFieldIndex(paramCount++); 234 } else if (!isValidArrayIndex(getArrayIndex(tuple.key))) { 235 symbol.setFieldIndex(fieldCount++); 236 } 237 } 238 } 239 240 paddedFieldCount = getPaddedFieldCount(fieldCount); 241 } 242 243} 244