Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeBoolean.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 /**
10 * This class implements the Boolean native object.
11 * See ECMA 15.6.
12 * @author Norris Boyd
13 */
14 final class NativeBoolean extends IdScriptableObject
15 {
16 static final long serialVersionUID = -3716996899943880933L;
17
18 private static final Object BOOLEAN_TAG = "Boolean";
19
20 static void init(Scriptable scope, boolean sealed)
21 {
22 NativeBoolean obj = new NativeBoolean(false);
23 obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
24 }
25
26 NativeBoolean(boolean b)
27 {
28 booleanValue = b;
29 }
30
31 @Override
32 public String getClassName()
33 {
34 return "Boolean";
35 }
36
37 @Override
38 public Object getDefaultValue(Class<?> typeHint) {
39 // This is actually non-ECMA, but will be proposed
40 // as a change in round 2.
41 if (typeHint == ScriptRuntime.BooleanClass)
42 return ScriptRuntime.wrapBoolean(booleanValue);
43 return super.getDefaultValue(typeHint);
44 }
45
46 @Override
47 protected void initPrototypeId(int id)
48 {
49 String s;
50 int arity;
51 switch (id) {
52 case Id_constructor: arity=1; s="constructor"; break;
53 case Id_toString: arity=0; s="toString"; break;
54 case Id_toSource: arity=0; s="toSource"; break;
55 case Id_valueOf: arity=0; s="valueOf"; break;
56 default: throw new IllegalArgumentException(String.valueOf(id));
57 }
58 initPrototypeMethod(BOOLEAN_TAG, id, s, arity);
59 }
60
61 @Override
62 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
63 Scriptable thisObj, Object[] args)
64 {
65 if (!f.hasTag(BOOLEAN_TAG)) {
66 return super.execIdCall(f, cx, scope, thisObj, args);
67 }
68 int id = f.methodId();
69
70 if (id == Id_constructor) {
71 boolean b;
72 if (args.length == 0) {
73 b = false;
74 } else {
75 b = args[0] instanceof ScriptableObject &&
76 ((ScriptableObject) args[0]).avoidObjectDetection()
77 ? true
78 : ScriptRuntime.toBoolean(args[0]);
79 }
80 if (thisObj == null) {
81 // new Boolean(val) creates a new boolean object.
82 return new NativeBoolean(b);
83 }
84 // Boolean(val) converts val to a boolean.
85 return ScriptRuntime.wrapBoolean(b);
86 }
87
88 // The rest of Boolean.prototype methods require thisObj to be Boolean
89
90 if (!(thisObj instanceof NativeBoolean))
91 throw incompatibleCallError(f);
92 boolean value = ((NativeBoolean)thisObj).booleanValue;
93
94 switch (id) {
95
96 case Id_toString:
97 return value ? "true" : "false";
98
99 case Id_toSource:
100 return value ? "(new Boolean(true))" : "(new Boolean(false))";
101
102 case Id_valueOf:
103 return ScriptRuntime.wrapBoolean(value);
104 }
105 throw new IllegalArgumentException(String.valueOf(id));
106 }
107
108 // #string_id_map#
109
110 @Override
111 protected int findPrototypeId(String s)
112 {
113 int id;
114 // #generated# Last update: 2007-05-09 08:15:31 EDT
115 L0: { id = 0; String X = null; int c;
116 int s_length = s.length();
117 if (s_length==7) { X="valueOf";id=Id_valueOf; }
118 else if (s_length==8) {
119 c=s.charAt(3);
120 if (c=='o') { X="toSource";id=Id_toSource; }
121 else if (c=='t') { X="toString";id=Id_toString; }
122 }
123 else if (s_length==11) { X="constructor";id=Id_constructor; }
124 if (X!=null && X!=s && !X.equals(s)) id = 0;
125 break L0;
126 }
127 // #/generated#
128 return id;
129 }
130
131 private static final int
132 Id_constructor = 1,
133 Id_toString = 2,
134 Id_toSource = 3,
135 Id_valueOf = 4,
136 MAX_PROTOTYPE_ID = 4;
137
138 // #/string_id_map#
139
140 private boolean booleanValue;
141 }