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 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(), $nasgenmap$);
69 }
70
71 NativeBoolean(final boolean flag) {
72 this(flag, Global.instance());
73 }
74
75 @Override
76 public String safeToString() {
77 return "[Boolean " + toString() + "]";
78 }
79
80 @Override
81 public String toString() {
82 return Boolean.toString(getValue());
83 }
84
85 /**
86 * Get the value for this NativeBoolean
87 * @return true or false
88 */
89 public boolean getValue() {
90 return booleanValue();
91 }
92
93 /**
94 * Get the value for this NativeBoolean
95 * @return true or false
96 */
97 public boolean booleanValue() {
98 return value;
99 }
100
101 @Override
102 public String getClassName() {
103 return "Boolean";
104 }
105
106 /**
107 * ECMA 15.6.4.2 Boolean.prototype.toString ( )
108 *
109 * @param self self reference
110 * @return string representation of this boolean
111 */
112 @Function(attributes = Attribute.NOT_ENUMERABLE)
113 public static Object toString(final Object self) {
114 return getBoolean(self).toString();
115 }
116
117 /**
118 * ECMA 15.6.4.3 Boolean.prototype.valueOf ( )
119 *
120 * @param self self reference
121 * @return value of this boolean
122 */
123 @Function(attributes = Attribute.NOT_ENUMERABLE)
124 public static Object valueOf(final Object self) {
125 return getBoolean(self);
126 }
127
128 /**
129 * ECMA 15.6.2.1 new Boolean (value)
130 *
131 * @param newObj is the new operator used to instantiate this NativeBoolean
132 * @param self self reference
133 * @param value value of boolean
134 * @return the new NativeBoolean
135 */
136 @Constructor(arity = 1)
137 public static Object constructor(final boolean newObj, final Object self, final Object value) {
138 final boolean flag = JSType.toBoolean(value);
139
140 if (newObj) {
141 return new NativeBoolean(flag);
142 }
143
144 return flag;
145 }
146
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, PROTOFILTER);
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 @SuppressWarnings("unused")
182 private static Object protoFilter(final Object object) {
183 return Global.instance().getBooleanPrototype();
184 }
185
186 private static MethodHandle findOwnMH(final String name, final MethodType type) {
187 return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, name, type);
188 }
189 }
--- EOF ---