DateConstructor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2004, 2005, 2006, 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 "DateConstructor.h"
  23. #include "DateConversion.h"
  24. #include "DateInstance.h"
  25. #include "DatePrototype.h"
  26. #include "JSDateMath.h"
  27. #include "JSFunction.h"
  28. #include "JSGlobalObject.h"
  29. #include "JSString.h"
  30. #include "JSStringBuilder.h"
  31. #include "ObjectPrototype.h"
  32. #include "Operations.h"
  33. #include <math.h>
  34. #include <time.h>
  35. #include <wtf/MathExtras.h>
  36. #if OS(WINCE) && !PLATFORM(QT)
  37. extern "C" time_t time(time_t* timer); // Provided by libce.
  38. #endif
  39. #if HAVE(SYS_TIME_H)
  40. #include <sys/time.h>
  41. #endif
  42. #if HAVE(SYS_TIMEB_H)
  43. #include <sys/timeb.h>
  44. #endif
  45. using namespace WTF;
  46. namespace JSC {
  47. static EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
  48. static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
  49. static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
  50. }
  51. #include "DateConstructor.lut.h"
  52. namespace JSC {
  53. const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::dateConstructorTable, CREATE_METHOD_TABLE(DateConstructor) };
  54. /* Source for DateConstructor.lut.h
  55. @begin dateConstructorTable
  56. parse dateParse DontEnum|Function 1
  57. UTC dateUTC DontEnum|Function 7
  58. now dateNow DontEnum|Function 0
  59. @end
  60. */
  61. ASSERT_HAS_TRIVIAL_DESTRUCTOR(DateConstructor);
  62. DateConstructor::DateConstructor(JSGlobalObject* globalObject, Structure* structure)
  63. : InternalFunction(globalObject, structure)
  64. {
  65. }
  66. void DateConstructor::finishCreation(ExecState* exec, DatePrototype* datePrototype)
  67. {
  68. Base::finishCreation(exec->vm(), datePrototype->classInfo()->className);
  69. putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
  70. putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
  71. }
  72. bool DateConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
  73. {
  74. return getStaticFunctionSlot<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(cell), propertyName, slot);
  75. }
  76. bool DateConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  77. {
  78. return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(object), propertyName, descriptor);
  79. }
  80. // ECMA 15.9.3
  81. JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
  82. {
  83. int numArgs = args.size();
  84. double value;
  85. if (numArgs == 0) // new Date() ECMA 15.9.3.3
  86. value = jsCurrentTime();
  87. else if (numArgs == 1) {
  88. if (args.at(0).inherits(&DateInstance::s_info))
  89. value = asDateInstance(args.at(0))->internalNumber();
  90. else {
  91. JSValue primitive = args.at(0).toPrimitive(exec);
  92. if (primitive.isString())
  93. value = parseDate(exec, primitive.getString(exec));
  94. else
  95. value = primitive.toNumber(exec);
  96. }
  97. } else {
  98. double doubleArguments[7] = {
  99. args.at(0).toNumber(exec),
  100. args.at(1).toNumber(exec),
  101. args.at(2).toNumber(exec),
  102. args.at(3).toNumber(exec),
  103. args.at(4).toNumber(exec),
  104. args.at(5).toNumber(exec),
  105. args.at(6).toNumber(exec)
  106. };
  107. if (!std::isfinite(doubleArguments[0])
  108. || !std::isfinite(doubleArguments[1])
  109. || (numArgs >= 3 && !std::isfinite(doubleArguments[2]))
  110. || (numArgs >= 4 && !std::isfinite(doubleArguments[3]))
  111. || (numArgs >= 5 && !std::isfinite(doubleArguments[4]))
  112. || (numArgs >= 6 && !std::isfinite(doubleArguments[5]))
  113. || (numArgs >= 7 && !std::isfinite(doubleArguments[6])))
  114. value = QNaN;
  115. else {
  116. GregorianDateTime t;
  117. int year = JSC::toInt32(doubleArguments[0]);
  118. t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
  119. t.setMonth(JSC::toInt32(doubleArguments[1]));
  120. t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
  121. t.setHour(JSC::toInt32(doubleArguments[3]));
  122. t.setMinute(JSC::toInt32(doubleArguments[4]));
  123. t.setSecond(JSC::toInt32(doubleArguments[5]));
  124. t.setIsDST(-1);
  125. double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
  126. value = gregorianDateTimeToMS(exec, t, ms, false);
  127. }
  128. }
  129. return DateInstance::create(exec, globalObject->dateStructure(), value);
  130. }
  131. static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
  132. {
  133. ArgList args(exec);
  134. return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
  135. }
  136. ConstructType DateConstructor::getConstructData(JSCell*, ConstructData& constructData)
  137. {
  138. constructData.native.function = constructWithDateConstructor;
  139. return ConstructTypeHost;
  140. }
  141. // ECMA 15.9.2
  142. static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
  143. {
  144. GregorianDateTime ts;
  145. msToGregorianDateTime(exec, currentTimeMS(), false, ts);
  146. return JSValue::encode(jsNontrivialString(exec, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
  147. }
  148. CallType DateConstructor::getCallData(JSCell*, CallData& callData)
  149. {
  150. callData.native.function = callDate;
  151. return CallTypeHost;
  152. }
  153. static EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
  154. {
  155. return JSValue::encode(jsNumber(parseDate(exec, exec->argument(0).toString(exec)->value(exec))));
  156. }
  157. static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
  158. {
  159. return JSValue::encode(jsNumber(jsCurrentTime()));
  160. }
  161. static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
  162. {
  163. double doubleArguments[7] = {
  164. exec->argument(0).toNumber(exec),
  165. exec->argument(1).toNumber(exec),
  166. exec->argument(2).toNumber(exec),
  167. exec->argument(3).toNumber(exec),
  168. exec->argument(4).toNumber(exec),
  169. exec->argument(5).toNumber(exec),
  170. exec->argument(6).toNumber(exec)
  171. };
  172. int n = exec->argumentCount();
  173. if (std::isnan(doubleArguments[0])
  174. || std::isnan(doubleArguments[1])
  175. || (n >= 3 && std::isnan(doubleArguments[2]))
  176. || (n >= 4 && std::isnan(doubleArguments[3]))
  177. || (n >= 5 && std::isnan(doubleArguments[4]))
  178. || (n >= 6 && std::isnan(doubleArguments[5]))
  179. || (n >= 7 && std::isnan(doubleArguments[6])))
  180. return JSValue::encode(jsNaN());
  181. GregorianDateTime t;
  182. int year = JSC::toInt32(doubleArguments[0]);
  183. t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
  184. t.setMonth(JSC::toInt32(doubleArguments[1]));
  185. t.setMonthDay((n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
  186. t.setHour(JSC::toInt32(doubleArguments[3]));
  187. t.setMinute(JSC::toInt32(doubleArguments[4]));
  188. t.setSecond(JSC::toInt32(doubleArguments[5]));
  189. double ms = (n >= 7) ? doubleArguments[6] : 0;
  190. return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec, t, ms, true))));
  191. }
  192. } // namespace JSC