author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
32527
b105632002c5
8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents:
25865
diff
changeset
|
26 |
package jdk.nashorn.internal.runtime; |
16147 | 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 | 29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
30 |
||
31 |
import java.lang.invoke.MethodHandle; |
|
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 | 35 |
|
36 |
/** |
|
37 |
* Instances of this class serve as "prototype" object for script functions. |
|
38 |
* The purpose is to expose "constructor" property from "prototype". Also, nasgen |
|
39 |
* generated prototype classes extend from this class. |
|
40 |
*/ |
|
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 | 43 |
|
44 |
private Object constructor; |
|
45 |
||
46 |
private static final MethodHandle GET_CONSTRUCTOR = findOwnMH("getConstructor", Object.class, Object.class); |
|
47 |
private static final MethodHandle SET_CONSTRUCTOR = findOwnMH("setConstructor", void.class, Object.class, Object.class); |
|
48 |
||
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 | 53 |
} |
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 | 64 |
} |
65 |
||
66 |
/** |
|
67 |
* PropertyObject constructor |
|
68 |
* |
|
69 |
* @param map property map |
|
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 | 73 |
} |
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 | 82 |
this.constructor = func; |
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Get the constructor for this {@code PrototypeObject} |
|
87 |
* @param self self reference |
|
88 |
* @return constructor, probably, but not necessarily, a {@link ScriptFunction} |
|
89 |
*/ |
|
32527
b105632002c5
8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents:
25865
diff
changeset
|
90 |
public static Object getConstructor(final Object self) { |
16147 | 91 |
return (self instanceof PrototypeObject) ? |
92 |
((PrototypeObject)self).getConstructor() : |
|
93 |
UNDEFINED; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Reset the constructor for this {@code PrototypeObject} |
|
98 |
* @param self self reference |
|
99 |
* @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction} |
|
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 | 102 |
if (self instanceof PrototypeObject) { |
103 |
((PrototypeObject)self).setConstructor(constructor); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
private Object getConstructor() { |
|
108 |
return constructor; |
|
109 |
} |
|
110 |
||
111 |
private void setConstructor(final Object constructor) { |
|
112 |
this.constructor = constructor; |
|
113 |
} |
|
114 |
||
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 | 117 |
} |
118 |
} |