File indexing completed on 2024-05-12 15:43:23

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
0004  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
0005  *  Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
0006  *
0007  *  This library is free software; you can redistribute it and/or
0008  *  modify it under the terms of the GNU Library General Public
0009  *  License as published by the Free Software Foundation; either
0010  *  version 2 of the License, or (at your option) any later version.
0011  *
0012  *  This library is distributed in the hope that it will be useful,
0013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *  Library General Public License for more details.
0016  *
0017  *  You should have received a copy of the GNU Library General Public License
0018  *  along with this library; see the file COPYING.LIB.  If not, write to
0019  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  *  Boston, MA 02110-1301, USA.
0021  *
0022  */
0023 
0024 #ifndef INTERNAL_H
0025 #define INTERNAL_H
0026 
0027 #include "JSType.h"
0028 #include "interpreter.h"
0029 #include "object.h"
0030 #include "protect.h"
0031 #include "scope_chain.h"
0032 #include "types.h"
0033 #include "ustring.h"
0034 
0035 #include <wtf/Noncopyable.h>
0036 
0037 #ifndef I18N_NOOP
0038 #define I18N_NOOP(s) s
0039 #endif
0040 
0041 namespace KJS
0042 {
0043 
0044 // ---------------------------------------------------------------------------
0045 //                            Primitive impls
0046 // ---------------------------------------------------------------------------
0047 
0048 class StringImp : public JSCell
0049 {
0050 public:
0051     StringImp() : val(UString::empty) { }
0052     StringImp(const UString &v) : val(v)
0053     {
0054         Collector::reportExtraMemoryCost(v.cost());
0055     }
0056     enum HasOtherOwnerType { HasOtherOwner }; // e.g. storage cost already accounted for
0057     StringImp(const UString &value, HasOtherOwnerType) : val(value) { }
0058     StringImp(const char *v) : val(v) { }
0059     StringImp(const char *v, int len) : val(v, len) { }
0060     const UString &value() const
0061     {
0062         return val;
0063     }
0064 
0065     JSType type() const override
0066     {
0067         return StringType;
0068     }
0069 
0070     JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const override;
0071     bool getPrimitiveNumber(ExecState *, double &number, JSValue *&value) override;
0072     bool toBoolean(ExecState *exec) const override;
0073     double toNumber(ExecState *exec) const override;
0074     UString toString(ExecState *exec) const override;
0075     JSObject *toObject(ExecState *exec) const override;
0076 
0077 private:
0078     UString val;
0079 };
0080 
0081 class NumberImp : public JSCell
0082 {
0083     friend class ConstantValues;
0084     friend KJS_EXPORT JSValue *jsNumberCell(double);
0085 public:
0086     double value() const
0087     {
0088         return val;
0089     }
0090 
0091     JSType type() const override
0092     {
0093         return NumberType;
0094     }
0095 
0096     JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const override;
0097     bool getPrimitiveNumber(ExecState *, double &number, JSValue *&value) override;
0098     bool toBoolean(ExecState *exec) const override;
0099     double toNumber(ExecState *exec) const override;
0100     UString toString(ExecState *exec) const override;
0101     JSObject *toObject(ExecState *exec) const override;
0102 
0103 private:
0104     NumberImp(double v) : val(v) { }
0105 
0106     bool getUInt32(uint32_t &) const override;
0107     bool getTruncatedInt32(int32_t &) const override;
0108     bool getTruncatedUInt32(uint32_t &) const override;
0109 
0110     double val;
0111 };
0112 
0113 // ---------------------------------------------------------------------------
0114 //                            Evaluation
0115 // ---------------------------------------------------------------------------
0116 
0117 struct AttachedInterpreter;
0118 class DebuggerImp
0119 {
0120 public:
0121 
0122     DebuggerImp()
0123     {
0124         interps = nullptr;
0125         isAborted = false;
0126     }
0127 
0128     void abort()
0129     {
0130         isAborted = true;
0131     }
0132     bool aborted() const
0133     {
0134         return isAborted;
0135     }
0136 
0137     AttachedInterpreter *interps;
0138     bool isAborted;
0139 };
0140 
0141 // helper function for toInteger, toInt32, toUInt32 and toUInt16
0142 double roundValue(double d);
0143 inline double roundValue(ExecState *e, JSValue *v)
0144 {
0145     return roundValue(JSValue::toNumber(v, e));
0146 }
0147 
0148 int32_t toInt32(double dd);
0149 uint32_t toUInt32(double dd);
0150 uint16_t toUInt16(double dd);
0151 
0152 //#ifndef NDEBUG
0153 void printInfo(ExecState *exec, const char *s, JSValue *, int lineno = -1);
0154 //#endif
0155 
0156 } // namespace
0157 
0158 #endif //  INTERNAL_H