Undefined.java revision 1368:ed56500172f4
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 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 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.internal.dynalink.CallSiteDescriptor; 34import jdk.internal.dynalink.linker.GuardedInvocation; 35import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 36import jdk.internal.dynalink.support.Guards; 37import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 38 39/** 40 * Unique instance of this class is used to represent JavaScript undefined. 41 */ 42public 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) { 95 final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); 96 97 switch (operator) { 98 case "new": 99 case "call": { 100 final String name = desc.getNameTokenCount() > 2? desc.getNameToken(2) : null; 101 final String msg = name != null? "cant.call.undefined.arg" : "cant.call.undefined"; 102 throw typeError(msg, name); 103 } 104 105 case "callMethod": 106 throw lookupTypeError("cant.read.property.of.undefined", desc); 107 // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself 108 // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are 109 // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the 110 // operation has an associated name or not. 111 case "getProp": 112 case "getElem": 113 case "getMethod": 114 if (desc.getNameTokenCount() < 3) { 115 return findGetIndexMethod(desc); 116 } 117 return findGetMethod(desc); 118 case "setProp": 119 case "setElem": 120 if (desc.getNameTokenCount() < 3) { 121 return findSetIndexMethod(desc); 122 } 123 return findSetMethod(desc); 124 default: 125 break; 126 } 127 128 return null; 129 } 130 131 private static ECMAException lookupTypeError(final String msg, final CallSiteDescriptor desc) { 132 final String name = desc.getNameToken(2); 133 return typeError(msg, name != null && !name.isEmpty()? name : null); 134 } 135 136 private static final MethodHandle GET_METHOD = findOwnMH("get", Object.class, Object.class); 137 private static final MethodHandle SET_METHOD = MH.insertArguments(findOwnMH("set", void.class, Object.class, Object.class, int.class), 3, NashornCallSiteDescriptor.CALLSITE_STRICT); 138 139 private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) { 140 return new GuardedInvocation(MH.insertArguments(GET_METHOD, 1, desc.getNameToken(2)), UNDEFINED_GUARD).asType(desc); 141 } 142 143 private static GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc) { 144 return new GuardedInvocation(GET_METHOD, UNDEFINED_GUARD).asType(desc); 145 } 146 147 private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) { 148 return new GuardedInvocation(MH.insertArguments(SET_METHOD, 1, desc.getNameToken(2)), UNDEFINED_GUARD).asType(desc); 149 } 150 151 private static GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc) { 152 return new GuardedInvocation(SET_METHOD, UNDEFINED_GUARD).asType(desc); 153 } 154 155 @Override 156 public Object get(final Object key) { 157 throw typeError("cant.read.property.of.undefined", ScriptRuntime.safeToString(key)); 158 } 159 160 @Override 161 public void set(final Object key, final Object value, final int flags) { 162 throw typeError("cant.set.property.of.undefined", ScriptRuntime.safeToString(key)); 163 } 164 165 @Override 166 public boolean delete(final Object key, final boolean strict) { 167 throw typeError("cant.delete.property.of.undefined", ScriptRuntime.safeToString(key)); 168 } 169 170 @Override 171 public boolean has(final Object key) { 172 return false; 173 } 174 175 @Override 176 public boolean hasOwnProperty(final Object key) { 177 return false; 178 } 179 180 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 181 return MH.findVirtual(MethodHandles.lookup(), Undefined.class, name, MH.type(rtype, types)); 182 } 183} 184