src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/PrototypeObject.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    26
package jdk.nashorn.internal.runtime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 23375
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.lang.invoke.MethodHandles;
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
    33
import java.util.ArrayList;
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    34
import jdk.nashorn.internal.objects.Global;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
 * Instances of this class serve as "prototype" object for script functions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * The purpose is to expose "constructor" property from "prototype". Also, nasgen
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * generated prototype classes extend from this class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
public class PrototypeObject extends ScriptObject {
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 18610
diff changeset
    42
    private static final PropertyMap map$;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
    private Object constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
    private static final MethodHandle GET_CONSTRUCTOR = findOwnMH("getConstructor", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
    private static final MethodHandle SET_CONSTRUCTOR = findOwnMH("setConstructor", void.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
    static {
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
    50
        final ArrayList<Property> properties = new ArrayList<>(1);
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
    51
        properties.add(AccessorProperty.create("constructor", Property.NOT_ENUMERABLE, GET_CONSTRUCTOR, SET_CONSTRUCTOR));
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 19105
diff changeset
    52
        map$ = PropertyMap.newMap(properties);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    55
    private PrototypeObject(final Global global, final PropertyMap map) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 19105
diff changeset
    56
        super(global.getObjectPrototype(), map != map$? map.addAll(map$) : map$);
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    57
    }
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    58
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    59
    /**
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    60
     * Prototype constructor
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    61
     */
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    62
    protected PrototypeObject() {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    63
        this(Global.instance(), map$);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
     * PropertyObject constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
     * @param map property map
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
     */
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    71
    protected PrototypeObject(final PropertyMap map) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    72
        this(Global.instance(), map);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    75
    /**
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    76
     * PropertyObject constructor
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    77
     *
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    78
     * @param func constructor function
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    79
     */
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    80
    protected PrototypeObject(final ScriptFunction func) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
    81
        this(Global.instance(), map$);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
        this.constructor = func;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
     * Get the constructor for this {@code PrototypeObject}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
     * @return constructor, probably, but not necessarily, a {@link ScriptFunction}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
     */
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
    90
    public static Object getConstructor(final Object self) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        return (self instanceof PrototypeObject) ?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
            ((PrototypeObject)self).getConstructor() :
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
            UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
     * Reset the constructor for this {@code PrototypeObject}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
     * @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
     */
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 25865
diff changeset
   101
    public static void setConstructor(final Object self, final Object constructor) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
        if (self instanceof PrototypeObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
            ((PrototypeObject)self).setConstructor(constructor);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    private Object getConstructor() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
        return constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
    private void setConstructor(final Object constructor) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        this.constructor = constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18618
diff changeset
   116
        return MH.findStatic(MethodHandles.lookup(), PrototypeObject.class, name, MH.type(rtype, types));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
}