1 // -*- c-basic-offset: 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"
26 #include "operations.h"
27 #include "error_object.h"
31 // ------------------------------ BooleanInstance ---------------------------
33 const ClassInfo
BooleanInstance::info
= {"Boolean", 0, 0, 0};
35 BooleanInstance::BooleanInstance(JSObject
*proto
)
36 : JSWrapperObject(proto
)
40 // ------------------------------ BooleanPrototype --------------------------
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
)
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();
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
101 JSObject
*BooleanObjectImp::construct(ExecState
*exec
, const List
&args
)
103 BooleanInstance
*obj(new BooleanInstance(exec
->lexicalInterpreter()->builtinBooleanPrototype()));
107 b
= args
.begin()->toBoolean(exec
);
111 obj
->setInternalValue(jsBoolean(b
));
117 JSValue
*BooleanObjectImp::callAsFunction(ExecState
*exec
, JSObject
* /*thisObj*/, const List
&args
)
120 return jsBoolean(false);
122 return jsBoolean(args
[0]->toBoolean(exec
)); /* TODO: optimize for bool case */