src/jdk/nashorn/internal/objects/PrototypeObject.java

Fri, 05 Jul 2013 14:36:54 +0200

author
hannesw
date
Fri, 05 Jul 2013 14:36:54 +0200
changeset 415
edca88d3a03e
parent 414
ec84ba68ad39
child 418
36d6b6a3fbe0
permissions
-rw-r--r--

8017084: Use spill properties for large object literals
Reviewed-by: lagergren, sundar

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.objects;
jlaskey@3 27
jlaskey@3 28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
sundar@133 29 import static jdk.nashorn.internal.lookup.Lookup.MH;
jlaskey@3 30
jlaskey@3 31 import java.lang.invoke.MethodHandle;
jlaskey@3 32 import java.lang.invoke.MethodHandles;
jlaskey@3 33 import jdk.nashorn.internal.runtime.Property;
jlaskey@3 34 import jdk.nashorn.internal.runtime.PropertyMap;
jlaskey@3 35 import jdk.nashorn.internal.runtime.ScriptFunction;
jlaskey@3 36 import jdk.nashorn.internal.runtime.ScriptObject;
sundar@133 37 import jdk.nashorn.internal.lookup.Lookup;
sundar@372 38 import jdk.nashorn.internal.lookup.MethodHandleFactory;
jlaskey@3 39
jlaskey@3 40 /**
jlaskey@3 41 * Instances of this class serve as "prototype" object for script functions.
jlaskey@3 42 * The purpose is to expose "constructor" property from "prototype". Also, nasgen
jlaskey@3 43 * generated prototype classes extend from this class.
jlaskey@3 44 *
jlaskey@3 45 */
jlaskey@3 46 public class PrototypeObject extends ScriptObject {
hannesw@380 47 private static final PropertyMap map$;
jlaskey@3 48
jlaskey@3 49 private Object constructor;
jlaskey@3 50
jlaskey@3 51 private static final MethodHandle GET_CONSTRUCTOR = findOwnMH("getConstructor", Object.class, Object.class);
jlaskey@3 52 private static final MethodHandle SET_CONSTRUCTOR = findOwnMH("setConstructor", void.class, Object.class, Object.class);
jlaskey@3 53
jlaskey@3 54 static {
hannesw@415 55 PropertyMap map = PropertyMap.newMap();
jlaskey@3 56 map = Lookup.newProperty(map, "constructor", Property.NOT_ENUMERABLE, GET_CONSTRUCTOR, SET_CONSTRUCTOR);
hannesw@380 57 map$ = map;
jlaskey@3 58 }
jlaskey@3 59
sundar@414 60 static PropertyMap getInitialMap() {
sundar@414 61 return map$;
sundar@414 62 }
sundar@414 63
sundar@414 64 private PrototypeObject(final Global global, final PropertyMap map) {
sundar@414 65 super(map != map$? map.addAll(global.getPrototypeObjectMap()) : global.getPrototypeObjectMap());
sundar@414 66 setProto(global.getObjectPrototype());
sundar@414 67 }
sundar@414 68
jlaskey@3 69 PrototypeObject() {
sundar@414 70 this(Global.instance(), map$);
jlaskey@3 71 }
jlaskey@3 72
jlaskey@3 73 /**
jlaskey@3 74 * PropertyObject constructor
jlaskey@3 75 *
jlaskey@3 76 * @param map property map
jlaskey@3 77 */
jlaskey@3 78 public PrototypeObject(final PropertyMap map) {
sundar@414 79 this(Global.instance(), map);
jlaskey@3 80 }
jlaskey@3 81
jlaskey@3 82 PrototypeObject(final ScriptFunction func) {
sundar@414 83 this(Global.instance(), map$);
jlaskey@3 84 this.constructor = func;
jlaskey@3 85 }
jlaskey@3 86
jlaskey@3 87 /**
jlaskey@3 88 * Get the constructor for this {@code PrototypeObject}
jlaskey@3 89 * @param self self reference
jlaskey@3 90 * @return constructor, probably, but not necessarily, a {@link ScriptFunction}
jlaskey@3 91 */
jlaskey@3 92 public static Object getConstructor(final Object self) {
jlaskey@3 93 return (self instanceof PrototypeObject) ?
jlaskey@3 94 ((PrototypeObject)self).getConstructor() :
jlaskey@3 95 UNDEFINED;
jlaskey@3 96 }
jlaskey@3 97
jlaskey@3 98 /**
jlaskey@3 99 * Reset the constructor for this {@code PrototypeObject}
jlaskey@3 100 * @param self self reference
jlaskey@3 101 * @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction}
jlaskey@3 102 */
jlaskey@3 103 public static void setConstructor(final Object self, final Object constructor) {
jlaskey@3 104 if (self instanceof PrototypeObject) {
jlaskey@3 105 ((PrototypeObject)self).setConstructor(constructor);
jlaskey@3 106 }
jlaskey@3 107 }
jlaskey@3 108
jlaskey@3 109 private Object getConstructor() {
jlaskey@3 110 return constructor;
jlaskey@3 111 }
jlaskey@3 112
jlaskey@3 113 private void setConstructor(final Object constructor) {
jlaskey@3 114 this.constructor = constructor;
jlaskey@3 115 }
jlaskey@3 116
jlaskey@3 117 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
sundar@414 118 return MH.findStatic(MethodHandles.lookup(), PrototypeObject.class, name, MH.type(rtype, types));
jlaskey@3 119 }
jlaskey@3 120 }

mercurial