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 26 package jdk.nashorn.internal.objects; 27 28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 29 import static jdk.nashorn.internal.lookup.Lookup.MH; 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.util.ArrayList; 34 import jdk.nashorn.internal.runtime.AccessorProperty; 35 import jdk.nashorn.internal.runtime.Property; 36 import jdk.nashorn.internal.runtime.PropertyMap; 37 import jdk.nashorn.internal.runtime.ScriptFunction; 38 import jdk.nashorn.internal.runtime.ScriptObject; 39 40 /** 41 * Instances of this class serve as "prototype" object for script functions. 42 * The purpose is to expose "constructor" property from "prototype". Also, nasgen 43 * generated prototype classes extend from this class. 44 * 45 */ 46 public class PrototypeObject extends ScriptObject { 47 private static final PropertyMap map$; 48 49 private Object constructor; 50 51 private static final MethodHandle GET_CONSTRUCTOR = findOwnMH("getConstructor", Object.class, Object.class); 52 private static final MethodHandle SET_CONSTRUCTOR = findOwnMH("setConstructor", void.class, Object.class, Object.class); 53 54 static { 55 final ArrayList<Property> properties = new ArrayList<>(1); 56 properties.add(AccessorProperty.create("constructor", Property.NOT_ENUMERABLE, GET_CONSTRUCTOR, SET_CONSTRUCTOR)); 57 map$ = PropertyMap.newMap(properties); 58 } 59 60 static PropertyMap getInitialMap() { 61 return map$; 62 } 63 64 private PrototypeObject(final Global global, final PropertyMap map) { 65 super(global.getObjectPrototype(), map != map$? map.addAll(map$) : map$); 66 } 67 68 PrototypeObject() { 69 this(Global.instance(), map$); 70 } 71 72 /** 73 * PropertyObject constructor 74 * 75 * @param map property map 76 */ 77 PrototypeObject(final PropertyMap map) { 78 this(Global.instance(), map); 79 } 80 81 PrototypeObject(final ScriptFunction func) { 82 this(Global.instance(), map$); 83 this.constructor = func; 84 } 85 86 /** 87 * Get the constructor for this {@code PrototypeObject} 88 * @param self self reference 89 * @return constructor, probably, but not necessarily, a {@link ScriptFunction} 90 */ 91 static Object getConstructor(final Object self) { 92 return (self instanceof PrototypeObject) ? 93 ((PrototypeObject)self).getConstructor() : 94 UNDEFINED; 95 } 96 97 /** 98 * Reset the constructor for this {@code PrototypeObject} 99 * @param self self reference 100 * @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction} 101 */ 102 static void setConstructor(final Object self, final Object constructor) { 103 if (self instanceof PrototypeObject) { 104 ((PrototypeObject)self).setConstructor(constructor); 105 } 106 } 107 108 private Object getConstructor() { 109 return constructor; 110 } 111 112 private void setConstructor(final Object constructor) { 113 this.constructor = constructor; 114 } 115 116 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 117 return MH.findStatic(MethodHandles.lookup(), PrototypeObject.class, name, MH.type(rtype, types)); 118 } 119 }