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     private PrototypeObject(final Global global, final PropertyMap map) {
  61         super(global.getObjectPrototype(), map != map$? map.addAll(map$) : map$);
  62     }
  63 
  64     PrototypeObject() {
  65         this(Global.instance(), map$);
  66     }
  67 
  68     /**
  69      * PropertyObject constructor
  70      *
  71      * @param map property map
  72      */
  73     PrototypeObject(final PropertyMap map) {
  74         this(Global.instance(), map);
  75     }
  76 
  77     PrototypeObject(final ScriptFunction func) {
  78         this(Global.instance(), map$);
  79         this.constructor = func;
  80     }
  81 
  82     /**
  83      * Get the constructor for this {@code PrototypeObject}
  84      * @param self self reference
  85      * @return constructor, probably, but not necessarily, a {@link ScriptFunction}
  86      */
  87     static Object getConstructor(final Object self) {
  88         return (self instanceof PrototypeObject) ?
  89             ((PrototypeObject)self).getConstructor() :
  90             UNDEFINED;
  91     }
  92 
  93     /**
  94      * Reset the constructor for this {@code PrototypeObject}
  95      * @param self self reference
  96      * @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction}
  97      */
  98     static void setConstructor(final Object self, final Object constructor) {
  99         if (self instanceof PrototypeObject) {
 100             ((PrototypeObject)self).setConstructor(constructor);
 101         }
 102     }
 103 
 104     private Object getConstructor() {
 105         return constructor;
 106     }
 107 
 108     private void setConstructor(final Object constructor) {
 109         this.constructor = constructor;
 110     }
 111 
 112     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 113         return MH.findStatic(MethodHandles.lookup(), PrototypeObject.class, name, MH.type(rtype, types));
 114     }
 115 }
--- EOF ---