Undefined.java revision 1551:f3b883bec2d0
1219937Sedwin/*
2219937Sedwin * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3219937Sedwin * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219937Sedwin *
5219937Sedwin * This code is free software; you can redistribute it and/or modify it
6219937Sedwin * under the terms of the GNU General Public License version 2 only, as
7219937Sedwin * published by the Free Software Foundation.  Oracle designates this
8219937Sedwin * particular file as subject to the "Classpath" exception as provided
9219937Sedwin * by Oracle in the LICENSE file that accompanied this code.
10219937Sedwin *
11219937Sedwin * This code is distributed in the hope that it will be useful, but WITHOUT
12219937Sedwin * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13219937Sedwin * 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
26package jdk.nashorn.internal.runtime;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30
31import java.lang.invoke.MethodHandle;
32import java.lang.invoke.MethodHandles;
33import jdk.dynalink.CallSiteDescriptor;
34import jdk.dynalink.NamedOperation;
35import jdk.dynalink.StandardOperation;
36import jdk.dynalink.linker.GuardedInvocation;
37import jdk.dynalink.linker.support.Guards;
38import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
39
40/**
41 * Unique instance of this class is used to represent JavaScript undefined.
42 */
43public final class Undefined extends DefaultPropertyAccess {
44
45    private Undefined() {
46    }
47
48    private static final Undefined UNDEFINED = new Undefined();
49    private static final Undefined EMPTY     = new Undefined();
50
51    // Guard used for indexed property access/set on the Undefined instance
52    private static final MethodHandle UNDEFINED_GUARD = Guards.getIdentityGuard(UNDEFINED);
53
54    /**
55     * Get the value of {@code undefined}, this is represented as a global singleton
56     * instance of this class. It can always be reference compared
57     *
58     * @return the undefined object
59     */
60    public static Undefined getUndefined() {
61        return UNDEFINED;
62    }
63
64    /**
65     * Get the value of {@code empty}. This is represented as a global singleton
66     * instanceof this class. It can always be reference compared.
67     * <p>
68     * We need empty to differentiate behavior in things like array iterators
69     * <p>
70     * @return the empty object
71     */
72    public static Undefined getEmpty() {
73        return EMPTY;
74    }
75
76    /**
77     * Get the class name of Undefined
78     * @return "Undefined"
79     */
80    @SuppressWarnings("static-method")
81    public String getClassName() {
82        return "Undefined";
83    }
84
85    @Override
86    public String toString() {
87        return "undefined";
88    }
89
90    /**
91     * Lookup the appropriate method for an invoke dynamic call.
92     * @param desc The invoke dynamic callsite descriptor.
93     * @return GuardedInvocation to be invoked at call site.
94     */
95    public static GuardedInvocation lookup(final CallSiteDescriptor desc) {
96        final StandardOperation op = NashornCallSiteDescriptor.getFirstStandardOperation(desc);
97        switch (op) {
98        case CALL:
99        case NEW:
100            final String name = NashornCallSiteDescriptor.getOperand(desc);
101            final String msg = name != null? "not.a.function" : "cant.call.undefined";
102            throw typeError(msg, name);
103        case CALL_METHOD:
104            throw lookupTypeError("cant.read.property.of.undefined", desc);
105        case GET_PROPERTY:
106        case GET_ELEMENT:
107        case GET_METHOD:
108            // NOTE: we support GET_ELEMENT and SET_ELEMENT as JavaScript doesn't distinguish items from properties. Nashorn itself
109            // emits "GET_PROPERTY|GET_ELEMENT|GET_METHOD:identifier" for "<expr>.<identifier>" and "GET_ELEMENT|GET_PROPERTY|GET_METHOD" for "<expr>[<expr>]", but we are
110            // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
111            // operation has an associated name or not.
112            if (!(desc.getOperation() instanceof NamedOperation)) {
113                return findGetIndexMethod(desc);
114            }
115            return findGetMethod(desc);
116        case SET_PROPERTY:
117        case SET_ELEMENT:
118            if (!(desc.getOperation() instanceof NamedOperation)) {
119                return findSetIndexMethod(desc);
120            }
121            return findSetMethod(desc);
122        default:
123        }
124        return null;
125    }
126
127    private static ECMAException lookupTypeError(final String msg, final CallSiteDescriptor desc) {
128        final String name = NashornCallSiteDescriptor.getOperand(desc);
129        return typeError(msg, name != null && !name.isEmpty()? name : null);
130    }
131
132    private static final MethodHandle GET_METHOD = findOwnMH("get", Object.class, Object.class);
133    private static final MethodHandle SET_METHOD = MH.insertArguments(findOwnMH("set", void.class, Object.class, Object.class, int.class), 3, NashornCallSiteDescriptor.CALLSITE_STRICT);
134
135    private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
136        return new GuardedInvocation(MH.insertArguments(GET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc);
137    }
138
139    private static GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc) {
140        return new GuardedInvocation(GET_METHOD, UNDEFINED_GUARD).asType(desc);
141    }
142
143    private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
144        return new GuardedInvocation(MH.insertArguments(SET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc);
145    }
146
147    private static GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc) {
148        return new GuardedInvocation(SET_METHOD, UNDEFINED_GUARD).asType(desc);
149    }
150
151    @Override
152    public Object get(final Object key) {
153        throw typeError("cant.read.property.of.undefined", ScriptRuntime.safeToString(key));
154    }
155
156    @Override
157    public void set(final Object key, final Object value, final int flags) {
158        throw typeError("cant.set.property.of.undefined", ScriptRuntime.safeToString(key));
159    }
160
161    @Override
162    public boolean delete(final Object key, final boolean strict) {
163        throw typeError("cant.delete.property.of.undefined", ScriptRuntime.safeToString(key));
164    }
165
166    @Override
167    public boolean has(final Object key) {
168        return false;
169    }
170
171    @Override
172    public boolean hasOwnProperty(final Object key) {
173        return false;
174    }
175
176    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
177        return MH.findVirtual(MethodHandles.lookup(), Undefined.class, name, MH.type(rtype, types));
178    }
179}
180