blob: 5b3f3bd54b060c44c2bc106d01dfb4d56ffcdf5c [file] [log] [blame]
weinig@apple.comefdce0f2008-06-30 20:52:03 +00001/*
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.com6a03b4c2008-07-01 17:32:44 +000024#include "JSNumberCell.h"
weinig@apple.comefdce0f2008-06-30 20:52:03 +000025
weinig@apple.comefdce0f2008-06-30 20:52:03 +000026#include "NumberObject.h"
27#include "ustring.h"
28
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000029namespace JSC {
weinig@apple.comefdce0f2008-06-30 20:52:03 +000030
darin@apple.com44331f82008-10-24 16:22:51 +000031JSValue* JSNumberCell::toPrimitive(ExecState*, PreferredPrimitiveType) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +000032{
33 return const_cast<JSNumberCell*>(this);
34}
35
darin@apple.com44331f82008-10-24 16:22:51 +000036bool JSNumberCell::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
weinig@apple.comefdce0f2008-06-30 20:52:03 +000037{
weinig@apple.com2947a912008-07-07 02:49:29 +000038 number = m_value;
weinig@apple.comefdce0f2008-06-30 20:52:03 +000039 value = this;
40 return true;
41}
42
mjs@apple.com6c3268c2008-10-06 18:31:07 +000043bool JSNumberCell::toBoolean(ExecState*) const
44{
45 return m_value < 0.0 || m_value > 0.0; // false for NaN
46}
47
weinig@apple.com2947a912008-07-07 02:49:29 +000048double JSNumberCell::toNumber(ExecState*) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +000049{
weinig@apple.com2947a912008-07-07 02:49:29 +000050 return m_value;
weinig@apple.comefdce0f2008-06-30 20:52:03 +000051}
52
53UString JSNumberCell::toString(ExecState*) const
54{
weinig@apple.com2947a912008-07-07 02:49:29 +000055 if (m_value == 0.0) // +0.0 or -0.0
weinig@apple.comefdce0f2008-06-30 20:52:03 +000056 return "0";
weinig@apple.com2947a912008-07-07 02:49:29 +000057 return UString::from(m_value);
weinig@apple.comefdce0f2008-06-30 20:52:03 +000058}
59
60UString JSNumberCell::toThisString(ExecState*) const
61{
weinig@apple.com2947a912008-07-07 02:49:29 +000062 if (m_value == 0.0) // +0.0 or -0.0
weinig@apple.comefdce0f2008-06-30 20:52:03 +000063 return "0";
weinig@apple.com2947a912008-07-07 02:49:29 +000064 return UString::from(m_value);
weinig@apple.comefdce0f2008-06-30 20:52:03 +000065}
66
67JSObject* JSNumberCell::toObject(ExecState* exec) const
68{
69 return constructNumber(exec, const_cast<JSNumberCell*>(this));
70}
71
72JSObject* JSNumberCell::toThisObject(ExecState* exec) const
73{
74 return constructNumber(exec, const_cast<JSNumberCell*>(this));
75}
76
77bool JSNumberCell::getUInt32(uint32_t& uint32) const
78{
weinig@apple.com2947a912008-07-07 02:49:29 +000079 uint32 = static_cast<uint32_t>(m_value);
80 return uint32 == m_value;
weinig@apple.comefdce0f2008-06-30 20:52:03 +000081}
82
83bool JSNumberCell::getTruncatedInt32(int32_t& int32) const
84{
weinig@apple.com2947a912008-07-07 02:49:29 +000085 if (!(m_value >= -2147483648.0 && m_value < 2147483648.0))
weinig@apple.comefdce0f2008-06-30 20:52:03 +000086 return false;
weinig@apple.com2947a912008-07-07 02:49:29 +000087 int32 = static_cast<int32_t>(m_value);
weinig@apple.comefdce0f2008-06-30 20:52:03 +000088 return true;
89}
90
91bool JSNumberCell::getTruncatedUInt32(uint32_t& uint32) const
92{
weinig@apple.com2947a912008-07-07 02:49:29 +000093 if (!(m_value >= 0.0 && m_value < 4294967296.0))
weinig@apple.comefdce0f2008-06-30 20:52:03 +000094 return false;
weinig@apple.com2947a912008-07-07 02:49:29 +000095 uint32 = static_cast<uint32_t>(m_value);
weinig@apple.comefdce0f2008-06-30 20:52:03 +000096 return true;
97}
98
darin@apple.com44331f82008-10-24 16:22:51 +000099JSValue* JSNumberCell::getJSNumber()
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000100{
101 return this;
102}
103
darin@apple.com44331f82008-10-24 16:22:51 +0000104NEVER_INLINE JSValue* jsNumberCell(ExecState* exec, double d)
darin@apple.com8281d832008-09-21 02:29:12 +0000105{
106 return new (exec) JSNumberCell(exec, d);
107}
108
darin@apple.com44331f82008-10-24 16:22:51 +0000109NEVER_INLINE JSValue* jsNaN(ExecState* exec)
darin@apple.com8281d832008-09-21 02:29:12 +0000110{
111 return new (exec) JSNumberCell(exec, NaN);
112}
113
darin@apple.com44331f82008-10-24 16:22:51 +0000114NEVER_INLINE JSValue* jsNumberCell(JSGlobalData* globalData, double d)
darin@apple.com3d73fee2008-10-03 21:39:16 +0000115{
116 return new (globalData) JSNumberCell(globalData, d);
117}
118
darin@apple.com44331f82008-10-24 16:22:51 +0000119NEVER_INLINE JSValue* jsNaN(JSGlobalData* globalData)
darin@apple.com3d73fee2008-10-03 21:39:16 +0000120{
121 return new (globalData) JSNumberCell(globalData, NaN);
122}
123
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000124} // namespace JSC