Skip to content

Commit 043c480

Browse files
handle integers as f64, error for non-numeric types
1 parent 55db705 commit 043c480

File tree

11 files changed

+458
-601
lines changed

11 files changed

+458
-601
lines changed

std/assembly/util/number.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,19 @@ function dtoa_core(buffer: usize, value: f64, isSingle: bool): i32 {
742742
@lazy @inline const dtoa_buf = memory.data(MAX_DOUBLE_LENGTH << 1);
743743

744744
export function dtoa<T extends number>(value: T): String {
745-
// assert(isFloat<T>());
746-
if (sizeof<T>() == 4) {
747-
return dtoa_impl(<f32>value, true);
748-
} else {
745+
if (isInteger<T>()) {
749746
return dtoa_impl(<f64>value, false);
750747
}
748+
749+
if (isFloat<T>()) {
750+
if (sizeof<T>() == 4) {
751+
return dtoa_impl(<f32>value, true);
752+
} else {
753+
return dtoa_impl(<f64>value, false);
754+
}
755+
}
756+
757+
ERROR("dtoa: unsupported type");
751758
}
752759

753760
// @ts-ignore: decorator
@@ -843,12 +850,19 @@ export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
843850
}
844851

845852
export function dtoa_buffered<T extends number>(buffer: usize, value: T): u32 {
846-
// assert(isFloat<T>());
847-
if (sizeof<T>() == 4) {
848-
return dtoa_buffered_impl(buffer, <f32>value, true);
849-
} else {
853+
if (isInteger<T>()) {
850854
return dtoa_buffered_impl(buffer, <f64>value, false);
851855
}
856+
857+
if (isFloat<T>()) {
858+
if (sizeof<T>() == 4) {
859+
return dtoa_buffered_impl(buffer, <f32>value, true);
860+
} else {
861+
return dtoa_buffered_impl(buffer, <f64>value, false);
862+
}
863+
}
864+
865+
ERROR("dtoa_buffered: unsupported type");
852866
}
853867

854868
// @ts-ignore: decorator

tests/compiler/issues/2873.debug.wat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3983,6 +3983,10 @@
39833983
(local $value|3 f64)
39843984
(local $isSingle i32)
39853985
(local $sign i32)
3986+
i32.const 0
3987+
drop
3988+
i32.const 1
3989+
drop
39863990
i32.const 4
39873991
i32.const 4
39883992
i32.eq
@@ -4082,6 +4086,10 @@
40824086
(local $value|3 f64)
40834087
(local $isSingle i32)
40844088
(local $sign i32)
4089+
i32.const 0
4090+
drop
4091+
i32.const 1
4092+
drop
40854093
i32.const 8
40864094
i32.const 4
40874095
i32.eq
@@ -5189,6 +5197,10 @@
51895197
global.get $~lib/memory/__stack_pointer
51905198
i32.const 0
51915199
i32.store
5200+
i32.const 0
5201+
drop
5202+
i32.const 1
5203+
drop
51925204
i32.const 4
51935205
i32.const 4
51945206
i32.eq
@@ -5270,6 +5282,10 @@
52705282
global.get $~lib/memory/__stack_pointer
52715283
i32.const 0
52725284
i32.store
5285+
i32.const 0
5286+
drop
5287+
i32.const 1
5288+
drop
52735289
i32.const 8
52745290
i32.const 4
52755291
i32.eq

tests/compiler/resolve-binary.debug.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7719,6 +7719,10 @@
77197719
global.get $~lib/memory/__stack_pointer
77207720
i32.const 0
77217721
i32.store
7722+
i32.const 0
7723+
drop
7724+
i32.const 1
7725+
drop
77227726
i32.const 8
77237727
i32.const 4
77247728
i32.eq

tests/compiler/resolve-elementaccess.debug.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5325,6 +5325,10 @@
53255325
global.get $~lib/memory/__stack_pointer
53265326
i32.const 0
53275327
i32.store
5328+
i32.const 0
5329+
drop
5330+
i32.const 1
5331+
drop
53285332
i32.const 4
53295333
i32.const 4
53305334
i32.eq

tests/compiler/resolve-ternary.debug.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4774,6 +4774,10 @@
47744774
global.get $~lib/memory/__stack_pointer
47754775
i32.const 0
47764776
i32.store
4777+
i32.const 0
4778+
drop
4779+
i32.const 1
4780+
drop
47774781
i32.const 8
47784782
i32.const 4
47794783
i32.eq

tests/compiler/std/array.debug.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8199,6 +8199,10 @@
81998199
(local $value|3 f64)
82008200
(local $isSingle i32)
82018201
(local $sign i32)
8202+
i32.const 0
8203+
drop
8204+
i32.const 1
8205+
drop
82028206
i32.const 8
82038207
i32.const 4
82048208
i32.eq
@@ -46897,6 +46901,10 @@
4689746901
global.get $~lib/memory/__stack_pointer
4689846902
i32.const 0
4689946903
i32.store
46904+
i32.const 0
46905+
drop
46906+
i32.const 1
46907+
drop
4690046908
i32.const 8
4690146909
i32.const 4
4690246910
i32.eq

0 commit comments

Comments
 (0)