Skip to content

Commit 06e3356

Browse files
committed
lib: add internal Temporal utils
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
1 parent e8c279d commit 06e3356

5 files changed

Lines changed: 162 additions & 0 deletions

File tree

‎lib/internal/process/pre_execution.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ function prepareExecution(options) {
139139
require('internal/dns/utils').initializeDns();
140140
}
141141

142+
require('internal/util/temporal').initialize();
143+
142144
if (isMainThread) {
143145
assert(internalBinding('worker').isMainThread);
144146
// Worker threads will get the manifest in the message handler.

‎lib/internal/util/temporal.js‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
3+
// This module is initialized in the pre-execution phase, before user code is
4+
// run. Since the module namespace is not populated at snapshot time, this
5+
// module should be not be imported with a destructuring pattern in a module
6+
// header or used within top-level module code, as the properties may not yet
7+
// be initialized. Either import this module lazily, or import the namespace
8+
// object without destructuring and access its properties at the point of use.
9+
10+
// This module exposes the following exports:
11+
// * `hasTemporal`, a boolean property indicating whether the Temporal builtin
12+
// is present during initialization.
13+
// * Typecheck methods for Temporal objects: `isTemporalDuration()`,
14+
// `isTemporalInstant()` etc. If `hasTemporal` is false, then these methods
15+
// will always return `false`. Note that these are much less performant than
16+
// the native typecheck methods in internal/util/types, and should not be
17+
// used in performance-critical paths.
18+
// If `hasTemporal` is true, then this module also exports the following, in
19+
// primordial style:
20+
// * Temporal constructors: `TemporalDuration`, `TemporalInstant` etc.
21+
// * Static methods on temporal constructors: `TemporalDurationCompare`,
22+
// `TemporalDurationFrom` etc.
23+
// * Temporal prototype properties, methods and getters:
24+
// `TemporalDurationPrototypeAbs`, `TemporalDurationPrototypeGetBlank` etc.
25+
26+
// TODO(Renegade334): Remove typecheck methods once available via V8 API.
27+
// Replace with primordials if/when temporal_rs becomes a mandatory V8
28+
// build dependency and the harmony_temporal flag is removed.
29+
30+
const {
31+
ArrayPrototypeForEach,
32+
FunctionPrototypeCall,
33+
ObjectEntries,
34+
ObjectGetOwnPropertyDescriptors,
35+
SafeMap,
36+
StringPrototypeSubstring,
37+
StringPrototypeToUpperCase,
38+
globalThis,
39+
uncurryThis,
40+
} = primordials;
41+
42+
// The keys of this Map are the Temporal constructor names.
43+
// The values are the names of the most lightweight brand-aware getter within
44+
// each prototype, which can be called speculatively as a brand check.
45+
const constructors = new SafeMap()
46+
.set('Duration', 'nanoseconds')
47+
.set('Instant', 'epochNanoseconds')
48+
.set('PlainDate', 'calendarId')
49+
.set('PlainDateTime', 'calendarId')
50+
.set('PlainMonthDay', 'calendarId')
51+
.set('PlainTime', 'nanosecond')
52+
.set('PlainYearMonth', 'calendarId')
53+
.set('ZonedDateTime', 'calendarId');
54+
55+
exports.initialize = () => {
56+
const { Temporal } = globalThis;
57+
58+
if (Temporal === undefined) {
59+
exports.hasTemporal = false;
60+
61+
for (const name of constructors.keys()) {
62+
exports[`isTemporal${name}`] = () => false;
63+
}
64+
65+
return;
66+
}
67+
68+
exports.hasTemporal = true;
69+
70+
for (const { 0: name, 1: brandedGetter } of constructors) {
71+
const constructor = Temporal[name];
72+
exports[`Temporal${name}`] = constructor;
73+
74+
ArrayPrototypeForEach(
75+
ObjectEntries(ObjectGetOwnPropertyDescriptors(constructor)),
76+
({ 0: key, 1: desc }) => {
77+
if (typeof key === 'string' && typeof desc.value === 'function') {
78+
exports[`Temporal${name}${capitalizeKey(key)}`] = desc.value;
79+
}
80+
},
81+
);
82+
83+
const descriptors = ObjectGetOwnPropertyDescriptors(constructor.prototype);
84+
ArrayPrototypeForEach(
85+
ObjectEntries(descriptors),
86+
({ 0: key, 1: desc }) => {
87+
if (typeof key !== 'string') return;
88+
89+
if ('get' in desc) {
90+
exports[`Temporal${name}PrototypeGet${capitalizeKey(key)}`] = uncurryThis(desc.get);
91+
} else {
92+
exports[`Temporal${name}Prototype${capitalizeKey(key)}`] = typeof desc.value === 'function' ?
93+
uncurryThis(desc.value) :
94+
desc.value;
95+
}
96+
},
97+
);
98+
99+
exports[`isTemporal${name}`] = makeTypeCheckMethod(descriptors[brandedGetter].get);
100+
}
101+
};
102+
103+
function makeTypeCheckMethod(getter) {
104+
return (value) => {
105+
try {
106+
FunctionPrototypeCall(getter, value);
107+
return true;
108+
} catch {
109+
return false;
110+
}
111+
};
112+
}
113+
114+
function capitalizeKey(key) {
115+
return `${StringPrototypeToUpperCase(key[0])}${StringPrototypeSubstring(key, 1)}`;
116+
}

‎test/parallel/test-bootstrap-modules.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ expected.beforePreExec = new Set([
114114

115115
expected.atRunTime = new Set([
116116
'NativeModule internal/process/pre_execution',
117+
'NativeModule internal/util/temporal',
117118
]);
118119

119120
const { isMainThread } = require('worker_threads');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Flags: --expose-internals --no-harmony-temporal --no-warnings
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
8+
common.allowGlobals(globalThis.Temporal = {
9+
Instant: class Instant {},
10+
});
11+
12+
const instant = new Temporal.Instant();
13+
14+
// Module contents should be independent of global state at require time
15+
const temporalUtils = require('internal/util/temporal');
16+
17+
assert.strictEqual(temporalUtils.hasTemporal, false);
18+
assert.strictEqual(temporalUtils.isTemporalInstant(instant), false);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Flags: --expose-internals --no-warnings
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
8+
if (!common.hasTemporal) {
9+
common.skip('No Temporal support');
10+
}
11+
12+
const instant = Temporal.Now.instant();
13+
14+
delete globalThis.Temporal;
15+
assert.strictEqual(typeof Temporal, 'undefined');
16+
17+
// Module contents should be independent of global state at require time
18+
const temporalUtils = require('internal/util/temporal');
19+
20+
assert.strictEqual(temporalUtils.hasTemporal, true);
21+
assert.strictEqual(instant.constructor, temporalUtils.TemporalInstant);
22+
assert.strictEqual(instant.epochNanoseconds,
23+
temporalUtils.TemporalInstantPrototypeGetEpochNanoseconds(instant));
24+
assert.strictEqual(temporalUtils.isTemporalInstant(instant), true);
25+
assert.strictEqual(temporalUtils.isTemporalZonedDateTime(instant), false);

0 commit comments

Comments
 (0)