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