Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: src/builtins.cc

Issue 1579613002: [builtins] Refactor the remaining Date builtins. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: As per offline discussion, remove the weird test, which does not add a lot of value. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/builtins.h ('k') | src/compiler/js-intrinsic-lowering.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/builtins.h" 5 #include "src/builtins.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/api-natives.h" 8 #include "src/api-natives.h"
9 #include "src/arguments.h" 9 #include "src/arguments.h"
10 #include "src/base/once.h" 10 #include "src/base/once.h"
(...skipping 2104 matching lines...) Expand 10 before | Expand all | Expand 10 after
2115 if (!std::isnan(date)) { 2115 if (!std::isnan(date)) {
2116 date = isolate->date_cache()->ToUTC(static_cast<int64_t>(date)); 2116 date = isolate->date_cache()->ToUTC(static_cast<int64_t>(date));
2117 } 2117 }
2118 } else { 2118 } else {
2119 date -= tmp->get(7)->Number() * 1000.0; 2119 date -= tmp->get(7)->Number() * 1000.0;
2120 } 2120 }
2121 return date; 2121 return date;
2122 } 2122 }
2123 2123
2124 2124
2125 enum ToDateStringMode { kDateOnly, kTimeOnly, kDateAndTime };
2126
2127
2125 // ES6 section 20.3.4.41.1 ToDateString(tv) 2128 // ES6 section 20.3.4.41.1 ToDateString(tv)
2126 void ToDateString(double time_val, Vector<char> str, DateCache* date_cache) { 2129 void ToDateString(double time_val, Vector<char> str, DateCache* date_cache,
2130 ToDateStringMode mode = kDateAndTime) {
2127 if (std::isnan(time_val)) { 2131 if (std::isnan(time_val)) {
2128 SNPrintF(str, "Invalid Date"); 2132 SNPrintF(str, "Invalid Date");
2133 return;
2134 }
2135 int64_t time_ms = static_cast<int64_t>(time_val);
2136 int64_t local_time_ms = date_cache->ToLocal(time_ms);
2137 int year, month, day, weekday, hour, min, sec, ms;
2138 date_cache->BreakDownTime(local_time_ms, &year, &month, &day, &weekday, &hour,
2139 &min, &sec, &ms);
2140 int timezone_offset = -date_cache->TimezoneOffset(time_ms);
2141 int timezone_hour = std::abs(timezone_offset) / 60;
2142 int timezone_min = std::abs(timezone_offset) % 60;
2143 const char* local_timezone = date_cache->LocalTimezone(time_ms);
2144 switch (mode) {
2145 case kDateOnly:
2146 SNPrintF(str, "%s %s %02d %4d", kShortWeekDays[weekday],
2147 kShortMonths[month], day, year);
2148 return;
2149 case kTimeOnly:
2150 SNPrintF(str, "%02d:%02d:%02d GMT%c%02d%02d (%s)", hour, min, sec,
2151 (timezone_offset < 0) ? '-' : '+', timezone_hour, timezone_min,
2152 local_timezone);
2153 return;
2154 case kDateAndTime:
2155 SNPrintF(str, "%s %s %02d %4d %02d:%02d:%02d GMT%c%02d%02d (%s)",
2156 kShortWeekDays[weekday], kShortMonths[month], day, year, hour,
2157 min, sec, (timezone_offset < 0) ? '-' : '+', timezone_hour,
2158 timezone_min, local_timezone);
2159 return;
2160 }
2161 UNREACHABLE();
2162 }
2163
2164
2165 Object* SetLocalDateValue(Handle<JSDate> date, double time_val) {
2166 if (time_val >= -DateCache::kMaxTimeBeforeUTCInMs &&
2167 time_val <= DateCache::kMaxTimeBeforeUTCInMs) {
2168 Isolate* const isolate = date->GetIsolate();
2169 time_val = isolate->date_cache()->ToUTC(static_cast<int64_t>(time_val));
2129 } else { 2170 } else {
2130 int64_t time_ms = static_cast<int64_t>(time_val); 2171 time_val = std::numeric_limits<double>::quiet_NaN();
2131 int64_t local_time_ms = date_cache->ToLocal(time_ms);
2132 int year, month, day, weekday, hour, min, sec, ms;
2133 date_cache->BreakDownTime(local_time_ms, &year, &month, &day, &weekday,
2134 &hour, &min, &sec, &ms);
2135 int timezone_offset = -date_cache->TimezoneOffset(time_ms);
2136 int timezone_hour = std::abs(timezone_offset) / 60;
2137 int timezone_min = std::abs(timezone_offset) % 60;
2138 const char* local_timezone = date_cache->LocalTimezone(time_ms);
2139 SNPrintF(str, "%s %s %02d %4d %02d:%02d:%02d GMT%c%02d%02d (%s)",
2140 kShortWeekDays[weekday], kShortMonths[month], day, year, hour, min,
2141 sec, (timezone_offset < 0) ? '-' : '+', timezone_hour,
2142 timezone_min, local_timezone);
2143 } 2172 }
2173 return *JSDate::SetValue(date, TimeClip(time_val));
2144 } 2174 }
2145 2175
2146 } // namespace 2176 } // namespace
2147 2177
2148 2178
2149 // ES6 section 20.3.2 The Date Constructor for the [[Call]] case. 2179 // ES6 section 20.3.2 The Date Constructor for the [[Call]] case.
2150 BUILTIN(DateConstructor) { 2180 BUILTIN(DateConstructor) {
2151 HandleScope scope(isolate); 2181 HandleScope scope(isolate);
2152 double const time_val = JSDate::CurrentTimeValue(isolate); 2182 double const time_val = JSDate::CurrentTimeValue(isolate);
2153 char buffer[128]; 2183 char buffer[128];
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
2313 if (!std::isnan(year)) { 2343 if (!std::isnan(year)) {
2314 double const y = DoubleToInteger(year); 2344 double const y = DoubleToInteger(year);
2315 if (0.0 <= y && y <= 99) year = 1900 + y; 2345 if (0.0 <= y && y <= 99) year = 1900 + y;
2316 } 2346 }
2317 double const day = MakeDay(year, month, date); 2347 double const day = MakeDay(year, month, date);
2318 double const time = MakeTime(hours, minutes, seconds, ms); 2348 double const time = MakeTime(hours, minutes, seconds, ms);
2319 return *isolate->factory()->NewNumber(TimeClip(MakeDate(day, time))); 2349 return *isolate->factory()->NewNumber(TimeClip(MakeDate(day, time)));
2320 } 2350 }
2321 2351
2322 2352
2353 // ES6 section 20.3.4.20 Date.prototype.setDate ( date )
2354 BUILTIN(DatePrototypeSetDate) {
2355 HandleScope scope(isolate);
2356 CHECK_RECEIVER(JSDate, date, "Date.prototype.setDate");
2357 Handle<Object> value = args.atOrUndefined(isolate, 1);
2358 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(value));
2359 double time_val = date->value()->Number();
2360 if (!std::isnan(time_val)) {
2361 int64_t const time_ms = static_cast<int64_t>(time_val);
2362 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2363 int const days = isolate->date_cache()->DaysFromTime(local_time_ms);
2364 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, days);
2365 int year, month, day;
2366 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2367 time_val = MakeDate(MakeDay(year, month, value->Number()), time_within_day);
2368 }
2369 return SetLocalDateValue(date, time_val);
2370 }
2371
2372
2373 // ES6 section 20.3.4.21 Date.prototype.setFullYear (year, month, date)
2374 BUILTIN(DatePrototypeSetFullYear) {
2375 HandleScope scope(isolate);
2376 CHECK_RECEIVER(JSDate, date, "Date.prototype.setFullYear");
2377 int const argc = args.length() - 1;
2378 Handle<Object> year = args.atOrUndefined(isolate, 1);
2379 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, year, Object::ToNumber(year));
2380 double y = year->Number(), m = 0.0, dt = 1.0;
2381 int time_within_day = 0;
2382 if (!std::isnan(date->value()->Number())) {
2383 int64_t const time_ms = static_cast<int64_t>(date->value()->Number());
2384 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2385 int const days = isolate->date_cache()->DaysFromTime(local_time_ms);
2386 time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, days);
2387 int year, month, day;
2388 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2389 m = month;
2390 dt = day;
2391 }
2392 if (argc >= 2) {
2393 Handle<Object> month = args.at<Object>(2);
2394 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, month, Object::ToNumber(month));
2395 m = month->Number();
2396 if (argc >= 3) {
2397 Handle<Object> date = args.at<Object>(3);
2398 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, date, Object::ToNumber(date));
2399 dt = date->Number();
2400 }
2401 }
2402 double time_val = MakeDate(MakeDay(y, m, dt), time_within_day);
2403 return SetLocalDateValue(date, time_val);
2404 }
2405
2406
2407 // ES6 section 20.3.4.22 Date.prototype.setHours(hour, min, sec, ms)
2408 BUILTIN(DatePrototypeSetHours) {
2409 HandleScope scope(isolate);
2410 CHECK_RECEIVER(JSDate, date, "Date.prototype.setHours");
2411 int const argc = args.length() - 1;
2412 Handle<Object> hour = args.atOrUndefined(isolate, 1);
2413 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, hour, Object::ToNumber(hour));
2414 double h = hour->Number();
2415 double time_val = date->value()->Number();
2416 if (!std::isnan(time_val)) {
2417 int64_t const time_ms = static_cast<int64_t>(time_val);
2418 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2419 int day = isolate->date_cache()->DaysFromTime(local_time_ms);
2420 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, day);
2421 double m = (time_within_day / (60 * 1000)) % 60;
2422 double s = (time_within_day / 1000) % 60;
2423 double milli = time_within_day % 1000;
2424 if (argc >= 2) {
2425 Handle<Object> min = args.at<Object>(2);
2426 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, min, Object::ToNumber(min));
2427 m = min->Number();
2428 if (argc >= 3) {
2429 Handle<Object> sec = args.at<Object>(3);
2430 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2431 s = sec->Number();
2432 if (argc >= 4) {
2433 Handle<Object> ms = args.at<Object>(4);
2434 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2435 milli = ms->Number();
2436 }
2437 }
2438 }
2439 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2440 }
2441 return SetLocalDateValue(date, time_val);
2442 }
2443
2444
2445 // ES6 section 20.3.4.23 Date.prototype.setMilliseconds(ms)
2446 BUILTIN(DatePrototypeSetMilliseconds) {
2447 HandleScope scope(isolate);
2448 CHECK_RECEIVER(JSDate, date, "Date.prototype.setMilliseconds");
2449 Handle<Object> ms = args.atOrUndefined(isolate, 1);
2450 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2451 double time_val = date->value()->Number();
2452 if (!std::isnan(time_val)) {
2453 int64_t const time_ms = static_cast<int64_t>(time_val);
2454 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2455 int day = isolate->date_cache()->DaysFromTime(local_time_ms);
2456 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, day);
2457 int h = time_within_day / (60 * 60 * 1000);
2458 int m = (time_within_day / (60 * 1000)) % 60;
2459 int s = (time_within_day / 1000) % 60;
2460 time_val = MakeDate(day, MakeTime(h, m, s, ms->Number()));
2461 }
2462 return SetLocalDateValue(date, time_val);
2463 }
2464
2465
2466 // ES6 section 20.3.4.24 Date.prototype.setMinutes ( min, sec, ms )
2467 BUILTIN(DatePrototypeSetMinutes) {
2468 HandleScope scope(isolate);
2469 CHECK_RECEIVER(JSDate, date, "Date.prototype.setMinutes");
2470 int const argc = args.length() - 1;
2471 Handle<Object> min = args.atOrUndefined(isolate, 1);
2472 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, min, Object::ToNumber(min));
2473 double time_val = date->value()->Number();
2474 if (!std::isnan(time_val)) {
2475 int64_t const time_ms = static_cast<int64_t>(time_val);
2476 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2477 int day = isolate->date_cache()->DaysFromTime(local_time_ms);
2478 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, day);
2479 int h = time_within_day / (60 * 60 * 1000);
2480 double m = min->Number();
2481 double s = (time_within_day / 1000) % 60;
2482 double milli = time_within_day % 1000;
2483 if (argc >= 2) {
2484 Handle<Object> sec = args.at<Object>(2);
2485 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2486 s = sec->Number();
2487 if (argc >= 3) {
2488 Handle<Object> ms = args.at<Object>(3);
2489 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2490 milli = ms->Number();
2491 }
2492 }
2493 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2494 }
2495 return SetLocalDateValue(date, time_val);
2496 }
2497
2498
2499 // ES6 section 20.3.4.25 Date.prototype.setMonth ( month, date )
2500 BUILTIN(DatePrototypeSetMonth) {
2501 HandleScope scope(isolate);
2502 CHECK_RECEIVER(JSDate, date, "Date.prototype.setMonth");
2503 int const argc = args.length() - 1;
2504 Handle<Object> month = args.atOrUndefined(isolate, 1);
2505 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, month, Object::ToNumber(month));
2506 double time_val = date->value()->Number();
2507 if (!std::isnan(time_val)) {
2508 int64_t const time_ms = static_cast<int64_t>(time_val);
2509 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2510 int days = isolate->date_cache()->DaysFromTime(local_time_ms);
2511 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, days);
2512 int year, unused, day;
2513 isolate->date_cache()->YearMonthDayFromDays(days, &year, &unused, &day);
2514 double m = month->Number();
2515 double dt = day;
2516 if (argc >= 2) {
2517 Handle<Object> date = args.at<Object>(2);
2518 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, date, Object::ToNumber(date));
2519 dt = date->Number();
2520 }
2521 time_val = MakeDate(MakeDay(year, m, dt), time_within_day);
2522 }
2523 return SetLocalDateValue(date, time_val);
2524 }
2525
2526
2527 // ES6 section 20.3.4.26 Date.prototype.setSeconds ( sec, ms )
2528 BUILTIN(DatePrototypeSetSeconds) {
2529 HandleScope scope(isolate);
2530 CHECK_RECEIVER(JSDate, date, "Date.prototype.setSeconds");
2531 int const argc = args.length() - 1;
2532 Handle<Object> sec = args.atOrUndefined(isolate, 1);
2533 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2534 double time_val = date->value()->Number();
2535 if (!std::isnan(time_val)) {
2536 int64_t const time_ms = static_cast<int64_t>(time_val);
2537 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2538 int day = isolate->date_cache()->DaysFromTime(local_time_ms);
2539 int time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, day);
2540 int h = time_within_day / (60 * 60 * 1000);
2541 double m = (time_within_day / (60 * 1000)) % 60;
2542 double s = sec->Number();
2543 double milli = time_within_day % 1000;
2544 if (argc >= 2) {
2545 Handle<Object> ms = args.at<Object>(2);
2546 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2547 milli = ms->Number();
2548 }
2549 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2550 }
2551 return SetLocalDateValue(date, time_val);
2552 }
2553
2554
2555 // ES6 section 20.3.4.27 Date.prototype.setTime ( time )
2556 BUILTIN(DatePrototypeSetTime) {
2557 HandleScope scope(isolate);
2558 CHECK_RECEIVER(JSDate, date, "Date.prototype.setTime");
2559 Handle<Object> value = args.atOrUndefined(isolate, 1);
2560 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(value));
2561 return *JSDate::SetValue(date, TimeClip(value->Number()));
2562 }
2563
2564
2565 // ES6 section 20.3.4.28 Date.prototype.setUTCDate ( date )
2566 BUILTIN(DatePrototypeSetUTCDate) {
2567 HandleScope scope(isolate);
2568 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCDate");
2569 Handle<Object> value = args.atOrUndefined(isolate, 1);
2570 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(value));
2571 if (std::isnan(date->value()->Number())) return date->value();
2572 int64_t const time_ms = static_cast<int64_t>(date->value()->Number());
2573 int const days = isolate->date_cache()->DaysFromTime(time_ms);
2574 int const time_within_day = isolate->date_cache()->TimeInDay(time_ms, days);
2575 int year, month, day;
2576 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2577 double const time_val =
2578 MakeDate(MakeDay(year, month, value->Number()), time_within_day);
2579 return *JSDate::SetValue(date, TimeClip(time_val));
2580 }
2581
2582
2583 // ES6 section 20.3.4.29 Date.prototype.setUTCFullYear (year, month, date)
2584 BUILTIN(DatePrototypeSetUTCFullYear) {
2585 HandleScope scope(isolate);
2586 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCFullYear");
2587 int const argc = args.length() - 1;
2588 Handle<Object> year = args.atOrUndefined(isolate, 1);
2589 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, year, Object::ToNumber(year));
2590 double y = year->Number(), m = 0.0, dt = 1.0;
2591 int time_within_day = 0;
2592 if (!std::isnan(date->value()->Number())) {
2593 int64_t const time_ms = static_cast<int64_t>(date->value()->Number());
2594 int const days = isolate->date_cache()->DaysFromTime(time_ms);
2595 time_within_day = isolate->date_cache()->TimeInDay(time_ms, days);
2596 int year, month, day;
2597 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2598 m = month;
2599 dt = day;
2600 }
2601 if (argc >= 2) {
2602 Handle<Object> month = args.at<Object>(2);
2603 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, month, Object::ToNumber(month));
2604 m = month->Number();
2605 if (argc >= 3) {
2606 Handle<Object> date = args.at<Object>(3);
2607 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, date, Object::ToNumber(date));
2608 dt = date->Number();
2609 }
2610 }
2611 double const time_val = MakeDate(MakeDay(y, m, dt), time_within_day);
2612 return *JSDate::SetValue(date, TimeClip(time_val));
2613 }
2614
2615
2616 // ES6 section 20.3.4.30 Date.prototype.setUTCHours(hour, min, sec, ms)
2617 BUILTIN(DatePrototypeSetUTCHours) {
2618 HandleScope scope(isolate);
2619 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCHours");
2620 int const argc = args.length() - 1;
2621 Handle<Object> hour = args.atOrUndefined(isolate, 1);
2622 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, hour, Object::ToNumber(hour));
2623 double h = hour->Number();
2624 double time_val = date->value()->Number();
2625 if (!std::isnan(time_val)) {
2626 int64_t const time_ms = static_cast<int64_t>(time_val);
2627 int day = isolate->date_cache()->DaysFromTime(time_ms);
2628 int time_within_day = isolate->date_cache()->TimeInDay(time_ms, day);
2629 double m = (time_within_day / (60 * 1000)) % 60;
2630 double s = (time_within_day / 1000) % 60;
2631 double milli = time_within_day % 1000;
2632 if (argc >= 2) {
2633 Handle<Object> min = args.at<Object>(2);
2634 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, min, Object::ToNumber(min));
2635 m = min->Number();
2636 if (argc >= 3) {
2637 Handle<Object> sec = args.at<Object>(3);
2638 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2639 s = sec->Number();
2640 if (argc >= 4) {
2641 Handle<Object> ms = args.at<Object>(4);
2642 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2643 milli = ms->Number();
2644 }
2645 }
2646 }
2647 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2648 }
2649 return *JSDate::SetValue(date, TimeClip(time_val));
2650 }
2651
2652
2653 // ES6 section 20.3.4.31 Date.prototype.setUTCMilliseconds(ms)
2654 BUILTIN(DatePrototypeSetUTCMilliseconds) {
2655 HandleScope scope(isolate);
2656 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCMilliseconds");
2657 Handle<Object> ms = args.atOrUndefined(isolate, 1);
2658 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2659 double time_val = date->value()->Number();
2660 if (!std::isnan(time_val)) {
2661 int64_t const time_ms = static_cast<int64_t>(time_val);
2662 int day = isolate->date_cache()->DaysFromTime(time_ms);
2663 int time_within_day = isolate->date_cache()->TimeInDay(time_ms, day);
2664 int h = time_within_day / (60 * 60 * 1000);
2665 int m = (time_within_day / (60 * 1000)) % 60;
2666 int s = (time_within_day / 1000) % 60;
2667 time_val = MakeDate(day, MakeTime(h, m, s, ms->Number()));
2668 }
2669 return *JSDate::SetValue(date, TimeClip(time_val));
2670 }
2671
2672
2673 // ES6 section 20.3.4.32 Date.prototype.setUTCMinutes ( min, sec, ms )
2674 BUILTIN(DatePrototypeSetUTCMinutes) {
2675 HandleScope scope(isolate);
2676 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCMinutes");
2677 int const argc = args.length() - 1;
2678 Handle<Object> min = args.atOrUndefined(isolate, 1);
2679 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, min, Object::ToNumber(min));
2680 double time_val = date->value()->Number();
2681 if (!std::isnan(time_val)) {
2682 int64_t const time_ms = static_cast<int64_t>(time_val);
2683 int day = isolate->date_cache()->DaysFromTime(time_ms);
2684 int time_within_day = isolate->date_cache()->TimeInDay(time_ms, day);
2685 int h = time_within_day / (60 * 60 * 1000);
2686 double m = min->Number();
2687 double s = (time_within_day / 1000) % 60;
2688 double milli = time_within_day % 1000;
2689 if (argc >= 2) {
2690 Handle<Object> sec = args.at<Object>(2);
2691 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2692 s = sec->Number();
2693 if (argc >= 3) {
2694 Handle<Object> ms = args.at<Object>(3);
2695 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2696 milli = ms->Number();
2697 }
2698 }
2699 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2700 }
2701 return *JSDate::SetValue(date, TimeClip(time_val));
2702 }
2703
2704
2705 // ES6 section 20.3.4.31 Date.prototype.setUTCMonth ( month, date )
2706 BUILTIN(DatePrototypeSetUTCMonth) {
2707 HandleScope scope(isolate);
2708 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCMonth");
2709 int const argc = args.length() - 1;
2710 Handle<Object> month = args.atOrUndefined(isolate, 1);
2711 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, month, Object::ToNumber(month));
2712 double time_val = date->value()->Number();
2713 if (!std::isnan(time_val)) {
2714 int64_t const time_ms = static_cast<int64_t>(time_val);
2715 int days = isolate->date_cache()->DaysFromTime(time_ms);
2716 int time_within_day = isolate->date_cache()->TimeInDay(time_ms, days);
2717 int year, unused, day;
2718 isolate->date_cache()->YearMonthDayFromDays(days, &year, &unused, &day);
2719 double m = month->Number();
2720 double dt = day;
2721 if (argc >= 2) {
2722 Handle<Object> date = args.at<Object>(2);
2723 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, date, Object::ToNumber(date));
2724 dt = date->Number();
2725 }
2726 time_val = MakeDate(MakeDay(year, m, dt), time_within_day);
2727 }
2728 return *JSDate::SetValue(date, TimeClip(time_val));
2729 }
2730
2731
2732 // ES6 section 20.3.4.34 Date.prototype.setUTCSeconds ( sec, ms )
2733 BUILTIN(DatePrototypeSetUTCSeconds) {
2734 HandleScope scope(isolate);
2735 CHECK_RECEIVER(JSDate, date, "Date.prototype.setUTCSeconds");
2736 int const argc = args.length() - 1;
2737 Handle<Object> sec = args.atOrUndefined(isolate, 1);
2738 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sec, Object::ToNumber(sec));
2739 double time_val = date->value()->Number();
2740 if (!std::isnan(time_val)) {
2741 int64_t const time_ms = static_cast<int64_t>(time_val);
2742 int day = isolate->date_cache()->DaysFromTime(time_ms);
2743 int time_within_day = isolate->date_cache()->TimeInDay(time_ms, day);
2744 int h = time_within_day / (60 * 60 * 1000);
2745 double m = (time_within_day / (60 * 1000)) % 60;
2746 double s = sec->Number();
2747 double milli = time_within_day % 1000;
2748 if (argc >= 2) {
2749 Handle<Object> ms = args.at<Object>(2);
2750 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, ms, Object::ToNumber(ms));
2751 milli = ms->Number();
2752 }
2753 time_val = MakeDate(day, MakeTime(h, m, s, milli));
2754 }
2755 return *JSDate::SetValue(date, TimeClip(time_val));
2756 }
2757
2758
2759 // ES6 section 20.3.4.35 Date.prototype.toDateString ( )
2760 BUILTIN(DatePrototypeToDateString) {
2761 HandleScope scope(isolate);
2762 CHECK_RECEIVER(JSDate, date, "Date.prototype.toDateString");
2763 char buffer[128];
2764 Vector<char> str(buffer, arraysize(buffer));
2765 ToDateString(date->value()->Number(), str, isolate->date_cache(), kDateOnly);
2766 return *isolate->factory()->NewStringFromAsciiChecked(str.start());
2767 }
2768
2769
2323 // ES6 section 20.3.4.36 Date.prototype.toISOString ( ) 2770 // ES6 section 20.3.4.36 Date.prototype.toISOString ( )
2324 BUILTIN(DatePrototypeToISOString) { 2771 BUILTIN(DatePrototypeToISOString) {
2325 HandleScope scope(isolate); 2772 HandleScope scope(isolate);
2326 CHECK_RECEIVER(JSDate, date, "Date.prototype.toISOString"); 2773 CHECK_RECEIVER(JSDate, date, "Date.prototype.toISOString");
2327 double const time_val = date->value()->Number(); 2774 double const time_val = date->value()->Number();
2328 if (std::isnan(time_val)) { 2775 if (std::isnan(time_val)) {
2329 THROW_NEW_ERROR_RETURN_FAILURE( 2776 THROW_NEW_ERROR_RETURN_FAILURE(
2330 isolate, NewRangeError(MessageTemplate::kInvalidTimeValue)); 2777 isolate, NewRangeError(MessageTemplate::kInvalidTimeValue));
2331 } 2778 }
2332 int64_t const time_ms = static_cast<int64_t>(time_val); 2779 int64_t const time_ms = static_cast<int64_t>(time_val);
2333 int year, month, day, weekday, hour, min, sec, ms; 2780 int year, month, day, weekday, hour, min, sec, ms;
2334 isolate->date_cache()->BreakDownTime(time_ms, &year, &month, &day, &weekday, 2781 isolate->date_cache()->BreakDownTime(time_ms, &year, &month, &day, &weekday,
2335 &hour, &min, &sec, &ms); 2782 &hour, &min, &sec, &ms);
2336 char buffer[128]; 2783 char buffer[128];
2337 Vector<char> str(buffer, arraysize(buffer)); 2784 Vector<char> str(buffer, arraysize(buffer));
2338 if (year >= 0 && year <= 9999) { 2785 if (year >= 0 && year <= 9999) {
2339 SNPrintF(str, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month + 1, day, 2786 SNPrintF(str, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month + 1, day,
2340 hour, min, sec, ms); 2787 hour, min, sec, ms);
2341 } else if (year < 0) { 2788 } else if (year < 0) {
2342 SNPrintF(str, "-%06d-%02d-%02dT%02d:%02d:%02d.%03dZ", -year, month + 1, day, 2789 SNPrintF(str, "-%06d-%02d-%02dT%02d:%02d:%02d.%03dZ", -year, month + 1, day,
2343 hour, min, sec, ms); 2790 hour, min, sec, ms);
2344 } else { 2791 } else {
2345 SNPrintF(str, "+%06d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month + 1, day, 2792 SNPrintF(str, "+%06d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month + 1, day,
2346 hour, min, sec, ms); 2793 hour, min, sec, ms);
2347 } 2794 }
2348 return *isolate->factory()->NewStringFromAsciiChecked(str.start()); 2795 return *isolate->factory()->NewStringFromAsciiChecked(str.start());
2349 } 2796 }
2350 2797
2351 2798
2799 // ES6 section 20.3.4.41 Date.prototype.toString ( )
2800 BUILTIN(DatePrototypeToString) {
2801 HandleScope scope(isolate);
2802 CHECK_RECEIVER(JSDate, date, "Date.prototype.toString");
2803 char buffer[128];
2804 Vector<char> str(buffer, arraysize(buffer));
2805 ToDateString(date->value()->Number(), str, isolate->date_cache());
2806 return *isolate->factory()->NewStringFromAsciiChecked(str.start());
2807 }
2808
2809
2810 // ES6 section 20.3.4.42 Date.prototype.toTimeString ( )
2811 BUILTIN(DatePrototypeToTimeString) {
2812 HandleScope scope(isolate);
2813 CHECK_RECEIVER(JSDate, date, "Date.prototype.toTimeString");
2814 char buffer[128];
2815 Vector<char> str(buffer, arraysize(buffer));
2816 ToDateString(date->value()->Number(), str, isolate->date_cache(), kTimeOnly);
2817 return *isolate->factory()->NewStringFromAsciiChecked(str.start());
2818 }
2819
2820
2821 // ES6 section 20.3.4.43 Date.prototype.toUTCString ( )
2822 BUILTIN(DatePrototypeToUTCString) {
2823 HandleScope scope(isolate);
2824 CHECK_RECEIVER(JSDate, date, "Date.prototype.toUTCString");
2825 double const time_val = date->value()->Number();
2826 if (std::isnan(time_val)) {
2827 return *isolate->factory()->NewStringFromAsciiChecked("Invalid Date");
2828 }
2829 char buffer[128];
2830 Vector<char> str(buffer, arraysize(buffer));
2831 int64_t time_ms = static_cast<int64_t>(time_val);
2832 int year, month, day, weekday, hour, min, sec, ms;
2833 isolate->date_cache()->BreakDownTime(time_ms, &year, &month, &day, &weekday,
2834 &hour, &min, &sec, &ms);
2835 SNPrintF(str, "%s, %02d %s %4d %02d:%02d:%02d GMT", kShortWeekDays[weekday],
2836 day, kShortMonths[month], year, hour, min, sec);
2837 return *isolate->factory()->NewStringFromAsciiChecked(str.start());
2838 }
2839
2840
2352 // ES6 section 20.3.4.44 Date.prototype.valueOf ( ) 2841 // ES6 section 20.3.4.44 Date.prototype.valueOf ( )
2353 BUILTIN(DatePrototypeValueOf) { 2842 BUILTIN(DatePrototypeValueOf) {
2354 HandleScope scope(isolate); 2843 HandleScope scope(isolate);
2355 CHECK_RECEIVER(JSDate, date, "Date.prototype.valueOf"); 2844 CHECK_RECEIVER(JSDate, date, "Date.prototype.valueOf");
2356 return date->value(); 2845 return date->value();
2357 } 2846 }
2358 2847
2359 2848
2360 // ES6 section 20.3.4.45 Date.prototype [ @@toPrimitive ] ( hint ) 2849 // ES6 section 20.3.4.45 Date.prototype [ @@toPrimitive ] ( hint )
2361 BUILTIN(DatePrototypeToPrimitive) { 2850 BUILTIN(DatePrototypeToPrimitive) {
2362 HandleScope scope(isolate); 2851 HandleScope scope(isolate);
2363 DCHECK_EQ(2, args.length()); 2852 DCHECK_EQ(2, args.length());
2364 CHECK_RECEIVER(JSReceiver, receiver, "Date.prototype [ @@toPrimitive ]"); 2853 CHECK_RECEIVER(JSReceiver, receiver, "Date.prototype [ @@toPrimitive ]");
2365 Handle<Object> hint = args.at<Object>(1); 2854 Handle<Object> hint = args.at<Object>(1);
2366 Handle<Object> result; 2855 Handle<Object> result;
2367 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, 2856 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
2368 JSDate::ToPrimitive(receiver, hint)); 2857 JSDate::ToPrimitive(receiver, hint));
2369 return *result; 2858 return *result;
2370 } 2859 }
2371 2860
2372 2861
2862 // ES6 section B.2.4.1 Date.prototype.getYear ( )
2863 BUILTIN(DatePrototypeGetYear) {
2864 HandleScope scope(isolate);
2865 CHECK_RECEIVER(JSDate, date, "Date.prototype.getYear");
2866 double time_val = date->value()->Number();
2867 if (std::isnan(time_val)) return date->value();
2868 int64_t time_ms = static_cast<int64_t>(time_val);
2869 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2870 int days = isolate->date_cache()->DaysFromTime(local_time_ms);
2871 int year, month, day;
2872 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2873 return Smi::FromInt(year - 1900);
2874 }
2875
2876
2877 // ES6 section B.2.4.2 Date.prototype.setYear ( year )
2878 BUILTIN(DatePrototypeSetYear) {
2879 HandleScope scope(isolate);
2880 CHECK_RECEIVER(JSDate, date, "Date.prototype.setYear");
2881 Handle<Object> year = args.atOrUndefined(isolate, 1);
2882 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, year, Object::ToNumber(year));
2883 double m = 0.0, dt = 1.0, y = year->Number();
2884 if (0.0 <= y && y <= 99.0) {
2885 y = 1900.0 + DoubleToInteger(y);
2886 }
2887 int time_within_day = 0;
2888 if (!std::isnan(date->value()->Number())) {
2889 int64_t const time_ms = static_cast<int64_t>(date->value()->Number());
2890 int64_t local_time_ms = isolate->date_cache()->ToLocal(time_ms);
2891 int const days = isolate->date_cache()->DaysFromTime(local_time_ms);
2892 time_within_day = isolate->date_cache()->TimeInDay(local_time_ms, days);
2893 int year, month, day;
2894 isolate->date_cache()->YearMonthDayFromDays(days, &year, &month, &day);
2895 m = month;
2896 dt = day;
2897 }
2898 double time_val = MakeDate(MakeDay(y, m, dt), time_within_day);
2899 return SetLocalDateValue(date, time_val);
2900 }
2901
2902
2373 // static 2903 // static
2374 void Builtins::Generate_DatePrototypeGetDate(MacroAssembler* masm) { 2904 void Builtins::Generate_DatePrototypeGetDate(MacroAssembler* masm) {
2375 Generate_DatePrototype_GetField(masm, JSDate::kDay); 2905 Generate_DatePrototype_GetField(masm, JSDate::kDay);
2376 } 2906 }
2377 2907
2378 2908
2379 // static 2909 // static
2380 void Builtins::Generate_DatePrototypeGetDay(MacroAssembler* masm) { 2910 void Builtins::Generate_DatePrototypeGetDay(MacroAssembler* masm) {
2381 Generate_DatePrototype_GetField(masm, JSDate::kWeekday); 2911 Generate_DatePrototype_GetField(masm, JSDate::kWeekday);
2382 } 2912 }
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after
3435 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) 3965 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
3436 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) 3966 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
3437 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) 3967 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H)
3438 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) 3968 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
3439 #undef DEFINE_BUILTIN_ACCESSOR_C 3969 #undef DEFINE_BUILTIN_ACCESSOR_C
3440 #undef DEFINE_BUILTIN_ACCESSOR_A 3970 #undef DEFINE_BUILTIN_ACCESSOR_A
3441 3971
3442 3972
3443 } // namespace internal 3973 } // namespace internal
3444 } // namespace v8 3974 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins.h ('k') | src/compiler/js-intrinsic-lowering.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698