From 9ab7384122956b46108c143a3703875f9bf6a380 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sun, 11 Nov 2018 10:43:17 +0100 Subject: [PATCH] Improve date binding compatibility --- std/assembly/bindings/Date.ts | 4 +++- std/assembly/date.ts | 7 ++++--- std/assembly/index.d.ts | 2 +- std/portable/index.d.ts | 2 +- tests/compiler/std/date.optimized.wat | 10 ++++----- tests/compiler/std/date.untouched.wat | 29 ++++++++++++++++----------- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/std/assembly/bindings/Date.ts b/std/assembly/bindings/Date.ts index 80fcc5f9..c02c9a6d 100644 --- a/std/assembly/bindings/Date.ts +++ b/std/assembly/bindings/Date.ts @@ -1,10 +1,12 @@ export declare function UTC( + // NOTE: Using i32 below saves us a f64.convert_s instruction and moves the responsibility for + // converting the value to the WASM/JS boundary. year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32, - millisecond: i32 + millisecond: f64 ): f64; export declare function now(): f64; diff --git a/std/assembly/date.ts b/std/assembly/date.ts index 23655350..b2e7eff6 100644 --- a/std/assembly/date.ts +++ b/std/assembly/date.ts @@ -12,9 +12,9 @@ export class Date { hour: i32 = 0, minute: i32 = 0, second: i32 = 0, - millisecond: i32 = 0 + millisecond: i64 = 0 ): i64 { - return Date_UTC(year, month, day, hour, minute, second, millisecond); + return Date_UTC(year, month, day, hour, minute, second, millisecond); } @inline static now(): i64 { @@ -31,7 +31,8 @@ export class Date { return this.value; } - setTime(value: i64): void { + setTime(value: i64): i64 { this.value = value; + return value; } } diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index 1cc8f0e5..70010ad1 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -675,7 +675,7 @@ declare class Date { /** Returns the UTC timestamp of this date in milliseconds. */ getTime(): i64; /** Sets the UTC timestamp of this date in milliseconds. */ - setTime(value: i64): void; + setTime(value: i64): i64; } /** Environmental tracing function for debugging purposes. */ diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index 82c0a0b0..04dff225 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -442,7 +442,7 @@ declare class Date { /** Returns the UTC timestamp of this date in milliseconds. */ getTime(): number; /** Sets the UTC timestamp of this date in milliseconds. */ - setTime(value: number): void; + setTime(value: number): number; } declare namespace console { diff --git a/tests/compiler/std/date.optimized.wat b/tests/compiler/std/date.optimized.wat index b8df80fb..05f6b6dd 100644 --- a/tests/compiler/std/date.optimized.wat +++ b/tests/compiler/std/date.optimized.wat @@ -1,10 +1,10 @@ (module - (type $iiiiiiiF (func (param i32 i32 i32 i32 i32 i32 i32) (result f64))) + (type $iiiiiiFF (func (param i32 i32 i32 i32 i32 i32 f64) (result f64))) (type $iiiiv (func (param i32 i32 i32 i32))) (type $F (func (result f64))) (type $ii (func (param i32) (result i32))) (type $v (func)) - (import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 i32) (result f64))) + (import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 f64) (result f64))) (import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32))) (import "Date" "now" (func $~lib/bindings/Date/now (result f64))) (memory $0 1) @@ -93,7 +93,7 @@ i32.const 0 i32.const 0 i32.const 0 - i32.const 0 + f64.const 0 call $~lib/bindings/Date/UTC i64.trunc_s/f64 i64.const 0 @@ -112,7 +112,7 @@ i32.const 0 i32.const 0 i32.const 0 - i32.const 0 + f64.const 0 call $~lib/bindings/Date/UTC i64.trunc_s/f64 i64.const 0 @@ -131,7 +131,7 @@ i32.const 11 i32.const 0 i32.const 0 - i32.const 1 + f64.const 1 call $~lib/bindings/Date/UTC i64.trunc_s/f64 set_global $std/date/creationTime diff --git a/tests/compiler/std/date.untouched.wat b/tests/compiler/std/date.untouched.wat index 18c36586..18a78299 100644 --- a/tests/compiler/std/date.untouched.wat +++ b/tests/compiler/std/date.untouched.wat @@ -1,13 +1,13 @@ (module - (type $iiiiiiiF (func (param i32 i32 i32 i32 i32 i32 i32) (result f64))) + (type $iiiiiiFF (func (param i32 i32 i32 i32 i32 i32 f64) (result f64))) (type $iiiiv (func (param i32 i32 i32 i32))) (type $F (func (result f64))) (type $iIi (func (param i32 i64) (result i32))) (type $ii (func (param i32) (result i32))) (type $iI (func (param i32) (result i64))) - (type $iIv (func (param i32 i64))) + (type $iII (func (param i32 i64) (result i64))) (type $v (func)) - (import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 i32) (result f64))) + (import "Date" "UTC" (func $~lib/bindings/Date/UTC (param i32 i32 i32 i32 i32 i32 f64) (result f64))) (import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32))) (import "Date" "now" (func $~lib/bindings/Date/now (result f64))) (memory $0 1) @@ -136,10 +136,11 @@ get_local $0 i64.load ) - (func $~lib/date/Date#setTime (; 7 ;) (type $iIv) (param $0 i32) (param $1 i64) + (func $~lib/date/Date#setTime (; 7 ;) (type $iII) (param $0 i32) (param $1 i64) (result i64) get_local $0 get_local $1 i64.store + get_local $1 ) (func $start (; 8 ;) (type $v) (local $0 i32) @@ -148,7 +149,7 @@ (local $3 i32) (local $4 i32) (local $5 i32) - (local $6 i32) + (local $6 i64) get_global $HEAP_BASE get_global $~lib/internal/allocator/AL_MASK i32.add @@ -172,7 +173,7 @@ set_local $4 i32.const 0 set_local $5 - i32.const 0 + i64.const 0 set_local $6 get_local $0 get_local $1 @@ -181,6 +182,7 @@ get_local $4 get_local $5 get_local $6 + f64.convert_s/i64 call $~lib/bindings/Date/UTC i64.trunc_s/f64 end @@ -197,12 +199,10 @@ end block $~lib/date/Date.UTC|inlined.1 (result i64) i32.const 1970 - set_local $6 - i32.const 0 set_local $5 - i32.const 1 - set_local $4 i32.const 0 + set_local $4 + i32.const 1 set_local $3 i32.const 0 set_local $2 @@ -210,13 +210,16 @@ set_local $1 i32.const 0 set_local $0 - get_local $6 + i64.const 0 + set_local $6 get_local $5 get_local $4 get_local $3 get_local $2 get_local $1 get_local $0 + get_local $6 + f64.convert_s/i64 call $~lib/bindings/Date/UTC i64.trunc_s/f64 end @@ -244,7 +247,7 @@ set_local $4 i32.const 0 set_local $5 - i32.const 1 + i64.const 1 set_local $6 get_local $0 get_local $1 @@ -253,6 +256,7 @@ get_local $4 get_local $5 get_local $6 + f64.convert_s/i64 call $~lib/bindings/Date/UTC i64.trunc_s/f64 end @@ -306,6 +310,7 @@ i64.const 1 i64.add call $~lib/date/Date#setTime + drop get_global $std/date/date call $~lib/date/Date#getTime get_global $std/date/creationTime