-
Notifications
You must be signed in to change notification settings - Fork 660
/
Copy pathPlatformIntlAndroid.cpp
567 lines (477 loc) · 17.2 KB
/
PlatformIntlAndroid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* 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"
// Android ICU uses different package names than ICU4J, and claims
// other differences. So for now, consider this impl specific to
// Android. It's likely it could be made to work against the
// non-Android ICU4J packages, too, if necessary, but it would take a
// bit more work.
#if HERMES_PLATFORM_INTL == HERMES_PLATFORM_INTL_ANDROID
#include <fbjni/fbjni.h>
using namespace ::facebook;
using namespace ::hermes;
namespace hermes {
namespace platform_intl {
namespace {
template <typename E = jobject>
struct JArrayList : jni::JavaClass<JArrayList<E>, jni::JList<E>> {
constexpr static auto kJavaDescriptor = "Ljava/util/ArrayList;";
using Super = jni::JavaClass<JArrayList<E>, jni::JList<E>>;
static jni::local_ref<JArrayList<E>> create() {
return Super::newInstance();
}
static jni::local_ref<JArrayList<E>> create(int initialCapacity) {
return Super::newInstance(initialCapacity);
}
bool add(jni::alias_ref<jobject> elem) {
static auto addMethod =
Super::javaClassStatic()
->template getMethod<jboolean(jni::alias_ref<jobject>)>("add");
return addMethod(Super::self(), elem);
}
};
template <typename K = jobject, typename V = jobject>
struct JHashMap : jni::JavaClass<JHashMap<K, V>, jni::JMap<K, V>> {
constexpr static auto kJavaDescriptor = "Ljava/util/HashMap;";
using Super = jni::JavaClass<JHashMap<K, V>, jni::JMap<K, V>>;
static jni::local_ref<JHashMap<K, V>> create() {
return Super::newInstance();
}
void put(jni::alias_ref<jobject> key, jni::alias_ref<jobject> val) {
static auto putMethod =
Super::javaClassStatic()
->template getMethod<jni::alias_ref<jobject>(
jni::alias_ref<jobject>, jni::alias_ref<jobject>)>("put");
putMethod(Super::self(), key, val);
}
};
using JLocalesList = jni::JList<jni::JString>;
using JOptionsMap = jni::JMap<jni::JString, jni::JObject>;
using JPartMap = jni::JMap<jni::JString, jni::JString>;
using JPartsList = jni::JList<JPartMap>;
jni::local_ref<jstring> stringToJava(std::u16string str) {
return jni::make_jstring(str);
}
jni::local_ref<JLocalesList> localesToJava(
std::vector<std::u16string> locales) {
jni::local_ref<JArrayList<jni::JString>> ret =
JArrayList<jni::JString>::create(locales.size());
for (const auto &locale : locales) {
ret->add(jni::make_jstring(locale));
}
return ret;
}
jni::local_ref<JOptionsMap> optionsToJava(const Options &options) {
auto ret = JHashMap<jni::JString, jni::JObject>::create();
for (const auto &kv : options) {
jni::local_ref<jni::JObject> jvalue;
if (kv.second.isBool()) {
jvalue = jni::autobox(static_cast<jboolean>(kv.second.getBool()));
} else if (kv.second.isNumber()) {
jvalue = jni::autobox(static_cast<jdouble>(kv.second.getNumber()));
} else {
assert(kv.second.isString() && "Option is not valid type");
jvalue = jni::make_jstring(kv.second.getString());
}
ret->put(jni::make_jstring(kv.first), jvalue);
}
return ret;
}
std::u16string stringFromJava(jni::alias_ref<jni::JString> result) {
return result->toU16String();
}
Options optionsFromJava(jni::alias_ref<JOptionsMap> result) {
if (!result) {
return Options();
}
Options ret;
for (const auto &kv : *result) {
if (!kv.first || !kv.second) {
// ignore nulls
continue;
}
if (jni::JBoolean::javaClassStatic()->isAssignableFrom(
kv.second->getClass())) {
ret.emplace(
stringFromJava(kv.first),
Option(static_cast<bool>(
jni::static_ref_cast<jni::JBoolean>(kv.second)->booleanValue())));
} else if (jni::JInteger::javaClassStatic()->isAssignableFrom(
kv.second->getClass())) {
ret.emplace(
stringFromJava(kv.first),
Option(static_cast<double>(
jni::static_ref_cast<jni::JInteger>(kv.second)->intValue())));
} else if (jni::JDouble::javaClassStatic()->isAssignableFrom(
kv.second->getClass())) {
ret.emplace(
stringFromJava(kv.first),
Option(jni::static_ref_cast<jni::JDouble>(kv.second)->doubleValue()));
} else if (jni::JString::javaClassStatic()->isAssignableFrom(
kv.second->getClass())) {
ret.emplace(
stringFromJava(kv.first),
Option(
stringFromJava(jni::static_ref_cast<jni::JString>(kv.second))));
} else {
// ignore mistyped value
}
}
return ret;
}
// Part: Map<String, String>
Part partFromJava(jni::alias_ref<JPartMap> result) {
if (!result) {
return Part();
}
Part ret;
for (const auto &kv : *result) {
if (!kv.first || !kv.second) {
// ignore nulls
continue;
}
ret.emplace(stringFromJava(kv.first), stringFromJava(kv.second));
}
return ret;
}
vm::CallResult<std::vector<std::u16string>> localesFromJava(
vm::Runtime *runtime,
vm::CallResult<jni::local_ref<JLocalesList>> &&result) {
if (LLVM_UNLIKELY(result == vm::ExecutionStatus::EXCEPTION)) {
return vm::ExecutionStatus::EXCEPTION;
}
std::vector<std::u16string> ret;
if (!*result) {
return std::vector<std::u16string>();
}
for (const auto &element : **result) {
ret.push_back(stringFromJava(element));
}
return ret;
}
std::vector<Part> partsFromJava(jni::local_ref<JPartsList> &&result) {
std::vector<Part> ret;
if (!result) {
return {};
}
for (const auto &element : *result) {
ret.push_back(partFromJava(element));
}
return ret;
}
class JIntl : public jni::JavaClass<JIntl> {
public:
static constexpr auto kJavaDescriptor = "Lcom/facebook/hermes/intl/Intl;";
static jni::local_ref<JLocalesList> getCanonicalLocales(
jni::alias_ref<JLocalesList> locales) {
static const auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<JLocalesList>(
jni::alias_ref<JLocalesList> locales)>("getCanonicalLocales");
return method(javaClassStatic(), locales);
}
static jni::local_ref<jstring> toLocaleLowerCase(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<jstring> str) {
auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<jstring>(
jni::alias_ref<JLocalesList> locales, jni::alias_ref<jstring>)>(
"toLocaleLowerCase");
return method(javaClassStatic(), locales, str);
}
static jni::local_ref<jstring> toLocaleUpperCase(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<jstring> str) {
static const auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<jstring>(
jni::alias_ref<JLocalesList> locales, jni::alias_ref<jstring>)>(
"toLocaleUpperCase");
return method(javaClassStatic(), locales, str);
}
};
} // namespace
vm::CallResult<std::vector<std::u16string>> getCanonicalLocales(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales) {
try {
return localesFromJava(
runtime, JIntl::getCanonicalLocales(localesToJava(locales)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
vm::CallResult<std::u16string> toLocaleLowerCase(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const std::u16string &str) {
try {
return stringFromJava(
JIntl::toLocaleLowerCase(localesToJava(locales), stringToJava(str)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
vm::CallResult<std::u16string> toLocaleUpperCase(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const std::u16string &str) {
try {
return stringFromJava(
JIntl::toLocaleUpperCase(localesToJava(locales), stringToJava(str)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
namespace {
class JCollator : public jni::JavaClass<JCollator> {
public:
static constexpr auto kJavaDescriptor = "Lcom/facebook/hermes/intl/Collator;";
static jni::local_ref<javaobject> create(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
return newInstance(locales, options);
}
static jni::local_ref<JLocalesList> supportedLocalesOf(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
static const auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<JLocalesList>(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options)>("supportedLocalesOf");
return method(javaClassStatic(), locales, options);
}
jni::local_ref<JOptionsMap> resolvedOptions() {
static const auto method =
javaClassStatic()->getMethod<jni::local_ref<JOptionsMap>()>(
"resolvedOptions");
return method(self());
}
double compare(jni::alias_ref<jstring> x, jni::alias_ref<jstring> y) {
static const auto method =
javaClassStatic()
->getMethod<double(
jni::alias_ref<jstring>, jni::alias_ref<jstring>)>("compare");
return method(self(), x, y);
}
};
} // namespace
struct Collator::Impl {
jni::global_ref<JCollator> jCollator_;
};
Collator::Collator() : impl_(std::make_unique<Impl>()) {}
Collator::~Collator() {
jni::ThreadScope::WithClassLoader([&] { impl_.reset(); });
}
vm::CallResult<std::vector<std::u16string>> Collator::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
return localesFromJava(
runtime,
JCollator::supportedLocalesOf(
localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
vm::ExecutionStatus Collator::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
impl_->jCollator_ = jni::make_global(
JCollator::create(localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
return vm::ExecutionStatus::RETURNED;
}
Options Collator::resolvedOptions() noexcept {
return optionsFromJava(impl_->jCollator_->resolvedOptions());
}
double Collator::compare(
const std::u16string &x,
const std::u16string &y) noexcept {
return impl_->jCollator_->compare(stringToJava(x), stringToJava(y));
}
namespace {
class JDateTimeFormat : public jni::JavaClass<JDateTimeFormat> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/facebook/hermes/intl/DateTimeFormat;";
static jni::local_ref<javaobject> create(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
return newInstance(locales, options);
}
static jni::local_ref<JLocalesList> supportedLocalesOf(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
static const auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<JLocalesList>(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options)>("supportedLocalesOf");
return method(javaClassStatic(), locales, options);
}
jni::local_ref<JOptionsMap> resolvedOptions() {
static const auto method =
javaClassStatic()->getMethod<jni::local_ref<JOptionsMap>()>(
"resolvedOptions");
return method(self());
}
jni::local_ref<jstring> format(double jsTimeValue) {
static const auto method =
javaClassStatic()->getMethod<jni::alias_ref<jstring>(double)>("format");
return method(self(), jsTimeValue);
}
jni::local_ref<JPartsList> formatToParts(double jsTimeValue) {
static const auto method =
javaClassStatic()->getMethod<jni::alias_ref<JPartsList>(double)>(
"formatToParts");
return method(self(), jsTimeValue);
}
};
} // namespace
struct DateTimeFormat::Impl {
jni::global_ref<JDateTimeFormat> jDateTimeFormat_;
};
DateTimeFormat::DateTimeFormat() : impl_(std::make_unique<Impl>()) {}
DateTimeFormat::~DateTimeFormat() {
jni::ThreadScope::WithClassLoader([&] { impl_.reset(); });
}
vm::CallResult<std::vector<std::u16string>> DateTimeFormat::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
return localesFromJava(
runtime,
JDateTimeFormat::supportedLocalesOf(
localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
vm::ExecutionStatus DateTimeFormat::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
impl_->jDateTimeFormat_ = jni::make_global(JDateTimeFormat::create(
localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
return vm::ExecutionStatus::RETURNED;
}
Options DateTimeFormat::resolvedOptions() noexcept {
return optionsFromJava(impl_->jDateTimeFormat_->resolvedOptions());
}
std::u16string DateTimeFormat::format(double jsTimeValue) noexcept {
// I don't believe the Java logic can throw an exception (the JS
// method can, but the errors all come from the Intl.cpp logic). If
// I am incorrect, this will need to add a try/catch and take a
// runtime to call raiseRangeError on it. This is true for all the
// format methods.
return stringFromJava(impl_->jDateTimeFormat_->format(jsTimeValue));
}
std::vector<Part> DateTimeFormat::formatToParts(double jsTimeValue) noexcept {
return partsFromJava(impl_->jDateTimeFormat_->formatToParts(jsTimeValue));
}
namespace {
class JNumberFormat : public jni::JavaClass<JNumberFormat> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/facebook/hermes/intl/NumberFormat;";
static jni::local_ref<javaobject> create(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
return newInstance(locales, options);
}
static jni::local_ref<JLocalesList> supportedLocalesOf(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options) {
static const auto method =
javaClassStatic()
->getStaticMethod<jni::local_ref<JLocalesList>(
jni::alias_ref<JLocalesList> locales,
jni::alias_ref<JOptionsMap> options)>("supportedLocalesOf");
return method(javaClassStatic(), locales, options);
}
jni::local_ref<JOptionsMap> resolvedOptions() {
static const auto method =
javaClassStatic()->getMethod<jni::local_ref<JOptionsMap>()>(
"resolvedOptions");
return method(self());
}
jni::local_ref<jstring> format(double jsTimeValue) {
static const auto method =
javaClassStatic()->getMethod<jni::alias_ref<jstring>(double)>("format");
return method(self(), jsTimeValue);
}
jni::local_ref<JPartsList> formatToParts(double jsTimeValue) {
static const auto method =
javaClassStatic()->getMethod<jni::alias_ref<JPartsList>(double)>(
"formatToParts");
return method(self(), jsTimeValue);
}
};
} // namespace
struct NumberFormat::Impl {
jni::global_ref<JNumberFormat> jNumberFormat_;
};
NumberFormat::NumberFormat() : impl_(std::make_unique<Impl>()) {}
NumberFormat::~NumberFormat() {
jni::ThreadScope::WithClassLoader([&] { impl_.reset(); });
}
vm::CallResult<std::vector<std::u16string>> NumberFormat::supportedLocalesOf(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
return localesFromJava(
runtime,
JNumberFormat::supportedLocalesOf(
localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
}
vm::ExecutionStatus NumberFormat::initialize(
vm::Runtime *runtime,
const std::vector<std::u16string> &locales,
const Options &options) noexcept {
try {
impl_->jNumberFormat_ = jni::make_global(
JNumberFormat::create(localesToJava(locales), optionsToJava(options)));
} catch (const std::exception &ex) {
return runtime->raiseRangeError(ex.what());
}
return vm::ExecutionStatus::RETURNED;
}
Options NumberFormat::resolvedOptions() noexcept {
return optionsFromJava(impl_->jNumberFormat_->resolvedOptions());
}
std::u16string NumberFormat::format(double number) noexcept {
// I don't believe the Java logic can throw an exception (the JS
// method can, but the errors all come from the Intl.cpp logic). If
// I am incorrect, this will need to add a try/catch and take a
// runtime to call raiseRangeError on it. This is true for all the
// format methods.
return stringFromJava(impl_->jNumberFormat_->format(number));
}
std::vector<Part> NumberFormat::formatToParts(double number) noexcept {
return partsFromJava(impl_->jNumberFormat_->formatToParts(number));
}
} // namespace platform_intl
} // namespace hermes
#endif