Changeset 282257 in webkit


Ignore:
Timestamp:
Sep 9, 2021 8:23:20 PM (10 months ago)
Author:
ysuzuki@apple.com
Message:

[JSC] Intl.Locale weekendInfo should list all weekend days instead of range
https://bugs.webkit.org/show_bug.cgi?id=230108

Reviewed by Ross Kirsling.

JSTests:

  • stress/intl-locale-info.js:

(throw.new.Error):
(let.enGB.new.Intl.Locale.shouldBe): Deleted.

  • test262/config.yaml:

Source/JavaScriptCore:

We cannot assume that weekend is contiguous. For example, Burnei's weekend is Friday and Sunday.
This is raised in [1], and our conclusion in Sep-9 meeting is that we should have an array which
includes all the weekend days. The change is merged in [2], and this patch changes our implementation
accordingly.

[1]: https://github.com/tc39/proposal-intl-locale-info/issues/25
[2]: https://github.com/tc39/proposal-intl-locale-info/commit/afb1e269dd698476a2514129235cdad88af60e6f

  • runtime/IntlLocale.cpp:

(JSC::IntlLocale::weekInfo):

  • runtime/IntlObjectInlines.h:

(JSC::createArrayFromIntVector):

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JSTests/ChangeLog

    r282239 r282257  
     12021-09-09  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [JSC] Intl.Locale weekendInfo should list all weekend days instead of range
     4        https://bugs.webkit.org/show_bug.cgi?id=230108
     5
     6        Reviewed by Ross Kirsling.
     7
     8        * stress/intl-locale-info.js:
     9        (throw.new.Error):
     10        (let.enGB.new.Intl.Locale.shouldBe): Deleted.
     11        * test262/config.yaml:
     12
    1132021-09-09  Yusuke Suzuki  <ysuzuki@apple.com>
    214
  • trunk/JSTests/stress/intl-locale-info.js

    r281513 r282257  
    66{
    77    let he = new Intl.Locale("he")
    8     shouldBe(JSON.stringify(he.weekInfo), `{"firstDay":7,"weekendStart":5,"weekendEnd":6,"minimalDays":1}`);
     8    shouldBe(JSON.stringify(he.weekInfo), `{"firstDay":7,"weekend":[5,6],"minimalDays":1}`);
    99    let af = new Intl.Locale("af")
    10     shouldBe(JSON.stringify(af.weekInfo), `{"firstDay":7,"weekendStart":6,"weekendEnd":7,"minimalDays":1}`);
     10    shouldBe(JSON.stringify(af.weekInfo), `{"firstDay":7,"weekend":[6,7],"minimalDays":1}`);
    1111    let enGB = new Intl.Locale("en-GB")
    12     shouldBe(JSON.stringify(enGB.weekInfo), `{"firstDay":1,"weekendStart":6,"weekendEnd":7,"minimalDays":4}`);
     12    shouldBe(JSON.stringify(enGB.weekInfo), `{"firstDay":1,"weekend":[6,7],"minimalDays":4}`);
     13    let msBN = new Intl.Locale("ms-BN");
     14    // "weekend" should be [5,7]. But currently ICU/CLDR does not support representing non-contiguous weekend.
     15    shouldBe(JSON.stringify(msBN.weekInfo), `{"firstDay":1,"weekend":[6,7],"minimalDays":1}`);
    1316}
    1417{
  • trunk/JSTests/test262/config.yaml

    r282125 r282257  
    7272    - test/intl402/Locale/constructor-non-iana-canon.js
    7373    - test/intl402/Locale/likely-subtags.js
     74
     75    # The spec is changed, but the test is not updated yet. https://github.com/tc39/proposal-intl-locale-info/pull/44
     76    - test/intl402/Locale/prototype/weekInfo/output-object-keys.js
    7477
    7578    # Pass only in ICU 68~
  • trunk/Source/JavaScriptCore/ChangeLog

    r282242 r282257  
     12021-09-09  Yusuke Suzuki  <ysuzuki@apple.com>
     2
     3        [JSC] Intl.Locale weekendInfo should list all weekend days instead of range
     4        https://bugs.webkit.org/show_bug.cgi?id=230108
     5
     6        Reviewed by Ross Kirsling.
     7
     8        We cannot assume that weekend is contiguous. For example, Burnei's weekend is Friday and Sunday.
     9        This is raised in [1], and our conclusion in Sep-9 meeting is that we should have an array which
     10        includes all the weekend days. The change is merged in [2], and this patch changes our implementation
     11        accordingly.
     12
     13        [1]: https://github.com/tc39/proposal-intl-locale-info/issues/25
     14        [2]: https://github.com/tc39/proposal-intl-locale-info/commit/afb1e269dd698476a2514129235cdad88af60e6f
     15
     16        * runtime/IntlLocale.cpp:
     17        (JSC::IntlLocale::weekInfo):
     18        * runtime/IntlObjectInlines.h:
     19        (JSC::createArrayFromIntVector):
     20
    1212021-09-09  Darin Adler  <darin@apple.com>
    222
  • trunk/Source/JavaScriptCore/runtime/IntlLocale.cpp

    r281688 r282257  
    793793    };
    794794
    795     static_assert(UCAL_SUNDAY == 1);
    796     static_assert(UCAL_SATURDAY == 7);
    797     UCalendarWeekdayType previous = canonicalizeDayOfWeekType(ucal_getDayOfWeekType(calendar.get(), UCAL_SATURDAY, &status));
    798     if (!U_SUCCESS(status)) {
    799         throwTypeError(globalObject, scope, "invalid locale"_s);
    800         return nullptr;
    801     }
    802 
    803     int32_t weekendStart = 0;
    804     int32_t weekendEnd = 0;
    805     for (int32_t day = UCAL_SUNDAY; day <= UCAL_SATURDAY; ++day) {
    806         UCalendarWeekdayType type = canonicalizeDayOfWeekType(ucal_getDayOfWeekType(calendar.get(), static_cast<UCalendarDaysOfWeek>(day), &status));
    807         if (!U_SUCCESS(status)) {
    808             throwTypeError(globalObject, scope, "invalid locale"_s);
    809             return nullptr;
    810         }
    811         if (previous != type) {
    812             switch (type) {
    813             case UCAL_WEEKDAY: // WeekEnd => WeekDay
    814                 if (day == UCAL_SUNDAY)
    815                     weekendEnd = UCAL_SATURDAY;
    816                 else
    817                     weekendEnd = day - 1;
    818                 break;
    819             case UCAL_WEEKEND: // WeekDay => WeekEnd
    820                 weekendStart = day;
    821                 break;
    822             default:
    823                 ASSERT_NOT_REACHED();
    824                 break;
    825             }
    826         }
    827         previous = type;
    828     }
     795    auto convertMondayBasedDayToUCalendarDaysOfWeek = [](int32_t day) -> UCalendarDaysOfWeek {
     796        // Convert from
     797        //     Monday => 1
     798        //     Sunday => 7
     799        // to
     800        //     Sunday => 1
     801        //     Saturday => 7
     802        static_assert(UCAL_SUNDAY == 1);
     803        static_assert(UCAL_SATURDAY == 7);
     804        if (day == 7)
     805            return UCAL_SUNDAY;
     806        return static_cast<UCalendarDaysOfWeek>(day + 1);
     807    };
    829808
    830809    auto convertUCalendarDaysOfWeekToMondayBasedDay = [](int32_t day) -> int32_t {
     
    840819    };
    841820
     821    Vector<int32_t, 7> weekend;
     822    for (int32_t day = 1; day <= 7; ++day) {
     823        UCalendarWeekdayType type = canonicalizeDayOfWeekType(ucal_getDayOfWeekType(calendar.get(), convertMondayBasedDayToUCalendarDaysOfWeek(day), &status));
     824        if (!U_SUCCESS(status)) {
     825            throwTypeError(globalObject, scope, "invalid locale"_s);
     826            return nullptr;
     827        }
     828        switch (type) {
     829        case UCAL_WEEKDAY:
     830            break;
     831        case UCAL_WEEKEND:
     832            weekend.append(day);
     833            break;
     834        default:
     835            ASSERT_NOT_REACHED();
     836            break;
     837        }
     838    }
     839
     840    auto* weekendArray = createArrayFromIntVector(globalObject, WTFMove(weekend));
     841    RETURN_IF_EXCEPTION(scope, { });
     842
    842843    JSObject* result = constructEmptyObject(globalObject);
    843844    result->putDirect(vm, Identifier::fromString(vm, "firstDay"), jsNumber(convertUCalendarDaysOfWeekToMondayBasedDay(firstDayOfWeek)));
    844     result->putDirect(vm, Identifier::fromString(vm, "weekendStart"), jsNumber(convertUCalendarDaysOfWeekToMondayBasedDay(weekendStart)));
    845     result->putDirect(vm, Identifier::fromString(vm, "weekendEnd"), jsNumber(convertUCalendarDaysOfWeekToMondayBasedDay(weekendEnd)));
     845    result->putDirect(vm, Identifier::fromString(vm, "weekend"), weekendArray);
    846846    result->putDirect(vm, Identifier::fromString(vm, "minimalDays"), jsNumber(minimalDays));
    847847    return result;
  • trunk/Source/JavaScriptCore/runtime/IntlObjectInlines.h

    r281788 r282257  
    244244}
    245245
     246template<typename Container>
     247JSArray* createArrayFromIntVector(JSGlobalObject* globalObject, const Container& elements)
     248{
     249    VM& vm = globalObject->vm();
     250    auto scope = DECLARE_THROW_SCOPE(vm);
     251
     252    JSArray* result = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), elements.size());
     253    if (!result) {
     254        throwOutOfMemoryError(globalObject, scope);
     255        return nullptr;
     256    }
     257    for (unsigned index = 0; index < elements.size(); ++index) {
     258        result->putDirectIndex(globalObject, index, jsNumber(elements[index]));
     259        RETURN_IF_EXCEPTION(scope, { });
     260    }
     261    return result;
     262}
     263
    246264} // namespace JSC
Note: See TracChangeset for help on using the changeset viewer.