| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 82 |
return new NativeBoolean(b); |
| 83 |
} |
| 84 |
|
| 85 |
return ScriptRuntime.wrapBoolean(b); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 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 |
|
| 109 |
|
| 110 |
@Override |
| 111 |
protected int findPrototypeId(String s) |
| 112 |
{ |
| 113 |
int id; |
| 114 |
|
| 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 |
|
| 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 |
|
| 139 |
|
| 140 |
private boolean booleanValue; |
| 141 |
} |