ObjectCreator.java revision 953:221a84ef44c0
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.SCOPE;
29
30import java.util.List;
31import jdk.nashorn.internal.codegen.types.Type;
32import jdk.nashorn.internal.runtime.PropertyMap;
33import jdk.nashorn.internal.runtime.ScriptObject;
34
35/**
36 * Base class for object creation code generation.
37 * @param <T> value type
38 */
39public abstract class ObjectCreator<T> {
40
41    /** List of keys & symbols to initiate in this ObjectCreator */
42    final List<MapTuple<T>> tuples;
43
44    /** Code generator */
45    final CodeGenerator codegen;
46
47    /** Property map */
48    protected PropertyMap   propertyMap;
49
50    private final boolean       isScope;
51    private final boolean       hasArguments;
52
53    /**
54     * Constructor
55     *
56     * @param codegen      the code generator
57     * @param tuples       key,symbol,value (optional) tuples
58     * @param isScope      is this object scope
59     * @param hasArguments does the created object have an "arguments" property
60     */
61    ObjectCreator(final CodeGenerator codegen, final List<MapTuple<T>> tuples, final boolean isScope, final boolean hasArguments) {
62        this.codegen       = codegen;
63        this.tuples        = tuples;
64        this.isScope       = isScope;
65        this.hasArguments  = hasArguments;
66    }
67
68    /**
69     * Generate code for making the object.
70     * @param method Script method.
71     */
72    protected abstract void makeObject(final MethodEmitter method);
73
74    /**
75     * Construct the property map appropriate for the object.
76     * @return the newly created property map
77     */
78    protected abstract PropertyMap makeMap();
79
80    /**
81     * Create a new MapCreator
82     * @param clazz type of MapCreator
83     * @return map creator instantiated by type
84     */
85    protected MapCreator<?> newMapCreator(final Class<? extends ScriptObject> clazz) {
86        return new MapCreator<>(clazz, tuples);
87    }
88
89    /**
90     * Loads the scope on the stack through the passed method emitter.
91     * @param method the method emitter to use
92     */
93    protected void loadScope(final MethodEmitter method) {
94        method.loadCompilerConstant(SCOPE);
95    }
96
97    /**
98     * Emit the correct map for the object.
99     * @param method method emitter
100     * @return the method emitter
101     */
102    protected MethodEmitter loadMap(final MethodEmitter method) {
103        codegen.loadConstant(propertyMap);
104        return method;
105    }
106
107    PropertyMap getMap() {
108        return propertyMap;
109    }
110
111    /**
112     * Is this a scope object
113     * @return true if scope
114     */
115    protected boolean isScope() {
116        return isScope;
117    }
118
119    /**
120     * Does the created object have an "arguments" property
121     * @return true if has an "arguments" property
122     */
123    protected boolean hasArguments() {
124        return hasArguments;
125    }
126
127    /**
128     * Technique for loading an initial value. Defined by anonymous subclasses in code gen.
129     *
130     * @param value Value to load.
131     * @param type the type of the value to load
132     */
133    protected abstract void loadValue(T value, Type type);
134
135    MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) {
136        loadValue(tuple.value, tuple.type);
137        if (pack && tuple.isPrimitive()) {
138            method.pack();
139        } else {
140            method.convert(Type.OBJECT);
141        }
142        return method;
143    }
144
145    MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple) {
146        return loadTuple(method, tuple, true);
147    }
148
149}
150