Repository navigation
Expand file tree
/
Copy pathIntl.cpp
More file actions
1708 lines (1484 loc) · 54.8 KB
/
Copy pathIntl.cpp
File metadata and controls
1708 lines (1484 loc) · 54.8 KB
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
#include "JSLibInternal.h"
#include "hermes/Platform/Intl/PlatformIntl.h"
#ifdef HERMES_ENABLE_INTL
#include "hermes/VM/ArrayLike.h"
#include "hermes/VM/JSLib/DateUtil.h"
#include "hermes/VM/PrimitiveBox.h"
#include "hermes/VM/Runtime.h"
#include "hermes/VM/StackFrame-inline.h"
#include "hermes/VM/StringView.h"
namespace hermes {
// TODO T65916424: Move this out of the hermes::vm namespace.
namespace vm {
// This implementation tries to avoid errors in platform code causing
// crashes or throwing JS exceptions. However, it can still result in
// non-compliant behavior. For example, resolvedOptions methods are
// required to have a particular set of properties, but if the
// platform result doesn't include them, then they will simply not be
// present. This could manifest as a JS error later if one of the
// properties is referenced.
namespace {
CallResult<std::u16string> stringFromJS(
Runtime &runtime,
PseudoHandle<> value) {
CallResult<PseudoHandle<StringPrimitive>> strRes =
toString_RJS(runtime, runtime.makeHandle(std::move(value)));
if (LLVM_UNLIKELY(strRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto view = vm::StringPrimitive::createStringView(
runtime, runtime.makeHandle(std::move(*strRes)));
return std::u16string(view.begin(), view.end());
}
CallResult<HermesValue> localesToJS(
Runtime &runtime,
CallResult<std::vector<std::u16string>> result) {
if (LLVM_UNLIKELY(result == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<Handle<JSArray>> arrayRes =
JSArray::create(runtime, result->size(), result->size());
if (LLVM_UNLIKELY(arrayRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSArray> array = *arrayRes;
MutableHandle<> name{runtime};
uint64_t index = 0;
GCScopeMarkerRAII marker{runtime};
for (auto &locale : *result) {
marker.flush();
CallResult<HermesValue> nameRes =
StringPrimitive::createEfficient(runtime, std::move(locale));
if (LLVM_UNLIKELY(nameRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
name = *nameRes;
JSArray::setElementAt(array, runtime, index++, name);
}
return array.getHermesValue();
}
CallResult<HermesValue> optionsToJS(
Runtime &runtime,
platform_intl::Options result) {
CallResult<PseudoHandle<JSObject>> objRes = JSObject::create(runtime);
if (LLVM_UNLIKELY(objRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> obj = runtime.makeHandle(std::move(*objRes));
MutableHandle<> key{runtime};
MutableHandle<> value{runtime};
GCScopeMarkerRAII marker{runtime};
for (auto &kv : result) {
marker.flush();
CallResult<HermesValue> keyRes = StringPrimitive::createEfficient(
runtime, createUTF16Ref(kv.first.c_str()));
if (LLVM_UNLIKELY(keyRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
key = *keyRes;
if (kv.second.isBool()) {
value = HermesValue::encodeBoolValue(kv.second.getBool());
} else if (kv.second.isNumber()) {
value = HermesValue::encodeUntrustedNumberValue(kv.second.getNumber());
} else {
assert(kv.second.isString() && "Option is neither bool nor string");
CallResult<HermesValue> strRes = StringPrimitive::createEfficient(
runtime, std::move(kv.second.getString()));
if (LLVM_UNLIKELY(strRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
value = *strRes;
}
auto putRes = JSObject::putComputed_RJS(obj, runtime, key, value);
if (LLVM_UNLIKELY(putRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
assert(*putRes && "put returned false on a plain object");
}
return obj.getHermesValue();
}
CallResult<Handle<JSObject>> partToJS(
Runtime &runtime,
std::unordered_map<std::u16string, std::u16string> result) {
CallResult<PseudoHandle<JSObject>> objRes = JSObject::create(runtime);
if (LLVM_UNLIKELY(objRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> obj = runtime.makeHandle(std::move(*objRes));
MutableHandle<> key{runtime};
MutableHandle<> value{runtime};
GCScopeMarkerRAII marker{runtime};
for (auto &kv : result) {
marker.flush();
CallResult<HermesValue> keyRes = StringPrimitive::createEfficient(
runtime, createUTF16Ref(kv.first.c_str()));
if (LLVM_UNLIKELY(keyRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
key = *keyRes;
CallResult<HermesValue> valueRes =
StringPrimitive::createEfficient(runtime, std::move(kv.second));
if (LLVM_UNLIKELY(valueRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
value = *valueRes;
auto putRes = JSObject::putComputed_RJS(obj, runtime, key, value);
if (LLVM_UNLIKELY(putRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
assert(*putRes && "put returned false on a plain object");
}
return obj;
}
CallResult<HermesValue> partsToJS(
Runtime &runtime,
CallResult<std::vector<std::unordered_map<std::u16string, std::u16string>>>
result) {
if (LLVM_UNLIKELY(result == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<Handle<JSArray>> arrayRes =
JSArray::create(runtime, result->size(), result->size());
if (LLVM_UNLIKELY(arrayRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSArray> array = *arrayRes;
uint64_t index = 0;
GCScopeMarkerRAII marker{runtime};
for (auto &part : *result) {
marker.flush();
CallResult<Handle<JSObject>> partRes = partToJS(runtime, std::move(part));
if (LLVM_UNLIKELY(partRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
JSArray::setElementAt(array, runtime, index++, *partRes);
}
return array.getHermesValue();
}
CallResult<std::vector<std::u16string>> normalizeLocales(
Runtime &runtime,
Handle<> locales) {
std::vector<std::u16string> ret;
if (locales->isUndefined()) {
return ret;
}
if (locales->isString()) {
auto view = StringPrimitive::createStringView(
runtime, Handle<StringPrimitive>::vmcast(locales));
ret.emplace_back(view.begin(), view.end());
return ret;
}
CallResult<HermesValue> objRes = toObject(runtime, locales);
if (LLVM_UNLIKELY(objRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto localeObj = runtime.makeHandle(vmcast<JSObject>(*objRes));
CallResult<uint64_t> lengthRes = getArrayLikeLength_RJS(localeObj, runtime);
if (LLVM_UNLIKELY(lengthRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
bool isProxy = localeObj->isProxyObject();
if (LLVM_UNLIKELY(
createListFromArrayLike_RJS(
loca
8BD0
leObj,
runtime,
*lengthRes,
[&ret, isProxy](
Runtime &runtime, uint64_t index, PseudoHandle<> value) {
// When the locales list is a proxy object, gaps are allowed.
if (isProxy && value->isUndefined())
return ExecutionStatus::RETURNED;
if (!value->isString() && !value->isObject()) {
return runtime.raiseTypeError("Incorrect object type");
}
CallResult<std::u16string> strRes =
stringFromJS(runtime, std::move(value));
if (LLVM_UNLIKELY(strRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
ret.emplace_back(*strRes);
return ExecutionStatus::RETURNED;
}) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return std::move(ret);
}
constexpr int kDateRequired = 1 << 0;
constexpr int kTimeRequired = 1 << 1;
constexpr int kDateDefault = 1 << 2;
constexpr int kTimeDefault = 1 << 3;
// To normalize options, we need to know the valid options names, and
// the expected type of each.
struct OptionData {
std::u16string_view name;
platform_intl::Option::Kind kind;
int flags;
};
constexpr OptionData kSupportedLocalesOfOptions[] = {
{u"localeMatcher", platform_intl::Option::Kind::String, 0}};
constexpr OptionData kCollatorOptions[] = {
{u"usage", platform_intl::Option::Kind::String, 0},
{u"localeMatcher", platform_intl::Option::Kind::String, 0},
{u"numeric", platform_intl::Option::Kind::Bool, 0},
{u"caseFirst", platform_intl::Option::Kind::String, 0},
{u"sensitivity", platform_intl::Option::Kind::String, 0},
{u"ignorePunctuation", platform_intl::Option::Kind::Bool, 0},
{u"collation", platform_intl::Option::Kind::String, 0},
};
constexpr OptionData kDTFOptions[] = {
{u"localeMatcher", platform_intl::Option::Kind::String, 0},
{u"calendar", platform_intl::Option::Kind::String, 0},
{u"numberingSystem", platform_intl::Option::Kind::String, 0},
{u"hour12", platform_intl::Option::Kind::Bool, 0},
{u"hourCycle", platform_intl::Option::Kind::String, 0},
{u"timeZone", platform_intl::Option::Kind::String, 0},
{u"formatMatcher", platform_intl::Option::Kind::String, 0},
{u"weekday", platform_intl::Option::Kind::String, 0},
{u"era", platform_intl::Option::Kind::String, 0},
{u"year",
platform_intl::Option::Kind::String,
kDateRequired | kDateDefault},
{u"month",
platform_intl::Option::Kind::String,
kDateRequired | kDateDefault},
{u"dayPeriod", platform_intl::Option::Kind::String, 0},
{u"day", platform_intl::Option::Kind::String, kDateRequired | kDateDefault},
{u"hour",
platform_intl::Option::Kind::String,
kTimeRequired | kTimeDefault},
{u"minute",
platform_intl::Option::Kind::String,
kTimeRequired | kTimeDefault},
{u"second",
platform_intl::Option::Kind::String,
kTimeRequired | kTimeDefault},
{u"timeZoneName", platform_intl::Option::Kind::String, 0},
{u"dateStyle", platform_intl::Option::Kind::String, 0},
{u"timeStyle", platform_intl::Option::Kind::String, 0},
{u"fractionalSecondDigits", platform_intl::Option::Kind::Number, 0},
};
constexpr OptionData kNumberFormatOptions[] = {
{u"localeMatcher", platform_intl::Option::Kind::String, 0},
{u"numberingSystem", platform_intl::Option::Kind::String, 0},
{u"style", platform_intl::Option::Kind::String, 0},
{u"currency", platform_intl::Option::Kind::String, 0},
{u"currencyDisplay", platform_intl::Option::Kind::String, 0},
{u"currencySign", platform_intl::Option::Kind::String, 0},
{u"unit", platform_intl::Option::Kind::String, 0},
{u"unitDisplay", platform_intl::Option::Kind::String, 0},
{u"notation", platform_intl::Option::Kind::String, 0},
{u"minimumIntegerDigits", platform_intl::Option::Kind::Number, 0},
{u"minimumFractionDigits", platform_intl::Option::Kind::Number, 0},
{u"maximumFractionDigits", platform_intl::Option::Kind::Number, 0},
{u"minimumSignificantDigits", platform_intl::Option::Kind::Number, 0},
{u"maximumSignificantDigits", platform_intl::Option::Kind::Number, 0},
{u"compactDisplay", platform_intl::Option::Kind::String, 0},
{u"useGrouping", platform_intl::Option::Kind::Bool, 0},
{u"signDisplay", platform_intl::Option::Kind::String, 0},
};
CallResult<platform_intl::Options> normalizeOptions(
Runtime &runtime,
Handle<> options,
llvh::ArrayRef<OptionData> optionData) {
platform_intl::Options ret;
if (options->isNull())
return runtime.raiseTypeError("Options object can't be null !");
auto optionsObj = Handle<JSObject>::dyn_vmcast(options);
if (!optionsObj) {
return ret;
}
MutableHandle<> name{runtime};
MutableHandle<> value{runtime};
MutableHandle<StringPrimitive> strValue{runtime};
GCScopeMarkerRAII marker{runtime};
for (const OptionData &pod : optionData) {
marker.flush();
CallResult<HermesValue> nameRes = StringPrimitive::createEfficient(
runtime, UTF16Ref{pod.name.data(), pod.name.size()});
if (LLVM_UNLIKELY(nameRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
name = *nameRes;
CallResult<PseudoHandle<>> valRes =
JSObject::getComputed_RJS(optionsObj, runtime, name);
if (LLVM_UNLIKELY(valRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
if ((*valRes)->isUndefined()) {
continue;
}
if (pod.kind == platform_intl::Option::Kind::Bool) {
ret.emplace(
pod.name, platform_intl::Option(toBoolean(valRes->getHermesValue())));
} else if (pod.kind == platform_intl::Option::Kind::Number) {
value = std::move(*valRes);
CallResult<HermesValue> numRes = toNumber_RJS(runtime, value);
if (LLVM_UNLIKELY(numRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
ret.emplace(pod.name, platform_intl::Option(numRes->getNumber()));
} else {
assert(
pod.kind == platform_intl::Option::Kind::String &&
"Unknown option kind");
value = std::move(*valRes);
CallResult<PseudoHandle<StringPrimitive>> strRes =
toString_RJS(runtime, value);
if (LLVM_UNLIKELY(strRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
strValue = std::move(*strRes);
auto view = StringPrimitive::createStringView(runtime, strValue);
ret.emplace(
pod.name,
platform_intl::Option(std::u16string(view.begin(), view.end())));
}
}
return std::move(ret);
}
template <typename T>
ExecutionStatus checkOptions(Runtime &runtime, const platform_intl::Options &) {
return ExecutionStatus::RETURNED;
}
template <>
ExecutionStatus checkOptions<platform_intl::DateTimeFormat>(
Runtime &runtime,
const platform_intl::Options &options) {
// ECMA 402 2023 11.1.2
const bool hasExplicitFormatComponents = options.count(u"weekday") > 0 ||
options.count(u"era") > 0 || options.count(u"year") > 0 ||
options.count(u"month") > 0 || options.count(u"day") > 0 ||
options.count(u"dayPeriod") > 0 || options.count(u"hour") > 0 ||
options.count(u"minute") > 0 || options.count(u"second") > 0 ||
options.count(u"fractionalSecondDigits") > 0 ||
options.count(u"timeZoneName") > 0;
const bool hasStyle =
options.count(u"dateStyle") > 0 || options.count(u"timeStyle") > 0;
// 42. If dateStyle is not undefined or timeStyle is not undefined, then
// a. If hasExplicitFormatComponents is true, then
// i. Throw a TypeError exception.
if (hasStyle && hasExplicitFormatComponents) {
return runtime.raiseTypeError(
"{data/time}Style and explicit format components shouldn't be used together");
}
return ExecutionStatus::RETURNED;
}
template <>
ExecutionStatus checkOptions<platform_intl::NumberFormat>(
Runtime &runtime,
const platform_intl::Options &options) {
// As best as I can tell, there are only two places (in
// SetNumberFormatUnitOptions) where option value handling can throw
// a TypeError, and there is no other place where the platform code
// needs a TypeError. So, rather than have to deal with multiple
// Java error classes, we just handle those two cases right here.
auto styleIt = options.find(u"style");
if (styleIt == options.end()) {
return ExecutionStatus::RETURNED;
}
if (styleIt->second.isString() &&
styleIt->second.getString() == u"currency" &&
options.count(u"currency") == 0) {
return runtime.raiseTypeError(
"Style 'currency' requires option 'currency'");
}
if (styleIt->second.isString() && styleIt->second.getString() == u"unit" &&
options.count(u"unit") == 0) {
return runtime.raiseTypeError("Style 'unit' requires option 'unit'");
}
return ExecutionStatus::RETURNED;
}
// T here is one of the platform_intl types. This exists to avoid
// duplicating code, since all the ctors are basically the same.
template <typename T>
CallResult<HermesValue> intlServiceConstructor(
Runtime &runtime,
NativeArgs args,
llvh::ArrayRef<OptionData> optionData,
Handle<JSObject> servicePrototype,
unsigned int additionalSlots) {
CallResult<std::vector<std::u16string>> localesRes =
normalizeLocales(runtime, args.getArgHandle(0));
if (LLVM_UNLIKELY(localesRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<platform_intl::Options> optionsRes =
normalizeOptions(runtime, args.getArgHandle(1), optionData);
if (LLVM_UNLIKELY(optionsRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
// Service specific checks.
if (LLVM_UNLIKELY(
checkOptions<T>(runtime, *optionsRes) ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<std::unique_ptr<T>> nativeRes =
T::create(runtime, *localesRes, *optionsRes);
if (LLVM_UNLIKELY(nativeRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
std::unique_ptr<T> native = std::move(*nativeRes);
auto typeHandle = runtime.makeHandle(
HermesValue::encodeUntrustedNumberValue((uint32_t)T::getNativeType()));
auto setType = [&](Handle<DecoratedObject> obj) {
auto res = JSObject::defineNewOwnProperty(
obj,
runtime,
Predefined::getSymbolID(Predefined::InternalPropertyIntlNativeType),
PropertyFlags::defaultNewNamedPropertyFlags(),
typeHandle);
(void)res;
assert(res != ExecutionStatus::EXCEPTION && "Setting type cannot fail.");
};
// If constructor, use the allocated object
if (args.isConstructorCall()) {
Handle<DecoratedObject> selfHandle = args.vmcastThis<DecoratedObject>();
selfHandle->setDecoration(std::move(native));
setType(selfHandle);
return HermesValue::encodeUndefinedValue();
}
// Otherwise allocate a new one.
auto newHandle = runtime.makeHandle(DecoratedObject::create(
runtime, servicePrototype, std::move(native), additionalSlots));
setType(newHandle);
return newHandle.getHermesValue();
}
// T is a class from platform_intl which has an appropriate
// supportedLocalesOf method.
template <typename T>
CallResult<HermesValue> intlServiceSupportedLocalesOf(
Runtime &runtime,
NativeArgs args) {
CallResult<std::vector<std::u16string>> localesRes =
normalizeLocales(runtime, args.getArgHandle(0));
if (LLVM_UNLIKELY(localesRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<platform_intl::Options> optionsRes = normalizeOptions(
runtime, args.getArgHandle(1), kSupportedLocalesOfOptions);
if (LLVM_UNLIKELY(optionsRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return localesToJS(
runtime, T::supportedLocalesOf(runtime, *localesRes, *optionsRes));
}
// This checks that the handle is valid (the caller may have used
// dyn_vmcast to make it), and that the native decoration on the
// handle corresponds to the expected class.
template <typename T>
CallResult<T *> verifyDecoration(
Runtime &runtime,
Handle<DecoratedObject> handle,
const char *what) {
if (!handle) {
return runtime.raiseTypeError(
TwineChar16(what) + " called with incompatible 'this'");
}
NamedPropertyDescriptor desc;
bool exists = JSObject::getOwnNamedDescriptor(
handle,
runtime,
Predefined::getSymbolID(Predefined::InternalPropertyIntlNativeType),
desc);
if (!exists) {
return runtime.raiseTypeError(
TwineChar16(what) + " called with incompatible 'this'");
}
auto val = JSObject::getNamedSlotValueUnsafe(*handle, runtime, desc);
if (val.getNumber(runtime) != (uint32_t)T::getNativeType()) {
return runtime.raiseTypeError(
TwineChar16(what) + " called with incompatible 'this'");
}
return static_cast<T *>(handle->getDecoration());
}
} // namespace
namespace { // Collator impl stuff.
// Collator internal slots.
enum class CollatorSlotIndexes { boundCompare, COUNT };
PseudoHandle<NativeFunction> getBoundCompare(
DecoratedObject *collator,
Runtime &runtime) {
return createPseudoHandle(dyn_vmcast<NativeFunction>(
DecoratedObject::getAdditionalSlotValue(
collator,
runtime,
static_cast<unsigned int>(CollatorSlotIndexes::boundCompare))
.unboxToHV(runtime)));
}
void setBoundCompare(
DecoratedObject *collator,
Runtime &runtime,
PseudoHandle<NativeFunction> value) {
DecoratedObject::setAdditionalSlotValue(
collator,
runtime,
static_cast<unsigned int>(CollatorSlotIndexes::boundCompare),
SmallHermesValue::encodeObjectValue(value.get(), runtime));
}
// Collator compare internal slots
enum class CollatorCompareSlotIndexes { collator, COUNT };
PseudoHandle<DecoratedObject> getCollator(
PseudoHandle<NativeFunction> compare,
Runtime &runtime) {
return createPseudoHandle(vmcast<DecoratedObject>(
NativeFunction::getAdditionalSlotValue(
compare.get(),
runtime,
static_cast<unsigned int>(CollatorCompareSlotIndexes::collator))
.getObject(runtime)));
}
void setCollator(
PseudoHandle<NativeFunction> compare,
Runtime &runtime,
PseudoHandle<DecoratedObject> value) {
NativeFunction::setAdditionalSlotValue(
compare.get(),
runtime,
static_cast<unsigned int>(CollatorCompareSlotIndexes::collator),
SmallHermesValue::encodeObjectValue(value.get(), runtime));
}
CallResult<PseudoHandle<JSObject>>
intlCollatorCreator(Runtime &runtime, Handle<JSObject> parentHandle, void *) {
return createPseudoHandle<JSObject>(
DecoratedObject::create(
runtime,
parentHandle,
nullptr,
static_cast<unsigned int>(CollatorSlotIndexes::COUNT))
.get());
}
void defineIntlCollator(Runtime &runtime, Handle<JSObject> intl) {
// Create %CollatorPrototype% intrinsic. Properties will be added later.
Handle<JSObject> prototype = runtime.makeHandle(JSObject::create(runtime));
runtime.intlCollatorPrototype = prototype.getHermesValue();
// Create %Collator% intrinsic.
Handle<NativeConstructor> constructor = defineSystemConstructor(
runtime,
Predefined::getSymbolID(Predefined::Collator),
intlCollatorConstructor,
prototype,
0,
intlCollatorCreator,
CellKind::DecoratedObjectKind);
runtime.intlCollator = constructor.getHermesValue();
{
DefinePropertyFlags dpf{};
dpf.setValue = 1;
defineProperty(
runtime,
constructor,
Predefined::getSymbolID(Predefined::prototype),
prototype,
dpf);
}
defineMethod(
runtime,
constructor,
Predefined::getSymbolID(Predefined::supportedLocalesOf),
nullptr,
intlCollatorSupportedLocalesOf,
1);
// Add properties to prototype.
defineProperty(
runtime,
prototype,
Predefined::getSymbolID(Predefined::constructor),
constructor);
{
auto dpf = DefinePropertyFlags::getDefaultNewPropertyFlags();
dpf.writable = 0;
dpf.enumerable = 0;
defineProperty(
runtime,
prototype,
Predefined::getSymbolID(Predefined::SymbolToStringTag),
runtime.getPredefinedStringHandle(Predefined::IntlCollator),
dpf);
}
defineAccessor(
runtime,
prototype,
Predefined::getSymbolID(Predefined::compare),
nullptr,
intlCollatorPrototypeCompareGetter,
nullptr,
false,
true);
defineMethod(
runtime,
prototype,
Predefined::getSymbolID(Predefined::resolvedOptions),
nullptr,
intlCollatorPrototypeResolvedOptions,
0);
// Add Collator to Intl
defineProperty(
runtime,
intl,
Predefined::getSymbolID(Predefined::Collator),
constructor);
}
} // namespace
CallResult<HermesValue>
intlCollatorConstructor(void *, Runtime &runtime, NativeArgs args) {
return intlServiceConstructor<platform_intl::Collator>(
runtime,
args,
kCollatorOptions,
Handle<JSObject>::vmcast(&runtime.intlCollatorPrototype),
static_cast<unsigned int>(CollatorSlotIndexes::COUNT));
}
CallResult<HermesValue>
intlCollatorSupportedLocalesOf(void *, Runtime &runtime, NativeArgs args) {
return intlServiceSupportedLocalesOf<platform_intl::Collator>(runtime, args);
}
CallResult<HermesValue>
intlCollatorCompare(void *, Runtime &runtime, NativeArgs args) {
auto *nf = vmcast<NativeFunction>(
runtime.getCurrentFrame()->getCalleeClosureUnsafe());
PseudoHandle<DecoratedObject> collatorHandle =
getCollator(createPseudoHandle(nf), runtime);
// Since collatorHandle came out of an internal slot, it's an
// assertable failure if it has the wrong type.
platform_intl::Collator *collator =
static_cast<platform_intl::Collator *>(collatorHandle->getDecoration());
assert(collator && "Intl.Collator platform part is nullptr");
CallResult<std::u16string> xRes =
stringFromJS(runtime, createPseudoHandle(args.getArg(0)));
if (LLVM_UNLIKELY(xRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
CallResult<std::u16string> yRes =
stringFromJS(runtime, createPseudoHandle(args.getArg(1)));
if (LLVM_UNLIKELY(yRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return HermesValue::encodeUntrustedNumberValue(
collator->compare(*xRes, *yRes));
}
CallResult<HermesValue>
intlCollatorPrototypeCompareGetter(void *, Runtime &runtime, NativeArgs args) {
Handle<DecoratedObject> collatorHandle = args.dyncastThis<DecoratedObject>();
CallResult<platform_intl::Collator *> collatorRes =
verifyDecoration<platform_intl::Collator>(
runtime, collatorHandle, "Intl.Collator.prototype.compare getter");
if (LLVM_UNLIKELY(collatorRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
PseudoHandle<NativeFunction> boundCompare =
getBoundCompare(*collatorHandle, runtime);
if (boundCompare) {
return boundCompare.getHermesValue();
}
Handle<NativeFunction> compare = NativeFunction::createWithoutPrototype(
runtime,
nullptr,
intlCollatorCompare,
Predefined::getSymbolID(Predefined::emptyString),
2,
static_cast<unsigned int>(CollatorCompareSlotIndexes::COUNT));
setCollator(compare, runtime, collatorHandle);
setBoundCompare(*collatorHandle, runtime, compare);
return compare.getHermesValue();
}
CallResult<HermesValue> intlCollatorPrototypeResolvedOptions(
void *,
Runtime &runtime,
NativeArgs args) {
Handle<DecoratedObject> collatorHandle = args.dyncastThis<DecoratedObject>();
CallResult<platform_intl::Collator *> collatorRes =
verifyDecoration<platform_intl::Collator>(
runtime, collatorHandle, "Intl.Collator.prototype.resolvedOptions");
if (LLVM_UNLIKELY(collatorRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return optionsToJS(runtime, (*collatorRes)->resolvedOptions());
}
namespace { // DateTimeFormat impl stuff.
// DateTimeFormat internal slots.
enum class DTFSlotIndexes { boundFormat, COUNT };
PseudoHandle<NativeFunction> getDTFBoundFormat(
PseudoHandle<DecoratedObject> dtf,
Runtime &runtime) {
return createPseudoHandle(dyn_vmcast<NativeFunction>(
DecoratedObject::getAdditionalSlotValue(
dtf.get(),
runtime,
static_cast<unsigned int>(DTFSlotIndexes::boundFormat))
.unboxToHV(runtime)));
}
void setDTFBoundFormat(
PseudoHandle<DecoratedObject> dtf,
Runtime &runtime,
PseudoHandle<NativeFunction> value) {
DecoratedObject::setAdditionalSlotValue(
dtf.get(),
runtime,
static_cast<unsigned int>(DTFSlotIndexes::boundFormat),
SmallHermesValue::encodeObjectValue(value.get(), runtime));
}
// DateTimeFormat format internal slots
enum class DTFFormatSlotIndexes { dateTimeFormat, COUNT };
PseudoHandle<DecoratedObject> getDateTimeFormat(
PseudoHandle<NativeFunction> format,
Runtime &runtime) {
return createPseudoHandle(vmcast<DecoratedObject>(
NativeFunction::getAdditionalSlotValue(
format.get(),
runtime,
static_cast<unsigned int>(DTFFormatSlotIndexes::dateTimeFormat))
.getObject(runtime)));
}
void setDateTimeFormat(
PseudoHandle<NativeFunction> format,
Runtime &runtime,
PseudoHandle<DecoratedObject> value) {
NativeFunction::setAdditionalSlotValue(
format.get(),
runtime,
static_cast<unsigned int>(DTFFormatSlotIndexes::dateTimeFormat),
SmallHermesValue::encodeObjectValue(value.get(), runtime));
}
CallResult<PseudoHandle<JSObject>> intlDateTimeFormatCreator(
Runtime &runtime,
Handle<JSObject> parentHandle,
void *) {
return createPseudoHandle<JSObject>(
DecoratedObject::create(
runtime,
parentHandle,
nullptr,
static_cast<unsigned int>(DTFSlotIndexes::COUNT))
.get());
}
void defineIntlDateTimeFormat(Runtime &runtime, Handle<JSObject> intl) {
// Create %DateTimeFormatPrototype% intrinsic. Properties will be added
// later.
Handle<JSObject> prototype = runtime.makeHandle(
8
JSObject
B056
::create(runtime));
runtime.intlDateTimeFormatPrototype = prototype.getHermesValue();
// Create %DateTimeFormat% intrinsic.
Handle<NativeConstructor> constructor = defineSystemConstructor(
runtime,
Predefined::getSymbolID(Predefined::DateTimeFormat),
intlDateTimeFormatConstructor,
prototype,
0,
intlDateTimeFormatCreator,
CellKind::DecoratedObjectKind);
runtime.intlDateTimeFormat = constructor.getHermesValue();
{
DefinePropertyFlags dpf{};
dpf.setValue = 1;
defineProperty(
runtime,
constructor,
Predefined::getSymbolID(Predefined::prototype),
prototype,
dpf);
}
defineMethod(
runtime,
constructor,
Predefined::getSymbolID(Predefined::supportedLocalesOf),
nullptr,
intlDateTimeFormatSupportedLocalesOf,
1);
// Add properties to prototype.
defineProperty(
runtime,
prototype,
Predefined::getSymbolID(Predefined::constructor),
constructor);
{
auto dpf = DefinePropertyFlags::getDefaultNewPropertyFlags();
dpf.writable = 0;
dpf.enumerable = 0;
defineProperty(
runtime,
prototype,
Predefined::getSymbolID(Predefined::SymbolToStringTag),
runtime.getPredefinedStringHandle(Predefined::IntlDateTimeFormat),
dpf);
}
defineAccessor(
runtime,
prototype,
Predefined::getSymbolID(Predefined::format),
nullptr,
intlDateTimeFormatPrototypeFormatGetter,
nullptr,
false,
true);
defineMethod(
runtime,
prototype,
Predefined::getSymbolID(Predefined::formatToParts),
nullptr,
intlDateTimeFormatPrototypeFormatToParts,
1);
defineMethod(
runtime,
prototype,
Predefined::getSymbolID(Predefined::resolvedOptions),
nullptr,
intlDateTimeFormatPrototypeResolvedOptions,
0);
// Add DateTimeFormat to Intl
defineProperty(
runtime,
intl,
Predefined::getSymbolID(Predefined::DateTimeFormat),
constructor);
}
} // namespace
CallResult<HermesValue>
intlDateTimeFormatConstructor(void *, Runtime &runtime, NativeArgs args) {
return intlServiceConstructor<platform_intl::DateTimeFormat>(
runtime,
args,
kDTFOptions,
Handle<JSObject>::vmcast(&runtime.intlDateTimeFormatPrototype),
static_cast<unsigned int>(DTFSlotIndexes::COUNT));
}
CallResult<HermesValue> intlDateTimeFormatSupportedLocalesOf(
void *,
Runtime &runtime,
NativeArgs args) {
return intlServiceSupportedLocalesOf<platform_intl::DateTimeFormat>(
runtime, args);
}
namespace {
CallResult<double> dateNowValue(Runtime &runtime, NativeArgs args) {
// ECMA 402 13.1.5:
// 1 and 2 are implicit in the callers.
// 3. If date is not provided or is undefined, then
if (args.getArg(0).isUndefined()) {
// a. Let x be Call(%Date_now%, undefined).
// Reusing NativeArgs is kind of a hack, but dateNow doesn't use
// it (or the context arg), so this is ok.
CallResult<HermesValue> dateRes = dateNow(nullptr, runtime, args);
if (LLVM_UNLIKELY(dateRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return dateRes->getNumber();
}
// 4.a. Let x be ? ToNumber(date).
CallResult<HermesValue> xRes = toNumber_RJS(runtime, args.getArgHandle(0));
if (LLVM_UNLIKELY(xRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
// ECMA 402 13.1.6, shared by the callers to this function: