1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\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
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
13 * The Original Code is Rhino code, released
\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
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
38 package org.mozilla.javascript;
\r
41 * This class implements the Boolean native object.
\r
43 * @author Norris Boyd
\r
45 public class NativeBoolean extends IdScriptable {
\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
54 * Zero-parameter constructor: just used to create Boolean.prototype
\r
56 public NativeBoolean() {
\r
59 public NativeBoolean(boolean b) {
\r
63 public String getClassName() {
\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
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
81 return super.methodArity(methodId);
\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
89 if (prototypeFlag) {
\r
90 if (methodId == Id_constructor) {
\r
91 return jsConstructor(args, thisObj == null);
\r
93 else if (methodId == Id_toString) {
\r
94 return realThis(thisObj, f).jsFunction_toString();
\r
96 else if (methodId == Id_valueOf) {
\r
97 return wrap_boolean(realThis(thisObj, f).jsFunction_valueOf());
\r
101 return super.execMethod(methodId, f, cx, scope, thisObj, args);
\r
104 private NativeBoolean realThis(Scriptable thisObj, IdFunction f) {
\r
105 while (!(thisObj instanceof NativeBoolean)) {
\r
106 thisObj = nextInstanceCheck(thisObj, f, true);
\r
108 return (NativeBoolean)thisObj;
\r
112 private Object jsConstructor(Object[] args, boolean inNewExpr) {
\r
113 boolean b = ScriptRuntime.toBoolean(args, 0);
\r
115 // new Boolean(val) creates a new boolean object.
\r
116 return new NativeBoolean(b);
\r
119 // Boolean(val) converts val to a boolean.
\r
120 return wrap_boolean(b);
\r
123 private String jsFunction_toString() {
\r
124 return booleanValue ? "true" : "false";
\r
127 private boolean jsFunction_valueOf() {
\r
128 return booleanValue;
\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
142 protected int mapNameToId(String s) {
\r
143 if (!prototypeFlag) { return 0; }
\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
157 private static final int
\r
158 Id_constructor = 1,
\r
161 MAX_PROTOTYPE_ID = 3;
\r
163 // #/string_id_map#
\r
165 private boolean booleanValue;
\r
167 private boolean prototypeFlag;
\r