fix logic
[personal-kdelibs.git] / kjs / bool_object.cpp
blob734b4531b9b8dbb734a53c49614a83a31d078913
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.
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.
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.
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
23 #include "bool_object.h"
24 #include <config.h>
26 #include "operations.h"
27 #include "error_object.h"
29 using namespace KJS;
31 // ------------------------------ BooleanInstance ---------------------------
33 const ClassInfo BooleanInstance::info = {"Boolean", 0, 0, 0};
35 BooleanInstance::BooleanInstance(JSObject *proto)
36 : JSWrapperObject(proto)
40 // ------------------------------ BooleanPrototype --------------------------
42 // ECMA 15.6.4
44 BooleanPrototype::BooleanPrototype(ExecState* exec, ObjectPrototype* objectProto, FunctionPrototype* funcProto)
45 : BooleanInstance(objectProto)
47 // The constructor will be added later by Interpreter::Interpreter()
49 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum);
50 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ValueOf, 0, exec->propertyNames().valueOf), DontEnum);
51 setInternalValue(jsBoolean(false));
55 // ------------------------------ BooleanProtoFunc --------------------------
57 BooleanProtoFunc::BooleanProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
58 : InternalFunctionImp(funcProto, name)
59 , id(i)
61 putDirect(exec->propertyNames().length, len, DontDelete|ReadOnly|DontEnum);
65 // ECMA 15.6.4.2 + 15.6.4.3
66 JSValue *BooleanProtoFunc::callAsFunction(ExecState* exec, JSObject *thisObj, const List &/*args*/)
68 // no generic function. "this" has to be a Boolean object
69 if (!thisObj->inherits(&BooleanInstance::info))
70 return throwError(exec, TypeError);
72 // execute "toString()" or "valueOf()", respectively
74 JSValue *v = static_cast<BooleanInstance*>(thisObj)->internalValue();
75 assert(v);
77 if (id == ToString)
78 return jsString(v->toString(exec));
79 return jsBoolean(v->toBoolean(exec)); /* TODO: optimize for bool case */
82 // ------------------------------ BooleanObjectImp -----------------------------
85 BooleanObjectImp::BooleanObjectImp(ExecState* exec, FunctionPrototype* funcProto, BooleanPrototype* booleanProto)
86 : InternalFunctionImp(funcProto)
88 putDirect(exec->propertyNames().prototype, booleanProto, DontEnum|DontDelete|ReadOnly);
90 // no. of arguments for constructor
91 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
95 bool BooleanObjectImp::implementsConstruct() const
97 return true;
100 // ECMA 15.6.2
101 JSObject *BooleanObjectImp::construct(ExecState *exec, const List &args)
103 BooleanInstance *obj(new BooleanInstance(exec->lexicalInterpreter()->builtinBooleanPrototype()));
105 bool b;
106 if (args.size() > 0)
107 b = args.begin()->toBoolean(exec);
108 else
109 b = false;
111 obj->setInternalValue(jsBoolean(b));
113 return obj;
116 // ECMA 15.6.1
117 JSValue *BooleanObjectImp::callAsFunction(ExecState *exec, JSObject * /*thisObj*/, const List &args)
119 if (args.isEmpty())
120 return jsBoolean(false);
121 else
122 return jsBoolean(args[0]->toBoolean(exec)); /* TODO: optimize for bool case */