Skip to content

Commit

Permalink
Implement DateTimeFormat.format for non android/ios environments (#1362)
Browse files Browse the repository at this point in the history
Summary:
Related to #1350 and #1211
Follow up of #1361

I'd like to expand non iOS/android support of `Intl`. In this case, I'm implementing both `DateTimeFormat.prototype.resolvedOptions` and `DateTimeFormat.prototype.format`. The implementation is a mix from the Apple and hermes-windows' implementations.

- created `PlatformIntlShared` with helper functions that are exactly the same between Apple and ICU
- Implemented `DateTimeFormatICU` based on `DateTimeFormatApple`
    - Used an ICU equivalent whenever NS was used for Apple

Pull Request resolved: #1362

Test Plan: Testing against `test262/test/intl402/DateTimeFormat`

Reviewed By: tmikov

Differential Revision: D55893825

Pulled By: neildhar

fbshipit-source-id: 03eef39721915ef03ebf6859980c374e10d337e9
  • Loading branch information
manuelpuyol authored and facebook-github-bot committed Apr 11, 2024
1 parent d00fe80 commit 042024c
Show file tree
Hide file tree
Showing 4 changed files with 1,538 additions and 56 deletions.
100 changes: 100 additions & 0 deletions include/hermes/Platform/Intl/PlatformIntlShared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#ifndef HERMES_PLATFORMINTL_PLATFORMINTLSHARED_H
#define HERMES_PLATFORMINTL_PLATFORMINTLSHARED_H

#ifdef HERMES_ENABLE_INTL
#include "hermes/Platform/Intl/PlatformIntl.h"

namespace hermes {
namespace platform_intl {

struct LocaleMatch {
std::u16string locale;
std::map<std::u16string, std::u16string> extensions;
};

struct ResolvedLocale {
std::u16string locale;
std::u16string dataLocale;
std::unordered_map<std::u16string, std::u16string> extensions;
};

/// https://402.ecma-international.org/8.0/#sec-canonicalizelocalelist
vm::CallResult<std::vector<std::u16string>> canonicalizeLocaleList(
vm::Runtime &runtime,
const std::vector<std::u16string> &locales);

/// https://402.ecma-international.org/8.0/#sec-intl.getcanonicallocales
vm::CallResult<std::vector<std::u16string>> getCanonicalLocales(
vm::Runtime &runtime,
const std::vector<std::u16string> &locales);

/// https://402.ecma-international.org/8.0/#sec-bestavailablelocale
std::optional<std::u16string> bestAvailableLocale(
const std::vector<std::u16string> &availableLocales,
const std::u16string &locale);

/// https://402.ecma-international.org/8.0/#sec-lookupsupportedlocales
std::vector<std::u16string> lookupSupportedLocales(
const std::vector<std::u16string> &availableLocales,
const std::vector<std::u16string> &requestedLocales);

/// https://402.ecma-international.org/8.0/#sec-supportedlocales
std::vector<std::u16string> supportedLocales(
const std::vector<std::u16string> &availableLocales,
const std::vector<std::u16string> &requestedLocales);

/// https://402.ecma-international.org/8.0/#sec-getoption
vm::CallResult<std::optional<std::u16string>> getOptionString(
vm::Runtime &runtime,
const Options &options,
const std::u16string &property,
llvh::ArrayRef<std::u16string_view> values,
std::optional<std::u16string_view> fallback);

/// https://402.ecma-international.org/8.0/#sec-getoption
std::optional<bool> getOptionBool(
vm::Runtime &runtime,
const Options &options,
const std::u16string &property,
std::optional<bool> fallback);

/// https://402.ecma-international.org/8.0/#sec-todatetimeoptions
vm::CallResult<Options> toDateTimeOptions(
vm::Runtime &runtime,
Options options,
std::u16string_view required,
std::u16string_view defaults);

/// https://402.ecma-international.org/8.0/#sec-case-sensitivity-and-case-mapping
std::u16string toASCIIUppercase(std::u16string_view tz);

/// https://402.ecma-international.org/8.0/#sec-defaultnumberoption
vm::CallResult<std::optional<uint8_t>> defaultNumberOption(
vm::Runtime &runtime,
const std::u16string &property,
std::optional<Option> value,
const std::uint8_t minimum,
const std::uint8_t maximum,
std::optional<uint8_t> fallback);

/// https://402.ecma-international.org/8.0/#sec-getoption
vm::CallResult<std::optional<uint8_t>> getNumberOption(
vm::Runtime &runtime,
const Options &options,
const std::u16string &property,
const std::uint8_t minimum,
const std::uint8_t maximum,
std::optional<uint8_t> fallback);
} // namespace platform_intl
} // namespace hermes

#endif

#endif
3 changes: 2 additions & 1 deletion lib/Platform/Intl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ if(HERMES_ENABLE_INTL)
set_target_properties(hermesPlatformIntl PROPERTIES UNITY_BUILD false)
target_compile_options(hermesPlatformIntl PRIVATE -fobjc-arc)
else()
add_hermes_library(hermesPlatformIntl STATIC PlatformIntlICU.cpp
add_hermes_library(hermesPlatformIntl STATIC PlatformIntlICU.cpp PlatformIntlShared.cpp
LINK_LIBS
hermesBCP47Parser
hermesPublic
)
hermes_link_icu(hermesPlatformIntl)
endif()
endif()

0 comments on commit 042024c

Please sign in to comment.