|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #include "ecma-alloc.h"
|
| 18 | +#include "ecma-builtin-helpers.h" |
| 19 | +#include "ecma-conversion.h" |
18 | 20 | #include "ecma-globals.h"
|
19 | 21 | #include "ecma-helpers.h"
|
| 22 | +#include "ecma-try-catch-macro.h" |
20 | 23 |
|
21 | 24 | #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
|
22 | 25 |
|
|
37 | 40 | * @{
|
38 | 41 | */
|
39 | 42 |
|
| 43 | +/** |
| 44 | + * Helper function to try to parse a part of a date string |
| 45 | + * |
| 46 | + * @return NaN if cannot read from string, ToNumber() otherwise |
| 47 | + */ |
| 48 | +static ecma_number_t |
| 49 | +ecma_date_parse_date_chars (lit_utf8_iterator_t *iter, /**< iterator of the utf8 string */ |
| 50 | + uint32_t num_of_chars) /**< number of characters to read and convert */ |
| 51 | +{ |
| 52 | + JERRY_ASSERT (num_of_chars > 0); |
| 53 | + |
| 54 | + lit_utf8_size_t copy_size = 0; |
| 55 | + const lit_utf8_byte_t *str_start_p = iter->buf_p + iter->buf_pos.offset; |
| 56 | + |
| 57 | + while (num_of_chars--) |
| 58 | + { |
| 59 | + if (lit_utf8_iterator_is_eos (iter) |
| 60 | + || !lit_char_is_unicode_digit (lit_utf8_iterator_peek_next (iter))) |
| 61 | + { |
| 62 | + return ecma_number_make_nan (); |
| 63 | + } |
| 64 | + |
| 65 | + copy_size += lit_get_unicode_char_size_by_utf8_first_byte (*(iter->buf_p + iter->buf_pos.offset)); |
| 66 | + lit_utf8_iterator_incr (iter); |
| 67 | + } |
| 68 | + |
| 69 | + return ecma_utf8_string_to_number (str_start_p, copy_size); |
| 70 | +} /* ecma_date_parse_date_chars */ |
| 71 | + |
40 | 72 | /**
|
41 | 73 | * The Date object's 'parse' routine
|
42 | 74 | *
|
43 | 75 | * See also:
|
44 | 76 | * ECMA-262 v5, 15.9.4.2
|
| 77 | + * ECMA-262 v5, 15.9.1.15 |
45 | 78 | *
|
46 | 79 | * @return completion value
|
47 | 80 | * Returned value must be freed with ecma_free_completion_value.
|
48 | 81 | */
|
49 | 82 | static ecma_completion_value_t
|
50 |
| -ecma_builtin_date_parse (ecma_value_t this_arg, /**< this argument */ |
| 83 | +ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argument */ |
51 | 84 | ecma_value_t arg) /**< string */
|
52 | 85 | {
|
53 |
| - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg); |
| 86 | + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); |
| 87 | + ecma_number_t *date_num_p = ecma_alloc_number (); |
| 88 | + *date_num_p = ecma_number_make_nan (); |
| 89 | + |
| 90 | + /* Date Time String fromat (ECMA-262 v5, 15.9.1.15) */ |
| 91 | + ECMA_TRY_CATCH (date_str_value, |
| 92 | + ecma_op_to_string (arg), |
| 93 | + ret_value); |
| 94 | + |
| 95 | + ecma_string_t *date_str_p = ecma_get_string_from_value (date_str_value); |
| 96 | + |
| 97 | + lit_utf8_size_t date_str_size = ecma_string_get_size (date_str_p); |
| 98 | + MEM_DEFINE_LOCAL_ARRAY (date_start_p, date_str_size, lit_utf8_byte_t); |
| 99 | + |
| 100 | + ecma_string_to_utf8_string (date_str_p, date_start_p, (ssize_t) date_str_size); |
| 101 | + lit_utf8_iterator_t iter = lit_utf8_iterator_create (date_start_p, date_str_size); |
| 102 | + |
| 103 | + /* 1. read year */ |
| 104 | + ecma_number_t year = ecma_date_parse_date_chars (&iter, 4); |
| 105 | + |
| 106 | + if (!ecma_number_is_nan (year) |
| 107 | + && year >= 0) |
| 108 | + { |
| 109 | + ecma_number_t month = ECMA_NUMBER_ONE; |
| 110 | + ecma_number_t day = ECMA_NUMBER_ONE; |
| 111 | + ecma_number_t time = ECMA_NUMBER_ZERO; |
| 112 | + |
| 113 | + /* 2. read month if any */ |
| 114 | + if (!lit_utf8_iterator_is_eos (&iter) |
| 115 | + && lit_utf8_iterator_peek_next (&iter) == '-') |
| 116 | + { |
| 117 | + /* eat up '-' */ |
| 118 | + lit_utf8_iterator_incr (&iter); |
| 119 | + month = ecma_date_parse_date_chars (&iter, 2); |
| 120 | + |
| 121 | + if (month > 12 || month < 1) |
| 122 | + { |
| 123 | + month = ecma_number_make_nan (); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /* 3. read day if any */ |
| 128 | + if (!lit_utf8_iterator_is_eos (&iter) |
| 129 | + && lit_utf8_iterator_peek_next (&iter) == '-') |
| 130 | + { |
| 131 | + /* eat up '-' */ |
| 132 | + lit_utf8_iterator_incr (&iter); |
| 133 | + day = ecma_date_parse_date_chars (&iter, 2); |
| 134 | + |
| 135 | + if (day < 1 || day > 31) |
| 136 | + { |
| 137 | + day = ecma_number_make_nan (); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /* 4. read time if any */ |
| 142 | + if (!lit_utf8_iterator_is_eos (&iter) |
| 143 | + && lit_utf8_iterator_peek_next (&iter) == 'T') |
| 144 | + { |
| 145 | + ecma_number_t hours = ECMA_NUMBER_ZERO; |
| 146 | + ecma_number_t minutes = ECMA_NUMBER_ZERO; |
| 147 | + ecma_number_t seconds = ECMA_NUMBER_ZERO; |
| 148 | + ecma_number_t milliseconds = ECMA_NUMBER_ZERO; |
| 149 | + |
| 150 | + ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter); |
| 151 | + ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1; |
| 152 | + |
| 153 | + if ((date_str_len - num_of_visited_chars) >= 5) |
| 154 | + { |
| 155 | + /* eat up 'T' */ |
| 156 | + lit_utf8_iterator_incr (&iter); |
| 157 | + |
| 158 | + /* 4.1 read hours and minutes */ |
| 159 | + hours = ecma_date_parse_date_chars (&iter, 2); |
| 160 | + |
| 161 | + if (hours < 0 || hours > 24) |
| 162 | + { |
| 163 | + hours = ecma_number_make_nan (); |
| 164 | + } |
| 165 | + else if (hours == 24) |
| 166 | + { |
| 167 | + hours = ECMA_NUMBER_ZERO; |
| 168 | + } |
| 169 | + |
| 170 | + /* eat up ':' */ |
| 171 | + lit_utf8_iterator_incr (&iter); |
| 172 | + |
| 173 | + minutes = ecma_date_parse_date_chars (&iter, 2); |
| 174 | + |
| 175 | + if (minutes < 0 || minutes > 59) |
| 176 | + { |
| 177 | + minutes = ecma_number_make_nan (); |
| 178 | + } |
| 179 | + |
| 180 | + /* 4.2 read seconds if any */ |
| 181 | + if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == ':') |
| 182 | + { |
| 183 | + /* eat up ':' */ |
| 184 | + lit_utf8_iterator_incr (&iter); |
| 185 | + seconds = ecma_date_parse_date_chars (&iter, 2); |
| 186 | + |
| 187 | + if (seconds < 0 || seconds > 59) |
| 188 | + { |
| 189 | + seconds = ecma_number_make_nan (); |
| 190 | + } |
| 191 | + |
| 192 | + /* 4.3 read milliseconds if any */ |
| 193 | + if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == '.') |
| 194 | + { |
| 195 | + /* eat up '.' */ |
| 196 | + lit_utf8_iterator_incr (&iter); |
| 197 | + milliseconds = ecma_date_parse_date_chars (&iter, 3); |
| 198 | + |
| 199 | + if (milliseconds < 0) |
| 200 | + { |
| 201 | + milliseconds = ecma_number_make_nan (); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + time = ecma_date_make_time (hours, minutes, seconds, milliseconds); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + time = ecma_number_make_nan (); |
| 211 | + } |
| 212 | + |
| 213 | + /* 4.4 read timezone if any */ |
| 214 | + if (!lit_utf8_iterator_is_eos (&iter) |
| 215 | + && lit_utf8_iterator_peek_next (&iter) == 'Z' |
| 216 | + && !ecma_number_is_nan (time)) |
| 217 | + { |
| 218 | + lit_utf8_iterator_incr (&iter); |
| 219 | + time = ecma_date_utc (ecma_date_make_time (hours, |
| 220 | + minutes, |
| 221 | + seconds, |
| 222 | + milliseconds)); |
| 223 | + } |
| 224 | + else if (!lit_utf8_iterator_is_eos (&iter) |
| 225 | + && (lit_utf8_iterator_peek_next (&iter) == '+' |
| 226 | + || lit_utf8_iterator_peek_next (&iter) == '-')) |
| 227 | + { |
| 228 | + ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter); |
| 229 | + ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1; |
| 230 | + |
| 231 | + if ((date_str_len - num_of_visited_chars) == 5) |
| 232 | + { |
| 233 | + bool is_negative = false; |
| 234 | + |
| 235 | + if (lit_utf8_iterator_peek_next (&iter) == '-') |
| 236 | + { |
| 237 | + is_negative = true; |
| 238 | + } |
| 239 | + |
| 240 | + /* eat up '+/-' */ |
| 241 | + lit_utf8_iterator_incr (&iter); |
| 242 | + |
| 243 | + /* read hours and minutes */ |
| 244 | + hours = ecma_date_parse_date_chars (&iter, 2); |
| 245 | + |
| 246 | + if (hours < 0 || hours > 24) |
| 247 | + { |
| 248 | + hours = ecma_number_make_nan (); |
| 249 | + } |
| 250 | + else if (hours == 24) |
| 251 | + { |
| 252 | + hours = ECMA_NUMBER_ZERO; |
| 253 | + } |
| 254 | + |
| 255 | + /* eat up ':' */ |
| 256 | + lit_utf8_iterator_incr (&iter); |
| 257 | + |
| 258 | + minutes = ecma_date_parse_date_chars (&iter, 2); |
| 259 | + |
| 260 | + if (minutes < 0 || minutes > 59) |
| 261 | + { |
| 262 | + minutes = ecma_number_make_nan (); |
| 263 | + } |
| 264 | + |
| 265 | + if (is_negative) |
| 266 | + { |
| 267 | + time += ecma_date_make_time (hours, minutes, ECMA_NUMBER_ZERO, ECMA_NUMBER_ZERO); |
| 268 | + } |
| 269 | + else |
| 270 | + { |
| 271 | + time -= ecma_date_make_time (hours, minutes, ECMA_NUMBER_ZERO, ECMA_NUMBER_ZERO); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + if (lit_utf8_iterator_is_eos (&iter)) |
| 278 | + { |
| 279 | + ecma_number_t date = ecma_date_make_day (year, month - 1, day); |
| 280 | + *date_num_p = ecma_date_make_date (date, time); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (date_num_p)); |
| 285 | + |
| 286 | + MEM_FINALIZE_LOCAL_ARRAY (date_start_p); |
| 287 | + ECMA_FINALIZE (date_str_value); |
| 288 | + |
| 289 | + return ret_value; |
54 | 290 | } /* ecma_builtin_date_parse */
|
55 | 291 |
|
56 | 292 | /**
|
|
0 commit comments