DatePrototype.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/String.h>
  28. #include <LibCore/DateTime.h>
  29. #include <LibJS/Runtime/Date.h>
  30. #include <LibJS/Runtime/DatePrototype.h>
  31. #include <LibJS/Runtime/Error.h>
  32. #include <LibJS/Runtime/GlobalObject.h>
  33. #include <LibJS/Runtime/Value.h>
  34. namespace JS {
  35. static Date* typed_this(VM& vm, GlobalObject& global_object)
  36. {
  37. auto* this_object = vm.this_value(global_object).to_object(global_object);
  38. if (!this_object)
  39. return nullptr;
  40. if (!this_object->is_date()) {
  41. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Date");
  42. return nullptr;
  43. }
  44. return static_cast<Date*>(this_object);
  45. }
  46. DatePrototype::DatePrototype(GlobalObject& global_object)
  47. : Object(*global_object.object_prototype())
  48. {
  49. }
  50. void DatePrototype::initialize(GlobalObject& global_object)
  51. {
  52. Object::initialize(global_object);
  53. u8 attr = Attribute::Writable | Attribute::Configurable;
  54. define_native_function("getDate", get_date, 0, attr);
  55. define_native_function("getDay", get_day, 0, attr);
  56. define_native_function("getFullYear", get_full_year, 0, attr);
  57. define_native_function("getHours", get_hours, 0, attr);
  58. define_native_function("getMilliseconds", get_milliseconds, 0, attr);
  59. define_native_function("getMinutes", get_minutes, 0, attr);
  60. define_native_function("getMonth", get_month, 0, attr);
  61. define_native_function("getSeconds", get_seconds, 0, attr);
  62. define_native_function("getTime", get_time, 0, attr);
  63. define_native_function("getUTCDate", get_utc_date, 0, attr);
  64. define_native_function("getUTCDay", get_utc_day, 0, attr);
  65. define_native_function("getUTCFullYear", get_utc_full_year, 0, attr);
  66. define_native_function("getUTCHours", get_utc_hours, 0, attr);
  67. define_native_function("getUTCMilliseconds", get_utc_milliseconds, 0, attr);
  68. define_native_function("getUTCMinutes", get_utc_minutes, 0, attr);
  69. define_native_function("getUTCMonth", get_utc_month, 0, attr);
  70. define_native_function("getUTCSeconds", get_utc_seconds, 0, attr);
  71. define_native_function("toDateString", to_date_string, 0, attr);
  72. define_native_function("toISOString", to_iso_string, 0, attr);
  73. define_native_function("toLocaleDateString", to_locale_date_string, 0, attr);
  74. define_native_function("toLocaleString", to_locale_string, 0, attr);
  75. define_native_function("toLocaleTimeString", to_locale_time_string, 0, attr);
  76. define_native_function("toTimeString", to_time_string, 0, attr);
  77. define_native_function("toString", to_string, 0, attr);
  78. // Aliases.
  79. define_native_function("valueOf", get_time, 0, attr);
  80. // toJSON() isn't quite an alias for toISOString():
  81. // - it returns null instead of throwing RangeError
  82. // - its .length is 1, not 0
  83. // - it can be transferred to other prototypes
  84. }
  85. DatePrototype::~DatePrototype()
  86. {
  87. }
  88. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date)
  89. {
  90. auto* this_object = typed_this(vm, global_object);
  91. if (!this_object)
  92. return {};
  93. return Value(static_cast<double>(this_object->date()));
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day)
  96. {
  97. auto* this_object = typed_this(vm, global_object);
  98. if (!this_object)
  99. return {};
  100. return Value(static_cast<double>(this_object->day()));
  101. }
  102. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year)
  103. {
  104. auto* this_object = typed_this(vm, global_object);
  105. if (!this_object)
  106. return {};
  107. return Value(static_cast<double>(this_object->full_year()));
  108. }
  109. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
  110. {
  111. auto* this_object = typed_this(vm, global_object);
  112. if (!this_object)
  113. return {};
  114. return Value(static_cast<double>(this_object->hours()));
  115. }
  116. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds)
  117. {
  118. auto* this_object = typed_this(vm, global_object);
  119. if (!this_object)
  120. return {};
  121. return Value(static_cast<double>(this_object->milliseconds()));
  122. }
  123. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes)
  124. {
  125. auto* this_object = typed_this(vm, global_object);
  126. if (!this_object)
  127. return {};
  128. return Value(static_cast<double>(this_object->minutes()));
  129. }
  130. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month)
  131. {
  132. auto* this_object = typed_this(vm, global_object);
  133. if (!this_object)
  134. return {};
  135. return Value(static_cast<double>(this_object->month()));
  136. }
  137. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds)
  138. {
  139. auto* this_object = typed_this(vm, global_object);
  140. if (!this_object)
  141. return {};
  142. return Value(static_cast<double>(this_object->seconds()));
  143. }
  144. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)
  145. {
  146. auto* this_object = typed_this(vm, global_object);
  147. if (!this_object)
  148. return {};
  149. return Value(this_object->time());
  150. }
  151. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_date)
  152. {
  153. auto* this_object = typed_this(vm, global_object);
  154. if (!this_object)
  155. return {};
  156. return Value(static_cast<double>(this_object->utc_date()));
  157. }
  158. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_day)
  159. {
  160. auto* this_object = typed_this(vm, global_object);
  161. if (!this_object)
  162. return {};
  163. return Value(static_cast<double>(this_object->utc_day()));
  164. }
  165. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_full_year)
  166. {
  167. auto* this_object = typed_this(vm, global_object);
  168. if (!this_object)
  169. return {};
  170. return Value(static_cast<double>(this_object->utc_full_year()));
  171. }
  172. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_hours)
  173. {
  174. auto* this_object = typed_this(vm, global_object);
  175. if (!this_object)
  176. return {};
  177. return Value(static_cast<double>(this_object->utc_hours()));
  178. }
  179. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_milliseconds)
  180. {
  181. auto* this_object = typed_this(vm, global_object);
  182. if (!this_object)
  183. return {};
  184. return Value(static_cast<double>(this_object->utc_milliseconds()));
  185. }
  186. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_month)
  187. {
  188. auto* this_object = typed_this(vm, global_object);
  189. if (!this_object)
  190. return {};
  191. return Value(static_cast<double>(this_object->utc_month()));
  192. }
  193. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_minutes)
  194. {
  195. auto* this_object = typed_this(vm, global_object);
  196. if (!this_object)
  197. return {};
  198. return Value(static_cast<double>(this_object->utc_minutes()));
  199. }
  200. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_seconds)
  201. {
  202. auto* this_object = typed_this(vm, global_object);
  203. if (!this_object)
  204. return {};
  205. return Value(static_cast<double>(this_object->utc_seconds()));
  206. }
  207. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_date_string)
  208. {
  209. auto* this_object = typed_this(vm, global_object);
  210. if (!this_object)
  211. return {};
  212. auto string = this_object->date_string();
  213. return js_string(vm, move(string));
  214. }
  215. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_iso_string)
  216. {
  217. auto* this_object = typed_this(vm, global_object);
  218. if (!this_object)
  219. return {};
  220. auto string = this_object->iso_date_string();
  221. return js_string(vm, move(string));
  222. }
  223. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
  224. {
  225. auto* this_object = typed_this(vm, global_object);
  226. if (!this_object)
  227. return {};
  228. // FIXME: Optional locales, options params.
  229. auto string = this_object->locale_date_string();
  230. return js_string(vm, move(string));
  231. }
  232. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
  233. {
  234. auto* this_object = typed_this(vm, global_object);
  235. if (!this_object)
  236. return {};
  237. // FIXME: Optional locales, options params.
  238. auto string = this_object->locale_string();
  239. return js_string(vm, move(string));
  240. }
  241. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
  242. {
  243. auto* this_object = typed_this(vm, global_object);
  244. if (!this_object)
  245. return {};
  246. // FIXME: Optional locales, options params.
  247. auto string = this_object->locale_time_string();
  248. return js_string(vm, move(string));
  249. }
  250. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_time_string)
  251. {
  252. auto* this_object = typed_this(vm, global_object);
  253. if (!this_object)
  254. return {};
  255. auto string = this_object->time_string();
  256. return js_string(vm, move(string));
  257. }
  258. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string)
  259. {
  260. auto* this_object = typed_this(vm, global_object);
  261. if (!this_object)
  262. return {};
  263. auto string = this_object->string();
  264. return js_string(vm, move(string));
  265. }
  266. }