KJS

bool_object.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2000 Harri Porten ([email protected])
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 
33 BooleanInstance::BooleanInstance(JSObject *proto)
34  : JSWrapperObject(proto)
35 {
36 }
37 
38 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 
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 
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
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 
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 
98 bool BooleanObjectImp::implementsConstruct() const
99 {
100  return true;
101 }
102 
103 // ECMA 15.6.2
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
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 
Class Information.
Definition: object.h:48
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
bool isEmpty() const
Definition: list.h:104
JSObject * builtinBooleanPrototype() const
Returns the builtin "Boolean.prototype" object.
int size() const
Definition: list.h:112
Represents the current state of script execution.
Definition: ExecState.h:53
Represents an Identifier for a Javascript object.
Definition: identifier.h:36
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:56
This class is used as a base for classes such as String, Number, Boolean and Date which which are wra...
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: ExecState.cpp:35
Native list type.
Definition: list.h:52
QString name(StandardShortcut id)
virtual QVariant callAsFunction(ScriptableExtension *callerPrincipal, quint64 objId, const ArgList &args)
ListIterator begin() const
Definition: list.h:275
const QList< QKeySequence > & copy()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 03:56:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.