2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / NativeBoolean.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Norris Boyd\r
23  * Igor Bukanov\r
24  * Mike McCabe\r
25  *\r
26  * Alternatively, the contents of this file may be used under the\r
27  * terms of the GNU Public License (the "GPL"), in which case the\r
28  * provisions of the GPL are applicable instead of those above.\r
29  * If you wish to allow use of your version of this file only\r
30  * under the terms of the GPL and not to allow others to use your\r
31  * version of this file under the NPL, indicate your decision by\r
32  * deleting the provisions above and replace them with the notice\r
33  * and other provisions required by the GPL.  If you do not delete\r
34  * the provisions above, a recipient may use your version of this\r
35  * file under either the NPL or the GPL.\r
36  */\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 /**\r
41  * This class implements the Boolean native object.\r
42  * See ECMA 15.6.\r
43  * @author Norris Boyd\r
44  */\r
45 public class NativeBoolean extends IdScriptable {\r
46 \r
47     public static void init(Context cx, Scriptable scope, boolean sealed) {\r
48         NativeBoolean obj = new NativeBoolean();\r
49         obj.prototypeFlag = true;\r
50         obj.addAsPrototype(MAX_PROTOTYPE_ID, cx, scope, sealed);\r
51     }\r
52 \r
53     /**\r
54      * Zero-parameter constructor: just used to create Boolean.prototype\r
55      */\r
56     public NativeBoolean() {\r
57     }\r
58 \r
59     public NativeBoolean(boolean b) {\r
60         booleanValue = b;\r
61     }\r
62 \r
63     public String getClassName() {\r
64         return "Boolean";\r
65     }\r
66 \r
67     public Object getDefaultValue(Class typeHint) {\r
68         // This is actually non-ECMA, but will be proposed\r
69         // as a change in round 2.\r
70         if (typeHint == ScriptRuntime.BooleanClass)\r
71             return wrap_boolean(booleanValue);\r
72         return super.getDefaultValue(typeHint);\r
73     }\r
74 \r
75     public int methodArity(int methodId) {\r
76         if (prototypeFlag) {\r
77             if (methodId == Id_constructor) return 1;\r
78             if (methodId == Id_toString) return 0;\r
79             if (methodId == Id_valueOf) return 0;\r
80         }\r
81         return super.methodArity(methodId);\r
82     }\r
83 \r
84     public Object execMethod\r
85         (int methodId, IdFunction f,\r
86          Context cx, Scriptable scope, Scriptable thisObj, Object[] args)\r
87         throws JavaScriptException\r
88     {\r
89         if (prototypeFlag) {\r
90             if (methodId == Id_constructor) {\r
91                 return jsConstructor(args, thisObj == null);\r
92             }\r
93             else if (methodId == Id_toString) {\r
94                 return realThis(thisObj, f).jsFunction_toString();\r
95             }\r
96             else if (methodId == Id_valueOf) {\r
97                 return wrap_boolean(realThis(thisObj, f).jsFunction_valueOf());\r
98             }\r
99         }\r
100 \r
101         return super.execMethod(methodId, f, cx, scope, thisObj, args);\r
102     }\r
103 \r
104     private NativeBoolean realThis(Scriptable thisObj, IdFunction f) {\r
105         while (!(thisObj instanceof NativeBoolean)) {\r
106             thisObj = nextInstanceCheck(thisObj, f, true);\r
107         }\r
108         return (NativeBoolean)thisObj;\r
109     }\r
110 \r
111 \r
112     private Object jsConstructor(Object[] args, boolean inNewExpr) {\r
113         boolean b = ScriptRuntime.toBoolean(args, 0);\r
114         if (inNewExpr) {\r
115             // new Boolean(val) creates a new boolean object.\r
116             return new NativeBoolean(b);\r
117         }\r
118 \r
119         // Boolean(val) converts val to a boolean.\r
120         return wrap_boolean(b);\r
121     }\r
122 \r
123     private String jsFunction_toString() {\r
124         return booleanValue ? "true" : "false";\r
125     }\r
126 \r
127     private boolean jsFunction_valueOf() {\r
128         return booleanValue;\r
129     }\r
130 \r
131     protected String getIdName(int id) {\r
132         if (prototypeFlag) {\r
133             if (id == Id_constructor) return "constructor";\r
134             if (id == Id_toString) return "toString";\r
135             if (id == Id_valueOf) return "valueOf";\r
136         }\r
137         return null;        \r
138     }\r
139 \r
140 // #string_id_map#\r
141 \r
142     protected int mapNameToId(String s) {\r
143         if (!prototypeFlag) { return 0; }\r
144         int id;\r
145 // #generated# Last update: 2001-04-23 10:38:18 CEST\r
146         L0: { id = 0; String X = null;\r
147             int s_length = s.length();\r
148             if (s_length==7) { X="valueOf";id=Id_valueOf; }\r
149             else if (s_length==8) { X="toString";id=Id_toString; }\r
150             else if (s_length==11) { X="constructor";id=Id_constructor; }\r
151             if (X!=null && X!=s && !X.equals(s)) id = 0;\r
152         }\r
153 // #/generated#\r
154         return id;\r
155     }\r
156 \r
157     private static final int\r
158         Id_constructor          = 1,\r
159         Id_toString             = 2,\r
160         Id_valueOf              = 3,\r
161         MAX_PROTOTYPE_ID        = 3;\r
162 \r
163 // #/string_id_map#\r
164 \r
165     private boolean booleanValue;\r
166 \r
167     private boolean prototypeFlag;\r
168 }\r