weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999-2002 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
| 4 | * Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public License |
| 17 | * along with this library; see the file COPYING.LIB. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 24 | #include "JSNumberCell.h" |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 25 | |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 26 | #include "NumberObject.h" |
| 27 | #include "ustring.h" |
| 28 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 29 | namespace JSC { |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 30 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 31 | JSValue* JSNumberCell::toPrimitive(ExecState*, PreferredPrimitiveType) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 32 | { |
| 33 | return const_cast<JSNumberCell*>(this); |
| 34 | } |
| 35 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 36 | bool JSNumberCell::getPrimitiveNumber(ExecState*, double& number, JSValue*& value) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 37 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 38 | number = m_value; |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 39 | value = this; |
| 40 | return true; |
| 41 | } |
| 42 | |
mjs@apple.com | 6c3268c | 2008-10-06 18:31:07 +0000 | [diff] [blame] | 43 | bool JSNumberCell::toBoolean(ExecState*) const |
| 44 | { |
| 45 | return m_value < 0.0 || m_value > 0.0; // false for NaN |
| 46 | } |
| 47 | |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 48 | double JSNumberCell::toNumber(ExecState*) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 49 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 50 | return m_value; |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | UString JSNumberCell::toString(ExecState*) const |
| 54 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 55 | if (m_value == 0.0) // +0.0 or -0.0 |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 56 | return "0"; |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 57 | return UString::from(m_value); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | UString JSNumberCell::toThisString(ExecState*) const |
| 61 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 62 | if (m_value == 0.0) // +0.0 or -0.0 |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 63 | return "0"; |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 64 | return UString::from(m_value); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | JSObject* JSNumberCell::toObject(ExecState* exec) const |
| 68 | { |
| 69 | return constructNumber(exec, const_cast<JSNumberCell*>(this)); |
| 70 | } |
| 71 | |
| 72 | JSObject* JSNumberCell::toThisObject(ExecState* exec) const |
| 73 | { |
| 74 | return constructNumber(exec, const_cast<JSNumberCell*>(this)); |
| 75 | } |
| 76 | |
| 77 | bool JSNumberCell::getUInt32(uint32_t& uint32) const |
| 78 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 79 | uint32 = static_cast<uint32_t>(m_value); |
| 80 | return uint32 == m_value; |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool JSNumberCell::getTruncatedInt32(int32_t& int32) const |
| 84 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 85 | if (!(m_value >= -2147483648.0 && m_value < 2147483648.0)) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 86 | return false; |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 87 | int32 = static_cast<int32_t>(m_value); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool JSNumberCell::getTruncatedUInt32(uint32_t& uint32) const |
| 92 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 93 | if (!(m_value >= 0.0 && m_value < 4294967296.0)) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 94 | return false; |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 95 | uint32 = static_cast<uint32_t>(m_value); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 99 | JSValue* JSNumberCell::getJSNumber() |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 100 | { |
| 101 | return this; |
| 102 | } |
| 103 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 104 | NEVER_INLINE JSValue* jsNumberCell(ExecState* exec, double d) |
darin@apple.com | 8281d83 | 2008-09-21 02:29:12 +0000 | [diff] [blame] | 105 | { |
| 106 | return new (exec) JSNumberCell(exec, d); |
| 107 | } |
| 108 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 109 | NEVER_INLINE JSValue* jsNaN(ExecState* exec) |
darin@apple.com | 8281d83 | 2008-09-21 02:29:12 +0000 | [diff] [blame] | 110 | { |
| 111 | return new (exec) JSNumberCell(exec, NaN); |
| 112 | } |
| 113 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 114 | NEVER_INLINE JSValue* jsNumberCell(JSGlobalData* globalData, double d) |
darin@apple.com | 3d73fee | 2008-10-03 21:39:16 +0000 | [diff] [blame] | 115 | { |
| 116 | return new (globalData) JSNumberCell(globalData, d); |
| 117 | } |
| 118 | |
darin@apple.com | 44331f8 | 2008-10-24 16:22:51 +0000 | [diff] [blame] | 119 | NEVER_INLINE JSValue* jsNaN(JSGlobalData* globalData) |
darin@apple.com | 3d73fee | 2008-10-03 21:39:16 +0000 | [diff] [blame] | 120 | { |
| 121 | return new (globalData) JSNumberCell(globalData, NaN); |
| 122 | } |
| 123 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 124 | } // namespace JSC |