author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 41842 | nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Undefined.java@50202a344d28 |
child 48354 | c96d4c720995 |
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.runtime; |
|
27 |
||
24719 | 28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 29 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
30 |
||
31 |
import java.lang.invoke.MethodHandle; |
|
32 |
import java.lang.invoke.MethodHandles; |
|
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
33 |
import jdk.dynalink.CallSiteDescriptor; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
34 |
import jdk.dynalink.NamedOperation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
35 |
import jdk.dynalink.linker.GuardedInvocation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
36 |
import jdk.dynalink.linker.support.Guards; |
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
37 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
16147 | 38 |
|
39 |
/** |
|
40 |
* Unique instance of this class is used to represent JavaScript undefined. |
|
41 |
*/ |
|
42 |
public final class Undefined extends DefaultPropertyAccess { |
|
43 |
||
44 |
private Undefined() { |
|
45 |
} |
|
46 |
||
47 |
private static final Undefined UNDEFINED = new Undefined(); |
|
48 |
private static final Undefined EMPTY = new Undefined(); |
|
49 |
||
50 |
// Guard used for indexed property access/set on the Undefined instance |
|
51 |
private static final MethodHandle UNDEFINED_GUARD = Guards.getIdentityGuard(UNDEFINED); |
|
52 |
||
53 |
/** |
|
54 |
* Get the value of {@code undefined}, this is represented as a global singleton |
|
55 |
* instance of this class. It can always be reference compared |
|
56 |
* |
|
57 |
* @return the undefined object |
|
58 |
*/ |
|
59 |
public static Undefined getUndefined() { |
|
60 |
return UNDEFINED; |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Get the value of {@code empty}. This is represented as a global singleton |
|
65 |
* instanceof this class. It can always be reference compared. |
|
66 |
* <p> |
|
67 |
* We need empty to differentiate behavior in things like array iterators |
|
68 |
* <p> |
|
69 |
* @return the empty object |
|
70 |
*/ |
|
71 |
public static Undefined getEmpty() { |
|
72 |
return EMPTY; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Get the class name of Undefined |
|
77 |
* @return "Undefined" |
|
78 |
*/ |
|
79 |
@SuppressWarnings("static-method") |
|
80 |
public String getClassName() { |
|
81 |
return "Undefined"; |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
public String toString() { |
|
86 |
return "undefined"; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Lookup the appropriate method for an invoke dynamic call. |
|
91 |
* @param desc The invoke dynamic callsite descriptor. |
|
92 |
* @return GuardedInvocation to be invoked at call site. |
|
93 |
*/ |
|
94 |
public static GuardedInvocation lookup(final CallSiteDescriptor desc) { |
|
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
95 |
switch (NashornCallSiteDescriptor.getStandardOperation(desc)) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
96 |
case CALL: |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
97 |
case NEW: |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
98 |
final String name = NashornCallSiteDescriptor.getOperand(desc); |
32049
af8f6292d54d
8133119: Error message associated with TypeError for call and new should include stringified Node
sundar
parents:
32048
diff
changeset
|
99 |
final String msg = name != null? "not.a.function" : "cant.call.undefined"; |
32048
8023426b93ab
8073733: TypeError messages with "call" and "new" could be improved
sundar
parents:
26765
diff
changeset
|
100 |
throw typeError(msg, name); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
101 |
case GET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
102 |
// NOTE: we support GET:ELEMENT and SET:ELEMENT as JavaScript doesn't distinguish items from properties. Nashorn itself |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
103 |
// emits "GET:PROPERTY|ELEMENT|METHOD:identifier" for "<expr>.<identifier>" and "GET:ELEMENT|PROPERTY|METHOD" for "<expr>[<expr>]", but we are |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
104 |
// more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
105 |
// operation has an associated name or not. |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
106 |
if (!(desc.getOperation() instanceof NamedOperation)) { |
16147 | 107 |
return findGetIndexMethod(desc); |
108 |
} |
|
24719 | 109 |
return findGetMethod(desc); |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
41239
diff
changeset
|
110 |
case SET: |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
111 |
if (!(desc.getOperation() instanceof NamedOperation)) { |
16147 | 112 |
return findSetIndexMethod(desc); |
113 |
} |
|
24719 | 114 |
return findSetMethod(desc); |
16147 | 115 |
default: |
116 |
} |
|
117 |
return null; |
|
118 |
} |
|
119 |
||
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
120 |
private static ECMAException lookupTypeError(final String msg, final CallSiteDescriptor desc) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
121 |
final String name = NashornCallSiteDescriptor.getOperand(desc); |
32048
8023426b93ab
8073733: TypeError messages with "call" and "new" could be improved
sundar
parents:
26765
diff
changeset
|
122 |
return typeError(msg, name != null && !name.isEmpty()? name : null); |
16147 | 123 |
} |
124 |
||
24719 | 125 |
private static final MethodHandle GET_METHOD = findOwnMH("get", Object.class, Object.class); |
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
126 |
private static final MethodHandle SET_METHOD = MH.insertArguments(findOwnMH("set", void.class, Object.class, Object.class, int.class), 3, NashornCallSiteDescriptor.CALLSITE_STRICT); |
16147 | 127 |
|
24719 | 128 |
private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) { |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
129 |
return new GuardedInvocation(MH.insertArguments(GET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc); |
16147 | 130 |
} |
131 |
||
24719 | 132 |
private static GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc) { |
133 |
return new GuardedInvocation(GET_METHOD, UNDEFINED_GUARD).asType(desc); |
|
134 |
} |
|
135 |
||
136 |
private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) { |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
137 |
return new GuardedInvocation(MH.insertArguments(SET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc); |
24719 | 138 |
} |
139 |
||
16147 | 140 |
private static GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc) { |
24719 | 141 |
return new GuardedInvocation(SET_METHOD, UNDEFINED_GUARD).asType(desc); |
16147 | 142 |
} |
143 |
||
144 |
@Override |
|
145 |
public Object get(final Object key) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
146 |
throw typeError("cant.read.property.of.undefined", ScriptRuntime.safeToString(key)); |
16147 | 147 |
} |
148 |
||
149 |
@Override |
|
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
150 |
public void set(final Object key, final Object value, final int flags) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
151 |
throw typeError("cant.set.property.of.undefined", ScriptRuntime.safeToString(key)); |
16147 | 152 |
} |
153 |
||
154 |
@Override |
|
155 |
public boolean delete(final Object key, final boolean strict) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16234
diff
changeset
|
156 |
throw typeError("cant.delete.property.of.undefined", ScriptRuntime.safeToString(key)); |
16147 | 157 |
} |
158 |
||
159 |
@Override |
|
160 |
public boolean has(final Object key) { |
|
161 |
return false; |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
public boolean hasOwnProperty(final Object key) { |
|
166 |
return false; |
|
167 |
} |
|
168 |
||
169 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
170 |
return MH.findVirtual(MethodHandles.lookup(), Undefined.class, name, MH.type(rtype, types)); |
|
171 |
} |
|
172 |
} |