001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Norris Boyd
026: * Igor Bukanov
027: * Mike McCabe
028: *
029: * Alternatively, the contents of this file may be used under the terms of
030: * the GNU General Public License Version 2 or later (the "GPL"), in which
031: * case the provisions of the GPL are applicable instead of those above. If
032: * you wish to allow use of your version of this file only under the terms of
033: * the GPL and not to allow others to use your version of this file under the
034: * MPL, indicate your decision by deleting the provisions above and replacing
035: * them with the notice and other provisions required by the GPL. If you do
036: * not delete the provisions above, a recipient may use your version of this
037: * file under either the MPL or the GPL.
038: *
039: * ***** END LICENSE BLOCK ***** */
040:
041: package org.mozilla.javascript;
042:
043: /**
044: * This class implements the Boolean native object.
045: * See ECMA 15.6.
046: * @author Norris Boyd
047: */
048: final class NativeBoolean extends IdScriptableObject {
049: static final long serialVersionUID = -3716996899943880933L;
050:
051: private static final Object BOOLEAN_TAG = new Object();
052:
053: static void init(Scriptable scope, boolean sealed) {
054: NativeBoolean obj = new NativeBoolean(false);
055: obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
056: }
057:
058: private NativeBoolean(boolean b) {
059: booleanValue = b;
060: }
061:
062: public String getClassName() {
063: return "Boolean";
064: }
065:
066: public Object getDefaultValue(Class typeHint) {
067: // This is actually non-ECMA, but will be proposed
068: // as a change in round 2.
069: if (typeHint == ScriptRuntime.BooleanClass)
070: return ScriptRuntime.wrapBoolean(booleanValue);
071: return super .getDefaultValue(typeHint);
072: }
073:
074: protected void initPrototypeId(int id) {
075: String s;
076: int arity;
077: switch (id) {
078: case Id_constructor:
079: arity = 1;
080: s = "constructor";
081: break;
082: case Id_toString:
083: arity = 0;
084: s = "toString";
085: break;
086: case Id_toSource:
087: arity = 0;
088: s = "toSource";
089: break;
090: case Id_valueOf:
091: arity = 0;
092: s = "valueOf";
093: break;
094: default:
095: throw new IllegalArgumentException(String.valueOf(id));
096: }
097: initPrototypeMethod(BOOLEAN_TAG, id, s, arity);
098: }
099:
100: public Object execIdCall(IdFunctionObject f, Context cx,
101: Scriptable scope, Scriptable this Obj, Object[] args) {
102: if (!f.hasTag(BOOLEAN_TAG)) {
103: return super .execIdCall(f, cx, scope, this Obj, args);
104: }
105: int id = f.methodId();
106:
107: if (id == Id_constructor) {
108: boolean b;
109: if (args.length == 0) {
110: b = false;
111: } else {
112: b = args[0] instanceof ScriptableObject
113: && ((ScriptableObject) args[0])
114: .avoidObjectDetection() ? true
115: : ScriptRuntime.toBoolean(args[0]);
116: }
117: if (this Obj == null) {
118: // new Boolean(val) creates a new boolean object.
119: return new NativeBoolean(b);
120: }
121: // Boolean(val) converts val to a boolean.
122: return ScriptRuntime.wrapBoolean(b);
123: }
124:
125: // The rest of Boolean.prototype methods require thisObj to be Boolean
126:
127: if (!(this Obj instanceof NativeBoolean))
128: throw incompatibleCallError(f);
129: boolean value = ((NativeBoolean) this Obj).booleanValue;
130:
131: switch (id) {
132:
133: case Id_toString:
134: return value ? "true" : "false";
135:
136: case Id_toSource:
137: return value ? "(new Boolean(true))"
138: : "(new Boolean(false))";
139:
140: case Id_valueOf:
141: return ScriptRuntime.wrapBoolean(value);
142: }
143: throw new IllegalArgumentException(String.valueOf(id));
144: }
145:
146: // #string_id_map#
147:
148: protected int findPrototypeId(String s) {
149: int id;
150: // #generated# Last update: 2007-05-09 08:15:31 EDT
151: L0: {
152: id = 0;
153: String X = null;
154: int c;
155: int s_length = s.length();
156: if (s_length == 7) {
157: X = "valueOf";
158: id = Id_valueOf;
159: } else if (s_length == 8) {
160: c = s.charAt(3);
161: if (c == 'o') {
162: X = "toSource";
163: id = Id_toSource;
164: } else if (c == 't') {
165: X = "toString";
166: id = Id_toString;
167: }
168: } else if (s_length == 11) {
169: X = "constructor";
170: id = Id_constructor;
171: }
172: if (X != null && X != s && !X.equals(s))
173: id = 0;
174: break L0;
175: }
176: // #/generated#
177: return id;
178: }
179:
180: private static final int Id_constructor = 1, Id_toString = 2,
181: Id_toSource = 3, Id_valueOf = 4, MAX_PROTOTYPE_ID = 4;
182:
183: // #/string_id_map#
184:
185: private boolean booleanValue;
186: }
|