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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "builtin/temporal/TemporalFields.h"
#include "mozilla/Assertions.h"
#include "mozilla/Likely.h"
#include "mozilla/Maybe.h"
#include "mozilla/Range.h"
#include "mozilla/RangedPtr.h"
#include <algorithm>
#include <cstring>
#include <iterator>
#include <stdint.h>
#include <utility>
#include "jsnum.h"
#include "jspubtd.h"
#include "NamespaceImports.h"
#include "builtin/temporal/Temporal.h"
#include "ds/Sort.h"
#include "gc/Barrier.h"
#include "gc/Tracer.h"
#include "js/AllocPolicy.h"
#include "js/ComparisonOperators.h"
#include "js/ErrorReport.h"
#include "js/friend/ErrorMessages.h"
#include "js/GCVector.h"
#include "js/Id.h"
#include "js/Printer.h"
#include "js/RootingAPI.h"
#include "js/TracingAPI.h"
#include "js/TypeDecls.h"
#include "js/Utility.h"
#include "js/Value.h"
#include "util/Text.h"
#include "vm/BytecodeUtil.h"
#include "vm/JSAtomState.h"
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/PlainObject.h"
#include "vm/StringType.h"
#include "vm/SymbolType.h"
#include "vm/JSAtomUtils-inl.h"
#include "vm/ObjectOperations-inl.h"
using namespace js;
using namespace js::temporal;
void TemporalFields::trace(JSTracer* trc) {
TraceNullableRoot(trc, &monthCode, "TemporalFields::monthCode");
TraceNullableRoot(trc, &offset, "TemporalFields::offset");
TraceNullableRoot(trc, &era, "TemporalFields::era");
TraceRoot(trc, &timeZone, "TemporalFields::timeZone");
}
static PropertyName* ToPropertyName(JSContext* cx, TemporalField field) {
switch (field) {
case TemporalField::Year:
return cx->names().year;
case TemporalField::Month:
return cx->names().month;
case TemporalField::MonthCode:
return cx->names().monthCode;
case TemporalField::Day:
return cx->names().day;
case TemporalField::Hour:
return cx->names().hour;
case TemporalField::Minute:
return cx->names().minute;
case TemporalField::Second:
return cx->names().second;
case TemporalField::Millisecond:
return cx->names().millisecond;
case TemporalField::Microsecond:
return cx->names().microsecond;
case TemporalField::Nanosecond:
return cx->names().nanosecond;
case TemporalField::Offset:
return cx->names().offset;
case TemporalField::Era:
return cx->names().era;
case TemporalField::EraYear:
return cx->names().eraYear;
case TemporalField::TimeZone:
return cx->names().timeZone;
}
MOZ_CRASH("invalid temporal field name");
}
static const char* ToCString(TemporalField field) {
switch (field) {
case TemporalField::Year:
return "year";
case TemporalField::Month:
return "month";
case TemporalField::MonthCode:
return "monthCode";
case TemporalField::Day:
return "day";
case TemporalField::Hour:
return "hour";
case TemporalField::Minute:
return "minute";
case TemporalField::Second:
return "second";
case TemporalField::Millisecond:
return "millisecond";
case TemporalField::Microsecond:
return "microsecond";
case TemporalField::Nanosecond:
return "nanosecond";
case TemporalField::Offset:
return "offset";
case TemporalField::Era:
return "era";
case TemporalField::EraYear:
return "eraYear";
case TemporalField::TimeZone:
return "timeZone";
}
MOZ_CRASH("invalid temporal field name");
}
static JS::UniqueChars QuoteString(JSContext* cx, const char* str) {
Sprinter sprinter(cx);
if (!sprinter.init()) {
return nullptr;
}
mozilla::Range range(reinterpret_cast<const Latin1Char*>(str),
std::strlen(str));
QuoteString<QuoteTarget::String>(&sprinter, range);
return sprinter.release();
}
static JS::UniqueChars QuoteString(JSContext* cx, PropertyKey key) {
if (key.isString()) {
return QuoteString(cx, key.toString());
}
if (key.isInt()) {
Int32ToCStringBuf buf;
size_t length;
const char* str = Int32ToCString(&buf, key.toInt(), &length);
return DuplicateString(cx, str, length);
}
MOZ_ASSERT(key.isSymbol());
return QuoteString(cx, key.toSymbol()->description());
}
static mozilla::Maybe<TemporalField> ToTemporalField(JSContext* cx,
PropertyKey property) {
static constexpr TemporalField fieldNames[] = {
TemporalField::Year, TemporalField::Month,
TemporalField::MonthCode, TemporalField::Day,
TemporalField::Hour, TemporalField::Minute,
TemporalField::Second, TemporalField::Millisecond,
TemporalField::Microsecond, TemporalField::Nanosecond,
TemporalField::Offset, TemporalField::Era,
TemporalField::EraYear, TemporalField::TimeZone,
};
for (const auto& fieldName : fieldNames) {
auto* name = ToPropertyName(cx, fieldName);
if (property.isAtom(name)) {
return mozilla::Some(fieldName);
}
}
return mozilla::Nothing();
}
static JSString* ToPrimitiveAndRequireString(JSContext* cx,
Handle<Value> value) {
Rooted<Value> primitive(cx, value);
if (!ToPrimitive(cx, JSTYPE_STRING, &primitive)) {
return nullptr;
}
if (!primitive.isString()) {
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, primitive,
nullptr, "not a string");
return nullptr;
}
return primitive.toString();
}
static Value TemporalFieldDefaultValue(TemporalField field) {
switch (field) {
case TemporalField::Year:
case TemporalField::Month:
case TemporalField::MonthCode:
case TemporalField::Day:
case TemporalField::Offset:
case TemporalField::Era:
case TemporalField::EraYear:
case TemporalField::TimeZone:
return UndefinedValue();
case TemporalField::Hour:
case TemporalField::Minute:
case TemporalField::Second:
case TemporalField::Millisecond:
case TemporalField::Microsecond:
case TemporalField::Nanosecond:
return Int32Value(0);
}
MOZ_CRASH("invalid temporal field name");
}
static bool TemporalFieldConvertValue(JSContext* cx, TemporalField field,
MutableHandle<Value> value) {
const auto* name = ToCString(field);
switch (field) {
case TemporalField::Year:
case TemporalField::Hour:
case TemporalField::Minute:
case TemporalField::Second:
case TemporalField::Millisecond:
case TemporalField::Microsecond:
case TemporalField::Nanosecond:
case TemporalField::EraYear: {
double num;
if (!ToIntegerWithTruncation(cx, value, name, &num)) {
return false;
}
value.setNumber(num);
return true;
}
case TemporalField::Month:
case TemporalField::Day: {
double num;
if (!ToPositiveIntegerWithTruncation(cx, value, name, &num)) {
return false;
}
value.setNumber(num);
return true;
}
case TemporalField::MonthCode:
case TemporalField::Offset:
case TemporalField::Era: {
JSString* str = ToPrimitiveAndRequireString(cx, value);
if (!str) {
return false;
}
value.setString(str);
return true;
}
case TemporalField::TimeZone:
// NB: timeZone has no conversion function.
return true;
}
MOZ_CRASH("invalid temporal field name");
}
static int32_t ComparePropertyKey(PropertyKey x, PropertyKey y) {
MOZ_ASSERT(x.isAtom() || x.isInt());
MOZ_ASSERT(y.isAtom() || y.isInt());
if (MOZ_LIKELY(x.isAtom() && y.isAtom())) {
return CompareStrings(x.toAtom(), y.toAtom());
}
if (x.isInt() && y.isInt()) {
return x.toInt() - y.toInt();
}
uint32_t index = uint32_t(x.isInt() ? x.toInt() : y.toInt());
JSAtom* str = x.isAtom() ? x.toAtom() : y.toAtom();
char16_t buf[UINT32_CHAR_BUFFER_LENGTH];
mozilla::RangedPtr<char16_t> end(std::end(buf), buf, std::end(buf));
mozilla::RangedPtr<char16_t> start = BackfillIndexInCharBuffer(index, end);
int32_t result = CompareChars(start.get(), end - start, str);
return x.isInt() ? result : -result;
}
#ifdef DEBUG
static bool IsSorted(std::initializer_list<TemporalField> fieldNames) {
return std::is_sorted(fieldNames.begin(), fieldNames.end(),
[](auto x, auto y) {
auto* a = ToCString(x);
auto* b = ToCString(y);
return std::strcmp(a, b) < 0;
});
}
static bool IsSorted(const TemporalFieldNames& fieldNames) {
return std::is_sorted(
fieldNames.begin(), fieldNames.end(),
[](auto x, auto y) { return ComparePropertyKey(x, y) < 0; });
}
#endif
// clang-format off
//
// TODO: |fields| is often a built-in Temporal type, so we likely want to
// optimise for this case.
//
// Consider the case when PlainDate.prototype.toPlainMonthDay is called. The
// following steps are applied:
//
// 1. CalendarFields(calendar, «"day", "monthCode"») is called to retrieve the
// relevant calendar fields. For (most?) built-in calendars this will just
// return the input list «"day", "monthCode"».
// 2. PrepareTemporalFields(plainDate, «"day", "monthCode"») is called. This
// will access the properties `plainDate.day` and `plainDate.monthCode`.
// a. `plainDate.day` will call CalendarDay(calendar, plainDate).
// b. For built-in calendars, this will simply access `plainDate.[[IsoDay]]`.
// c. `plainDate.monthCode` will call CalendarMonthCode(calendar, plainDate).
// d. For built-in calendars, ISOMonthCode(plainDate.[[IsoMonth]]) is called.
// 3. CalendarMonthDayFromFields(calendar, {day, monthCode}) is called.
// 4. For built-in calendars, this calls PrepareTemporalFields({day, monthCode},
// «"day", "month", "monthCode", "year"», «"day"»).
// 5. The previous PrepareTemporalFields call is a no-op and returns {day, monthCode}.
// 6. Then ISOMonthDayFromFields({day, monthCode}, "constrain") gets called.
// 7. ResolveISOMonth(monthCode) is called to parse the just created `monthCode`.
// 8. RegulateISODate(referenceISOYear, month, day, "constrain") is called.
// 9. Finally CreateTemporalMonthDay is called to create the PlainMonthDay instance.
//
// All these steps could be simplified to just:
// 1. CreateTemporalMonthDay(referenceISOYear, plainDate.[[IsoMonth]], plainDate.[[IsoDay]]).
//
// When the following conditions are true:
// 1. The `plainDate` is a Temporal.PlainDate instance and has no overridden methods.
// 2. The `calendar` is a Temporal.Calendar instance and has no overridden methods.
// 3. Temporal.PlainDate.prototype and Temporal.Calendar.prototype are in their initial state.
// 4. Array iteration is still in its initial state. (Required by CalendarFields)
//
// PlainDate_toPlainMonthDay has an example implementation for this optimisation.
//
// clang-format on
/**
* PrepareTemporalFields ( fields, fieldNames, requiredFields )
*/
bool js::temporal::PrepareTemporalFields(
JSContext* cx, Handle<JSObject*> fields,
std::initializer_list<TemporalField> fieldNames,
std::initializer_list<TemporalField> requiredFields,
MutableHandle<TemporalFields> result) {
// Steps 1-3. (Not applicable in our implementation.)
// Step 4. (|fieldNames| is sorted in our implementation.)
MOZ_ASSERT(IsSorted(fieldNames));
// Step 5. (The list doesn't contain duplicates in our implementation.)
MOZ_ASSERT(std::adjacent_find(fieldNames.begin(), fieldNames.end()) ==
fieldNames.end());
// |requiredFields| is sorted and doesn't contain any duplicate elements.
MOZ_ASSERT(IsSorted(requiredFields));
MOZ_ASSERT(std::adjacent_find(requiredFields.begin(), requiredFields.end()) ==
requiredFields.end());
// Step 6.
Rooted<Value> value(cx);
for (auto fieldName : fieldNames) {
auto* property = ToPropertyName(cx, fieldName);
const auto* cstr = ToCString(fieldName);
// Step 6.a. (Not applicable in our implementation.)
// Step 6.b.i.
if (!GetProperty(cx, fields, fields, property, &value)) {
return false;
}
// Steps 6.b.ii-iii.
if (!value.isUndefined()) {
// Step 6.b.ii.1. (Not applicable in our implementation.)
// Steps 6.b.ii.2-3.
switch (fieldName) {
case TemporalField::Year:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.year())) {
return false;
}
break;
case TemporalField::Month:
if (!ToPositiveIntegerWithTruncation(cx, value, cstr,
&result.month())) {
return false;
}
break;
case TemporalField::MonthCode: {
JSString* str = ToPrimitiveAndRequireString(cx, value);
if (!str) {
return false;
}
result.monthCode().set(str);
break;
}
case TemporalField::Day:
if (!ToPositiveIntegerWithTruncation(cx, value, cstr,
&result.day())) {
return false;
}
break;
case TemporalField::Hour:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.hour())) {
return false;
}
break;
case TemporalField::Minute:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.minute())) {
return false;
}
break;
case TemporalField::Second:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.second())) {
return false;
}
break;
case TemporalField::Millisecond:
if (!ToIntegerWithTruncation(cx, value, cstr,
&result.millisecond())) {
return false;
}
break;
case TemporalField::Microsecond:
if (!ToIntegerWithTruncation(cx, value, cstr,
&result.microsecond())) {
return false;
}
break;
case TemporalField::Nanosecond:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.nanosecond())) {
return false;
}
break;
case TemporalField::Offset: {
JSString* str = ToPrimitiveAndRequireString(cx, value);
if (!str) {
return false;
}
result.offset().set(str);
break;
}
case TemporalField::Era: {
JSString* str = ToPrimitiveAndRequireString(cx, value);
if (!str) {
return false;
}
result.era().set(str);
break;
}
case TemporalField::EraYear:
if (!ToIntegerWithTruncation(cx, value, cstr, &result.eraYear())) {
return false;
}
break;
case TemporalField::TimeZone:
// NB: TemporalField::TimeZone has no conversion function.
result.timeZone().set(value);
break;
}
} else {
// Step 6.b.iii.1.
if (std::find(requiredFields.begin(), requiredFields.end(), fieldName) !=
requiredFields.end()) {
if (auto chars = QuoteString(cx, cstr)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_MISSING_PROPERTY,
chars.get());
}
return false;
}
// `const` can be changed to `constexpr` when we switch to C++20.
const TemporalFields FallbackValues{};
// Steps 6.b.iii.2-3.
switch (fieldName) {
case TemporalField::Year:
result.year() = FallbackValues.year;
break;
case TemporalField::Month:
result.month() = FallbackValues.month;
break;
case TemporalField::MonthCode:
result.monthCode().set(FallbackValues.monthCode);
break;
case TemporalField::Day:
result.day() = FallbackValues.day;
break;
case TemporalField::Hour:
result.hour() = FallbackValues.hour;
break;
case TemporalField::Minute:
result.minute() = FallbackValues.minute;
break;
case TemporalField::Second:
result.second() = FallbackValues.second;
break;
case TemporalField::Millisecond:
result.millisecond() = FallbackValues.millisecond;
break;
case TemporalField::Microsecond:
result.microsecond() = FallbackValues.microsecond;
break;
case TemporalField::Nanosecond:
result.nanosecond() = FallbackValues.nanosecond;
break;
case TemporalField::Offset:
result.offset().set(FallbackValues.offset);
break;
case TemporalField::Era:
result.era().set(FallbackValues.era);
break;
case TemporalField::EraYear:
result.eraYear() = FallbackValues.eraYear;
break;
case TemporalField::TimeZone:
result.timeZone().set(FallbackValues.timeZone);
break;
}
}
// Steps 6.c-d. (Not applicable in our implementation.)
}
// Step 7. (Not applicable in our implementation.)
// Step 8.
return true;
}
/**
* PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
* duplicateBehaviour ] )
*/
PlainObject* js::temporal::PrepareTemporalFields(
JSContext* cx, Handle<JSObject*> fields,
Handle<TemporalFieldNames> fieldNames) {
// Step 1. (Not applicable in our implementation.)
// Step 2.
Rooted<PlainObject*> result(cx, NewPlainObjectWithProto(cx, nullptr));
if (!result) {
return nullptr;
}
// Step 3. (Not applicable in our implementation.)
// Step 4. (The list is already sorted in our implementation.)
MOZ_ASSERT(IsSorted(fieldNames));
// Step 5. (The list doesn't contain duplicates in our implementation.)
MOZ_ASSERT(std::adjacent_find(fieldNames.begin(), fieldNames.end()) ==
fieldNames.end());
// Step 6.
Rooted<Value> value(cx);
for (size_t i = 0; i < fieldNames.length(); i++) {
Handle<PropertyKey> property = fieldNames[i];
// Step 6.a.
MOZ_ASSERT(property != NameToId(cx->names().constructor));
MOZ_ASSERT(property != NameToId(cx->names().proto_));
// Step 6.b.i.
if (!GetProperty(cx, fields, fields, property, &value)) {
return nullptr;
}
// Steps 6.b.ii-iii.
if (auto fieldName = ToTemporalField(cx, property)) {
if (!value.isUndefined()) {
// Step 6.b.ii.1. (Not applicable in our implementation.)
// Step 6.b.ii.2.
if (!TemporalFieldConvertValue(cx, *fieldName, &value)) {
return nullptr;
}
} else {
// Step 6.b.iii.1. (Not applicable in our implementation.)
// Step 6.b.iii.2.
value = TemporalFieldDefaultValue(*fieldName);
}
}
// Steps 6.b.ii.3 and 6.b.iii.3.
if (!DefineDataProperty(cx, result, property, value)) {
return nullptr;
}
// Steps 6.c-d. (Not applicable in our implementation.)
}
// Step 7. (Not applicable in our implementation.)
// Step 8.
return result;
}
/**
* PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
* duplicateBehaviour ] )
*/
PlainObject* js::temporal::PrepareTemporalFields(
JSContext* cx, Handle<JSObject*> fields,
Handle<TemporalFieldNames> fieldNames,
std::initializer_list<TemporalField> requiredFields) {
// Step 1. (Not applicable in our implementation.)
// Step 2.
Rooted<PlainObject*> result(cx, NewPlainObjectWithProto(cx, nullptr));
if (!result) {
return nullptr;
}
// Step 3. (Not applicable in our implementation.)
// Step 4. (The list is already sorted in our implementation.)
MOZ_ASSERT(IsSorted(fieldNames));
// Step 5. (The list doesn't contain duplicates in our implementation.)
MOZ_ASSERT(std::adjacent_find(fieldNames.begin(), fieldNames.end()) ==
fieldNames.end());
// |requiredFields| is sorted and doesn't include any duplicate elements.
MOZ_ASSERT(IsSorted(requiredFields));
MOZ_ASSERT(std::adjacent_find(requiredFields.begin(), requiredFields.end()) ==
requiredFields.end());
// Step 6.
Rooted<Value> value(cx);
for (size_t i = 0; i < fieldNames.length(); i++) {
Handle<PropertyKey> property = fieldNames[i];
// Step 6.a.
MOZ_ASSERT(property != NameToId(cx->names().constructor));
MOZ_ASSERT(property != NameToId(cx->names().proto_));
// Step 6.b.i.
if (!GetProperty(cx, fields, fields, property, &value)) {
return nullptr;
}
// Steps 6.b.ii-iii.
if (auto fieldName = ToTemporalField(cx, property)) {
if (!value.isUndefined()) {
// Step 6.b.ii.1. (Not applicable in our implementation.)
// Step 6.b.ii.2.
if (!TemporalFieldConvertValue(cx, *fieldName, &value)) {
return nullptr;
}
} else {
// Step 6.b.iii.1.
if (std::find(requiredFields.begin(), requiredFields.end(),
*fieldName) != requiredFields.end()) {
if (auto chars = QuoteString(cx, property.toString())) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_MISSING_PROPERTY,
chars.get());
}
return nullptr;
}
// Step 6.b.iii.2.
value = TemporalFieldDefaultValue(*fieldName);
}
}
// Steps 6.b.ii.3 and 6.b.iii.3.
if (!DefineDataProperty(cx, result, property, value)) {
return nullptr;
}
// Steps 6.c-d. (Not applicable in our implementation.)
}
// Step 7. (Not applicable in our implementation.)
// Step 8.
return result;
}
/**
* PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
* duplicateBehaviour ] )
*/
PlainObject* js::temporal::PreparePartialTemporalFields(
JSContext* cx, Handle<JSObject*> fields,
Handle<TemporalFieldNames> fieldNames) {
// Step 1. (Not applicable in our implementation.)
// Step 2.
Rooted<PlainObject*> result(cx, NewPlainObjectWithProto(cx, nullptr));
if (!result) {
return nullptr;
}
// Step 3.
bool any = false;
// Step 4. (The list is already sorted in our implementation.)
MOZ_ASSERT(IsSorted(fieldNames));
// Step 5. (The list doesn't contain duplicates in our implementation.)
MOZ_ASSERT(std::adjacent_find(fieldNames.begin(), fieldNames.end()) ==
fieldNames.end());
// Step 6.
Rooted<Value> value(cx);
for (size_t i = 0; i < fieldNames.length(); i++) {
Handle<PropertyKey> property = fieldNames[i];
// Step 6.a.
MOZ_ASSERT(property != NameToId(cx->names().constructor));
MOZ_ASSERT(property != NameToId(cx->names().proto_));
// Step 6.b.i.
if (!GetProperty(cx, fields, fields, property, &value)) {
return nullptr;
}
// Steps 6.b.ii-iii.
if (!value.isUndefined()) {
// Step 6.b.ii.1.
any = true;
// Step 6.b.ii.2.
if (auto fieldName = ToTemporalField(cx, property)) {
if (!TemporalFieldConvertValue(cx, *fieldName, &value)) {
return nullptr;
}
}
// Steps 6.b.ii.3.
if (!DefineDataProperty(cx, result, property, value)) {
return nullptr;
}
} else {
// Step 6.b.iii. (Not applicable in our implementation.)
}
// Steps 6.c-d. (Not applicable in our implementation.)
}
// Step 7.
if (!any) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_MISSING_TEMPORAL_FIELDS);
return nullptr;
}
// Step 8.
return result;
}
/**
* Performs list-concatenation, removes any duplicates, and sorts the result.
*/
bool js::temporal::ConcatTemporalFieldNames(
const TemporalFieldNames& receiverFieldNames,
const TemporalFieldNames& inputFieldNames,
TemporalFieldNames& concatenatedFieldNames) {
MOZ_ASSERT(IsSorted(receiverFieldNames));
MOZ_ASSERT(IsSorted(inputFieldNames));
MOZ_ASSERT(concatenatedFieldNames.empty());
auto appendUnique = [&](auto key) {
if (concatenatedFieldNames.empty() ||
concatenatedFieldNames.back() != key) {
return concatenatedFieldNames.append(key);
}
return true;
};
size_t i = 0;
size_t j = 0;
// Append the names from |receiverFieldNames| and |inputFieldNames|.
while (i < receiverFieldNames.length() && j < inputFieldNames.length()) {
auto x = receiverFieldNames[i];
auto y = inputFieldNames[j];
PropertyKey z;
if (ComparePropertyKey(x, y) <= 0) {
z = x;
i++;
} else {
z = y;
j++;
}
if (!appendUnique(z)) {
return false;
}
}
// Append the remaining names from |receiverFieldNames|.
while (i < receiverFieldNames.length()) {
if (!appendUnique(receiverFieldNames[i++])) {
return false;
}
}
// Append the remaining names from |inputFieldNames|.
while (j < inputFieldNames.length()) {
if (!appendUnique(inputFieldNames[j++])) {
return false;
}
}
return true;
}
bool js::temporal::AppendSorted(
JSContext* cx, TemporalFieldNames& fieldNames,
std::initializer_list<TemporalField> additionalNames) {
// |fieldNames| is sorted and doesn't include any duplicates
MOZ_ASSERT(IsSorted(fieldNames));
MOZ_ASSERT(std::adjacent_find(fieldNames.begin(), fieldNames.end()) ==
fieldNames.end());
// |additionalNames| is non-empty, sorted, and doesn't include any duplicates.
MOZ_ASSERT(additionalNames.size() > 0);
MOZ_ASSERT(IsSorted(additionalNames));
MOZ_ASSERT(
std::adjacent_find(additionalNames.begin(), additionalNames.end()) ==
additionalNames.end());
// Allocate space for entries from |additionalNames|.
if (!fieldNames.growBy(additionalNames.size())) {
return false;
}
const auto* left = std::prev(fieldNames.end(), additionalNames.size());
const auto* right = additionalNames.end();
auto* out = fieldNames.end();
// Write backwards into the newly allocated space.
while (left != fieldNames.begin() && right != additionalNames.begin()) {
MOZ_ASSERT(out != fieldNames.begin());
auto x = *std::prev(left);
auto y = NameToId(ToPropertyName(cx, *std::prev(right)));
int32_t r = ComparePropertyKey(x, y);
// Reject duplicates per PrepareTemporalFields, step 6.c.
if (r == 0) {
if (auto chars = QuoteString(cx, x)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_DUPLICATE_PROPERTY,
chars.get());
}
return false;
}
// Insert the lexicographically greater key.
PropertyKey z;
if (r > 0) {
z = x;
left--;
} else {
z = y;
right--;
}
*--out = z;
}
// Avoid unnecessary copying if possible.
if (left == out) {
MOZ_ASSERT(right == additionalNames.begin());
return true;
}
// Prepend the remaining names from |fieldNames|.
while (left != fieldNames.begin()) {
MOZ_ASSERT(out != fieldNames.begin());
*--out = *--left;
}
// Prepend the remaining names from |additionalNames|.
while (right != additionalNames.begin()) {
MOZ_ASSERT(out != fieldNames.begin());
*--out = NameToId(ToPropertyName(cx, *--right));
}
// All field names were written into the result list.
MOZ_ASSERT(out == fieldNames.begin());
return true;
}
bool js::temporal::SortTemporalFieldNames(JSContext* cx,
TemporalFieldNames& fieldNames) {
// Create scratch space for MergeSort().
TemporalFieldNames scratch(cx);
if (!scratch.resize(fieldNames.length())) {
return false;
}
// Sort all field names in alphabetical order.
auto comparator = [](const auto& x, const auto& y, bool* lessOrEqual) {
*lessOrEqual = ComparePropertyKey(x, y) <= 0;
return true;
};
MOZ_ALWAYS_TRUE(MergeSort(fieldNames.begin(), fieldNames.length(),
scratch.begin(), comparator));
for (size_t i = 0; i < fieldNames.length(); i++) {
auto property = fieldNames[i];
// Reject "constructor" and "__proto__" per PrepareTemporalFields, step 6.a.
if (property == NameToId(cx->names().constructor) ||
property == NameToId(cx->names().proto_)) {
if (auto chars = QuoteString(cx, property)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_INVALID_PROPERTY, chars.get());
}
return false;
}
// Reject duplicates per PrepareTemporalFields, step 6.c.
if (i > 0 && property == fieldNames[i - 1]) {
if (auto chars = QuoteString(cx, property)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_TEMPORAL_DUPLICATE_PROPERTY,
chars.get());
}
return false;
}
}
return true;
}
|