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