blob: 090a345a54a4f187afd34b94fa71cbbf8a16cf34 [file] [log] [blame]
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +00001/*
2 * Copyright (C) 1999-2000,2003 Harri Porten (porten@kde.org)
ggaren@apple.com19fe5092011-05-18 02:39:00 +00003 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +00004 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
19 *
20 */
21
22#include "config.h"
23#include "NumberConstructor.h"
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000024
eric@webkit.org66cf5782010-04-21 14:09:49 +000025#include "Lookup.h"
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000026#include "NumberObject.h"
27#include "NumberPrototype.h"
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000028#include "Operations.h"
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000029
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000030namespace JSC {
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000031
barraclough@apple.com38d3c752012-05-12 00:39:43 +000032static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName);
33static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName);
34static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName);
35static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName);
36static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName);
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000037
38} // namespace JSC
39
40#include "NumberConstructor.lut.h"
41
42namespace JSC {
43
mhahnenberg@apple.comc58d54d2011-12-16 19:06:44 +000044ASSERT_HAS_TRIVIAL_DESTRUCTOR(NumberConstructor);
45
mhahnenberg@apple.com3e084662011-09-26 07:05:28 +000046const ClassInfo NumberConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::numberConstructorTable, CREATE_METHOD_TABLE(NumberConstructor) };
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000047
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000048/* Source for NumberConstructor.lut.h
ggaren@apple.com19fe5092011-05-18 02:39:00 +000049@begin numberConstructorTable
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000050 NaN numberConstructorNaNValue DontEnum|DontDelete|ReadOnly
51 NEGATIVE_INFINITY numberConstructorNegInfinity DontEnum|DontDelete|ReadOnly
52 POSITIVE_INFINITY numberConstructorPosInfinity DontEnum|DontDelete|ReadOnly
53 MAX_VALUE numberConstructorMaxValue DontEnum|DontDelete|ReadOnly
54 MIN_VALUE numberConstructorMinValue DontEnum|DontDelete|ReadOnly
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000055@end
56*/
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000057
mhahnenberg@apple.com5e109722011-09-14 18:55:25 +000058NumberConstructor::NumberConstructor(JSGlobalObject* globalObject, Structure* structure)
mhahnenberg@apple.com7317a7f2011-09-09 21:43:14 +000059 : InternalFunction(globalObject, structure)
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000060{
mhahnenberg@apple.com7317a7f2011-09-09 21:43:14 +000061}
62
63void NumberConstructor::finishCreation(ExecState* exec, NumberPrototype* numberPrototype)
64{
barraclough@apple.com38d3c752012-05-12 00:39:43 +000065 Base::finishCreation(exec->globalData(), numberPrototype->s_info.className);
barraclough@apple.com737a1582011-02-21 19:31:42 +000066 ASSERT(inherits(&s_info));
67
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000068 // Number.Prototype
oliver@apple.com168e5062011-01-31 20:07:21 +000069 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, numberPrototype, DontEnum | DontDelete | ReadOnly);
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000070
71 // no. of arguments for constructor
oliver@apple.com168e5062011-01-31 20:07:21 +000072 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000073}
74
barraclough@apple.com38d3c752012-05-12 00:39:43 +000075bool NumberConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
mhahnenberg@apple.com19869642011-10-08 21:31:32 +000076{
mhahnenberg@apple.com135f0512011-11-11 20:40:10 +000077 return getStaticValueSlot<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), propertyName, slot);
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000078}
79
barraclough@apple.com38d3c752012-05-12 00:39:43 +000080bool NumberConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
oliver@apple.com4b4f7852009-08-26 16:52:15 +000081{
mhahnenberg@apple.com135f0512011-11-11 20:40:10 +000082 return getStaticValueDescriptor<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(object), propertyName, descriptor);
oliver@apple.com4b4f7852009-08-26 16:52:15 +000083}
84
barraclough@apple.com38d3c752012-05-12 00:39:43 +000085void NumberConstructor::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
barraclough@apple.com38bb9972011-12-27 04:56:37 +000086{
87 lookupPut<NumberConstructor, InternalFunction>(exec, propertyName, value, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), slot);
88}
89
barraclough@apple.com38d3c752012-05-12 00:39:43 +000090static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName)
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +000091{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +000092 return jsNaN();
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000093}
94
barraclough@apple.com38d3c752012-05-12 00:39:43 +000095static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName)
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000096{
barraclough@apple.comf1fa5792011-06-11 02:03:00 +000097 return jsNumber(-std::numeric_limits<double>::infinity());
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +000098}
99
barraclough@apple.com38d3c752012-05-12 00:39:43 +0000100static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName)
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +0000101{
barraclough@apple.comf1fa5792011-06-11 02:03:00 +0000102 return jsNumber(std::numeric_limits<double>::infinity());
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +0000103}
104
barraclough@apple.com38d3c752012-05-12 00:39:43 +0000105static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName)
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +0000106{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000107 return jsNumber(1.7976931348623157E+308);
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +0000108}
109
barraclough@apple.com38d3c752012-05-12 00:39:43 +0000110static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName)
weinig@apple.comcaf5e3b2008-09-27 02:36:15 +0000111{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000112 return jsNumber(5E-324);
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000113}
114
115// ECMA 15.7.1
barraclough@apple.com11d351a2010-06-04 21:38:38 +0000116static EncodedJSValue JSC_HOST_CALL constructWithNumberConstructor(ExecState* exec)
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000117{
oliver@apple.comfcacd3c2011-07-18 17:47:13 +0000118 NumberObject* object = NumberObject::create(exec->globalData(), asInternalFunction(exec->callee())->globalObject()->numberObjectStructure());
barraclough@apple.com11d351a2010-06-04 21:38:38 +0000119 double n = exec->argumentCount() ? exec->argument(0).toNumber(exec) : 0;
oliver@apple.com168e5062011-01-31 20:07:21 +0000120 object->setInternalValue(exec->globalData(), jsNumber(n));
barraclough@apple.com11d351a2010-06-04 21:38:38 +0000121 return JSValue::encode(object);
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000122}
123
mhahnenberg@apple.com79c8e6e2011-10-08 23:26:41 +0000124ConstructType NumberConstructor::getConstructData(JSCell*, ConstructData& constructData)
125{
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000126 constructData.native.function = constructWithNumberConstructor;
ggaren@apple.com9a0472b2008-07-28 20:04:48 +0000127 return ConstructTypeHost;
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000128}
129
130// ECMA 15.7.2
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000131static EncodedJSValue JSC_HOST_CALL callNumberConstructor(ExecState* exec)
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000132{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000133 return JSValue::encode(jsNumber(!exec->argumentCount() ? 0 : exec->argument(0).toNumber(exec)));
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000134}
135
mhahnenberg@apple.com2413eb82011-09-27 22:46:51 +0000136CallType NumberConstructor::getCallData(JSCell*, CallData& callData)
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000137{
138 callData.native.function = callNumberConstructor;
ggaren@apple.com1914ee92008-07-18 22:18:21 +0000139 return CallTypeHost;
weinig@apple.com2ce2a7f2008-06-28 21:22:01 +0000140}
141
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000142} // namespace JSC