Skip to content

Commit 9ab7384

Browse files
committed
Improve date binding compatibility
1 parent 1fad3d2 commit 9ab7384

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

std/assembly/bindings/Date.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export declare function UTC(
2+
// NOTE: Using i32 below saves us a f64.convert_s instruction and moves the responsibility for
3+
// converting the value to the WASM/JS boundary.
24
year: i32,
35
month: i32,
46
day: i32,
57
hour: i32,
68
minute: i32,
79
second: i32,
8-
millisecond: i32
10+
millisecond: f64
911
): f64;
1012
export declare function now(): f64;

std/assembly/date.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export class Date {
1212
hour: i32 = 0,
1313
minute: i32 = 0,
1414
second: i32 = 0,
15-
millisecond: i32 = 0
15+
millisecond: i64 = 0
1616
): i64 {
17-
return <i64>Date_UTC(year, month, day, hour, minute, second, millisecond);
17+
return <i64>Date_UTC(year, month, day, hour, minute, second, <f64>millisecond);
1818
}
1919

2020
@inline static now(): i64 {
@@ -31,7 +31,8 @@ export class Date {
3131
return this.value;
3232
}
3333

34-
setTime(value: i64): void {
34+
setTime(value: i64): i64 {
3535
this.value = value;
36+
return value;
3637
}
3738
}

std/assembly/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ declare class Date {
675675
/** Returns the UTC timestamp of this date in milliseconds. */
676676
getTime(): i64;
677677
/** Sets the UTC timestamp of this date in milliseconds. */
678-
setTime(value: i64): void;
678+
setTime(value: i64): i64;
679679
}
680680

681681
/** Environmental tracing function for debugging purposes. */

std/portable/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ declare class Date {
442442
/** Returns the UTC timestamp of this date in milliseconds. */
443443
getTime(): number;
444444
/** Sets the UTC timestamp of this date in milliseconds. */
445-
setTime(value: number): void;
445+
setTime(value: number): number;
446446
}
447447

448448
declare namespace console {

tests/compiler/std/date.optimized.wat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(module
2-
(type $iiiiiiiF (func (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
2+
(type $iiiiiiFF (func (param i32 i32 i32 i32 i32 i32 f64) (result f64)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $F (func (result f64)))
55
(type $ii (func (param i32) (result i32)))
66
(type $v (func))
7-
(import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
7+
(import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 f64) (result f64)))
88
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
99
(import "Date" "now" (func $~lib/bindings/Date/now (result f64)))
1010
(memory $0 1)
@@ -93,7 +93,7 @@
9393
i32.const 0
9494
i32.const 0
9595
i32.const 0
96-
i32.const 0
96+
f64.const 0
9797
call $~lib/bindings/Date/UTC
9898
i64.trunc_s/f64
9999
i64.const 0
@@ -112,7 +112,7 @@
112112
i32.const 0
113113
i32.const 0
114114
i32.const 0
115-
i32.const 0
115+
f64.const 0
116116
call $~lib/bindings/Date/UTC
117117
i64.trunc_s/f64
118118
i64.const 0
@@ -131,7 +131,7 @@
131131
i32.const 11
132132
i32.const 0
133133
i32.const 0
134-
i32.const 1
134+
f64.const 1
135135
call $~lib/bindings/Date/UTC
136136
i64.trunc_s/f64
137137
set_global $std/date/creationTime

tests/compiler/std/date.untouched.wat

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(module
2-
(type $iiiiiiiF (func (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
2+
(type $iiiiiiFF (func (param i32 i32 i32 i32 i32 i32 f64) (result f64)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $F (func (result f64)))
55
(type $iIi (func (param i32 i64) (result i32)))
66
(type $ii (func (param i32) (result i32)))
77
(type $iI (func (param i32) (result i64)))
8-
(type $iIv (func (param i32 i64)))
8+
(type $iII (func (param i32 i64) (result i64)))
99
(type $v (func))
10-
(import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 i32) (result f64)))
10+
(import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 f64) (result f64)))
1111
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
1212
(import "Date" "now" (func $~lib/bindings/Date/now (result f64)))
1313
(memory $0 1)
@@ -136,10 +136,11 @@
136136
get_local $0
137137
i64.load
138138
)
139-
(func $~lib/date/Date#setTime (; 7 ;) (type $iIv) (param $0 i32) (param $1 i64)
139+
(func $~lib/date/Date#setTime (; 7 ;) (type $iII) (param $0 i32) (param $1 i64) (result i64)
140140
get_local $0
141141
get_local $1
142142
i64.store
143+
get_local $1
143144
)
144145
(func $start (; 8 ;) (type $v)
145146
(local $0 i32)
@@ -148,7 +149,7 @@
148149
(local $3 i32)
149150
(local $4 i32)
150151
(local $5 i32)
151-
(local $6 i32)
152+
(local $6 i64)
152153
get_global $HEAP_BASE
153154
get_global $~lib/internal/allocator/AL_MASK
154155
i32.add
@@ -172,7 +173,7 @@
172173
set_local $4
173174
i32.const 0
174175
set_local $5
175-
i32.const 0
176+
i64.const 0
176177
set_local $6
177178
get_local $0
178179
get_local $1
@@ -181,6 +182,7 @@
181182
get_local $4
182183
get_local $5
183184
get_local $6
185+
f64.convert_s/i64
184186
call $~lib/bindings/Date/UTC
185187
i64.trunc_s/f64
186188
end
@@ -197,26 +199,27 @@
197199
end
198200
block $~lib/date/Date.UTC|inlined.1 (result i64)
199201
i32.const 1970
200-
set_local $6
201-
i32.const 0
202202
set_local $5
203-
i32.const 1
204-
set_local $4
205203
i32.const 0
204+
set_local $4
205+
i32.const 1
206206
set_local $3
207207
i32.const 0
208208
set_local $2
209209
i32.const 0
210210
set_local $1
211211
i32.const 0
212212
set_local $0
213-
get_local $6
213+
i64.const 0
214+
set_local $6
214215
get_local $5
215216
get_local $4
216217
get_local $3
217218
get_local $2
218219
get_local $1
219220
get_local $0
221+
get_local $6
222+
f64.convert_s/i64
220223
call $~lib/bindings/Date/UTC
221224
i64.trunc_s/f64
222225
end
@@ -244,7 +247,7 @@
244247
set_local $4
245248
i32.const 0
246249
set_local $5
247-
i32.const 1
250+
i64.const 1
248251
set_local $6
249252
get_local $0
250253
get_local $1
@@ -253,6 +256,7 @@
253256
get_local $4
254257
get_local $5
255258
get_local $6
259+
f64.convert_s/i64
256260
call $~lib/bindings/Date/UTC
257261
i64.trunc_s/f64
258262
end
@@ -306,6 +310,7 @@
306310
i64.const 1
307311
i64.add
308312
call $~lib/date/Date#setTime
313+
drop
309314
get_global $std/date/date
310315
call $~lib/date/Date#getTime
311316
get_global $std/date/creationTime

0 commit comments

Comments
 (0)