Skip to content

Commit

Permalink
Provide a dummy impl of the intl platform code
Browse files Browse the repository at this point in the history
Summary:
This mainly exists so we can test the Intl build locally.
The implementation is nothing even close to compliant, but it does
link.  Ship this and you will be fired.

Reviewed By: avp

Differential Revision: D21420118

fbshipit-source-id: 4c05fd43e40cffd0f0f641a901d752ab65b33139
  • Loading branch information
mhorowitz authored and facebook-github-bot committed May 19, 2020
1 parent b755487 commit 13d3b3f
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions lib/Platform/Intl/PlatformIntlDummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

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

#if HERMES_PLATFORM_INTL == HERMES_PLATFORM_INTL_DUMMY

#include <deque>
#include <string>
#include <unordered_map>

using namespace ::facebook;
using namespace ::hermes;

namespace hermes {
namespace platform_intl {

vm::CallResult<std::vector<std::u16string>> getCanonicalLocales(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales) {
return std::vector<std::u16string>{u"fr-FR", u"es-ES"};
}

vm::CallResult<std::u16string> toLocaleLowerCase(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const std::u16string &str) {
return std::u16string(u"lowered");
}
vm::CallResult<std::u16string> toLocaleUpperCase(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const std::u16string &str) {
return std::u16string(u"uppered");
}

struct Collator::Impl {
std::u16string locale;
};

Collator::Collator() : impl_(std::make_unique<Impl>()) {}
Collator::~Collator() {}

vm::CallResult<std::vector<std::u16string>> Collator::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
return std::vector<std::u16string>{u"en-CA", u"de-DE"};
}

vm::ExecutionStatus Collator::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
impl_->locale = u"en-US";
return vm::ExecutionStatus::RETURNED;
}

Options Collator::resolvedOptions() noexcept {
Options options;
options.emplace(u"locale", Option(impl_->locale));
options.emplace(u"numeric", Option(false));
return options;
}

double Collator::compare(
const std::u16string &x,
const std::u16string &y) noexcept {
return x.compare(y);
}

struct DateTimeFormat::Impl {
std::u16string locale;
};

DateTimeFormat::DateTimeFormat() : impl_(std::make_unique<Impl>()) {}
DateTimeFormat::~DateTimeFormat() {}

vm::CallResult<std::vector<std::u16string>> DateTimeFormat::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
return std::vector<std::u16string>{u"en-CA", u"de-DE"};
}

vm::ExecutionStatus DateTimeFormat::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
impl_->locale = u"en-US";
return vm::ExecutionStatus::RETURNED;
}

Options DateTimeFormat::resolvedOptions() noexcept {
Options options;
options.emplace(u"locale", Option(impl_->locale));
options.emplace(u"numeric", Option(false));
return options;
}

std::u16string DateTimeFormat::format(double jsTimeValue) noexcept {
auto s = std::to_string(jsTimeValue);
return std::u16string(s.begin(), s.end());
}

std::vector<std::unordered_map<std::u16string, std::u16string>>
DateTimeFormat::formatToParts(double jsTimeValue) noexcept {
std::unordered_map<std::u16string, std::u16string> part;
part[u"type"] = u"integer";
// This isn't right, but I didn't want to do more work for a stub.
std::string s = std::to_string(jsTimeValue);
part[u"value"] = {s.begin(), s.end()};
return std::vector<std::unordered_map<std::u16string, std::u16string>>{part};
}

struct NumberFormat::Impl {
std::u16string locale;
};

NumberFormat::NumberFormat() : impl_(std::make_unique<Impl>()) {}
NumberFormat::~NumberFormat() {}

vm::CallResult<std::vector<std::u16string>> NumberFormat::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
return std::vector<std::u16string>{u"en-CA", u"de-DE"};
}

vm::ExecutionStatus NumberFormat::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
impl_->locale = u"en-US";
return vm::ExecutionStatus::RETURNED;
}

Options NumberFormat::resolvedOptions() noexcept {
Options options;
options.emplace(u"locale", Option(impl_->locale));
options.emplace(u"numeric", Option(false));
return options;
}

std::u16string NumberFormat::format(double number) noexcept {
auto s = std::to_string(number);
return std::u16string(s.begin(), s.end());
}

std::vector<std::unordered_map<std::u16string, std::u16string>>
NumberFormat::formatToParts(double number) noexcept {
std::unordered_map<std::u16string, std::u16string> part;
part[u"type"] = u"integer";
// This isn't right, but I didn't want to do more work for a stub.
std::string s = std::to_string(number);
part[u"value"] = {s.begin(), s.end()};
return std::vector<std::unordered_map<std::u16string, std::u16string>>{part};
}

} // namespace platform_intl
} // namespace hermes

#endif

0 comments on commit 13d3b3f

Please sign in to comment.