• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
bool_object.cpp
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 "value.h"
23 #include "object.h"
24 #include "types.h"
25 #include "interpreter.h"
26 #include "operations.h"
27 #include "bool_object.h"
28 #include "error_object.h"
29 #include "lookup.h"
30 
31 #include <assert.h>
32 
33 using namespace KJS;
34 
35 // ------------------------------ BooleanInstanceImp ---------------------------
36 
37 const ClassInfo BooleanInstanceImp::info = {"Boolean", 0, 0, 0};
38 
39 BooleanInstanceImp::BooleanInstanceImp(ObjectImp *proto)
40  : ObjectImp(proto)
41 {
42 }
43 
44 // ------------------------------ BooleanPrototypeImp --------------------------
45 
46 // ECMA 15.6.4
47 
48 BooleanPrototypeImp::BooleanPrototypeImp(ExecState *exec,
49  ObjectPrototypeImp *objectProto,
50  FunctionPrototypeImp *funcProto)
51  : BooleanInstanceImp(objectProto)
52 {
53  Value protect(this);
54  // The constructor will be added later by InterpreterImp::InterpreterImp()
55 
56  putDirect(toStringPropertyName, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ToString,0,toStringPropertyName), DontEnum);
57  putDirect(valueOfPropertyName, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ValueOf,0,valueOfPropertyName), DontEnum);
58  setInternalValue(Boolean(false));
59 }
60 
61 
62 // ------------------------------ BooleanProtoFuncImp --------------------------
63 
64 BooleanProtoFuncImp::BooleanProtoFuncImp(ExecState * /*exec*/,
65  FunctionPrototypeImp *funcProto, int i, int len, const Identifier &_ident)
66  : InternalFunctionImp(funcProto), id(i)
67 {
68  Value protect(this);
69  putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
70  ident = _ident;
71 }
72 
73 
74 bool BooleanProtoFuncImp::implementsCall() const
75 {
76  return true;
77 }
78 
79 
80 // ECMA 15.6.4.2 + 15.6.4.3
81 Value BooleanProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
82 {
83  // no generic function. "this" has to be a Boolean object
84  KJS_CHECK_THIS( BooleanInstanceImp, thisObj );
85 
86  // execute "toString()" or "valueOf()", respectively
87 
88  Value v = thisObj.internalValue();
89  assert(v.isValid());
90 
91  if (id == ToString)
92  return String(v.toString(exec));
93  return Boolean(v.toBoolean(exec)); /* TODO: optimize for bool case */
94 }
95 
96 // ------------------------------ BooleanObjectImp -----------------------------
97 
98 
99 BooleanObjectImp::BooleanObjectImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
100  BooleanPrototypeImp *booleanProto)
101  : InternalFunctionImp(funcProto)
102 {
103  Value protect(this);
104  putDirect(prototypePropertyName, booleanProto, DontEnum|DontDelete|ReadOnly);
105 
106  // no. of arguments for constructor
107  putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
108 }
109 
110 
111 bool BooleanObjectImp::implementsConstruct() const
112 {
113  return true;
114 }
115 
116 // ECMA 15.6.2
117 Object BooleanObjectImp::construct(ExecState *exec, const List &args)
118 {
119  Object obj(new BooleanInstanceImp(exec->lexicalInterpreter()->builtinBooleanPrototype().imp()));
120 
121  Boolean b;
122  if (args.size() > 0)
123  b = args.begin()->dispatchToBoolean(exec);
124  else
125  b = Boolean(false);
126 
127  obj.setInternalValue(b);
128 
129  return obj;
130 }
131 
132 bool BooleanObjectImp::implementsCall() const
133 {
134  return true;
135 }
136 
137 // ECMA 15.6.1
138 Value BooleanObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
139 {
140  if (args.isEmpty())
141  return Boolean(false);
142  else
143  return Boolean(args[0].toBoolean(exec)); /* TODO: optimize for bool case */
144 }
145 
KJS::Boolean
Represents an primitive Boolean value.
Definition: value.h:316
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor)
Definition: function_object.h:34
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::Interpreter::builtinBooleanPrototype
Object builtinBooleanPrototype() const
Returns the builtin "Boolean.prototype" object.
Definition: interpreter.cpp:233
KJS::List
Native list type.
Definition: list.h:48
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86
KJS::List::begin
ListIterator begin() const
Definition: list.h:186
KJS::List::size
int size() const
Definition: list.h:90
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Object::internalValue
Value internalValue() const
Returns the internal value of the object.
Definition: object.h:717
KJS::String
Represents an primitive String value.
Definition: value.h:340
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:246
KJS::Value::toBoolean
bool toBoolean(ExecState *exec) const
Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
Definition: value.h:216
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181
KJS::ClassInfo
Class Information.
Definition: object.h:58

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.