|
| 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 from within |
| 6 | +// snapshotted modules unless imported lazily. |
| 7 | + |
| 8 | +// TODO(Renegade334): Remove typecheck methods once available via V8 API. |
| 9 | +// Replace with primordials if/when temporal_rs becomes a mandatory V8 |
| 10 | +// build dependency and the harmony_temporal flag is removed. |
| 11 | + |
| 12 | +const { |
| 13 | + ArrayPrototypeForEach, |
| 14 | + FunctionPrototypeCall, |
| 15 | + ObjectEntries, |
| 16 | + ObjectGetOwnPropertyDescriptors, |
| 17 | + SafeMap, |
| 18 | + StringPrototypeSubstring, |
| 19 | + StringPrototypeToUpperCase, |
| 20 | + globalThis, |
| 21 | + uncurryThis, |
| 22 | +} = primordials; |
| 23 | + |
| 24 | +// The keys of this Map are the Temporal constructor names. |
| 25 | +// The values are the names of the most lightweight brand-aware getter within |
| 26 | +// each prototype, which can be called speculatively as a brand check. |
| 27 | +const constructors = new SafeMap() |
| 28 | + .set('Duration', 'nanoseconds') |
| 29 | + .set('Instant', 'epochNanoseconds') |
| 30 | + .set('PlainDate', 'calendarId') |
| 31 | + .set('PlainDateTime', 'calendarId') |
| 32 | + .set('PlainMonthDay', 'calendarId') |
| 33 | + .set('PlainTime', 'nanosecond') |
| 34 | + .set('PlainYearMonth', 'calendarId') |
| 35 | + .set('ZonedDateTime', 'calendarId'); |
| 36 | + |
| 37 | +exports.initialize = () => { |
| 38 | + const { Temporal } = globalThis; |
| 39 | + |
| 40 | + if (Temporal === undefined) { |
| 41 | + exports.hasTemporal = false; |
| 42 | + |
| 43 | + for (const name of constructors.keys()) { |
| 44 | + exports[`isTemporal${name}`] = () => false; |
| 45 | + } |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + exports.hasTemporal = true; |
| 51 | + |
| 52 | + for (const { 0: name, 1: brandedGetter } of constructors.entries()) { |
| 53 | + const constructor = Temporal[name]; |
| 54 | + exports[`Temporal${name}`] = constructor; |
| 55 | + |
| 56 | + ArrayPrototypeForEach( |
| 57 | + ObjectEntries(ObjectGetOwnPropertyDescriptors(constructor)), |
| 58 | + ({ 0: key, 1: desc }) => { |
| 59 | + if (typeof key === 'string' && typeof desc.value === 'function') { |
| 60 | + exports[`Temporal${name}${capitalizeKey(key)}`] = desc.value; |
| 61 | + } |
| 62 | + }, |
| 63 | + ); |
| 64 | + |
| 65 | + ArrayPrototypeForEach( |
| 66 | + ObjectEntries(ObjectGetOwnPropertyDescriptors(constructor.prototype)), |
| 67 | + ({ 0: key, 1: desc }) => { |
| 68 | + if (typeof key !== 'string') return; |
| 69 | + |
| 70 | + if ('get' in desc) { |
| 71 | + exports[`Temporal${name}PrototypeGet${capitalizeKey(key)}`] = uncurryThis(desc.get); |
| 72 | + if (key === brandedGetter) { |
| 73 | + exports[`isTemporal${name}`] = makeTypeCheckMethod(desc.get); |
| 74 | + } |
| 75 | + } else { |
| 76 | + exports[`Temporal${name}Prototype${capitalizeKey(key)}`] = typeof desc.value === 'function' ? |
| 77 | + uncurryThis(desc.value) : |
| 78 | + desc.value; |
| 79 | + } |
| 80 | + }, |
| 81 | + ); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +function makeTypeCheckMethod(getter) { |
| 86 | + return (value) => { |
| 87 | + try { |
| 88 | + FunctionPrototypeCall(getter, value); |
| 89 | + return true; |
| 90 | + } catch { |
| 91 | + return false; |
| 92 | + } |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function capitalizeKey(key) { |
| 97 | + return `${StringPrototypeToUpperCase(key[0])}${StringPrototypeSubstring(key, 1)}`; |
| 98 | +} |
0 commit comments