JSValue.h 8.23 KB
Newer Older
1 2 3
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4
 *  Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
mjs's avatar
mjs committed
18
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
 *  Boston, MA 02110-1301, USA.
20 21 22
 *
 */

darin@apple.com's avatar
darin@apple.com committed
23 24
#ifndef JSValue_h
#define JSValue_h
25

26 27
#include "CallData.h"
#include "ConstructData.h"
darin's avatar
darin committed
28 29 30 31
#include "JSImmediate.h"
#include "ustring.h"
#include <stddef.h> // for size_t

32 33 34 35
// The magic number 0x4000 is not important here, it is being subtracted back out (avoiding using zero since this
// can have unexpected effects in this type of macro, particularly where multiple-inheritance is involved).
#define OBJECT_OFFSET(class, member) (reinterpret_cast<ptrdiff_t>(&(reinterpret_cast<class*>(0x4000)->member)) - 0x4000)

36
namespace JSC {
37

weinig@apple.com's avatar
weinig@apple.com committed
38 39 40 41 42 43
    class ExecState;
    class Identifier;
    class JSCell;
    class JSObject;
    class JSString;
    class PropertySlot;
weinig@apple.com's avatar
weinig@apple.com committed
44 45
    class PutPropertySlot;
    class StructureID;
weinig@apple.com's avatar
weinig@apple.com committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    struct ClassInfo;
    struct Instruction;

    /**
     * JSValue is the base type for all primitives (Undefined, Null, Boolean,
     * String, Number) and objects in ECMAScript.
     *
     * Note: you should never inherit from JSValue as it is for primitive types
     * only (all of which are provided internally by KJS). Instead, inherit from
     * JSObject.
     */
    class JSValue : Noncopyable {
        friend class JSCell; // so it can derive from this class
    private:
        JSValue();
        virtual ~JSValue();

    public:
        // Querying the type.
        bool isUndefined() const;
        bool isNull() const;
        bool isUndefinedOrNull() const;
        bool isBoolean() const;
        bool isNumber() const;
        bool isString() const;
darin@apple.com's avatar
darin@apple.com committed
71
        bool isGetterSetter() const;
weinig@apple.com's avatar
weinig@apple.com committed
72 73
        bool isObject() const;
        bool isObject(const ClassInfo*) const; // FIXME: Merge with inherits.
weinig@apple.com's avatar
weinig@apple.com committed
74
        
weinig@apple.com's avatar
weinig@apple.com committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        // Extracting the value.
        bool getBoolean(bool&) const;
        bool getBoolean() const; // false if not a boolean
        bool getNumber(double&) const;
        double getNumber() const; // NaN if not a number
        double uncheckedGetNumber() const;
        bool getString(UString&) const;
        UString getString() const; // null string if not a string
        JSObject* getObject(); // NULL if not an object
        const JSObject* getObject() const; // NULL if not an object

        CallType getCallData(CallData&);
        ConstructType getConstructData(ConstructData&);

        // Extracting integer values.
        bool getUInt32(uint32_t&) const;
        bool getTruncatedInt32(int32_t&) const;
        bool getTruncatedUInt32(uint32_t&) const;
        
        // Basic conversions.
darin@apple.com's avatar
darin@apple.com committed
95 96
        enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
        JSValue* toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
weinig@apple.com's avatar
weinig@apple.com committed
97 98
        bool getPrimitiveNumber(ExecState*, double& number, JSValue*&);

mjs@apple.com's avatar
mjs@apple.com committed
99
        bool toBoolean(ExecState*) const;
weinig@apple.com's avatar
weinig@apple.com committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

        // toNumber conversion is expected to be side effect free if an exception has
        // been set in the ExecState already.
        double toNumber(ExecState*) const;
        JSValue* toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
        UString toString(ExecState*) const;
        JSObject* toObject(ExecState*) const;

        // Integer conversions.
        double toInteger(ExecState*) const;
        double toIntegerPreserveNaN(ExecState*) const;
        int32_t toInt32(ExecState*) const;
        int32_t toInt32(ExecState*, bool& ok) const;
        uint32_t toUInt32(ExecState*) const;
        uint32_t toUInt32(ExecState*, bool& ok) const;

        // These are identical logic to above, and faster than jsNumber(number)->toInt32(exec)
        static int32_t toInt32(double);
ggaren@apple.com's avatar
ggaren@apple.com committed
118
        static uint32_t toUInt32(double);
weinig@apple.com's avatar
weinig@apple.com committed
119 120 121 122 123 124 125 126 127 128 129 130 131

        // Floating point conversions.
        float toFloat(ExecState*) const;

        // Garbage collection.
        void mark();
        bool marked() const;

        static int32_t toInt32SlowCase(double, bool& ok);
        static uint32_t toUInt32SlowCase(double, bool& ok);

        // Object operations, with the toObject operation included.
        JSValue* get(ExecState*, const Identifier& propertyName) const;
weinig@apple.com's avatar
weinig@apple.com committed
132
        JSValue* get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
weinig@apple.com's avatar
weinig@apple.com committed
133
        JSValue* get(ExecState*, unsigned propertyName) const;
weinig@apple.com's avatar
weinig@apple.com committed
134 135
        JSValue* get(ExecState*, unsigned propertyName, PropertySlot&) const;
        void put(ExecState*, const Identifier& propertyName, JSValue*, PutPropertySlot&);
weinig@apple.com's avatar
weinig@apple.com committed
136 137 138 139
        void put(ExecState*, unsigned propertyName, JSValue*);
        bool deleteProperty(ExecState*, const Identifier& propertyName);
        bool deleteProperty(ExecState*, unsigned propertyName);

mjs@apple.com's avatar
mjs@apple.com committed
140
        bool needsThisConversion() const;
weinig@apple.com's avatar
weinig@apple.com committed
141 142 143 144 145 146
        JSObject* toThisObject(ExecState*) const;
        UString toThisString(ExecState*) const;
        JSString* toThisJSString(ExecState*);

        JSValue* getJSNumber(); // 0 if this is not a JSNumber or number object

mjs@apple.com's avatar
mjs@apple.com committed
147 148 149
        JSCell* asCell();
        const JSCell* asCell() const;

weinig@apple.com's avatar
weinig@apple.com committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    private:
        bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
        bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
        int32_t toInt32SlowCase(ExecState*, bool& ok) const;
        uint32_t toUInt32SlowCase(ExecState*, bool& ok) const;
    };

    inline JSValue::JSValue()
    {
    }

    inline JSValue::~JSValue()
    {
    }

    inline bool JSValue::isUndefined() const
    {
        return this == jsUndefined();
    }

    inline bool JSValue::isNull() const
    {
        return this == jsNull();
    }

    inline bool JSValue::isUndefinedOrNull() const
    {
        return JSImmediate::isUndefinedOrNull(this);
    }

    inline bool JSValue::isBoolean() const
    {
        return JSImmediate::isBoolean(this);
ggaren's avatar
ggaren committed
183
    }
weinig@apple.com's avatar
weinig@apple.com committed
184 185 186 187 188 189 190 191 192 193 194 195 196

    inline bool JSValue::getBoolean(bool& v) const
    {
        if (JSImmediate::isBoolean(this)) {
            v = JSImmediate::toBoolean(this);
            return true;
        }
        
        return false;
    }

    inline bool JSValue::getBoolean() const
    {
darin@apple.com's avatar
darin@apple.com committed
197
        return this == jsBoolean(true);
weinig@apple.com's avatar
weinig@apple.com committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    }

    ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
    {
        int32_t i;
        if (getTruncatedInt32(i))
            return i;
        bool ok;
        return toInt32SlowCase(exec, ok);
    }

    inline uint32_t JSValue::toUInt32(ExecState* exec) const
    {
        uint32_t i;
        if (getTruncatedUInt32(i))
            return i;
        bool ok;
        return toUInt32SlowCase(exec, ok);
    }

    inline int32_t JSValue::toInt32(double val)
    {
        if (!(val >= -2147483648.0 && val < 2147483648.0)) {
            bool ignored;
            return toInt32SlowCase(val, ignored);
        }
        return static_cast<int32_t>(val);
225
    }
weinig@apple.com's avatar
weinig@apple.com committed
226

ggaren@apple.com's avatar
ggaren@apple.com committed
227
    inline uint32_t JSValue::toUInt32(double val)
weinig@apple.com's avatar
weinig@apple.com committed
228 229 230 231 232 233
    {
        if (!(val >= 0.0 && val < 4294967296.0)) {
            bool ignored;
            return toUInt32SlowCase(val, ignored);
        }
        return static_cast<uint32_t>(val);
234
    }
weinig@apple.com's avatar
weinig@apple.com committed
235 236 237 238 239 240 241 242 243

    inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
    {
        int32_t i;
        if (getTruncatedInt32(i)) {
            ok = true;
            return i;
        }
        return toInt32SlowCase(exec, ok);
darin's avatar
darin committed
244
    }
weinig@apple.com's avatar
weinig@apple.com committed
245 246 247 248 249 250 251 252 253

    inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
    {
        uint32_t i;
        if (getTruncatedUInt32(i)) {
            ok = true;
            return i;
        }
        return toUInt32SlowCase(exec, ok);
darin's avatar
darin committed
254 255
    }

256
} // namespace JSC
257

darin@apple.com's avatar
darin@apple.com committed
258
#endif // JSValue_h