Undefined.java revision 1805:7caf1f762f1d
1139823Simp/*
21541Srgrimes * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3166841Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4166841Srwatson *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.  Oracle designates this
81541Srgrimes * particular file as subject to the "Classpath" exception as provided
91541Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101541Srgrimes *
111541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151541Srgrimes * accompanied this code).
161541Srgrimes *
171541Srgrimes * You should have received a copy of the GNU General Public License version
181541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201541Srgrimes *
211541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221541Srgrimes * or visit www.oracle.com if you need additional information or have any
231541Srgrimes * questions.
241541Srgrimes */
251541Srgrimes
261541Srgrimespackage jdk.nashorn.internal.runtime;
271541Srgrimes
281541Srgrimesimport static jdk.nashorn.internal.lookup.Lookup.MH;
291541Srgrimesimport static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
301541Srgrimes
3150477Speterimport java.lang.invoke.MethodHandle;
321541Srgrimesimport java.lang.invoke.MethodHandles;
331541Srgrimesimport jdk.dynalink.CallSiteDescriptor;
342169Spaulimport jdk.dynalink.NamedOperation;
35166841Srwatsonimport jdk.dynalink.linker.GuardedInvocation;
362169Spaulimport jdk.dynalink.linker.support.Guards;
371541Srgrimesimport jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
381541Srgrimes
391541Srgrimes/**
40166841Srwatson * Unique instance of this class is used to represent JavaScript undefined.
41166841Srwatson */
42166841Srwatsonpublic final class Undefined extends DefaultPropertyAccess {
431541Srgrimes
441541Srgrimes    private Undefined() {
451541Srgrimes    }
461541Srgrimes
471541Srgrimes    private static final Undefined UNDEFINED = new Undefined();
481541Srgrimes    private static final Undefined EMPTY     = new Undefined();
491541Srgrimes
501541Srgrimes    // Guard used for indexed property access/set on the Undefined instance
511541Srgrimes    private static final MethodHandle UNDEFINED_GUARD = Guards.getIdentityGuard(UNDEFINED);
521541Srgrimes
531541Srgrimes    /**
54192649Sbz     * Get the value of {@code undefined}, this is represented as a global singleton
55192649Sbz     * instance of this class. It can always be reference compared
56192649Sbz     *
57192649Sbz     * @return the undefined object
58192649Sbz     */
59192649Sbz    public static Undefined getUndefined() {
60192649Sbz        return UNDEFINED;
61192649Sbz    }
62192649Sbz
63192649Sbz    /**
64192649Sbz     * Get the value of {@code empty}. This is represented as a global singleton
65192649Sbz     * instanceof this class. It can always be reference compared.
66192649Sbz     * <p>
67194062Svanhu     * We need empty to differentiate behavior in things like array iterators
68194062Svanhu     * <p>
69194062Svanhu     * @return the empty object
70194062Svanhu     */
71194062Svanhu    public static Undefined getEmpty() {
72194062Svanhu        return EMPTY;
73166841Srwatson    }
741541Srgrimes
751541Srgrimes    /**
761541Srgrimes     * Get the class name of Undefined
771541Srgrimes     * @return "Undefined"
7852904Sshin     */
791541Srgrimes    @SuppressWarnings("static-method")
801541Srgrimes    public String getClassName() {
811541Srgrimes        return "Undefined";
821541Srgrimes    }
831541Srgrimes
8416143Swollman    @Override
851541Srgrimes    public String toString() {
861541Srgrimes        return "undefined";
8728270Swollman    }
8852904Sshin
8952904Sshin    /**
90170613Sbms     * Lookup the appropriate method for an invoke dynamic call.
911541Srgrimes     * @param desc The invoke dynamic callsite descriptor.
921541Srgrimes     * @return GuardedInvocation to be invoked at call site.
93190962Srwatson     */
94196039Srwatson    public static GuardedInvocation lookup(final CallSiteDescriptor desc) {
95196039Srwatson        switch (NashornCallSiteDescriptor.getStandardOperation(desc)) {
96196039Srwatson        case CALL:
97196039Srwatson        case NEW:
98190962Srwatson            final String name = NashornCallSiteDescriptor.getOperand(desc);
99190962Srwatson            final String msg = name != null? "not.a.function" : "cant.call.undefined";
100196039Srwatson            throw typeError(msg, name);
101196039Srwatson        case GET:
102196039Srwatson            // NOTE: we support GET:ELEMENT and SET:ELEMENT as JavaScript doesn't distinguish items from properties. Nashorn itself
103196039Srwatson            // emits "GET:PROPERTY|ELEMENT|METHOD:identifier" for "<expr>.<identifier>" and "GET:ELEMENT|PROPERTY|METHOD" for "<expr>[<expr>]", but we are
104196039Srwatson            // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
105196039Srwatson            // operation has an associated name or not.
106196039Srwatson            if (!(desc.getOperation() instanceof NamedOperation)) {
107190962Srwatson                return findGetIndexMethod(desc);
108190962Srwatson            }
1091541Srgrimes            return findGetMethod(desc);
110166841Srwatson        case SET:
1111541Srgrimes            if (!(desc.getOperation() instanceof NamedOperation)) {
1121541Srgrimes                return findSetIndexMethod(desc);
113166841Srwatson            }
1146472Swollman            return findSetMethod(desc);
1156472Swollman        default:
11636079Swollman        }
117166841Srwatson        return null;
1181541Srgrimes    }
119166841Srwatson
120166841Srwatson    private static ECMAException lookupTypeError(final String msg, final CallSiteDescriptor desc) {
121166841Srwatson        final String name = NashornCallSiteDescriptor.getOperand(desc);
122166841Srwatson        return typeError(msg, name != null && !name.isEmpty()? name : null);
123166841Srwatson    }
124166841Srwatson
125166841Srwatson    private static final MethodHandle GET_METHOD = findOwnMH("get", Object.class, Object.class);
1261541Srgrimes    private static final MethodHandle SET_METHOD = MH.insertArguments(findOwnMH("set", void.class, Object.class, Object.class, int.class), 3, NashornCallSiteDescriptor.CALLSITE_STRICT);
1271541Srgrimes
12855205Speter    private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
12944078Sdfr        return new GuardedInvocation(MH.insertArguments(GET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc);
13044078Sdfr    }
131166841Srwatson
132195699Srwatson    private static GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc) {
133195699Srwatson        return new GuardedInvocation(GET_METHOD, UNDEFINED_GUARD).asType(desc);
134195727Srwatson    }
135195727Srwatson
136195699Srwatson    private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
137166841Srwatson        return new GuardedInvocation(MH.insertArguments(SET_METHOD, 1, NashornCallSiteDescriptor.getOperand(desc)), UNDEFINED_GUARD).asType(desc);
138166841Srwatson    }
139207369Sbz
140207369Sbz    private static GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc) {
141207369Sbz        return new GuardedInvocation(SET_METHOD, UNDEFINED_GUARD).asType(desc);
142207369Sbz    }
143166842Srwatson
1441541Srgrimes    @Override
145192649Sbz    public Object get(final Object key) {
146192649Sbz        throw typeError("cant.read.property.of.undefined", ScriptRuntime.safeToString(key));
147192649Sbz    }
148166841Srwatson
149217126Sjhb    @Override
150166841Srwatson    public void set(final Object key, final Object value, final int flags) {
151193731Szec        throw typeError("cant.set.property.of.undefined", ScriptRuntime.safeToString(key));
152193731Szec    }
153193731Szec
154166841Srwatson    @Override
155166841Srwatson    public boolean delete(final Object key, final boolean strict) {
156166841Srwatson        throw typeError("cant.delete.property.of.undefined", ScriptRuntime.safeToString(key));
157186813Srrs    }
158186813Srrs
1591541Srgrimes    @Override
1602169Spaul    public boolean has(final Object key) {
1612169Spaul        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}
173