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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ToString,0,toStringPropertyName), DontEnum); 00058 putDirect(valueOfPropertyName, new BooleanProtoFuncImp(exec,funcProto,BooleanProtoFuncImp::ValueOf,0,valueOfPropertyName), DontEnum); 00059 setInternalValue(Boolean(false)); 00060 } 00061 00062 00063 // ------------------------------ BooleanProtoFuncImp -------------------------- 00064 00065 BooleanProtoFuncImp::BooleanProtoFuncImp(ExecState * /*exec*/, 00066 FunctionPrototypeImp *funcProto, int i, int len, const Identifier &_ident) 00067 : InternalFunctionImp(funcProto), id(i) 00068 { 00069 Value protect(this); 00070 putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum); 00071 ident = _ident; 00072 } 00073 00074 00075 bool BooleanProtoFuncImp::implementsCall() const 00076 { 00077 return true; 00078 } 00079 00080 00081 // ECMA 15.6.4.2 + 15.6.4.3 00082 Value BooleanProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/) 00083 { 00084 // no generic function. "this" has to be a Boolean object 00085 KJS_CHECK_THIS( BooleanInstanceImp, thisObj ); 00086 00087 // execute "toString()" or "valueOf()", respectively 00088 00089 Value v = thisObj.internalValue(); 00090 assert(v.isValid()); 00091 00092 if (id == ToString) 00093 return String(v.toString(exec)); 00094 return Boolean(v.toBoolean(exec)); /* TODO: optimize for bool case */ 00095 } 00096 00097 // ------------------------------ BooleanObjectImp ----------------------------- 00098 00099 00100 BooleanObjectImp::BooleanObjectImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto, 00101 BooleanPrototypeImp *booleanProto) 00102 : InternalFunctionImp(funcProto) 00103 { 00104 Value protect(this); 00105 putDirect(prototypePropertyName, booleanProto, DontEnum|DontDelete|ReadOnly); 00106 00107 // no. of arguments for constructor 00108 putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum); 00109 } 00110 00111 00112 bool BooleanObjectImp::implementsConstruct() const 00113 { 00114 return true; 00115 } 00116 00117 // ECMA 15.6.2 00118 Object BooleanObjectImp::construct(ExecState *exec, const List &args) 00119 { 00120 Object obj(new BooleanInstanceImp(exec->lexicalInterpreter()->builtinBooleanPrototype().imp())); 00121 00122 Boolean b; 00123 if (args.size() > 0) 00124 b = args.begin()->dispatchToBoolean(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