kjs Library API Documentation

bool_object.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "value.h"
00023 #include "object.h"
00024 #include "types.h"
00025 #include "interpreter.h"
00026 #include "operations.h"
00027 #include "bool_object.h"
00028 #include "error_object.h"
00029 #include "lookup.h"
00030 
00031 #include <assert.h>
00032 
00033 using namespace KJS;
00034 
00035 // ------------------------------ BooleanInstanceImp ---------------------------
00036 
00037 const ClassInfo BooleanInstanceImp::info = {"Boolean", 0, 0, 0};
00038 
00039 BooleanInstanceImp::BooleanInstanceImp(const Object &proto)
00040   : ObjectImp(proto)
00041 {
00042 }
00043 
00044 // ------------------------------ BooleanPrototypeImp --------------------------
00045 
00046 // ECMA 15.6.4
00047 
00048 BooleanPrototypeImp::BooleanPrototypeImp(ExecState *exec,
00049                                          ObjectPrototypeImp *objectProto,
00050                                          FunctionPrototypeImp *funcProto)
00051   : BooleanInstanceImp(Object(objectProto))
00052 {
00053   Value protect(this);
00054   // The constructor will be added later by InterpreterImp::InterpreterImp()
00055 
00056   put(exec,"toString", Object(new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ToString,0)), DontEnum);
00057   put(exec,"valueOf",  Object(new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ValueOf,0)),  DontEnum);
00058   setInternalValue(Boolean(false));
00059 }
00060 
00061 
00062 // ------------------------------ BooleanProtoFuncImp --------------------------
00063 
00064 BooleanProtoFuncImp::BooleanProtoFuncImp(ExecState *exec,
00065                                          FunctionPrototypeImp *funcProto, int i, int len)
00066   : InternalFunctionImp(funcProto), id(i)
00067 {
00068   Value protect(this);
00069   put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum);
00070 }
00071 
00072 
00073 bool BooleanProtoFuncImp::implementsCall() const
00074 {
00075   return true;
00076 }
00077 
00078 
00079 // ECMA 15.6.4.2 + 15.6.4.3
00080 Value BooleanProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
00081 {
00082   // no generic function. "this" has to be a Boolean object
00083   KJS_CHECK_THIS( BooleanInstanceImp, thisObj );
00084 
00085   // execute "toString()" or "valueOf()", respectively
00086 
00087   Value v = thisObj.internalValue();
00088   assert(!v.isNull());
00089 
00090   if (id == ToString)
00091     return String(v.toString(exec));
00092   else
00093     return Boolean(v.toBoolean(exec)); /* TODO: optimize for bool case */
00094 }
00095 
00096 // ------------------------------ BooleanObjectImp -----------------------------
00097 
00098 
00099 BooleanObjectImp::BooleanObjectImp(ExecState *exec, FunctionPrototypeImp *funcProto,
00100                                    BooleanPrototypeImp *booleanProto)
00101   : InternalFunctionImp(funcProto)
00102 {
00103   Value protect(this);
00104   put(exec,"prototype", Object(booleanProto),DontEnum|DontDelete|ReadOnly);
00105 
00106   // no. of arguments for constructor
00107   put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum);
00108 }
00109 
00110 
00111 bool BooleanObjectImp::implementsConstruct() const
00112 {
00113   return true;
00114 }
00115 
00116 // ECMA 15.6.2
00117 Object BooleanObjectImp::construct(ExecState *exec, const List &args)
00118 {
00119   Object proto = exec->interpreter()->builtinBooleanPrototype();
00120   Object obj(new BooleanInstanceImp(proto));
00121 
00122   Boolean b;
00123   if (args.size() > 0)
00124     b = args.begin()->toBoolean(exec);
00125   else
00126     b = Boolean(false);
00127 
00128   obj.setInternalValue(b);
00129 
00130   return obj;
00131 }
00132 
00133 bool BooleanObjectImp::implementsCall() const
00134 {
00135   return true;
00136 }
00137 
00138 // ECMA 15.6.1
00139 Value BooleanObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00140 {
00141   if (args.isEmpty())
00142     return Boolean(false);
00143   else
00144     return Boolean(args[0].toBoolean(exec)); /* TODO: optimize for bool case */
00145 }
00146 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:16 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001