Ignore:
Timestamp:
Nov 11, 2006, 12:50:38 AM (19 years ago)
Author:
ap
Message:

Reviewed by Maciej.

http://bugs.webkit.org/show_bug.cgi?id=11508
Undisable some warnings for JSImmediate.h

Fix suggested by Don Gibson.

  • kjs/JSImmediate.h: Re-enable all MSVC warnings, move the remaining runtime checks to compile-time.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/JSImmediate.h

    r17587 r17723  
    22 *  This file is part of the KDE libraries
    33 *  Copyright (C) 2003-2006 Apple Computer, Inc
     4 *  Copyright (C) 2006 Alexey Proskuryakov ([email protected])
    45 *
    56 *  This library is free software; you can redistribute it and/or
     
    2829#include <stdlib.h>
    2930
    30 #if COMPILER(MSVC)
    31 #pragma warning(push)
    32 #pragma warning(disable: 4127)
    33 #pragma warning(disable: 4244)
    34 #pragma warning(disable: 4305)
    35 #pragma warning(disable: 4307)
    36 #pragma warning(disable: 4309)
    37 #pragma warning(disable: 4341)
    38 #endif
    39 
    4031namespace KJS {
    4132
     
    4536class UString;
    4637
    47 template<bool for32bit, bool for64bit> struct NanAsBitsValue {};
    48 template<> struct NanAsBitsValue<true, false> {
    49     enum { value = 0x7fc00000 };
    50 };
    51 template<> struct NanAsBitsValue<false, true> {
    52     enum { value = 0x7ff80000ULL << 32 };
    53 };
    54 
    55 template<bool for32bit, bool for64bit> struct oneAsBitsValue {};
    56 template<> struct oneAsBitsValue<true, false> {
    57     enum { value = 0x3f800000 };
    58 };
    59 template<> struct oneAsBitsValue<false, true> {
    60     enum { value = 0x3ff00000ULL << 32 };
    61 };
    62 
    6338/*
    64  * A JSValue * is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged
    65  * IEEE floating point bit pattern masquerading as a pointer). The low two bits in a JSValue * are available
     39 * A JSValue* is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged
     40 * IEEE floating point bit pattern masquerading as a pointer). The low two bits in a JSValue* are available
    6641 * for type tagging because allocator alignment guarantees they will be 00 in cell pointers.
    6742 *
    6843 * For example, on a 32 bit system:
    6944 *
    70  * JSCell *:      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 00
     45 * JSCell*:       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                 00
    7146 *               [ high 30 bits: pointer address ]  [ low 2 bits -- always 0 ]
    7247 *
     
    8560class JSImmediate {
    8661public:
    87     static bool isImmediate(const JSValue *v)
     62    static bool isImmediate(const JSValue* v)
    8863    {
    8964        return getTag(v) != 0;
    9065    }
    9166   
    92     static bool isNumber(const JSValue *v)
     67    static bool isNumber(const JSValue* v)
    9368    {
    9469        return (getTag(v) == NumberType);
    9570    }
    9671   
    97     static bool isBoolean(const JSValue *v)
     72    static bool isBoolean(const JSValue* v)
    9873    {
    9974        return (getTag(v) == BooleanType);
     
    10176   
    10277    // Since we have room for only 3 unique tags, null and undefined have to share.
    103     static bool isUndefinedOrNull(const JSValue *v)
     78    static bool isUndefinedOrNull(const JSValue* v)
    10479    {
    10580        return (getTag(v) == UndefinedType);
    10681    }
    10782
    108     static JSValue *fromDouble(double d)
    109     {
    110         if (is32bit) {
    111             FloatUnion floatUnion;
    112             floatUnion.asFloat = d;
    113            
    114             // check for data loss from tagging
    115             if ((floatUnion.asBits & TagMask) != 0)
    116               return 0;
    117            
    118             // check for data loss from conversion to float
    119             DoubleUnion doubleUnion1, doubleUnion2;
    120             doubleUnion1.asDouble = floatUnion.asFloat;
    121             doubleUnion2.asDouble = d;
    122             if (doubleUnion1.asBits != doubleUnion2.asBits)
    123                 return 0;
    124            
    125             return tag(floatUnion.asBits, NumberType);
    126         } else if (is64bit) {
    127             DoubleUnion doubleUnion;
    128             doubleUnion.asDouble = d;
    129            
    130             // check for data loss from tagging
    131             if ((doubleUnion.asBits & TagMask) != 0)
    132                 return 0;
    133 
    134             return tag(doubleUnion.asBits, NumberType);
    135         } else {
    136             // could just return 0 without aborting, but nicer to be explicit about not supporting the platform well
    137             abort();
    138             return 0;
    139         }
    140     }
    141    
    142     static double toDouble(const JSValue *v)
    143     {
    144         ASSERT(isImmediate(v));
    145        
    146         if (is32bit) {
    147             FloatUnion floatUnion;
    148             floatUnion.asBits = unTag(v);
    149             return floatUnion.asFloat;
    150         } else if (is64bit) {
    151             DoubleUnion doubleUnion;
    152             doubleUnion.asBits = unTag(v);
    153             return doubleUnion.asDouble;
    154         } else {
    155             abort();
    156             return 0;
    157         }
    158     }
    159 
    160     static bool toBoolean(const JSValue *v)
    161     {
    162         ASSERT(isImmediate(v));
    163        
    164         uintptr_t bits = unTag(v);
    165         if ((bits << 1) == 0) // -0.0 has the sign bit set
    166             return false;
    167 
    168         return bits != NanAsBits();
    169     }
    170    
    171     static JSObject *toObject(const JSValue *, ExecState *);
    172     static UString toString(const JSValue *);
    173     static JSType type(const JSValue *);
     83    static JSValue* fromDouble(double d);
     84    static double toDouble(const JSValue* v);
     85    static bool toBoolean(const JSValue* v);
     86    static JSObject* toObject(const JSValue*, ExecState*);
     87    static UString toString(const JSValue*);
     88    static JSType type(const JSValue*);
    17489   
    17590    // It would nice just to use fromDouble() to create these values, but that would prevent them from
    17691    // turning into compile-time constants.
    177     static JSValue *trueImmediate() { return tag(oneAsBits(), BooleanType); }
    178     static JSValue *falseImmediate() { return tag(zeroAsBits(), BooleanType); }
    179     static JSValue *NaNImmediate() { return tag(NanAsBits(), NumberType); }
    180     static JSValue *undefinedImmediate() { return tag(NanAsBits(), UndefinedType); }
    181     static JSValue *nullImmediate() { return tag(zeroAsBits(), UndefinedType); }
     92    static JSValue* trueImmediate();
     93    static JSValue* falseImmediate();
     94    static JSValue* NaNImmediate();
     95    static JSValue* undefinedImmediate();
     96    static JSValue* nullImmediate();
    18297   
    18398private:
    18499    static const uintptr_t TagMask = 3; // type tags are 2 bits long
    185100   
    186     static JSValue *tag(uintptr_t bits, uintptr_t tag)
    187     {
    188         return reinterpret_cast<JSValue *>(bits | tag);
    189     }
    190    
    191     static uintptr_t unTag(const JSValue *v)
     101    static JSValue* tag(uintptr_t bits, uintptr_t tag)
     102    {
     103        return reinterpret_cast<JSValue*>(bits | tag);
     104    }
     105   
     106    static uintptr_t unTag(const JSValue* v)
    192107    {
    193108        return reinterpret_cast<uintptr_t>(v) & ~TagMask;
    194109    }
    195110   
    196     static uintptr_t getTag(const JSValue *v)
     111    static uintptr_t getTag(const JSValue* v)
    197112    {
    198113        return reinterpret_cast<uintptr_t>(v) & TagMask;
     
    219134        sizeof(float) == sizeof(uint32_t) && sizeof(double) == sizeof(uint64_t) && sizeof(uintptr_t) == sizeof(uint64_t);
    220135
    221     static uintptr_t NanAsBits()
    222     {
    223         return NanAsBitsValue<is32bit, is64bit>::value;
    224     }
    225 
    226     static uintptr_t zeroAsBits()
    227     {
    228         return 0x0;
    229     }
    230 
    231     static uintptr_t oneAsBits()
    232     {
    233         return oneAsBitsValue<is32bit, is64bit>::value;
    234     }
     136    template<bool for32bit, bool for64bit> struct FPBitValues {};
    235137};
    236138
     139template<> struct JSImmediate::FPBitValues<true, false> {
     140    static const uint32_t nanAsBits = 0x7fc00000;
     141    static const uint32_t oneAsBits = 0x3f800000;
     142    static const uint32_t zeroAsBits = 0x0;
     143
     144    static JSValue* fromDouble(double d)
     145    {
     146        FloatUnion floatUnion;
     147        floatUnion.asFloat = static_cast<float>(d);
     148
     149        // check for data loss from tagging
     150        if ((floatUnion.asBits & TagMask) != 0)
     151            return 0;
     152
     153        // check for data loss from conversion to float
     154        DoubleUnion doubleUnion1, doubleUnion2;
     155        doubleUnion1.asDouble = floatUnion.asFloat;
     156        doubleUnion2.asDouble = d;
     157        if (doubleUnion1.asBits != doubleUnion2.asBits)
     158            return 0;
     159
     160        return tag(floatUnion.asBits, NumberType);
     161    }
     162
     163    static double toDouble(const JSValue* v)
     164    {
     165        ASSERT(isImmediate(v));
     166
     167        FloatUnion floatUnion;
     168        floatUnion.asBits = static_cast<uint32_t>(unTag(v));
     169        return floatUnion.asFloat;
     170    }
     171};
     172
     173template<> struct JSImmediate::FPBitValues<false, true> {
     174    static const uint64_t nanAsBits = 0x7ff80000ULL << 32;
     175    static const uint64_t oneAsBits = 0x3ff00000ULL << 32;
     176    static const uint64_t zeroAsBits = 0x0;
     177
     178    static JSValue* fromDouble(double d)
     179    {
     180        DoubleUnion doubleUnion;
     181        doubleUnion.asDouble = d;
     182
     183        // check for data loss from tagging
     184        if ((doubleUnion.asBits & TagMask) != 0)
     185            return 0;
     186
     187        return tag(static_cast<uintptr_t>(doubleUnion.asBits), NumberType);
     188    }
     189
     190    static double toDouble(const JSValue* v)
     191    {
     192        ASSERT(isImmediate(v));
     193
     194        DoubleUnion doubleUnion;
     195        doubleUnion.asBits = unTag(v);
     196        return doubleUnion.asDouble;
     197    }
     198};
     199
     200inline JSValue* JSImmediate::trueImmediate() { return tag(FPBitValues<is32bit, is64bit>::oneAsBits, BooleanType); }
     201inline JSValue* JSImmediate::falseImmediate() { return tag(FPBitValues<is32bit, is64bit>::zeroAsBits, BooleanType); }
     202inline JSValue* JSImmediate::NaNImmediate() { return tag(FPBitValues<is32bit, is64bit>::nanAsBits, NumberType); }
     203inline JSValue* JSImmediate::undefinedImmediate() { return tag(FPBitValues<is32bit, is64bit>::nanAsBits, UndefinedType); }
     204inline JSValue* JSImmediate::nullImmediate() { return tag(FPBitValues<is32bit, is64bit>::zeroAsBits, UndefinedType); }
     205
     206inline bool JSImmediate::toBoolean(const JSValue* v)
     207{
     208    ASSERT(isImmediate(v));
     209
     210    uintptr_t bits = unTag(v);
     211    if ((bits << 1) == 0) // -0.0 has the sign bit set
     212        return false;
     213
     214    return bits != FPBitValues<is32bit, is64bit>::nanAsBits;
     215}
     216
     217inline JSValue* JSImmediate::fromDouble(double d)
     218{
     219    return FPBitValues<is32bit, is64bit>::fromDouble(d);
     220}
     221
     222inline double JSImmediate::toDouble(const JSValue* v)
     223{
     224    return FPBitValues<is32bit, is64bit>::toDouble(v);
     225}
     226
    237227} // namespace KJS
    238228
    239 #if COMPILER(MSVC)
    240 #pragma warning(pop)
    241229#endif
    242 
    243 #endif
Note: See TracChangeset for help on using the changeset viewer.