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