File indexing completed on 2024-05-12 15:43:15
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 0004 * Copyright (C) 2003 Apple Computer, Inc. 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Lesser General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Lesser General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Lesser General Public 0017 * License along with this library; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0019 * 0020 */ 0021 0022 #include "bool_object.h" 0023 0024 #include "operations.h" 0025 #include "error_object.h" 0026 0027 using namespace KJS; 0028 0029 // ------------------------------ BooleanInstance --------------------------- 0030 0031 const ClassInfo BooleanInstance::info = {"Boolean", nullptr, nullptr, nullptr}; 0032 0033 BooleanInstance::BooleanInstance(JSObject *proto) 0034 : JSWrapperObject(proto) 0035 { 0036 } 0037 0038 JSObject *BooleanInstance::valueClone(Interpreter *targetCtx) const 0039 { 0040 BooleanInstance *copy = new BooleanInstance(targetCtx->builtinBooleanPrototype()); 0041 copy->setInternalValue(internalValue()); 0042 return copy; 0043 } 0044 0045 // ------------------------------ BooleanPrototype -------------------------- 0046 0047 // ECMA 15.6.4 0048 0049 BooleanPrototype::BooleanPrototype(ExecState *exec, ObjectPrototype *objectProto, FunctionPrototype *funcProto) 0050 : BooleanInstance(objectProto) 0051 { 0052 // The constructor will be added later by Interpreter::Interpreter() 0053 0054 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum); 0055 putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ValueOf, 0, exec->propertyNames().valueOf), DontEnum); 0056 setInternalValue(jsBoolean(false)); 0057 } 0058 0059 // ------------------------------ BooleanProtoFunc -------------------------- 0060 0061 BooleanProtoFunc::BooleanProtoFunc(ExecState *exec, FunctionPrototype *funcProto, int i, int len, const Identifier &name) 0062 : InternalFunctionImp(funcProto, name) 0063 , id(i) 0064 { 0065 putDirect(exec->propertyNames().length, len, DontDelete | ReadOnly | DontEnum); 0066 } 0067 0068 // ECMA 15.6.4.2 + 15.6.4.3 0069 JSValue *BooleanProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &/*args*/) 0070 { 0071 // no generic function. "this" has to be a Boolean object 0072 if (!thisObj->inherits(&BooleanInstance::info)) { 0073 return throwError(exec, TypeError); 0074 } 0075 0076 // execute "toString()" or "valueOf()", respectively 0077 0078 JSValue *v = static_cast<BooleanInstance *>(thisObj)->internalValue(); 0079 assert(v); 0080 0081 if (id == ToString) { 0082 return jsString(JSValue::toString(v, exec)); 0083 } 0084 return jsBoolean(JSValue::toBoolean(v, exec)); /* TODO: optimize for bool case */ 0085 } 0086 0087 // ------------------------------ BooleanObjectImp ----------------------------- 0088 0089 BooleanObjectImp::BooleanObjectImp(ExecState *exec, FunctionPrototype *funcProto, BooleanPrototype *booleanProto) 0090 : InternalFunctionImp(funcProto) 0091 { 0092 putDirect(exec->propertyNames().prototype, booleanProto, DontEnum | DontDelete | ReadOnly); 0093 0094 // no. of arguments for constructor 0095 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum); 0096 } 0097 0098 bool BooleanObjectImp::implementsConstruct() const 0099 { 0100 return true; 0101 } 0102 0103 // ECMA 15.6.2 0104 JSObject *BooleanObjectImp::construct(ExecState *exec, const List &args) 0105 { 0106 BooleanInstance *obj(new BooleanInstance(exec->lexicalInterpreter()->builtinBooleanPrototype())); 0107 0108 bool b; 0109 if (args.size() > 0) { 0110 b = JSValue::toBoolean(*args.begin(), exec); 0111 } else { 0112 b = false; 0113 } 0114 0115 obj->setInternalValue(jsBoolean(b)); 0116 0117 return obj; 0118 } 0119 0120 // ECMA 15.6.1 0121 JSValue *BooleanObjectImp::callAsFunction(ExecState *exec, JSObject * /*thisObj*/, const List &args) 0122 { 0123 if (args.isEmpty()) { 0124 return jsBoolean(false); 0125 } else { 0126 return jsBoolean(JSValue::toBoolean(args[0], exec)); /* TODO: optimize for bool case */ 0127 } 0128 } 0129