NumberConstructor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 1999-2000,2003 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
  4. *
  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. #include "config.h"
  22. #include "NumberConstructor.h"
  23. #include "Lookup.h"
  24. #include "NumberObject.h"
  25. #include "NumberPrototype.h"
  26. #include "Operations.h"
  27. namespace JSC {
  28. static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName);
  29. static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName);
  30. static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName);
  31. static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName);
  32. static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName);
  33. } // namespace JSC
  34. #include "NumberConstructor.lut.h"
  35. namespace JSC {
  36. ASSERT_HAS_TRIVIAL_DESTRUCTOR(NumberConstructor);
  37. const ClassInfo NumberConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::numberConstructorTable, CREATE_METHOD_TABLE(NumberConstructor) };
  38. /* Source for NumberConstructor.lut.h
  39. @begin numberConstructorTable
  40. NaN numberConstructorNaNValue DontEnum|DontDelete|ReadOnly
  41. NEGATIVE_INFINITY numberConstructorNegInfinity DontEnum|DontDelete|ReadOnly
  42. POSITIVE_INFINITY numberConstructorPosInfinity DontEnum|DontDelete|ReadOnly
  43. MAX_VALUE numberConstructorMaxValue DontEnum|DontDelete|ReadOnly
  44. MIN_VALUE numberConstructorMinValue DontEnum|DontDelete|ReadOnly
  45. @end
  46. */
  47. NumberConstructor::NumberConstructor(JSGlobalObject* globalObject, Structure* structure)
  48. : InternalFunction(globalObject, structure)
  49. {
  50. }
  51. void NumberConstructor::finishCreation(ExecState* exec, NumberPrototype* numberPrototype)
  52. {
  53. Base::finishCreation(exec->vm(), numberPrototype->s_info.className);
  54. ASSERT(inherits(&s_info));
  55. // Number.Prototype
  56. putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, numberPrototype, DontEnum | DontDelete | ReadOnly);
  57. // no. of arguments for constructor
  58. putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
  59. }
  60. bool NumberConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  61. {
  62. return getStaticValueSlot<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), propertyName, slot);
  63. }
  64. bool NumberConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  65. {
  66. return getStaticValueDescriptor<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(object), propertyName, descriptor);
  67. }
  68. void NumberConstructor::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  69. {
  70. lookupPut<NumberConstructor, InternalFunction>(exec, propertyName, value, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), slot);
  71. }
  72. static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName)
  73. {
  74. return jsNaN();
  75. }
  76. static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName)
  77. {
  78. return jsNumber(-std::numeric_limits<double>::infinity());
  79. }
  80. static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName)
  81. {
  82. return jsNumber(std::numeric_limits<double>::infinity());
  83. }
  84. static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName)
  85. {
  86. return jsNumber(1.7976931348623157E+308);
  87. }
  88. static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName)
  89. {
  90. return jsNumber(5E-324);
  91. }
  92. // ECMA 15.7.1
  93. static EncodedJSValue JSC_HOST_CALL constructWithNumberConstructor(ExecState* exec)
  94. {
  95. NumberObject* object = NumberObject::create(exec->vm(), asInternalFunction(exec->callee())->globalObject()->numberObjectStructure());
  96. double n = exec->argumentCount() ? exec->argument(0).toNumber(exec) : 0;
  97. object->setInternalValue(exec->vm(), jsNumber(n));
  98. return JSValue::encode(object);
  99. }
  100. ConstructType NumberConstructor::getConstructData(JSCell*, ConstructData& constructData)
  101. {
  102. constructData.native.function = constructWithNumberConstructor;
  103. return ConstructTypeHost;
  104. }
  105. // ECMA 15.7.2
  106. static EncodedJSValue JSC_HOST_CALL callNumberConstructor(ExecState* exec)
  107. {
  108. return JSValue::encode(jsNumber(!exec->argumentCount() ? 0 : exec->argument(0).toNumber(exec)));
  109. }
  110. CallType NumberConstructor::getCallData(JSCell*, CallData& callData)
  111. {
  112. callData.native.function = callNumberConstructor;
  113. return CallTypeHost;
  114. }
  115. } // namespace JSC