-
-
Notifications
You must be signed in to change notification settings - Fork 36.6k
Expand file tree
/
Copy pathtemporal.js
More file actions
93 lines (80 loc) Β· 2.8 KB
/
Copy pathtemporal.js
File metadata and controls
93 lines (80 loc) Β· 2.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
'use strict';
// This module is initialized in the pre-execution phase, before user code is
// run. Since the module namespace is not populated at snapshot time, this
// module should be not be imported with a destructuring pattern unless
// imported lazily.
// TODO(Renegade334): Remove typecheck methods once available via V8 API.
// Replace with primordials if/when temporal_rs becomes a mandatory V8
// build dependency and the harmony_temporal flag is removed.
const {
ArrayPrototypeForEach,
FunctionPrototypeCall,
ObjectEntries,
ObjectGetOwnPropertyDescriptors,
SafeMap,
StringPrototypeSubstring,
StringPrototypeToUpperCase,
globalThis,
uncurryThis,
} = primordials;
// The keys of this Map are the Temporal constructor names.
// The values are the names of the most lightweight brand-aware getter within
// each prototype, which can be called speculatively as a brand check.
const constructors = new SafeMap()
.set('Duration', 'nanoseconds')
.set('Instant', 'epochNanoseconds')
.set('PlainDate', 'calendarId')
.set('PlainDateTime', 'calendarId')
.set('PlainMonthDay', 'calendarId')
.set('PlainTime', 'nanosecond')
.set('PlainYearMonth', 'calendarId')
.set('ZonedDateTime', 'calendarId');
exports.initialize = () => {
const { Temporal } = globalThis;
if (Temporal === undefined) {
for (const name of constructors.keys()) {
exports[`isTemporal${name}`] = () => false;
}
return;
}
for (const { 0: name, 1: brandedGetter } of constructors.entries()) {
const constructor = Temporal[name];
exports[`Temporal${name}`] = constructor;
ArrayPrototypeForEach(
ObjectEntries(ObjectGetOwnPropertyDescriptors(constructor)),
({ 0: key, 1: desc }) => {
if (typeof key !== 'string' || typeof desc.value !== 'function') return;
exports[`Temporal${name}${capitalizeKey(key)}`] = desc.value;
},
);
ArrayPrototypeForEach(
ObjectEntries(ObjectGetOwnPropertyDescriptors(constructor.prototype)),
({ 0: key, 1: desc }) => {
if (typeof key !== 'string') return;
if ('get' in desc) {
exports[`Temporal${name}PrototypeGet${capitalizeKey(key)}`] = uncurryThis(desc.get);
if (key === brandedGetter) {
exports[`isTemporal${name}`] = makeTypeCheckMethod(desc.get);
}
} else {
exports[`Temporal${name}Prototype${capitalizeKey(key)}`] = typeof desc.value === 'function' ?
uncurryThis(desc.value) :
desc.value;
}
},
);
}
};
function makeTypeCheckMethod(getter) {
return (value) => {
try {
FunctionPrototypeCall(getter, value);
return true;
} catch {
return false;
}
};
}
function capitalizeKey(key) {
return `${StringPrototypeToUpperCase(key[0])}${StringPrototypeSubstring(key, 1)}`;
}