author | sundar |
Mon, 08 Jul 2013 16:33:50 +0530 | |
changeset 18855 | 408663ef8f66 |
parent 18852 | 604c1d681b6f |
child 19105 | a01ba9d17eab |
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 |
||
26 |
package jdk.nashorn.internal.objects; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
16277 | 29 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 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; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
34 |
import jdk.nashorn.internal.runtime.AccessorProperty; |
16147 | 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 { |
|
18618
136279c4cbe6
8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents:
18610
diff
changeset
|
47 |
private static final PropertyMap map$; |
16147 | 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 { |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
55 |
final ArrayList<Property> properties = new ArrayList<>(1); |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
56 |
properties.add(AccessorProperty.create("constructor", Property.NOT_ENUMERABLE, GET_CONSTRUCTOR, SET_CONSTRUCTOR)); |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
57 |
map$ = PropertyMap.newMap(properties).setIsShared(); |
16147 | 58 |
} |
59 |
||
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
60 |
static PropertyMap getInitialMap() { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
61 |
return map$; |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
62 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
63 |
|
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
64 |
private PrototypeObject(final Global global, final PropertyMap map) { |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
65 |
super(map != map$? map.addAll(global.getPrototypeObjectMap()) : global.getPrototypeObjectMap()); |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
66 |
setProto(global.getObjectPrototype()); |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
67 |
} |
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
68 |
|
16147 | 69 |
PrototypeObject() { |
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
70 |
this(Global.instance(), map$); |
16147 | 71 |
} |
72 |
||
73 |
/** |
|
74 |
* PropertyObject constructor |
|
75 |
* |
|
76 |
* @param map property map |
|
77 |
*/ |
|
78 |
public PrototypeObject(final PropertyMap map) { |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
79 |
this(Global.instance(), map); |
16147 | 80 |
} |
81 |
||
82 |
PrototypeObject(final ScriptFunction func) { |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
18618
diff
changeset
|
83 |
this(Global.instance(), map$); |
16147 | 84 |
this.constructor = func; |
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Get the constructor for this {@code PrototypeObject} |
|
89 |
* @param self self reference |
|
90 |
* @return constructor, probably, but not necessarily, a {@link ScriptFunction} |
|
91 |
*/ |
|
92 |
public static Object getConstructor(final Object self) { |
|
93 |
return (self instanceof PrototypeObject) ? |
|
94 |
((PrototypeObject)self).getConstructor() : |
|
95 |
UNDEFINED; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Reset the constructor for this {@code PrototypeObject} |
|
100 |
* @param self self reference |
|
101 |
* @param constructor constructor, probably, but not necessarily, a {@link ScriptFunction} |
|
102 |
*/ |
|
103 |
public static void setConstructor(final Object self, final Object constructor) { |
|
104 |
if (self instanceof PrototypeObject) { |
|
105 |
((PrototypeObject)self).setConstructor(constructor); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private Object getConstructor() { |
|
110 |
return constructor; |
|
111 |
} |
|
112 |
||
113 |
private void setConstructor(final Object constructor) { |
|
114 |
this.constructor = constructor; |
|
115 |
} |
|
116 |
||
117 |
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
|
118 |
return MH.findStatic(MethodHandles.lookup(), PrototypeObject.class, name, MH.type(rtype, types)); |
16147 | 119 |
} |
120 |
} |