src/jdk/nashorn/internal/objects/NativeBoolean.java

Print this page




  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 jdk.internal.dynalink.linker.GuardedInvocation;
  34 import jdk.internal.dynalink.linker.LinkRequest;
  35 import jdk.nashorn.internal.objects.annotations.Attribute;
  36 import jdk.nashorn.internal.objects.annotations.Constructor;
  37 import jdk.nashorn.internal.objects.annotations.Function;
  38 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  39 import jdk.nashorn.internal.runtime.JSType;
  40 import jdk.nashorn.internal.runtime.PropertyMap;
  41 import jdk.nashorn.internal.runtime.ScriptObject;
  42 import jdk.nashorn.internal.runtime.ScriptRuntime;
  43 import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
  44 
  45 /**
  46  * ECMA 15.6 Boolean Objects.
  47  */
  48 
  49 @ScriptClass("Boolean")
  50 public final class NativeBoolean extends ScriptObject {
  51     private final boolean value;
  52 
  53     final static MethodHandle WRAPFILTER = findWrapFilter();

  54 
  55     // initialized by nasgen
  56     private static PropertyMap $nasgenmap$;
  57 
  58     static PropertyMap getInitialMap() {
  59         return $nasgenmap$;
  60     }
  61 
  62     private NativeBoolean(final boolean value, final ScriptObject proto, final PropertyMap map) {
  63         super(proto, map);
  64         this.value = value;
  65     }
  66 
  67     NativeBoolean(final boolean flag, final Global global) {
  68         this(flag, global.getBooleanPrototype(), getInitialMap());
  69     }
  70 
  71     NativeBoolean(final boolean flag) {
  72         this(flag, Global.instance());
  73     }


 147     private static Boolean getBoolean(final Object self) {
 148         if (self instanceof Boolean) {
 149             return ((Boolean)self);
 150         } else if (self instanceof NativeBoolean) {
 151             return ((NativeBoolean)self).getValue();
 152         } else if (self != null && self == Global.instance().getBooleanPrototype()) {
 153             return false;
 154         } else {
 155             throw typeError("not.a.boolean", ScriptRuntime.safeToString(self));
 156         }
 157     }
 158 
 159     /**
 160      * Lookup the appropriate method for an invoke dynamic call.
 161      *
 162      * @param request  The link request
 163      * @param receiver The receiver for the call
 164      * @return Link to be invoked at call site.
 165      */
 166     public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
 167         return PrimitiveLookup.lookupPrimitive(request, Boolean.class, new NativeBoolean((Boolean)receiver), WRAPFILTER);
 168     }
 169 
 170     /**
 171      * Wrap a native string in a NativeString object.
 172      *
 173      * @param receiver Native string.
 174      * @return Wrapped object.
 175      */
 176     @SuppressWarnings("unused")
 177     private static NativeBoolean wrapFilter(final Object receiver) {
 178         return new NativeBoolean((Boolean)receiver);
 179     }
 180 
 181     private static MethodHandle findWrapFilter() {
 182         return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, "wrapFilter", MH.type(NativeBoolean.class, Object.class));





 183     }
 184 }


  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     }


 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 }