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
26 package jdk.nashorn.internal.codegen;
27
28 import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
29
30 import java.util.List;
31 import jdk.nashorn.internal.codegen.types.Type;
32 import jdk.nashorn.internal.runtime.PropertyMap;
33 import jdk.nashorn.internal.runtime.ScriptObject;
34
35 /**
36 * Base class for object creation code generation.
37 * @param <T> value type
38 */
39 public abstract class ObjectCreator<T> implements CodeGenerator.SplitLiteralCreator {
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;
139 protected boolean hasArguments() {
140 return hasArguments;
141 }
142
143 /**
144 * Get the class of objects created by this ObjectCreator
145 * @return class of created object
146 */
147 abstract protected Class<? extends ScriptObject> getAllocatorClass();
148
149 /**
150 * Technique for loading an initial value. Defined by anonymous subclasses in code gen.
151 *
152 * @param value Value to load.
153 * @param type the type of the value to load
154 */
155 protected abstract void loadValue(T value, Type type);
156
157 MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) {
158 loadValue(tuple.value, tuple.type);
159 if (pack && codegen.useDualFields() && tuple.isPrimitive()) {
160 method.pack();
161 } else {
162 method.convert(Type.OBJECT);
163 }
164 return method;
165 }
166
167 MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple) {
168 return loadTuple(method, tuple, true);
169 }
170 }
|
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
26 package jdk.nashorn.internal.codegen;
27
28 import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
29
30 import java.util.List;
31 import jdk.nashorn.internal.codegen.types.Type;
32 import jdk.nashorn.internal.runtime.JSType;
33 import jdk.nashorn.internal.runtime.PropertyMap;
34 import jdk.nashorn.internal.runtime.ScriptObject;
35
36 /**
37 * Base class for object creation code generation.
38 * @param <T> value type
39 */
40 public abstract class ObjectCreator<T> implements CodeGenerator.SplitLiteralCreator {
41
42 /** List of keys & symbols to initiate in this ObjectCreator */
43 final List<MapTuple<T>> tuples;
44
45 /** Code generator */
46 final CodeGenerator codegen;
47
48 /** Property map */
49 protected PropertyMap propertyMap;
50
51 private final boolean isScope;
52 private final boolean hasArguments;
140 protected boolean hasArguments() {
141 return hasArguments;
142 }
143
144 /**
145 * Get the class of objects created by this ObjectCreator
146 * @return class of created object
147 */
148 abstract protected Class<? extends ScriptObject> getAllocatorClass();
149
150 /**
151 * Technique for loading an initial value. Defined by anonymous subclasses in code gen.
152 *
153 * @param value Value to load.
154 * @param type the type of the value to load
155 */
156 protected abstract void loadValue(T value, Type type);
157
158 MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) {
159 loadValue(tuple.value, tuple.type);
160 if (!codegen.useDualFields() || !tuple.isPrimitive()) {
161 method.convert(Type.OBJECT);
162 } else if (pack) {
163 method.pack();
164 }
165 return method;
166 }
167
168 MethodEmitter loadIndex(final MethodEmitter method, final long index) {
169 return JSType.isRepresentableAsInt(index) ? method.load((int) index) : method.load((double) index);
170 }
171 }
|