ConstructorGenerator.java revision 417:36d6b6a3fbe0
1233545Sjchandra/*
2233545Sjchandra * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3233545Sjchandra * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4233545Sjchandra *
5233545Sjchandra * This code is free software; you can redistribute it and/or modify it
6233545Sjchandra * under the terms of the GNU General Public License version 2 only, as
7233545Sjchandra * published by the Free Software Foundation.  Oracle designates this
8233545Sjchandra * particular file as subject to the "Classpath" exception as provided
9233545Sjchandra * by Oracle in the LICENSE file that accompanied this code.
10233545Sjchandra *
11233545Sjchandra * This code is distributed in the hope that it will be useful, but WITHOUT
12233545Sjchandra * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13233545Sjchandra * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14233545Sjchandra * version 2 for more details (a copy is included in the LICENSE file that
15233545Sjchandra * accompanied this code).
16233545Sjchandra *
17233545Sjchandra * You should have received a copy of the GNU General Public License version
18233545Sjchandra * 2 along with this work; if not, write to the Free Software Foundation,
19233545Sjchandra * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20233545Sjchandra *
21233545Sjchandra * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22233545Sjchandra * or visit www.oracle.com if you need additional information or have any
23233545Sjchandra * questions.
24233545Sjchandra */
25233545Sjchandra
26233545Sjchandrapackage jdk.nashorn.internal.tools.nasgen;
27233545Sjchandra
28233545Sjchandraimport static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
29233545Sjchandraimport static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER;
30233545Sjchandraimport static jdk.internal.org.objectweb.asm.Opcodes.H_INVOKESTATIC;
31233545Sjchandraimport static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
32233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.CONSTRUCTOR_SUFFIX;
33233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.DEFAULT_INIT_DESC;
34233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.INIT;
35233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_DESC;
36233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_DUPLICATE;
37233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_DUPLICATE_DESC;
38233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_FIELD_NAME;
39233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROPERTYMAP_TYPE;
40233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.OBJECT_DESC;
41233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROTOTYPEOBJECT_SETCONSTRUCTOR;
42233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROTOTYPEOBJECT_SETCONSTRUCTOR_DESC;
43233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.PROTOTYPEOBJECT_TYPE;
44233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTIONIMPL_INIT_DESC3;
45233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTIONIMPL_INIT_DESC4;
46233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTIONIMPL_TYPE;
47233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTION_SETARITY;
48233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTION_SETARITY_DESC;
49233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTION_SETPROTOTYPE;
50233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTION_SETPROTOTYPE_DESC;
51233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTFUNCTION_TYPE;
52233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTOBJECT_INIT_DESC;
53233545Sjchandraimport static jdk.nashorn.internal.tools.nasgen.StringConstants.SCRIPTOBJECT_TYPE;
54233545Sjchandra
55233545Sjchandraimport java.io.FileOutputStream;
56233545Sjchandraimport java.io.IOException;
57233545Sjchandraimport java.util.List;
58233545Sjchandraimport jdk.internal.org.objectweb.asm.Handle;
59233545Sjchandra
60233545Sjchandra/**
61233545Sjchandra * This class generates constructor class for a @ClassInfo annotated class.
62233545Sjchandra *
63233545Sjchandra */
64233545Sjchandrapublic class ConstructorGenerator extends ClassGenerator {
65233545Sjchandra    private final ScriptClassInfo scriptClassInfo;
66233545Sjchandra    private final String className;
67233545Sjchandra    private final MemberInfo constructor;
68233545Sjchandra    private final int memberCount;
69233545Sjchandra    private final List<MemberInfo> specs;
70233545Sjchandra
71233545Sjchandra    ConstructorGenerator(final ScriptClassInfo sci) {
72233545Sjchandra        this.scriptClassInfo = sci;
73233545Sjchandra
74233545Sjchandra        this.className = scriptClassInfo.getConstructorClassName();
75233545Sjchandra        this.constructor = scriptClassInfo.getConstructor();
76233545Sjchandra        this.memberCount = scriptClassInfo.getConstructorMemberCount();
77233545Sjchandra        this.specs = scriptClassInfo.getSpecializedConstructors();
78255368Sjchandra    }
79255368Sjchandra
80255368Sjchandra    byte[] getClassBytes() {
81233545Sjchandra        // new class extensing from ScriptObject
82233545Sjchandra        final String superClass = (constructor != null)? SCRIPTFUNCTIONIMPL_TYPE : SCRIPTOBJECT_TYPE;
83233545Sjchandra        cw.visit(V1_7, ACC_PUBLIC | ACC_SUPER, className, null, superClass, null);
84233545Sjchandra        if (memberCount > 0) {
85233545Sjchandra            // add fields
86233545Sjchandra            emitFields();
87233545Sjchandra            // add <clinit>
88233545Sjchandra            emitStaticInitializer();
89233545Sjchandra        }
90233545Sjchandra        // add <init>
91233545Sjchandra        emitConstructor();
92233545Sjchandra
93233545Sjchandra        if (constructor == null) {
94233545Sjchandra            emitGetClassName(scriptClassInfo.getName());
95233545Sjchandra        }
96233545Sjchandra
97233545Sjchandra        cw.visitEnd();
98233545Sjchandra        return cw.toByteArray();
99233545Sjchandra    }
100233545Sjchandra
101233545Sjchandra    // --Internals only below this point
102233545Sjchandra    private void emitFields() {
103233545Sjchandra        // Introduce "Function" type instance fields for each
104233545Sjchandra        // constructor @Function in script class and introduce instance
105233545Sjchandra        // fields for each constructor @Property in the script class.
106233545Sjchandra        for (MemberInfo memInfo : scriptClassInfo.getMembers()) {
107233545Sjchandra            if (memInfo.isConstructorFunction()) {
108233545Sjchandra                addFunctionField(memInfo.getJavaName());
109233545Sjchandra                memInfo = (MemberInfo)memInfo.clone();
110233545Sjchandra                memInfo.setJavaDesc(OBJECT_DESC);
111233545Sjchandra                memInfo.setJavaAccess(ACC_PUBLIC);
112233545Sjchandra                addGetter(className, memInfo);
113233545Sjchandra                addSetter(className, memInfo);
114233545Sjchandra            } else if (memInfo.isConstructorProperty()) {
115233545Sjchandra                if (memInfo.isStaticFinal()) {
116233545Sjchandra                    addGetter(scriptClassInfo.getJavaName(), memInfo);
117233545Sjchandra                } else {
118233545Sjchandra                    addField(memInfo.getJavaName(), memInfo.getJavaDesc());
119233545Sjchandra                    memInfo = (MemberInfo)memInfo.clone();
120233545Sjchandra                    memInfo.setJavaAccess(ACC_PUBLIC);
121233545Sjchandra                    addGetter(className, memInfo);
122233545Sjchandra                    addSetter(className, memInfo);
123233545Sjchandra                }
124233545Sjchandra            }
125233545Sjchandra        }
126233545Sjchandra
127233545Sjchandra        addMapField();
128233545Sjchandra    }
129233545Sjchandra
130233545Sjchandra    private void emitStaticInitializer() {
131233545Sjchandra        final MethodGenerator mi = makeStaticInitializer();
132233545Sjchandra        emitStaticInitPrefix(mi, className, memberCount);
133233545Sjchandra
134233545Sjchandra        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
135233545Sjchandra            if (memInfo.isConstructorFunction() || memInfo.isConstructorProperty()) {
136233545Sjchandra                linkerAddGetterSetter(mi, className, memInfo);
137233545Sjchandra            } else if (memInfo.isConstructorGetter()) {
138233545Sjchandra                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
139                linkerAddGetterSetter(mi, scriptClassInfo.getJavaName(), memInfo, setter);
140            }
141        }
142        emitStaticInitSuffix(mi, className);
143    }
144
145    private void emitConstructor() {
146        final MethodGenerator mi = makeConstructor();
147        mi.visitCode();
148        callSuper(mi);
149
150        if (memberCount > 0) {
151            // initialize Function type fields
152            initFunctionFields(mi);
153            // initialize data fields
154            initDataFields(mi);
155        }
156
157        if (constructor != null) {
158            final int arity = constructor.getArity();
159            if (arity != MemberInfo.DEFAULT_ARITY) {
160                mi.loadThis();
161                mi.push(arity);
162                mi.invokeVirtual(SCRIPTFUNCTION_TYPE, SCRIPTFUNCTION_SETARITY,
163                        SCRIPTFUNCTION_SETARITY_DESC);
164            }
165        }
166        mi.returnVoid();
167        mi.computeMaxs();
168        mi.visitEnd();
169    }
170
171    private void loadMap(final MethodGenerator mi) {
172        if (memberCount > 0) {
173            mi.getStatic(className, PROPERTYMAP_FIELD_NAME, PROPERTYMAP_DESC);
174            // make sure we use duplicated PropertyMap so that original map
175            // stays intact and so can be used for many globals.
176            mi.invokeVirtual(PROPERTYMAP_TYPE, PROPERTYMAP_DUPLICATE, PROPERTYMAP_DUPLICATE_DESC);
177        }
178    }
179
180    private void callSuper(final MethodGenerator mi) {
181        String superClass, superDesc;
182        mi.loadThis();
183        if (constructor == null) {
184            // call ScriptObject.<init>
185            superClass = SCRIPTOBJECT_TYPE;
186            superDesc = (memberCount > 0) ? SCRIPTOBJECT_INIT_DESC : DEFAULT_INIT_DESC;
187            loadMap(mi);
188        } else {
189            // call Function.<init>
190            superClass = SCRIPTFUNCTIONIMPL_TYPE;
191            superDesc = (memberCount > 0) ? SCRIPTFUNCTIONIMPL_INIT_DESC4 : SCRIPTFUNCTIONIMPL_INIT_DESC3;
192            mi.loadLiteral(constructor.getName());
193            mi.visitLdcInsn(new Handle(H_INVOKESTATIC, scriptClassInfo.getJavaName(), constructor.getJavaName(), constructor.getJavaDesc()));
194            loadMap(mi);
195            mi.memberInfoArray(scriptClassInfo.getJavaName(), specs); //pushes null if specs empty
196        }
197
198        mi.invokeSpecial(superClass, INIT, superDesc);
199    }
200
201    private void initFunctionFields(final MethodGenerator mi) {
202        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
203            if (!memInfo.isConstructorFunction()) {
204                continue;
205            }
206            mi.loadThis();
207            newFunction(mi, scriptClassInfo.getJavaName(), memInfo, scriptClassInfo.findSpecializations(memInfo.getJavaName()));
208            mi.putField(className, memInfo.getJavaName(), OBJECT_DESC);
209        }
210    }
211
212    private void initDataFields(final MethodGenerator mi) {
213         for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
214            if (!memInfo.isConstructorProperty() || memInfo.isFinal()) {
215                continue;
216            }
217            final Object value = memInfo.getValue();
218            if (value != null) {
219                mi.loadThis();
220                mi.loadLiteral(value);
221                mi.putField(className, memInfo.getJavaName(), memInfo.getJavaDesc());
222            } else if (!memInfo.getInitClass().isEmpty()) {
223                final String clazz = memInfo.getInitClass();
224                mi.loadThis();
225                mi.newObject(clazz);
226                mi.dup();
227                mi.invokeSpecial(clazz, INIT, DEFAULT_INIT_DESC);
228                mi.putField(className, memInfo.getJavaName(), memInfo.getJavaDesc());
229            }
230        }
231
232        if (constructor != null) {
233            mi.loadThis();
234            final String protoName = scriptClassInfo.getPrototypeClassName();
235            mi.newObject(protoName);
236            mi.dup();
237            mi.invokeSpecial(protoName, INIT, DEFAULT_INIT_DESC);
238            mi.dup();
239            mi.loadThis();
240            mi.invokeStatic(PROTOTYPEOBJECT_TYPE, PROTOTYPEOBJECT_SETCONSTRUCTOR,
241                    PROTOTYPEOBJECT_SETCONSTRUCTOR_DESC);
242            mi.invokeVirtual(SCRIPTFUNCTION_TYPE, SCRIPTFUNCTION_SETPROTOTYPE, SCRIPTFUNCTION_SETPROTOTYPE_DESC);
243        }
244    }
245
246    /**
247     * Entry point for ConstructorGenerator run separately as an application. Will display
248     * usage. Takes one argument, a class name.
249     * @param args args vector
250     * @throws IOException if class can't be read
251     */
252    public static void main(final String[] args) throws IOException {
253        if (args.length != 1) {
254            System.err.println("Usage: " + ConstructorGenerator.class.getName() + " <class>");
255            System.exit(1);
256        }
257
258        final String className = args[0].replace('.', '/');
259        final ScriptClassInfo sci = getScriptClassInfo(className + ".class");
260        if (sci == null) {
261            System.err.println("No @ScriptClass in " + className);
262            System.exit(2);
263            throw new IOException(); // get rid of warning for sci.verify() below - may be null
264        }
265
266        try {
267            sci.verify();
268        } catch (final Exception e) {
269            System.err.println(e.getMessage());
270            System.exit(3);
271        }
272        final ConstructorGenerator gen = new ConstructorGenerator(sci);
273        try (FileOutputStream fos = new FileOutputStream(className + CONSTRUCTOR_SUFFIX + ".class")) {
274            fos.write(gen.getClassBytes());
275        }
276    }
277}
278