1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2003 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23#include "bool_object.h"
24#include <config-kjs.h>
25
26#include "operations.h"
27#include "error_object.h"
28
29using namespace KJS;
30
31// ------------------------------ BooleanInstance ---------------------------
32
33const ClassInfo BooleanInstance::info = {"Boolean", 0, 0, 0};
34
35BooleanInstance::BooleanInstance(JSObject *proto)
36 : JSWrapperObject(proto)
37{
38}
39
40JSObject* BooleanInstance::valueClone(Interpreter* targetCtx) const
41{
42 BooleanInstance* copy = new BooleanInstance(targetCtx->builtinBooleanPrototype());
43 copy->setInternalValue(internalValue());
44 return copy;
45}
46
47// ------------------------------ BooleanPrototype --------------------------
48
49// ECMA 15.6.4
50
51BooleanPrototype::BooleanPrototype(ExecState* exec, ObjectPrototype* objectProto, FunctionPrototype* funcProto)
52 : BooleanInstance(objectProto)
53{
54 // The constructor will be added later by Interpreter::Interpreter()
55
56 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum);
57 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ValueOf, 0, exec->propertyNames().valueOf), DontEnum);
58 setInternalValue(jsBoolean(false));
59}
60
61
62// ------------------------------ BooleanProtoFunc --------------------------
63
64BooleanProtoFunc::BooleanProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
65 : InternalFunctionImp(funcProto, name)
66 , id(i)
67{
68 putDirect(exec->propertyNames().length, len, DontDelete|ReadOnly|DontEnum);
69}
70
71
72// ECMA 15.6.4.2 + 15.6.4.3
73JSValue *BooleanProtoFunc::callAsFunction(ExecState* exec, JSObject *thisObj, const List &/*args*/)
74{
75 // no generic function. "this" has to be a Boolean object
76 if (!thisObj->inherits(&BooleanInstance::info))
77 return throwError(exec, TypeError);
78
79 // execute "toString()" or "valueOf()", respectively
80
81 JSValue *v = static_cast<BooleanInstance*>(thisObj)->internalValue();
82 assert(v);
83
84 if (id == ToString)
85 return jsString(v->toString(exec));
86 return jsBoolean(v->toBoolean(exec)); /* TODO: optimize for bool case */
87}
88
89// ------------------------------ BooleanObjectImp -----------------------------
90
91
92BooleanObjectImp::BooleanObjectImp(ExecState* exec, FunctionPrototype* funcProto, BooleanPrototype* booleanProto)
93 : InternalFunctionImp(funcProto)
94{
95 putDirect(exec->propertyNames().prototype, booleanProto, DontEnum|DontDelete|ReadOnly);
96
97 // no. of arguments for constructor
98 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
99}
100
101
102bool BooleanObjectImp::implementsConstruct() const
103{
104 return true;
105}
106
107// ECMA 15.6.2
108JSObject *BooleanObjectImp::construct(ExecState *exec, const List &args)
109{
110 BooleanInstance *obj(new BooleanInstance(exec->lexicalInterpreter()->builtinBooleanPrototype()));
111
112 bool b;
113 if (args.size() > 0)
114 b = args.begin()->toBoolean(exec);
115 else
116 b = false;
117
118 obj->setInternalValue(jsBoolean(b));
119
120 return obj;
121}
122
123// ECMA 15.6.1
124JSValue *BooleanObjectImp::callAsFunction(ExecState *exec, JSObject * /*thisObj*/, const List &args)
125{
126 if (args.isEmpty())
127 return jsBoolean(false);
128 else
129 return jsBoolean(args[0]->toBoolean(exec)); /* TODO: optimize for bool case */
130}
131
132