@@ -17,26 +17,38 @@ export default function tameDateConstructor(dateTaming = 'safe') {
17
17
18
18
// Use concise methods to obtain named functions without constructors.
19
19
const tamedMethods = {
20
+ /**
21
+ * `%SharedDate%.now()` throw a `TypeError` starting with "secure mode".
22
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
23
+ */
20
24
now ( ) {
21
- return NaN ;
25
+ throw TypeError ( 'secure mode Calling %SharedDate%.now() throws' ) ;
22
26
} ,
23
27
} ;
24
28
25
- // Tame the Date constructor.
26
- // Common behavior
27
- // * new Date(x) coerces x into a number and then returns a Date
28
- // for that number of millis since the epoch
29
- // * new Date(NaN) returns a Date object which stringifies to
30
- // 'Invalid Date'
31
- // * new Date(undefined) returns a Date object which stringifies to
32
- // 'Invalid Date'
33
- // OriginalDate (normal standard) behavior
34
- // * Date(anything) gives a string with the current time
35
- // * new Date() returns the current time, as a Date object
36
- // SharedDate behavior
37
- // * Date(anything) returned 'Invalid Date'
38
- // * new Date() returns a Date object which stringifies to
39
- // 'Invalid Date'
29
+ /**
30
+ * Tame the Date constructor.
31
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
32
+ *
33
+ * Common behavior
34
+ * * `new Date(x)` coerces x into a number and then returns a Date
35
+ * for that number of millis since the epoch
36
+ * * `new Date(NaN)` returns a Date object which stringifies to
37
+ * 'Invalid Date'
38
+ * * `new Date(undefined)` returns a Date object which stringifies to
39
+ * 'Invalid Date'
40
+ *
41
+ * OriginalDate (normal standard) behavior preserved by
42
+ * `%InitialDate%`.
43
+ * * `Date(anything)` gives a string with the current time
44
+ * * `new Date()` returns the current time, as a Date object
45
+ *
46
+ * `%SharedDate%` behavior
47
+ * * `Date(anything)` throws a TypeError starting with "secure mode"
48
+ * * `new Date()` throws a TypeError starting with "secure mode"
49
+ *
50
+ * @param {{powers?: string} } [opts]
51
+ */
40
52
const makeDateConstructor = ( { powers = 'none' } = { } ) => {
41
53
let ResultDate ;
42
54
if ( powers === 'original' ) {
@@ -51,10 +63,14 @@ export default function tameDateConstructor(dateTaming = 'safe') {
51
63
// eslint-disable-next-line no-shadow
52
64
ResultDate = function Date ( ...rest ) {
53
65
if ( new . target === undefined ) {
54
- return 'Invalid Date' ;
66
+ throw TypeError (
67
+ 'secure mode Calling %SharedDate% constructor as a function throws' ,
68
+ ) ;
55
69
}
56
70
if ( rest . length === 0 ) {
57
- rest = [ NaN ] ;
71
+ throw TypeError (
72
+ 'secure mode Calling new %SharedDate%() with no arguments throws' ,
73
+ ) ;
58
74
}
59
75
return construct ( OriginalDate , rest , new . target ) ;
60
76
} ;
@@ -69,13 +85,13 @@ export default function tameDateConstructor(dateTaming = 'safe') {
69
85
configurable : false ,
70
86
} ,
71
87
parse : {
72
- value : Date . parse ,
88
+ value : OriginalDate . parse ,
73
89
writable : true ,
74
90
enumerable : false ,
75
91
configurable : true ,
76
92
} ,
77
93
UTC : {
78
- value : Date . UTC ,
94
+ value : OriginalDate . UTC ,
79
95
writable : true ,
80
96
enumerable : false ,
81
97
configurable : true ,
@@ -88,7 +104,7 @@ export default function tameDateConstructor(dateTaming = 'safe') {
88
104
89
105
defineProperties ( InitialDate , {
90
106
now : {
91
- value : Date . now ,
107
+ value : OriginalDate . now ,
92
108
writable : true ,
93
109
enumerable : false ,
94
110
configurable : true ,
0 commit comments