bool_object.cpp
// -*- c-basic-offset: 2 -*-
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "value.h"
#include "object.h"
#include "types.h"
#include "interpreter.h"
#include "operations.h"
#include "bool_object.h"
#include "error_object.h"
#include "lookup.h"
#include <assert.h>
using namespace KJS;
// ------------------------------ BooleanInstanceImp ---------------------------
const ClassInfo BooleanInstanceImp::info = {"Boolean", 0, 0, 0};
BooleanInstanceImp::BooleanInstanceImp(ObjectImp *proto)
: ObjectImp(proto)
{
}
// ------------------------------ BooleanPrototypeImp --------------------------
// ECMA 15.6.4
BooleanPrototypeImp::BooleanPrototypeImp(ExecState *exec,
ObjectPrototypeImp *objectProto,
FunctionPrototypeImp *funcProto)
: BooleanInstanceImp(objectProto)
{
Value protect(this);
// The constructor will be added later by InterpreterImp::InterpreterImp()
putDirect(toStringPropertyName, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ToString,0,toStringPropertyName), DontEnum);
putDirect(valueOfPropertyName, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ValueOf,0,valueOfPropertyName), DontEnum);
setInternalValue(Boolean(false));
}
// ------------------------------ BooleanProtoFuncImp --------------------------
BooleanProtoFuncImp::BooleanProtoFuncImp(ExecState * /*exec*/,
FunctionPrototypeImp *funcProto, int i, int len, const Identifier &_ident)
: InternalFunctionImp(funcProto), id(i)
{
Value protect(this);
putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
ident = _ident;
}
bool BooleanProtoFuncImp::implementsCall() const
{
return true;
}
// ECMA 15.6.4.2 + 15.6.4.3
Value BooleanProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
{
// no generic function. "this" has to be a Boolean object
KJS_CHECK_THIS( BooleanInstanceImp, thisObj );
// execute "toString()" or "valueOf()", respectively
Value v = thisObj.internalValue();
assert(v.isValid());
if (id == ToString)
return String(v.toString(exec));
return Boolean(v.toBoolean(exec)); /* TODO: optimize for bool case */
}
// ------------------------------ BooleanObjectImp -----------------------------
BooleanObjectImp::BooleanObjectImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
BooleanPrototypeImp *booleanProto)
: InternalFunctionImp(funcProto)
{
Value protect(this);
putDirect(prototypePropertyName, booleanProto, DontEnum|DontDelete|ReadOnly);
// no. of arguments for constructor
putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
}
bool BooleanObjectImp::implementsConstruct() const
{
return true;
}
// ECMA 15.6.2
Object BooleanObjectImp::construct(ExecState *exec, const List &args)
{
Object obj(new BooleanInstanceImp(exec->lexicalInterpreter()->builtinBooleanPrototype().imp()));
Boolean b;
if (args.size() > 0)
b = args.begin()->dispatchToBoolean(exec);
else
b = Boolean(false);
obj.setInternalValue(b);
return obj;
}
bool BooleanObjectImp::implementsCall() const
{
return true;
}
// ECMA 15.6.1
Value BooleanObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
{
if (args.isEmpty())
return Boolean(false);
else
return Boolean(args[0].toBoolean(exec)); /* TODO: optimize for bool case */
}
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'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::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

KDE Home | KDE Accessibility Home | Description of Access Keys