1/*
2 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 *  Copyright (C) 2003, 2008, 2011 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#include "Operations.h"
30
31namespace JSC {
32
33static EncodedJSValue JSC_HOST_CALL booleanProtoFuncToString(ExecState*);
34static EncodedJSValue JSC_HOST_CALL booleanProtoFuncValueOf(ExecState*);
35
36}
37
38#include "BooleanPrototype.lut.h"
39
40namespace JSC {
41
42const ClassInfo BooleanPrototype::s_info = { "Boolean", &BooleanObject::s_info, 0, ExecState::booleanPrototypeTable, CREATE_METHOD_TABLE(BooleanPrototype) };
43
44/* Source for BooleanPrototype.lut.h
45@begin booleanPrototypeTable
46  toString  booleanProtoFuncToString    DontEnum|Function 0
47  valueOf   booleanProtoFuncValueOf     DontEnum|Function 0
48@end
49*/
50
51ASSERT_HAS_TRIVIAL_DESTRUCTOR(BooleanPrototype);
52
53BooleanPrototype::BooleanPrototype(ExecState* exec, Structure* structure)
54    : BooleanObject(exec->vm(), structure)
55{
56}
57
58void BooleanPrototype::finishCreation(ExecState* exec, JSGlobalObject*)
59{
60    Base::finishCreation(exec->vm());
61    setInternalValue(exec->vm(), jsBoolean(false));
62
63    ASSERT(inherits(&s_info));
64}
65
66bool BooleanPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
67{
68    return getStaticFunctionSlot<BooleanObject>(exec, ExecState::booleanPrototypeTable(exec), jsCast<BooleanPrototype*>(cell), propertyName, slot);
69}
70
71bool BooleanPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
72{
73    return getStaticFunctionDescriptor<BooleanObject>(exec, ExecState::booleanPrototypeTable(exec), jsCast<BooleanPrototype*>(object), propertyName, descriptor);
74}
75
76// ------------------------------ Functions ---------------------------
77
78EncodedJSValue JSC_HOST_CALL booleanProtoFuncToString(ExecState* exec)
79{
80    VM* vm = &exec->vm();
81    JSValue thisValue = exec->hostThisValue();
82    if (thisValue == jsBoolean(false))
83        return JSValue::encode(vm->smallStrings.falseString());
84
85    if (thisValue == jsBoolean(true))
86        return JSValue::encode(vm->smallStrings.trueString());
87
88    if (!thisValue.inherits(&BooleanObject::s_info))
89        return throwVMTypeError(exec);
90
91    if (asBooleanObject(thisValue)->internalValue() == jsBoolean(false))
92        return JSValue::encode(vm->smallStrings.falseString());
93
94    ASSERT(asBooleanObject(thisValue)->internalValue() == jsBoolean(true));
95    return JSValue::encode(vm->smallStrings.trueString());
96}
97
98EncodedJSValue JSC_HOST_CALL booleanProtoFuncValueOf(ExecState* exec)
99{
100    JSValue thisValue = exec->hostThisValue();
101    if (thisValue.isBoolean())
102        return JSValue::encode(thisValue);
103
104    if (!thisValue.inherits(&BooleanObject::s_info))
105        return throwVMTypeError(exec);
106
107    return JSValue::encode(asBooleanObject(thisValue)->internalValue());
108}
109
110} // namespace JSC
111