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 26 package jdk.nashorn.internal.objects; 27 28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 29 import static jdk.nashorn.internal.lookup.Lookup.MH; 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.lang.invoke.MethodType; 34 import jdk.internal.dynalink.linker.GuardedInvocation; 35 import jdk.internal.dynalink.linker.LinkRequest; 36 import jdk.nashorn.internal.objects.annotations.Attribute; 37 import jdk.nashorn.internal.objects.annotations.Constructor; 38 import jdk.nashorn.internal.objects.annotations.Function; 39 import jdk.nashorn.internal.objects.annotations.ScriptClass; 40 import jdk.nashorn.internal.runtime.JSType; 41 import jdk.nashorn.internal.runtime.PropertyMap; 42 import jdk.nashorn.internal.runtime.ScriptObject; 43 import jdk.nashorn.internal.runtime.ScriptRuntime; 44 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup; 45 46 /** 47 * ECMA 15.6 Boolean Objects. 48 */ 49 50 @ScriptClass("Boolean") 51 public final class NativeBoolean extends ScriptObject { 52 private final boolean value; 53 54 static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeBoolean.class, Object.class)); 55 static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class)); 56 57 // initialized by nasgen 58 private static PropertyMap $nasgenmap$; 59 60 static PropertyMap getInitialMap() { 61 return $nasgenmap$; 62 } 63 64 private NativeBoolean(final boolean value, final ScriptObject proto, final PropertyMap map) { 65 super(proto, map); 66 this.value = value; 67 } 68 69 NativeBoolean(final boolean flag, final Global global) { 70 this(flag, global.getBooleanPrototype(), getInitialMap()); 71 } 72 73 NativeBoolean(final boolean flag) { 74 this(flag, Global.instance()); 75 } 76 77 @Override 78 public String safeToString() { 79 return "[Boolean " + toString() + "]"; 80 } 81 82 @Override 83 public String toString() { 84 return Boolean.toString(getValue()); 85 } 86 87 /** 88 * Get the value for this NativeBoolean 89 * @return true or false 90 */ 91 public boolean getValue() { 92 return booleanValue(); 93 } 94 95 /** 96 * Get the value for this NativeBoolean 97 * @return true or false 98 */ 99 public boolean booleanValue() { 100 return value; 101 } 102 103 @Override 104 public String getClassName() { 105 return "Boolean"; 106 } 107 108 /** 109 * ECMA 15.6.4.2 Boolean.prototype.toString ( ) 110 * 111 * @param self self reference 112 * @return string representation of this boolean 113 */ 114 @Function(attributes = Attribute.NOT_ENUMERABLE) 115 public static Object toString(final Object self) { 116 return getBoolean(self).toString(); 117 } 118 119 /** 120 * ECMA 15.6.4.3 Boolean.prototype.valueOf ( ) 121 * 122 * @param self self reference 123 * @return value of this boolean 124 */ 125 @Function(attributes = Attribute.NOT_ENUMERABLE) 126 public static Object valueOf(final Object self) { 127 return getBoolean(self); 128 } 129 130 /** 131 * ECMA 15.6.2.1 new Boolean (value) 132 * 133 * @param newObj is the new operator used to instantiate this NativeBoolean 134 * @param self self reference 135 * @param value value of boolean 136 * @return the new NativeBoolean 137 */ 138 @Constructor(arity = 1) 139 public static Object constructor(final boolean newObj, final Object self, final Object value) { 140 final boolean flag = JSType.toBoolean(value); 141 142 if (newObj) { 143 return new NativeBoolean(flag); 144 } 145 146 return flag; 147 } 148 149 private static Boolean getBoolean(final Object self) { 150 if (self instanceof Boolean) { 151 return ((Boolean)self); 152 } else if (self instanceof NativeBoolean) { 153 return ((NativeBoolean)self).getValue(); 154 } else if (self != null && self == Global.instance().getBooleanPrototype()) { 155 return false; 156 } else { 157 throw typeError("not.a.boolean", ScriptRuntime.safeToString(self)); 158 } 159 } 160 161 /** 162 * Lookup the appropriate method for an invoke dynamic call. 163 * 164 * @param request The link request 165 * @param receiver The receiver for the call 166 * @return Link to be invoked at call site. 167 */ 168 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) { 169 return PrimitiveLookup.lookupPrimitive(request, Boolean.class, new NativeBoolean((Boolean)receiver), WRAPFILTER, PROTOFILTER); 170 } 171 172 /** 173 * Wrap a native string in a NativeString object. 174 * 175 * @param receiver Native string. 176 * @return Wrapped object. 177 */ 178 @SuppressWarnings("unused") 179 private static NativeBoolean wrapFilter(final Object receiver) { 180 return new NativeBoolean((Boolean)receiver); 181 } 182 183 @SuppressWarnings("unused") 184 private static Object protoFilter(final Object object) { 185 return Global.instance().getBooleanPrototype(); 186 } 187 188 private static MethodHandle findOwnMH(final String name, final MethodType type) { 189 return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, name, type); 190 } 191 } --- EOF ---