-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
fs: add Temporal.Instant support to Stats and BigIntStats
#60789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ const { | |||||||||
| DatePrototypeGetTime, | ||||||||||
| ErrorCaptureStackTrace, | ||||||||||
| FunctionPrototypeCall, | ||||||||||
| MathFloor, | ||||||||||
| MathMin, | ||||||||||
| MathRound, | ||||||||||
| Number, | ||||||||||
|
|
@@ -22,6 +23,7 @@ const { | |||||||||
| Symbol, | ||||||||||
| TypedArrayPrototypeAt, | ||||||||||
| TypedArrayPrototypeIncludes, | ||||||||||
| globalThis, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this doesn't seem to work since at this point of bootstrapping |
||||||||||
| } = primordials; | ||||||||||
|
|
||||||||||
| const { Buffer } = require('buffer'); | ||||||||||
|
|
@@ -32,6 +34,7 @@ const { | |||||||||
| ERR_INCOMPATIBLE_OPTION_PAIR, | ||||||||||
| ERR_INVALID_ARG_TYPE, | ||||||||||
| ERR_INVALID_ARG_VALUE, | ||||||||||
| ERR_NO_TEMPORAL, | ||||||||||
| ERR_OUT_OF_RANGE, | ||||||||||
| }, | ||||||||||
| hideStackFrames, | ||||||||||
|
|
@@ -43,10 +46,10 @@ const { | |||||||||
| isUint8Array, | ||||||||||
| } = require('internal/util/types'); | ||||||||||
| const { | ||||||||||
| kEmptyObject, | ||||||||||
| once, | ||||||||||
| deprecate, | ||||||||||
| isWindows, | ||||||||||
| kEmptyObject, | ||||||||||
| once, | ||||||||||
| setOwnProperty, | ||||||||||
| } = require('internal/util'); | ||||||||||
| const { toPathIfFileURL } = require('internal/url'); | ||||||||||
|
|
@@ -62,6 +65,10 @@ const { | |||||||||
| const pathModule = require('path'); | ||||||||||
| const kType = Symbol('type'); | ||||||||||
| const kStats = Symbol('stats'); | ||||||||||
| const kPartialAtimeNs = Symbol('partialAtimeNs'); | ||||||||||
| const kPartialMtimeNs = Symbol('partialMtimeNs'); | ||||||||||
| const kPartialCtimeNs = Symbol('partialCtimeNs'); | ||||||||||
| const kPartialBirthtimeNs = Symbol('kPartialBirthtimeNs'); | ||||||||||
| const assert = require('internal/assert'); | ||||||||||
|
|
||||||||||
| const { | ||||||||||
|
|
@@ -430,6 +437,25 @@ function nsFromTimeSpecBigInt(sec, nsec) { | |||||||||
| return sec * kNsPerSecBigInt + nsec; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // TODO(LiviaMedeiros): TemporalInstant primordial | ||||||||||
| let TemporalInstant; | ||||||||||
|
|
||||||||||
| function instantFromNs(nsec) { | ||||||||||
| TemporalInstant ??= globalThis.Temporal?.Instant; | ||||||||||
| if (TemporalInstant === undefined) { | ||||||||||
| throw new ERR_NO_TEMPORAL(); | ||||||||||
| } | ||||||||||
| return new TemporalInstant(nsec); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function instantFromTimeSpecMs(msec, nsec) { | ||||||||||
| TemporalInstant ??= globalThis.Temporal?.Instant; | ||||||||||
| if (TemporalInstant === undefined) { | ||||||||||
| throw new ERR_NO_TEMPORAL(); | ||||||||||
| } | ||||||||||
| return new TemporalInstant(BigInt(MathFloor(msec / kMsPerSec)) * kNsPerSecBigInt + BigInt(nsec)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // The Date constructor performs Math.floor() on the absolute value | ||||||||||
| // of the timestamp: https://tc39.es/ecma262/#sec-timeclip | ||||||||||
| // Since there may be a precision loss when the timestamp is | ||||||||||
|
|
@@ -490,6 +516,106 @@ const lazyDateFields = { | |||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const lazyTemporalFields = { | ||||||||||
| __proto__: null, | ||||||||||
| atimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'atimeInstant', | ||||||||||
| instantFromTimeSpecMs(this.atimeMs, this[kPartialAtimeNs])); | ||||||||||
|
aduh95 marked this conversation as resolved.
|
||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'atimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| mtimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'mtimeInstant', | ||||||||||
| instantFromTimeSpecMs(this.mtimeMs, this[kPartialMtimeNs])); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'mtimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| ctimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'ctimeInstant', | ||||||||||
| instantFromTimeSpecMs(this.ctimeMs, this[kPartialCtimeNs])); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'ctimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| birthtimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'birthtimeInstant', | ||||||||||
| instantFromTimeSpecMs(this.birthtimeMs, this[kPartialBirthtimeNs])); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'birthtimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const lazyTemporalBigIntFields = { | ||||||||||
| __proto__: null, | ||||||||||
| atimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'atimeInstant', instantFromNs(this.atimeNs)); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'atimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| mtimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'mtimeInstant', instantFromNs(this.mtimeNs)); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'mtimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| ctimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'ctimeInstant', instantFromNs(this.ctimeNs)); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'ctimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| birthtimeInstant: { | ||||||||||
| __proto__: null, | ||||||||||
| enumerable: true, | ||||||||||
| configurable: true, | ||||||||||
| get() { | ||||||||||
| return setOwnProperty(this, 'birthtimeInstant', instantFromNs(this.birthtimeNs)); | ||||||||||
| }, | ||||||||||
| set(value) { | ||||||||||
| setOwnProperty(this, 'birthtimeInstant', value); | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, | ||||||||||
| ino, size, blocks, | ||||||||||
| atimeNs, mtimeNs, ctimeNs, birthtimeNs) { | ||||||||||
|
|
@@ -508,6 +634,7 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, | |||||||||
| ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype); | ||||||||||
| ObjectSetPrototypeOf(BigIntStats, StatsBase); | ||||||||||
| ObjectDefineProperties(BigIntStats.prototype, lazyDateFields); | ||||||||||
| ObjectDefineProperties(BigIntStats.prototype, lazyTemporalBigIntFields); | ||||||||||
|
|
||||||||||
| BigIntStats.prototype._checkModeProperty = function(property) { | ||||||||||
| if (isWindows && (property === S_IFIFO || property === S_IFBLK || | ||||||||||
|
|
@@ -519,18 +646,23 @@ BigIntStats.prototype._checkModeProperty = function(property) { | |||||||||
|
|
||||||||||
| function Stats(dev, mode, nlink, uid, gid, rdev, blksize, | ||||||||||
| ino, size, blocks, | ||||||||||
| atimeMs, mtimeMs, ctimeMs, birthtimeMs) { | ||||||||||
| atimeS, atimeNs, mtimeS, mtimeNs, ctimeS, ctimeNs, birthtimeS, birthtimeNs) { | ||||||||||
| FunctionPrototypeCall(StatsBase, this, dev, mode, nlink, uid, gid, rdev, | ||||||||||
| blksize, ino, size, blocks); | ||||||||||
| this.atimeMs = atimeMs; | ||||||||||
| this.mtimeMs = mtimeMs; | ||||||||||
| this.ctimeMs = ctimeMs; | ||||||||||
| this.birthtimeMs = birthtimeMs; | ||||||||||
| this.atimeMs = msFromTimeSpec(atimeS, atimeNs); | ||||||||||
| this.mtimeMs = msFromTimeSpec(mtimeS, mtimeNs); | ||||||||||
| this.ctimeMs = msFromTimeSpec(ctimeS, ctimeNs); | ||||||||||
| this.birthtimeMs = msFromTimeSpec(birthtimeS, birthtimeNs); | ||||||||||
| this[kPartialAtimeNs] = atimeNs; | ||||||||||
| this[kPartialMtimeNs] = mtimeNs; | ||||||||||
| this[kPartialCtimeNs] = ctimeNs; | ||||||||||
| this[kPartialBirthtimeNs] = birthtimeNs; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype); | ||||||||||
| ObjectSetPrototypeOf(Stats, StatsBase); | ||||||||||
| ObjectDefineProperties(Stats.prototype, lazyDateFields); | ||||||||||
| ObjectDefineProperties(Stats.prototype, lazyTemporalFields); | ||||||||||
|
|
||||||||||
| Stats.prototype._checkModeProperty = function(property) { | ||||||||||
| if (isWindows && (property === S_IFIFO || property === S_IFBLK || | ||||||||||
|
|
@@ -563,10 +695,10 @@ function getStatsFromBinding(stats, offset = 0) { | |||||||||
| stats[3 + offset], stats[4 + offset], stats[5 + offset], | ||||||||||
| stats[6 + offset], stats[7 + offset], stats[8 + offset], | ||||||||||
| stats[9 + offset], | ||||||||||
| msFromTimeSpec(stats[10 + offset], stats[11 + offset]), | ||||||||||
| msFromTimeSpec(stats[12 + offset], stats[13 + offset]), | ||||||||||
| msFromTimeSpec(stats[14 + offset], stats[15 + offset]), | ||||||||||
| msFromTimeSpec(stats[16 + offset], stats[17 + offset]), | ||||||||||
| stats[10 + offset], stats[11 + offset], // atime | ||||||||||
| stats[12 + offset], stats[13 + offset], // mtime | ||||||||||
| stats[14 + offset], stats[15 + offset], // ctime | ||||||||||
| stats[16 + offset], stats[17 + offset], // birthtime | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.