Skip to content

Commit 1fad3d2

Browse files
committed
Add initial Date bindings
Doesn't do much, yet, because we can't get the timezone with a plain binding, but it's at least something.
1 parent 410036e commit 1fad3d2

File tree

8 files changed

+640
-1
lines changed

8 files changed

+640
-1
lines changed

std/assembly/bindings/Date.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare function UTC(
2+
year: i32,
3+
month: i32,
4+
day: i32,
5+
hour: i32,
6+
minute: i32,
7+
second: i32,
8+
millisecond: i32
9+
): f64;
10+
export declare function now(): f64;

std/assembly/date.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
UTC as Date_UTC,
3+
now as Date_now
4+
} from "./bindings/Date";
5+
6+
export class Date {
7+
8+
@inline static UTC(
9+
year: i32,
10+
month: i32 = 0,
11+
day: i32 = 1,
12+
hour: i32 = 0,
13+
minute: i32 = 0,
14+
second: i32 = 0,
15+
millisecond: i32 = 0
16+
): i64 {
17+
return <i64>Date_UTC(year, month, day, hour, minute, second, millisecond);
18+
}
19+
20+
@inline static now(): i64 {
21+
return <i64>Date_now();
22+
}
23+
24+
private value: i64;
25+
26+
constructor(value: i64) {
27+
this.value = value;
28+
}
29+
30+
getTime(): i64 {
31+
return this.value;
32+
}
33+
34+
setTime(value: i64): void {
35+
this.value = value;
36+
}
37+
}

std/assembly/index.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,27 @@ declare const Math: IMath<f64>;
657657
/** Alias of {@link NativeMathf} or {@link JSMath} respectively. Defaults to `NativeMathf`. */
658658
declare const Mathf: IMath<f32>;
659659

660+
declare class Date {
661+
/** Returns the UTC timestamp in milliseconds of the specified date. */
662+
static UTC(
663+
year: i32,
664+
month: i32,
665+
day: i32,
666+
hour: i32,
667+
minute: i32,
668+
second: i32,
669+
millisecond: i32
670+
): i64;
671+
/** Returns the current UTC timestamp in milliseconds. */
672+
static now(): i64;
673+
/** Constructs a new date object from an UTC timestamp in milliseconds. */
674+
constructor(value: i64);
675+
/** Returns the UTC timestamp of this date in milliseconds. */
676+
getTime(): i64;
677+
/** Sets the UTC timestamp of this date in milliseconds. */
678+
setTime(value: i64): void;
679+
}
680+
660681
/** Environmental tracing function for debugging purposes. */
661682
declare function trace(msg: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void;
662683

std/portable/index.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,27 @@ declare const Math: IMath;
424424
declare const Mathf: IMath;
425425
declare const JSMath: IMath;
426426

427+
declare class Date {
428+
/** Returns the UTC timestamp in milliseconds of the specified date. */
429+
static UTC(
430+
year: i32,
431+
month: i32,
432+
day: i32,
433+
hour: i32,
434+
minute: i32,
435+
second: i32,
436+
millisecond: i32
437+
): number;
438+
/** Returns the current UTC timestamp in milliseconds. */
439+
static now(): number;
440+
/** Constructs a new date object from an UTC timestamp in milliseconds. */
441+
constructor(value: number);
442+
/** Returns the UTC timestamp of this date in milliseconds. */
443+
getTime(): number;
444+
/** Sets the UTC timestamp of this date in milliseconds. */
445+
setTime(value: number): void;
446+
}
447+
427448
declare namespace console {
428449
/** @deprecated */
429450
function log(message: string): void;

tests/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ tests.forEach(filename => {
192192
math: {
193193
mod: function(a, b) { return a % b; }
194194
},
195-
Math: Math,
195+
Math,
196+
Date,
196197

197198
// tests/declare
198199
declare: {
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
(module
2+
(type $iiiiiiiF (func (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
3+
(type $iiiiv (func (param i32 i32 i32 i32)))
4+
(type $F (func (result f64)))
5+
(type $ii (func (param i32) (result i32)))
6+
(type $v (func))
7+
(import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
8+
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
9+
(import "Date" "now" (func $~lib/bindings/Date/now (result f64)))
10+
(memory $0 1)
11+
(data (i32.const 8) "\0b\00\00\00s\00t\00d\00/\00d\00a\00t\00e\00.\00t\00s")
12+
(table $0 1 anyfunc)
13+
(elem (i32.const 0) $null)
14+
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
15+
(global $~lib/allocator/arena/offset (mut i32) (i32.const 0))
16+
(global $std/date/creationTime (mut i64) (i64.const 0))
17+
(global $std/date/date (mut i32) (i32.const 0))
18+
(export "memory" (memory $0))
19+
(export "table" (table $0))
20+
(start $start)
21+
(func $~lib/allocator/arena/__memory_allocate (; 3 ;) (type $ii) (param $0 i32) (result i32)
22+
(local $1 i32)
23+
(local $2 i32)
24+
(local $3 i32)
25+
get_local $0
26+
i32.const 1073741824
27+
i32.gt_u
28+
if
29+
unreachable
30+
end
31+
get_global $~lib/allocator/arena/offset
32+
tee_local $1
33+
get_local $0
34+
i32.const 1
35+
get_local $0
36+
i32.const 1
37+
i32.gt_u
38+
select
39+
i32.add
40+
i32.const 7
41+
i32.add
42+
i32.const -8
43+
i32.and
44+
tee_local $2
45+
current_memory
46+
tee_local $3
47+
i32.const 16
48+
i32.shl
49+
i32.gt_u
50+
if
51+
get_local $3
52+
get_local $2
53+
get_local $1
54+
i32.sub
55+
i32.const 65535
56+
i32.add
57+
i32.const -65536
58+
i32.and
59+
i32.const 16
60+
i32.shr_u
61+
tee_local $0
62+
get_local $3
63+
get_local $0
64+
i32.gt_s
65+
select
66+
grow_memory
67+
i32.const 0
68+
i32.lt_s
69+
if
70+
get_local $0
71+
grow_memory
72+
i32.const 0
73+
i32.lt_s
74+
if
75+
unreachable
76+
end
77+
end
78+
end
79+
get_local $2
80+
set_global $~lib/allocator/arena/offset
81+
get_local $1
82+
)
83+
(func $start (; 4 ;) (type $v)
84+
(local $0 i32)
85+
(local $1 i64)
86+
i32.const 40
87+
set_global $~lib/allocator/arena/startOffset
88+
get_global $~lib/allocator/arena/startOffset
89+
set_global $~lib/allocator/arena/offset
90+
i32.const 1970
91+
i32.const 0
92+
i32.const 1
93+
i32.const 0
94+
i32.const 0
95+
i32.const 0
96+
i32.const 0
97+
call $~lib/bindings/Date/UTC
98+
i64.trunc_s/f64
99+
i64.const 0
100+
i64.ne
101+
if
102+
i32.const 0
103+
i32.const 8
104+
i32.const 3
105+
i32.const 0
106+
call $~lib/env/abort
107+
unreachable
108+
end
109+
i32.const 1970
110+
i32.const 0
111+
i32.const 1
112+
i32.const 0
113+
i32.const 0
114+
i32.const 0
115+
i32.const 0
116+
call $~lib/bindings/Date/UTC
117+
i64.trunc_s/f64
118+
i64.const 0
119+
i64.ne
120+
if
121+
i32.const 0
122+
i32.const 8
123+
i32.const 4
124+
i32.const 0
125+
call $~lib/env/abort
126+
unreachable
127+
end
128+
i32.const 2018
129+
i32.const 10
130+
i32.const 10
131+
i32.const 11
132+
i32.const 0
133+
i32.const 0
134+
i32.const 1
135+
call $~lib/bindings/Date/UTC
136+
i64.trunc_s/f64
137+
set_global $std/date/creationTime
138+
get_global $std/date/creationTime
139+
i64.const 1541847600001
140+
i64.ne
141+
if
142+
i32.const 0
143+
i32.const 8
144+
i32.const 7
145+
i32.const 0
146+
call $~lib/env/abort
147+
unreachable
148+
end
149+
call $~lib/bindings/Date/now
150+
i64.trunc_s/f64
151+
get_global $std/date/creationTime
152+
i64.le_s
153+
if
154+
i32.const 0
155+
i32.const 8
156+
i32.const 9
157+
i32.const 0
158+
call $~lib/env/abort
159+
unreachable
160+
end
161+
get_global $std/date/creationTime
162+
set_local $1
163+
i32.const 8
164+
call $~lib/allocator/arena/__memory_allocate
165+
tee_local $0
166+
i64.const 0
167+
i64.store
168+
get_local $0
169+
get_local $1
170+
i64.store
171+
get_local $0
172+
set_global $std/date/date
173+
get_global $std/date/date
174+
i64.load
175+
get_global $std/date/creationTime
176+
i64.ne
177+
if
178+
i32.const 0
179+
i32.const 8
180+
i32.const 12
181+
i32.const 0
182+
call $~lib/env/abort
183+
unreachable
184+
end
185+
get_global $std/date/date
186+
get_global $std/date/creationTime
187+
i64.const 1
188+
i64.add
189+
i64.store
190+
get_global $std/date/date
191+
i64.load
192+
get_global $std/date/creationTime
193+
i64.const 1
194+
i64.add
195+
i64.ne
196+
if
197+
i32.const 0
198+
i32.const 8
199+
i32.const 14
200+
i32.const 0
201+
call $~lib/env/abort
202+
unreachable
203+
end
204+
)
205+
(func $null (; 5 ;) (type $v)
206+
nop
207+
)
208+
)

tests/compiler/std/date.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "allocator/arena";
2+
3+
assert(Date.UTC(1970, 0, 1) == 0);
4+
assert(Date.UTC(1970, 0, 1, 0, 0, 0, 0) == 0);
5+
6+
var creationTime = Date.UTC(2018, 10, 10, 11, 0, 0, 1);
7+
assert(creationTime == 1541847600001);
8+
9+
assert(Date.now() > creationTime);
10+
11+
var date = new Date(creationTime);
12+
assert(date.getTime() == creationTime);
13+
date.setTime(creationTime + 1);
14+
assert(date.getTime() == creationTime + 1);

0 commit comments

Comments
 (0)