Skip to content

Commit ab5a938

Browse files
committed
parseInt compatibility layer around parseI64, see #19
1 parent cae89e0 commit ab5a938

File tree

9 files changed

+282
-41
lines changed

9 files changed

+282
-41
lines changed

std/assembly.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ declare function changetype<T>(value: any): T;
189189
declare function isNaN<T = f32 | f64>(value: T): bool;
190190
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
191191
declare function isFinite<T = f32 | f64>(value: T): bool;
192-
/** Traps if the specified value is not true-ish, otherwise returns the value. */
192+
/** Traps if the specified value is not true-ish, otherwise returns the (non-nullable) value. */
193193
declare function assert<T>(isTrueish: T, message?: string): T & object; // any better way to model `: T != null`?
194194
/** Parses an integer string to a 64-bit float. */
195195
declare function parseInt(str: string, radix?: i32): f64;
196+
/** Parses an integer string to a 64-bit integer. */
197+
declare function parseI64(str: string, radix?: i32): i64;
196198
/** Parses a string to a 64-bit float. */
197199
declare function parseFloat(str: string): f64;
198200

std/assembly/string.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,30 @@ const enum CharCode {
350350
z = 0x7A
351351
}
352352

353-
export function parseInt(str: String, radix: i32 = 0): i64 {
353+
export function parseInt(str: String, radix: i32 = 0): f64 {
354+
var len: i32 = str.length;
355+
if (!len)
356+
return NaN;
357+
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
358+
var code = <i32>load<u16>(ptr, HEAD);
359+
360+
// pre-check sign
361+
if (code == CharCode.MINUS) {
362+
if (!--len)
363+
return NaN;
364+
} else if (code == CharCode.PLUS) {
365+
if (!--len)
366+
return NaN;
367+
}
368+
369+
// pre-check radix
370+
if (radix && (radix < 2 || radix > 36))
371+
return NaN;
372+
373+
return <f64>parseI64(str, radix);
374+
}
375+
376+
export function parseI64(str: String, radix: i32 = 0): i64 {
354377
var len: i32 = str.length;
355378
if (!len)
356379
return 0; // (NaN)
@@ -422,7 +445,7 @@ export function parseInt(str: String, radix: i32 = 0): i64 {
422445
return sign * num;
423446
}
424447

425-
export function parseFloat(str: string): f64 {
448+
export function parseFloat(str: String): f64 {
426449
var len: i32 = str.length;
427450
if (!len)
428451
return NaN;

tests/compiler/std/array.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,6 +4077,8 @@
40774077
ENUM: std:string/CharCode
40784078
FUNCTION_PROTOTYPE: std:string/parseInt
40794079
FUNCTION_PROTOTYPE: parseInt
4080+
FUNCTION_PROTOTYPE: std:string/parseI64
4081+
FUNCTION_PROTOTYPE: parseI64
40804082
FUNCTION_PROTOTYPE: std:string/parseFloat
40814083
FUNCTION_PROTOTYPE: parseFloat
40824084
GLOBAL: std/array/arr
@@ -4116,6 +4118,8 @@
41164118
CLASS_PROTOTYPE: String
41174119
FUNCTION_PROTOTYPE: parseInt
41184120
FUNCTION_PROTOTYPE: std:string/parseInt
4121+
FUNCTION_PROTOTYPE: parseI64
4122+
FUNCTION_PROTOTYPE: std:string/parseI64
41194123
FUNCTION_PROTOTYPE: parseFloat
41204124
FUNCTION_PROTOTYPE: std:string/parseFloat
41214125
FUNCTION_PROTOTYPE: allocate_memory

tests/compiler/std/carray.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@
284284
ENUM: std:string/CharCode
285285
FUNCTION_PROTOTYPE: std:string/parseInt
286286
FUNCTION_PROTOTYPE: parseInt
287+
FUNCTION_PROTOTYPE: std:string/parseI64
288+
FUNCTION_PROTOTYPE: parseI64
287289
FUNCTION_PROTOTYPE: std:string/parseFloat
288290
FUNCTION_PROTOTYPE: parseFloat
289291
GLOBAL: std/carray/arr
@@ -312,6 +314,8 @@
312314
CLASS_PROTOTYPE: String
313315
FUNCTION_PROTOTYPE: parseInt
314316
FUNCTION_PROTOTYPE: std:string/parseInt
317+
FUNCTION_PROTOTYPE: parseI64
318+
FUNCTION_PROTOTYPE: std:string/parseI64
315319
FUNCTION_PROTOTYPE: parseFloat
316320
FUNCTION_PROTOTYPE: std:string/parseFloat
317321
;)

tests/compiler/std/heap.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,8 @@
28932893
ENUM: std:string/CharCode
28942894
FUNCTION_PROTOTYPE: std:string/parseInt
28952895
FUNCTION_PROTOTYPE: parseInt
2896+
FUNCTION_PROTOTYPE: std:string/parseI64
2897+
FUNCTION_PROTOTYPE: parseI64
28962898
FUNCTION_PROTOTYPE: std:string/parseFloat
28972899
FUNCTION_PROTOTYPE: parseFloat
28982900
GLOBAL: std/heap/size
@@ -2934,6 +2936,8 @@
29342936
CLASS_PROTOTYPE: String
29352937
FUNCTION_PROTOTYPE: parseInt
29362938
FUNCTION_PROTOTYPE: std:string/parseInt
2939+
FUNCTION_PROTOTYPE: parseI64
2940+
FUNCTION_PROTOTYPE: std:string/parseI64
29372941
FUNCTION_PROTOTYPE: parseFloat
29382942
FUNCTION_PROTOTYPE: std:string/parseFloat
29392943
FUNCTION_PROTOTYPE: allocate_memory

tests/compiler/std/new.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@
234234
ENUM: std:string/CharCode
235235
FUNCTION_PROTOTYPE: std:string/parseInt
236236
FUNCTION_PROTOTYPE: parseInt
237+
FUNCTION_PROTOTYPE: std:string/parseI64
238+
FUNCTION_PROTOTYPE: parseI64
237239
FUNCTION_PROTOTYPE: std:string/parseFloat
238240
FUNCTION_PROTOTYPE: parseFloat
239241
CLASS_PROTOTYPE: std/new/AClass
@@ -274,6 +276,8 @@
274276
CLASS_PROTOTYPE: String
275277
FUNCTION_PROTOTYPE: parseInt
276278
FUNCTION_PROTOTYPE: std:string/parseInt
279+
FUNCTION_PROTOTYPE: parseI64
280+
FUNCTION_PROTOTYPE: std:string/parseI64
277281
FUNCTION_PROTOTYPE: parseFloat
278282
FUNCTION_PROTOTYPE: std:string/parseFloat
279283
FUNCTION_PROTOTYPE: allocate_memory

tests/compiler/std/set.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,6 +2802,8 @@
28022802
ENUM: std:string/CharCode
28032803
FUNCTION_PROTOTYPE: std:string/parseInt
28042804
FUNCTION_PROTOTYPE: parseInt
2805+
FUNCTION_PROTOTYPE: std:string/parseI64
2806+
FUNCTION_PROTOTYPE: parseI64
28052807
FUNCTION_PROTOTYPE: std:string/parseFloat
28062808
FUNCTION_PROTOTYPE: parseFloat
28072809
GLOBAL: std/set/set
@@ -2840,6 +2842,8 @@
28402842
CLASS_PROTOTYPE: String
28412843
FUNCTION_PROTOTYPE: parseInt
28422844
FUNCTION_PROTOTYPE: std:string/parseInt
2845+
FUNCTION_PROTOTYPE: parseI64
2846+
FUNCTION_PROTOTYPE: std:string/parseI64
28432847
FUNCTION_PROTOTYPE: parseFloat
28442848
FUNCTION_PROTOTYPE: std:string/parseFloat
28452849
FUNCTION_PROTOTYPE: allocate_memory

tests/compiler/std/string.optimized.wast

Lines changed: 106 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(type $iii (func (param i32 i32) (result i32)))
44
(type $iiii (func (param i32 i32 i32) (result i32)))
55
(type $iiI (func (param i32 i32) (result i64)))
6+
(type $iiF (func (param i32 i32) (result f64)))
67
(type $iF (func (param i32) (result f64)))
78
(type $v (func))
89
(global $std/string/str (mut i32) (i32.const 8))
@@ -374,7 +375,7 @@
374375
(func $std/string/getString (; 5 ;) (type $i) (result i32)
375376
(get_global $std/string/str)
376377
)
377-
(func $std:string/parseInt (; 6 ;) (type $iiI) (param $0 i32) (param $1 i32) (result i64)
378+
(func $std:string/parseI64 (; 6 ;) (type $iiI) (param $0 i32) (param $1 i32) (result i64)
378379
(local $2 i32)
379380
(local $3 i32)
380381
(local $4 i64)
@@ -741,7 +742,93 @@
741742
(get_local $4)
742743
)
743744
)
744-
(func $std:string/parseFloat (; 7 ;) (type $iF) (param $0 i32) (result f64)
745+
(func $std:string/parseInt (; 7 ;) (type $iiF) (param $0 i32) (param $1 i32) (result f64)
746+
(local $2 i32)
747+
(local $3 i32)
748+
(if
749+
(i32.eqz
750+
(tee_local $2
751+
(i32.load
752+
(get_local $0)
753+
)
754+
)
755+
)
756+
(return
757+
(f64.const nan:0x8000000000000)
758+
)
759+
)
760+
(if
761+
(i32.eq
762+
(tee_local $3
763+
(i32.load16_u offset=4
764+
(get_local $0)
765+
)
766+
)
767+
(i32.const 45)
768+
)
769+
(if
770+
(i32.eqz
771+
(i32.sub
772+
(get_local $2)
773+
(i32.const 1)
774+
)
775+
)
776+
(return
777+
(f64.const nan:0x8000000000000)
778+
)
779+
)
780+
(if
781+
(i32.eq
782+
(get_local $3)
783+
(i32.const 43)
784+
)
785+
(if
786+
(i32.eqz
787+
(i32.sub
788+
(get_local $2)
789+
(i32.const 1)
790+
)
791+
)
792+
(return
793+
(f64.const nan:0x8000000000000)
794+
)
795+
)
796+
)
797+
)
798+
(if
799+
(select
800+
(i32.and
801+
(select
802+
(i32.lt_s
803+
(get_local $1)
804+
(i32.const 2)
805+
)
806+
(i32.gt_s
807+
(get_local $1)
808+
(i32.const 36)
809+
)
810+
(i32.lt_s
811+
(get_local $1)
812+
(i32.const 2)
813+
)
814+
)
815+
(i32.const 1)
816+
)
817+
(get_local $1)
818+
(get_local $1)
819+
)
820+
(return
821+
(f64.const nan:0x8000000000000)
822+
)
823+
)
824+
(f64.convert_s/i64
825+
(call $std:string/parseI64
826+
(get_local $0)
827+
(get_local $1)
828+
)
829+
)
830+
)
831+
(func $std:string/parseFloat (; 8 ;) (type $iF) (param $0 i32) (result f64)
745832
(local $1 i32)
746833
(local $2 i32)
747834
(local $3 f64)
@@ -985,7 +1072,7 @@
9851072
(get_local $3)
9861073
)
9871074
)
988-
(func $start (; 8 ;) (type $v)
1075+
(func $start (; 9 ;) (type $v)
9891076
(if
9901077
(i32.ne
9911078
(get_global $std/string/str)
@@ -1068,82 +1155,82 @@
10681155
(unreachable)
10691156
)
10701157
(if
1071-
(i64.ne
1158+
(f64.ne
10721159
(call $std:string/parseInt
10731160
(i32.const 120)
10741161
(i32.const 0)
10751162
)
1076-
(i64.const 0)
1163+
(f64.const 0)
10771164
)
10781165
(unreachable)
10791166
)
10801167
(if
1081-
(i64.ne
1168+
(f64.ne
10821169
(call $std:string/parseInt
10831170
(i32.const 128)
10841171
(i32.const 0)
10851172
)
1086-
(i64.const 1)
1173+
(f64.const 1)
10871174
)
10881175
(unreachable)
10891176
)
10901177
(if
1091-
(i64.ne
1178+
(f64.ne
10921179
(call $std:string/parseInt
10931180
(i32.const 136)
10941181
(i32.const 0)
10951182
)
1096-
(i64.const 5)
1183+
(f64.const 5)
10971184
)
10981185
(unreachable)
10991186
)
11001187
(if
1101-
(i64.ne
1188+
(f64.ne
11021189
(call $std:string/parseInt
11031190
(i32.const 152)
11041191
(i32.const 0)
11051192
)
1106-
(i64.const 455)
1193+
(f64.const 455)
11071194
)
11081195
(unreachable)
11091196
)
11101197
(if
1111-
(i64.ne
1198+
(f64.ne
11121199
(call $std:string/parseInt
11131200
(i32.const 168)
11141201
(i32.const 0)
11151202
)
1116-
(i64.const 3855)
1203+
(f64.const 3855)
11171204
)
11181205
(unreachable)
11191206
)
11201207
(if
1121-
(i64.ne
1208+
(f64.ne
11221209
(call $std:string/parseInt
11231210
(i32.const 184)
11241211
(i32.const 0)
11251212
)
1126-
(i64.const 3855)
1213+
(f64.const 3855)
11271214
)
11281215
(unreachable)
11291216
)
11301217
(if
1131-
(i64.ne
1218+
(f64.ne
11321219
(call $std:string/parseInt
11331220
(i32.const 200)
11341221
(i32.const 0)
11351222
)
1136-
(i64.const 11)
1223+
(f64.const 11)
11371224
)
11381225
(unreachable)
11391226
)
11401227
(if
1141-
(i64.ne
1228+
(f64.ne
11421229
(call $std:string/parseInt
11431230
(i32.const 216)
11441231
(i32.const 0)
11451232
)
1146-
(i64.const 1)
1233+
(f64.const 1)
11471234
)
11481235
(unreachable)
11491236
)

0 commit comments

Comments
 (0)