webengine/osswebengine/JavaScriptCore/kjs/value.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  *  This file is part of the KDE libraries
       
     3  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
       
     4  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
       
     5  *  Copyright (C) 2003 Apple Computer, Inc.
       
     6  *
       
     7  *  This library is free software; you can redistribute it and/or
       
     8  *  modify it under the terms of the GNU Library General Public
       
     9  *  License as published by the Free Software Foundation; either
       
    10  *  version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  *  This library is distributed in the hope that it will be useful,
       
    13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  *  Library General Public License for more details.
       
    16  *
       
    17  *  You should have received a copy of the GNU Library General Public License
       
    18  *  along with this library; see the file COPYING.LIB.  If not, write to
       
    19  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20  *  Boston, MA 02110-1301, USA.
       
    21  *
       
    22  */
       
    23 
       
    24 #include "config.h"
       
    25 #include "value.h"
       
    26 
       
    27 #include "error_object.h"
       
    28 #include "nodes.h"
       
    29 #include "operations.h"
       
    30 #include <stdio.h>
       
    31 #include <string.h>
       
    32 #include <wtf/MathExtras.h>
       
    33 
       
    34 namespace KJS {
       
    35 
       
    36 static const double D16 = 65536.0;
       
    37 static const double D32 = 4294967296.0;
       
    38 
       
    39 EXPORT
       
    40 void *JSCell::operator new(size_t size)
       
    41 {
       
    42     return Collector::allocate(size);
       
    43 }
       
    44 
       
    45 EXPORT
       
    46 bool JSCell::getUInt32(unsigned&) const
       
    47 {
       
    48     return false;
       
    49 }
       
    50 
       
    51 // ECMA 9.4
       
    52 double JSValue::toInteger(ExecState *exec) const
       
    53 {
       
    54     uint32_t i;
       
    55     if (getUInt32(i))
       
    56         return i;
       
    57     return roundValue(exec, const_cast<JSValue*>(this));
       
    58 }
       
    59 
       
    60 EXPORT
       
    61 int32_t JSValue::toInt32(ExecState* exec) const
       
    62 {
       
    63     bool ok;
       
    64     return toInt32(exec, ok);
       
    65 }
       
    66 
       
    67 EXPORT
       
    68 int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
       
    69 {
       
    70     ok = true;
       
    71 
       
    72     uint32_t i;
       
    73     if (getUInt32(i))
       
    74         return i;
       
    75 
       
    76     double d = roundValue(exec, const_cast<JSValue*>(this));
       
    77     if (isNaN(d) || isInf(d)) {
       
    78         ok = false;
       
    79         return 0;
       
    80     }
       
    81     double d32 = fmod(d, D32);
       
    82 
       
    83     if (d32 >= D32 / 2)
       
    84         d32 -= D32;
       
    85     else if (d32 < -D32 / 2)
       
    86         d32 += D32;
       
    87 
       
    88     return static_cast<int32_t>(d32);
       
    89 }
       
    90 
       
    91 EXPORT
       
    92 uint32_t JSValue::toUInt32(ExecState* exec) const
       
    93 {
       
    94     bool ok;
       
    95     return toUInt32(exec, ok);
       
    96 }
       
    97 
       
    98 EXPORT
       
    99 uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
       
   100 {
       
   101     ok = true;
       
   102 
       
   103     uint32_t i;
       
   104     if (getUInt32(i))
       
   105         return i;
       
   106 
       
   107     double d = roundValue(exec, const_cast<JSValue*>(this));
       
   108     if (isNaN(d) || isInf(d)) {
       
   109         ok = false;
       
   110         return 0;
       
   111     }
       
   112     double d32 = fmod(d, D32);
       
   113 
       
   114     if (d32 < 0)
       
   115         d32 += D32;
       
   116 
       
   117     return static_cast<uint32_t>(d32);
       
   118 }
       
   119 
       
   120 uint16_t JSValue::toUInt16(ExecState *exec) const
       
   121 {
       
   122     uint32_t i;
       
   123     if (getUInt32(i))
       
   124         return static_cast<uint16_t>(i);
       
   125 
       
   126     double d = roundValue(exec, const_cast<JSValue*>(this));
       
   127     if (isNaN(d) || isInf(d))
       
   128         return 0;
       
   129     double d16 = fmod(d, D16);
       
   130 
       
   131     if (d16 < 0)
       
   132         d16 += D16;
       
   133 
       
   134     return static_cast<uint16_t>(d16);
       
   135 }
       
   136 
       
   137 EXPORT
       
   138 float JSValue::toFloat(ExecState* exec) const
       
   139 {
       
   140     return static_cast<float>(toNumber(exec));
       
   141 }
       
   142 
       
   143 bool JSCell::getNumber(double &numericValue) const
       
   144 {
       
   145     if (!isNumber())
       
   146         return false;
       
   147     numericValue = static_cast<const NumberImp *>(this)->value();
       
   148     return true;
       
   149 }
       
   150 
       
   151 EXPORT
       
   152 double JSCell::getNumber() const
       
   153 {
       
   154     return isNumber() ? static_cast<const NumberImp *>(this)->value() : NaN;
       
   155 }
       
   156 
       
   157 EXPORT
       
   158 bool JSCell::getString(UString &stringValue) const
       
   159 {
       
   160     if (!isString())
       
   161         return false;
       
   162     stringValue = static_cast<const StringImp *>(this)->value();
       
   163     return true;
       
   164 }
       
   165 
       
   166 UString JSCell::getString() const
       
   167 {
       
   168     return isString() ? static_cast<const StringImp *>(this)->value() : UString();
       
   169 }
       
   170 
       
   171 EXPORT
       
   172 JSObject *JSCell::getObject()
       
   173 {
       
   174     return isObject() ? static_cast<JSObject *>(this) : 0;
       
   175 }
       
   176 
       
   177 const JSObject *JSCell::getObject() const
       
   178 {
       
   179     return isObject() ? static_cast<const JSObject *>(this) : 0;
       
   180 }
       
   181 
       
   182 EXPORT
       
   183 JSCell* jsString(const char* s)
       
   184 {
       
   185     return new StringImp(s ? s : "");
       
   186 }
       
   187 
       
   188 EXPORT
       
   189 JSCell* jsString(const UString& s)
       
   190 {
       
   191     return s.isNull() ? new StringImp("") : new StringImp(s);
       
   192 }
       
   193 
       
   194 EXPORT
       
   195 JSCell* jsOwnedString(const UString& s)
       
   196 {
       
   197     return s.isNull() ? new StringImp("", StringImp::HasOtherOwner) : new StringImp(s, StringImp::HasOtherOwner);
       
   198 }
       
   199 
       
   200 // This method includes a PIC branch to set up the NumberImp's vtable, so we quarantine
       
   201 // it in a separate function to keep the normal case speedy.
       
   202 EXPORT
       
   203 JSValue *jsNumberCell(double d)
       
   204 {
       
   205     return new NumberImp(d);
       
   206 }
       
   207 
       
   208 } // namespace KJS