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