~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/NativeBoolean.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

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