NativeBoolean.java revision 953:221a84ef44c0
112510Sdg/*
274259Sjlemon * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
374178Sjlemon * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
412510Sdg *
512510Sdg * This code is free software; you can redistribute it and/or modify it
612510Sdg * under the terms of the GNU General Public License version 2 only, as
712510Sdg * published by the Free Software Foundation.  Oracle designates this
812510Sdg * particular file as subject to the "Classpath" exception as provided
912510Sdg * by Oracle in the LICENSE file that accompanied this code.
1012510Sdg *
1112510Sdg * This code is distributed in the hope that it will be useful, but WITHOUT
1212510Sdg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1312510Sdg * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1412510Sdg * version 2 for more details (a copy is included in the LICENSE file that
1512510Sdg * accompanied this code).
1612510Sdg *
1712510Sdg * You should have received a copy of the GNU General Public License version
1812510Sdg * 2 along with this work; if not, write to the Free Software Foundation,
1912510Sdg * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2012510Sdg *
2112510Sdg * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2212510Sdg * or visit www.oracle.com if you need additional information or have any
2312510Sdg * questions.
2412510Sdg */
2512510Sdg
2612510Sdgpackage jdk.nashorn.internal.objects;
2712510Sdg
2850477Speterimport static jdk.nashorn.internal.lookup.Lookup.MH;
2912510Sdgimport static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
3012510Sdg
3112510Sdgimport java.lang.invoke.MethodHandle;
3212510Sdgimport java.lang.invoke.MethodHandles;
3312510Sdgimport java.lang.invoke.MethodType;
3412510Sdgimport jdk.internal.dynalink.linker.GuardedInvocation;
3512510Sdgimport jdk.internal.dynalink.linker.LinkRequest;
3629138Sdgimport jdk.nashorn.internal.objects.annotations.Attribute;
3729138Sdgimport jdk.nashorn.internal.objects.annotations.Constructor;
3829138Sdgimport jdk.nashorn.internal.objects.annotations.Function;
3929138Sdgimport jdk.nashorn.internal.objects.annotations.ScriptClass;
4029138Sdgimport jdk.nashorn.internal.runtime.JSType;
4129138Sdgimport jdk.nashorn.internal.runtime.PropertyMap;
4229138Sdgimport jdk.nashorn.internal.runtime.ScriptObject;
4329138Sdgimport jdk.nashorn.internal.runtime.ScriptRuntime;
4429138Sdgimport jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
4529138Sdg
4629138Sdg/**
4729138Sdg * ECMA 15.6 Boolean Objects.
4876526Sjlemon */
4982425Sjlemon
5012510Sdg@ScriptClass("Boolean")
5129138Sdgpublic final class NativeBoolean extends ScriptObject {
5229138Sdg    private final boolean value;
5329138Sdg
5429138Sdg    // Method handle to create an object wrapper for a primitive boolean
5529138Sdg    private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeBoolean.class, Object.class));
5629138Sdg    // Method handle to retrieve the Boolean prototype object
5729138Sdg    private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class));
5829138Sdg
5922255Sdg    // initialized by nasgen
6022255Sdg    private static PropertyMap $nasgenmap$;
6122255Sdg
6222255Sdg    private NativeBoolean(final boolean value, final ScriptObject proto, final PropertyMap map) {
6322255Sdg        super(proto, map);
6412510Sdg        this.value = value;
6512510Sdg    }
6612510Sdg
6712510Sdg    NativeBoolean(final boolean flag, final Global global) {
6812510Sdg        this(flag, global.getBooleanPrototype(), $nasgenmap$);
6912510Sdg    }
7012510Sdg
7112510Sdg    NativeBoolean(final boolean flag) {
7212510Sdg        this(flag, Global.instance());
7312510Sdg    }
7412510Sdg
7512510Sdg    @Override
7676526Sjlemon    public String safeToString() {
7776526Sjlemon        return "[Boolean " + toString() + "]";
7876526Sjlemon    }
7976526Sjlemon
8076526Sjlemon    @Override
8176526Sjlemon    public String toString() {
8276526Sjlemon        return Boolean.toString(getValue());
8376526Sjlemon    }
8476526Sjlemon
8576526Sjlemon    /**
8676526Sjlemon     * Get the value for this NativeBoolean
8712510Sdg     * @return true or false
8812510Sdg     */
8912510Sdg    public boolean getValue() {
9012510Sdg        return booleanValue();
9112510Sdg    }
9212510Sdg
9312510Sdg    /**
9412510Sdg     * Get the value for this NativeBoolean
9512510Sdg     * @return true or false
9612510Sdg     */
9712510Sdg    public boolean booleanValue() {
9812510Sdg        return value;
9912510Sdg    }
10012510Sdg
10112510Sdg    @Override
10212510Sdg    public String getClassName() {
10312510Sdg        return "Boolean";
10412510Sdg    }
10512510Sdg
10612510Sdg    /**
10712510Sdg     * ECMA 15.6.4.2 Boolean.prototype.toString ( )
10812510Sdg     *
10912510Sdg     * @param self self reference
11012510Sdg     * @return string representation of this boolean
11112510Sdg     */
11212510Sdg    @Function(attributes = Attribute.NOT_ENUMERABLE)
11312510Sdg    public static String toString(final Object self) {
114113017Smux        return getBoolean(self).toString();
115113017Smux    }
116113017Smux
11712510Sdg    /**
11812510Sdg     * ECMA 15.6.4.3 Boolean.prototype.valueOf ( )
119113017Smux     *
120113017Smux     * @param self self reference
121113017Smux     * @return value of this boolean
122113017Smux     */
12312510Sdg    @Function(attributes = Attribute.NOT_ENUMERABLE)
124113151Smux    public static boolean valueOf(final Object self) {
12512510Sdg        return getBoolean(self);
126113151Smux    }
127113151Smux
128113151Smux    /**
129113151Smux     * ECMA 15.6.2.1 new Boolean (value)
130113151Smux     *
131113151Smux     * @param newObj is the new operator used to instantiate this NativeBoolean
132113151Smux     * @param self   self reference
133113151Smux     * @param value  value of boolean
134113151Smux     * @return the new NativeBoolean
135113151Smux     */
136113151Smux    @Constructor(arity = 1)
137113151Smux    public static Object constructor(final boolean newObj, final Object self, final Object value) {
138113151Smux        final boolean flag = JSType.toBoolean(value);
139113151Smux
140113151Smux        if (newObj) {
141113151Smux            return new NativeBoolean(flag);
142113151Smux        }
143113151Smux
14412510Sdg        return flag;
145113017Smux    }
146113017Smux
147113017Smux    private static Boolean getBoolean(final Object self) {
148113151Smux        if (self instanceof Boolean) {
149113151Smux            return ((Boolean)self);
150113151Smux        } else if (self instanceof NativeBoolean) {
151113151Smux            return ((NativeBoolean)self).getValue();
152113017Smux        } else if (self != null && self == Global.instance().getBooleanPrototype()) {
153113151Smux            return false;
154113151Smux        } else {
155113151Smux            throw typeError("not.a.boolean", ScriptRuntime.safeToString(self));
156113151Smux        }
157113151Smux    }
158113151Smux
159113151Smux    /**
160113151Smux     * Lookup the appropriate method for an invoke dynamic call.
161113151Smux     *
162113151Smux     * @param request  The link request
163113151Smux     * @param receiver The receiver for the call
164113151Smux     * @return Link to be invoked at call site.
165113151Smux     */
166113151Smux    public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
167113151Smux        return PrimitiveLookup.lookupPrimitive(request, Boolean.class, new NativeBoolean((Boolean)receiver), WRAPFILTER, PROTOFILTER);
168113151Smux    }
169113151Smux
170113151Smux    /**
171113151Smux     * Wrap a native string in a NativeString object.
172113151Smux     *
173113151Smux     * @param receiver Native string.
174113151Smux     * @return Wrapped object.
175113151Smux     */
176113151Smux    @SuppressWarnings("unused")
177113151Smux    private static NativeBoolean wrapFilter(final Object receiver) {
178113151Smux        return new NativeBoolean((Boolean)receiver);
179113151Smux    }
180113151Smux
181113151Smux    @SuppressWarnings("unused")
182113151Smux    private static Object protoFilter(final Object object) {
183113151Smux        return Global.instance().getBooleanPrototype();
184113151Smux    }
185113151Smux
186113151Smux    private static MethodHandle findOwnMH(final String name, final MethodType type) {
187113151Smux        return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, name, type);
188113151Smux    }
189113151Smux}
190113151Smux