PrototypeGenerator.java revision 877:cf4d2252d444
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.tools.nasgen; 27 28import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL; 29import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC; 30import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER; 31import static jdk.internal.org.objectweb.asm.Opcodes.V1_7; 32import static jdk.nashorn.internal.tools.nasgen.StringConstants.DEFAULT_INIT_DESC; 33import static jdk.nashorn.internal.tools.nasgen.StringConstants.INIT; 34import static jdk.nashorn.internal.tools.nasgen.StringConstants.OBJECT_DESC; 35import static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_DESC; 36import static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_FIELD_NAME; 37import static jdk.nashorn.internal.tools.nasgen.StringConstants.PROTOTYPEOBJECT_TYPE; 38import static jdk.nashorn.internal.tools.nasgen.StringConstants.PROTOTYPE_SUFFIX; 39import static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTOBJECT_INIT_DESC; 40 41import java.io.FileOutputStream; 42import java.io.IOException; 43 44/** 45 * This class generates prototype class for a @ClassInfo annotated class. 46 * 47 */ 48public class PrototypeGenerator extends ClassGenerator { 49 private final ScriptClassInfo scriptClassInfo; 50 private final String className; 51 private final int memberCount; 52 53 PrototypeGenerator(final ScriptClassInfo sci) { 54 this.scriptClassInfo = sci; 55 this.className = scriptClassInfo.getPrototypeClassName(); 56 this.memberCount = scriptClassInfo.getPrototypeMemberCount(); 57 } 58 59 byte[] getClassBytes() { 60 // new class extensing from ScriptObject 61 cw.visit(V1_7, ACC_FINAL | ACC_SUPER, className, null, PROTOTYPEOBJECT_TYPE, null); 62 if (memberCount > 0) { 63 // add fields 64 emitFields(); 65 // add <clinit> 66 emitStaticInitializer(); 67 } 68 69 // add <init> 70 emitConstructor(); 71 72 // add getClassName() 73 emitGetClassName(scriptClassInfo.getName()); 74 75 cw.visitEnd(); 76 return cw.toByteArray(); 77 } 78 79 // --Internals only below this point 80 private void emitFields() { 81 // introduce "Function" type instance fields for each 82 // prototype @Function in script class info 83 for (MemberInfo memInfo : scriptClassInfo.getMembers()) { 84 if (memInfo.isPrototypeFunction()) { 85 addFunctionField(memInfo.getJavaName()); 86 memInfo = (MemberInfo)memInfo.clone(); 87 memInfo.setJavaDesc(OBJECT_DESC); 88 addGetter(className, memInfo); 89 addSetter(className, memInfo); 90 } else if (memInfo.isPrototypeProperty()) { 91 if (memInfo.isStaticFinal()) { 92 addGetter(scriptClassInfo.getJavaName(), memInfo); 93 } else { 94 addField(memInfo.getJavaName(), memInfo.getJavaDesc()); 95 memInfo = (MemberInfo)memInfo.clone(); 96 memInfo.setJavaAccess(ACC_PUBLIC); 97 addGetter(className, memInfo); 98 addSetter(className, memInfo); 99 } 100 } 101 } 102 103 addMapField(); 104 } 105 106 private void emitStaticInitializer() { 107 final MethodGenerator mi = makeStaticInitializer(); 108 emitStaticInitPrefix(mi, className, memberCount); 109 for (final MemberInfo memInfo : scriptClassInfo.getMembers()) { 110 if (memInfo.isPrototypeFunction() || memInfo.isPrototypeProperty()) { 111 linkerAddGetterSetter(mi, className, memInfo); 112 } else if (memInfo.isPrototypeGetter()) { 113 final MemberInfo setter = scriptClassInfo.findSetter(memInfo); 114 linkerAddGetterSetter(mi, scriptClassInfo.getJavaName(), memInfo, setter); 115 } 116 } 117 emitStaticInitSuffix(mi, className); 118 } 119 120 private void emitConstructor() { 121 final MethodGenerator mi = makeConstructor(); 122 mi.visitCode(); 123 mi.loadThis(); 124 if (memberCount > 0) { 125 // call "super(map$)" 126 mi.getStatic(className, PROPERTYMAP_FIELD_NAME, PROPERTYMAP_DESC); 127 // make sure we use duplicated PropertyMap so that original map 128 // stays intact and so can be used for many global. 129 mi.invokeSpecial(PROTOTYPEOBJECT_TYPE, INIT, SCRIPTOBJECT_INIT_DESC); 130 // initialize Function type fields 131 initFunctionFields(mi); 132 } else { 133 // call "super()" 134 mi.invokeSpecial(PROTOTYPEOBJECT_TYPE, INIT, DEFAULT_INIT_DESC); 135 } 136 mi.returnVoid(); 137 mi.computeMaxs(); 138 mi.visitEnd(); 139 } 140 141 private void initFunctionFields(final MethodGenerator mi) { 142 for (final MemberInfo memInfo : scriptClassInfo.getMembers()) { 143 if (! memInfo.isPrototypeFunction()) { 144 continue; 145 } 146 mi.loadThis(); 147 newFunction(mi, scriptClassInfo.getJavaName(), memInfo, scriptClassInfo.findSpecializations(memInfo.getJavaName())); 148 mi.putField(className, memInfo.getJavaName(), OBJECT_DESC); 149 } 150 } 151 152 /** 153 * External entry point for PrototypeGenerator if called from the command line 154 * 155 * @param args arguments, takes 1 argument which is the class to process 156 * @throws IOException if class cannot be read 157 */ 158 public static void main(final String[] args) throws IOException { 159 if (args.length != 1) { 160 System.err.println("Usage: " + ConstructorGenerator.class.getName() + " <class>"); 161 System.exit(1); 162 } 163 164 final String className = args[0].replace('.', '/'); 165 final ScriptClassInfo sci = getScriptClassInfo(className + ".class"); 166 if (sci == null) { 167 System.err.println("No @ScriptClass in " + className); 168 System.exit(2); 169 throw new AssertionError(); //guard against warning that sci is null below 170 } 171 try { 172 sci.verify(); 173 } catch (final Exception e) { 174 System.err.println(e.getMessage()); 175 System.exit(3); 176 } 177 final PrototypeGenerator gen = new PrototypeGenerator(sci); 178 try (FileOutputStream fos = new FileOutputStream(className + PROTOTYPE_SUFFIX + ".class")) { 179 fos.write(gen.getClassBytes()); 180 } 181 } 182} 183