1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "config.h"
22 #include "BooleanPrototype.h"
23
24 #include "Error.h"
25 #include "ExceptionHelpers.h"
26 #include "JSFunction.h"
27 #include "JSString.h"
28 #include "ObjectPrototype.h"
29
30 namespace JSC {
31
32 ASSERT_CLASS_FITS_IN_CELL(BooleanPrototype);
33
34 // Functions
35 static EncodedJSValue JSC_HOST_CALL booleanProtoFuncToString(ExecState*);
36 static EncodedJSValue JSC_HOST_CALL booleanProtoFuncValueOf(ExecState*);
37
38 // ECMA 15.6.4
39
BooleanPrototype(ExecState * exec,JSGlobalObject * globalObject,Structure * structure,Structure * functionStructure)40 BooleanPrototype::BooleanPrototype(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, Structure* functionStructure)
41 : BooleanObject(exec->globalData(), structure)
42 {
43 setInternalValue(exec->globalData(), jsBoolean(false));
44
45 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, globalObject, functionStructure, 0, exec->propertyNames().toString, booleanProtoFuncToString), DontEnum);
46 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, globalObject, functionStructure, 0, exec->propertyNames().valueOf, booleanProtoFuncValueOf), DontEnum);
47 }
48
49
50 // ------------------------------ Functions --------------------------
51
52 // ECMA 15.6.4.2 + 15.6.4.3
53
booleanProtoFuncToString(ExecState * exec)54 EncodedJSValue JSC_HOST_CALL booleanProtoFuncToString(ExecState* exec)
55 {
56 JSValue thisValue = exec->hostThisValue();
57 if (thisValue == jsBoolean(false))
58 return JSValue::encode(jsNontrivialString(exec, "false"));
59
60 if (thisValue == jsBoolean(true))
61 return JSValue::encode(jsNontrivialString(exec, "true"));
62
63 if (!thisValue.inherits(&BooleanObject::s_info))
64 return throwVMTypeError(exec);
65
66 if (asBooleanObject(thisValue)->internalValue() == jsBoolean(false))
67 return JSValue::encode(jsNontrivialString(exec, "false"));
68
69 ASSERT(asBooleanObject(thisValue)->internalValue() == jsBoolean(true));
70 return JSValue::encode(jsNontrivialString(exec, "true"));
71 }
72
booleanProtoFuncValueOf(ExecState * exec)73 EncodedJSValue JSC_HOST_CALL booleanProtoFuncValueOf(ExecState* exec)
74 {
75 JSValue thisValue = exec->hostThisValue();
76 if (thisValue.isBoolean())
77 return JSValue::encode(thisValue);
78
79 if (!thisValue.inherits(&BooleanObject::s_info))
80 return throwVMTypeError(exec);
81
82 return JSValue::encode(asBooleanObject(thisValue)->internalValue());
83 }
84
85 } // namespace JSC
86