DatePrototype.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/Interpreter.h>
  30. #include <LibJS/Runtime/Date.h>
  31. #include <LibJS/Runtime/DatePrototype.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/GlobalObject.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. static Date* typed_this(Interpreter& interpreter, GlobalObject& global_object)
  37. {
  38. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  39. if (!this_object)
  40. return nullptr;
  41. if (!this_object->is_date()) {
  42. interpreter.throw_exception<TypeError>(ErrorType::NotA, "Date");
  43. return nullptr;
  44. }
  45. return static_cast<Date*>(this_object);
  46. }
  47. DatePrototype::DatePrototype(GlobalObject& global_object)
  48. : Object(*global_object.object_prototype())
  49. {
  50. }
  51. void DatePrototype::initialize(GlobalObject& global_object)
  52. {
  53. Object::initialize(global_object);
  54. u8 attr = Attribute::Writable | Attribute::Configurable;
  55. define_native_function("getDate", get_date, 0, attr);
  56. define_native_function("getDay", get_day, 0, attr);
  57. define_native_function("getFullYear", get_full_year, 0, attr);
  58. define_native_function("getHours", get_hours, 0, attr);
  59. define_native_function("getMilliseconds", get_milliseconds, 0, attr);
  60. define_native_function("getMinutes", get_minutes, 0, attr);
  61. define_native_function("getMonth", get_month, 0, attr);
  62. define_native_function("getSeconds", get_seconds, 0, attr);
  63. define_native_function("getTime", get_time, 0, attr);
  64. define_native_function("getUTCDate", get_utc_date, 0, attr);
  65. define_native_function("getUTCDay", get_utc_day, 0, attr);
  66. define_native_function("getUTCFullYear", get_utc_full_year, 0, attr);
  67. define_native_function("getUTCHours", get_utc_hours, 0, attr);
  68. define_native_function("getUTCMilliseconds", get_utc_milliseconds, 0, attr);
  69. define_native_function("getUTCMinutes", get_utc_minutes, 0, attr);
  70. define_native_function("getUTCMonth", get_utc_month, 0, attr);
  71. define_native_function("getUTCSeconds", get_utc_seconds, 0, attr);
  72. define_native_function("toDateString", to_date_string, 0, attr);
  73. define_native_function("toISOString", to_iso_string, 0, attr);
  74. define_native_function("toLocaleDateString", to_locale_date_string, 0, attr);
  75. define_native_function("toLocaleString", to_locale_string, 0, attr);
  76. define_native_function("toLocaleTimeString", to_locale_time_string, 0, attr);
  77. define_native_function("toTimeString", to_time_string, 0, attr);
  78. define_native_function("toString", to_string, 0, attr);
  79. // Aliases.
  80. define_native_function("valueOf", get_time, 0, attr);
  81. // toJSON() isn't quite an alias for toISOString():
  82. // - it returns null instead of throwing RangeError
  83. // - its .length is 1, not 0
  84. // - it can be transferred to other prototypes
  85. }
  86. DatePrototype::~DatePrototype()
  87. {
  88. }
  89. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date)
  90. {
  91. auto* this_object = typed_this(interpreter, global_object);
  92. if (!this_object)
  93. return {};
  94. return Value(static_cast<double>(this_object->date()));
  95. }
  96. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day)
  97. {
  98. auto* this_object = typed_this(interpreter, global_object);
  99. if (!this_object)
  100. return {};
  101. return Value(static_cast<double>(this_object->day()));
  102. }
  103. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year)
  104. {
  105. auto* this_object = typed_this(interpreter, global_object);
  106. if (!this_object)
  107. return {};
  108. return Value(static_cast<double>(this_object->full_year()));
  109. }
  110. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
  111. {
  112. auto* this_object = typed_this(interpreter, global_object);
  113. if (!this_object)
  114. return {};
  115. return Value(static_cast<double>(this_object->hours()));
  116. }
  117. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_milliseconds)
  118. {
  119. auto* this_object = typed_this(interpreter, global_object);
  120. if (!this_object)
  121. return {};
  122. return Value(static_cast<double>(this_object->milliseconds()));
  123. }
  124. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_minutes)
  125. {
  126. auto* this_object = typed_this(interpreter, global_object);
  127. if (!this_object)
  128. return {};
  129. return Value(static_cast<double>(this_object->minutes()));
  130. }
  131. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month)
  132. {
  133. auto* this_object = typed_this(interpreter, global_object);
  134. if (!this_object)
  135. return {};
  136. return Value(static_cast<double>(this_object->month()));
  137. }
  138. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds)
  139. {
  140. auto* this_object = typed_this(interpreter, global_object);
  141. if (!this_object)
  142. return {};
  143. return Value(static_cast<double>(this_object->seconds()));
  144. }
  145. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_time)
  146. {
  147. auto* this_object = typed_this(interpreter, global_object);
  148. if (!this_object)
  149. return {};
  150. return Value(this_object->time());
  151. }
  152. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_date)
  153. {
  154. auto* this_object = typed_this(interpreter, global_object);
  155. if (!this_object)
  156. return {};
  157. return Value(static_cast<double>(this_object->utc_date()));
  158. }
  159. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_day)
  160. {
  161. auto* this_object = typed_this(interpreter, global_object);
  162. if (!this_object)
  163. return {};
  164. return Value(static_cast<double>(this_object->utc_day()));
  165. }
  166. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_full_year)
  167. {
  168. auto* this_object = typed_this(interpreter, global_object);
  169. if (!this_object)
  170. return {};
  171. return Value(static_cast<double>(this_object->utc_full_year()));
  172. }
  173. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_hours)
  174. {
  175. auto* this_object = typed_this(interpreter, global_object);
  176. if (!this_object)
  177. return {};
  178. return Value(static_cast<double>(this_object->utc_hours()));
  179. }
  180. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_milliseconds)
  181. {
  182. auto* this_object = typed_this(interpreter, global_object);
  183. if (!this_object)
  184. return {};
  185. return Value(static_cast<double>(this_object->utc_milliseconds()));
  186. }
  187. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_month)
  188. {
  189. auto* this_object = typed_this(interpreter, global_object);
  190. if (!this_object)
  191. return {};
  192. return Value(static_cast<double>(this_object->utc_month()));
  193. }
  194. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_minutes)
  195. {
  196. auto* this_object = typed_this(interpreter, global_object);
  197. if (!this_object)
  198. return {};
  199. return Value(static_cast<double>(this_object->utc_minutes()));
  200. }
  201. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_utc_seconds)
  202. {
  203. auto* this_object = typed_this(interpreter, global_object);
  204. if (!this_object)
  205. return {};
  206. return Value(static_cast<double>(this_object->utc_seconds()));
  207. }
  208. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_date_string)
  209. {
  210. auto* this_object = typed_this(interpreter, global_object);
  211. if (!this_object)
  212. return {};
  213. auto string = this_object->date_string();
  214. return js_string(interpreter, move(string));
  215. }
  216. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_iso_string)
  217. {
  218. auto* this_object = typed_this(interpreter, global_object);
  219. if (!this_object)
  220. return {};
  221. auto string = this_object->iso_date_string();
  222. return js_string(interpreter, move(string));
  223. }
  224. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_date_string)
  225. {
  226. auto* this_object = typed_this(interpreter, global_object);
  227. if (!this_object)
  228. return {};
  229. // FIXME: Optional locales, options params.
  230. auto string = this_object->locale_date_string();
  231. return js_string(interpreter, move(string));
  232. }
  233. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_string)
  234. {
  235. auto* this_object = typed_this(interpreter, global_object);
  236. if (!this_object)
  237. return {};
  238. // FIXME: Optional locales, options params.
  239. auto string = this_object->locale_string();
  240. return js_string(interpreter, move(string));
  241. }
  242. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_locale_time_string)
  243. {
  244. auto* this_object = typed_this(interpreter, global_object);
  245. if (!this_object)
  246. return {};
  247. // FIXME: Optional locales, options params.
  248. auto string = this_object->locale_time_string();
  249. return js_string(interpreter, move(string));
  250. }
  251. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_time_string)
  252. {
  253. auto* this_object = typed_this(interpreter, global_object);
  254. if (!this_object)
  255. return {};
  256. auto string = this_object->time_string();
  257. return js_string(interpreter, move(string));
  258. }
  259. JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string)
  260. {
  261. auto* this_object = typed_this(interpreter, global_object);
  262. if (!this_object)
  263. return {};
  264. auto string = this_object->string();
  265. return js_string(interpreter, move(string));
  266. }
  267. }