1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/
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.
13
* The Original Code is Rhino code, released
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
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.
38
package org.mozilla.javascript;
41
* This class implements the Boolean native object.
45
public class NativeBoolean extends IdScriptable {
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);
54
* Zero-parameter constructor: just used to create Boolean.prototype
56
public NativeBoolean() {
59
public NativeBoolean(boolean b) {
63
public String getClassName() {
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);
75
public int methodArity(int methodId) {
77
if (methodId == Id_constructor) return 1;
78
if (methodId == Id_toString) return 0;
79
if (methodId == Id_valueOf) return 0;
81
return super.methodArity(methodId);
84
public Object execMethod
85
(int methodId, IdFunction f,
86
Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
87
throws JavaScriptException
90
if (methodId == Id_constructor) {
91
return jsConstructor(args, thisObj == null);
93
else if (methodId == Id_toString) {
94
return realThis(thisObj, f).jsFunction_toString();
96
else if (methodId == Id_valueOf) {
97
return wrap_boolean(realThis(thisObj, f).jsFunction_valueOf());
101
return super.execMethod(methodId, f, cx, scope, thisObj, args);
104
private NativeBoolean realThis(Scriptable thisObj, IdFunction f) {
105
while (!(thisObj instanceof NativeBoolean)) {
106
thisObj = nextInstanceCheck(thisObj, f, true);
108
return (NativeBoolean)thisObj;
112
private Object jsConstructor(Object[] args, boolean inNewExpr) {
113
boolean b = ScriptRuntime.toBoolean(args, 0);
115
// new Boolean(val) creates a new boolean object.
116
return new NativeBoolean(b);
119
// Boolean(val) converts val to a boolean.
120
return wrap_boolean(b);
123
private String jsFunction_toString() {
124
return booleanValue ? "true" : "false";
127
private boolean jsFunction_valueOf() {
131
protected String getIdName(int id) {
133
if (id == Id_constructor) return "constructor";
134
if (id == Id_toString) return "toString";
135
if (id == Id_valueOf) return "valueOf";
142
protected int mapNameToId(String s) {
143
if (!prototypeFlag) { return 0; }
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;
157
private static final int
161
MAX_PROTOTYPE_ID = 3;
165
private boolean booleanValue;
167
private boolean prototypeFlag;
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/
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.
13
* The Original Code is Rhino code, released
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
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.
38
package org.mozilla.javascript;
41
* This class implements the Boolean native object.
45
final class NativeBoolean extends IdScriptableObject
47
private static final Object BOOLEAN_TAG = new Object();
49
static void init(Context cx, Scriptable scope, boolean sealed)
51
NativeBoolean obj = new NativeBoolean(false);
52
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
55
private NativeBoolean(boolean b)
60
public String getClassName()
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);
73
protected void initPrototypeId(int 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));
84
initPrototypeMethod(BOOLEAN_TAG, id, s, arity);
87
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
88
Scriptable thisObj, Object[] args)
90
if (!f.hasTag(BOOLEAN_TAG)) {
91
return super.execIdCall(f, cx, scope, thisObj, args);
93
int id = f.methodId();
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);
101
// Boolean(val) converts val to a boolean.
102
return ScriptRuntime.wrapBoolean(b);
105
// The rest of Boolean.prototype methods require thisObj to be Boolean
107
if (!(thisObj instanceof NativeBoolean))
108
throw incompatibleCallError(f);
109
boolean value = ((NativeBoolean)thisObj).booleanValue;
114
return value ? "true" : "false";
117
return value ? "(new Boolean(true))" : "(new Boolean(false))";
120
return ScriptRuntime.wrapBoolean(value);
122
throw new IllegalArgumentException(String.valueOf(id));
127
protected int findPrototypeId(String s)
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) {
136
if (c=='o') { X="toSource";id=Id_toSource; }
137
else if (c=='t') { X="toString";id=Id_toString; }
139
else if (s_length==11) { X="constructor";id=Id_constructor; }
140
if (X!=null && X!=s && !X.equals(s)) id = 0;
146
private static final int
151
MAX_PROTOTYPE_ID = 4;
155
private boolean booleanValue;