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

kjs

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

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
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.8.11
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |