Skip to content

Commit 12b3f35

Browse files
authored
feat: Introduce new parse methods for builtin value types. Deprecate XX.parseInt/parseFloat (#2465)
1 parent 61317b4 commit 12b3f35

File tree

8 files changed

+7925
-7104
lines changed

8 files changed

+7925
-7104
lines changed

Diff for: package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: std/assembly/builtins.ts

+80
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { strtol, strtod, strtob } from "./util/string";
2+
13
type auto = i32;
24

35
// @ts-ignore: decorator
@@ -283,6 +285,12 @@ export namespace i8 {
283285
// @ts-ignore: decorator
284286
@lazy
285287
export const MAX_VALUE: i8 = 127;
288+
289+
// @ts-ignore: decorator
290+
@inline
291+
export function parse(value: string, radix: i32 = 0): i8 {
292+
return <i8>strtol<i32>(value, radix);
293+
}
286294
}
287295

288296
// @ts-ignore: decorator
@@ -298,6 +306,12 @@ export namespace i16 {
298306
// @ts-ignore: decorator
299307
@lazy
300308
export const MAX_VALUE: i16 = 32767;
309+
310+
// @ts-ignore: decorator
311+
@inline
312+
export function parse(value: string, radix: i32 = 0): i16 {
313+
return <i16>strtol<i32>(value, radix);
314+
}
301315
}
302316

303317
// @ts-ignore: decorator
@@ -314,6 +328,12 @@ export namespace i32 {
314328
@lazy
315329
export const MAX_VALUE: i32 = 2147483647;
316330

331+
// @ts-ignore: decorator
332+
@inline
333+
export function parse(value: string, radix: i32 = 0): i32 {
334+
return strtol<i32>(value, radix);
335+
}
336+
317337
// @ts-ignore: decorator
318338
@builtin
319339
export declare function clz(value: i32): i32;
@@ -545,6 +565,12 @@ export namespace i64 {
545565
@lazy
546566
export const MAX_VALUE: i64 = 9223372036854775807;
547567

568+
// @ts-ignore: decorator
569+
@inline
570+
export function parse(value: string, radix: i32 = 0): i64 {
571+
return strtol<i64>(value, radix);
572+
}
573+
548574
// @ts-ignore: decorator
549575
@builtin
550576
export declare function clz(value: i64): i64;
@@ -830,6 +856,12 @@ export namespace isize {
830856
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>()
831857
? 2147483647
832858
: <isize>9223372036854775807;
859+
860+
// @ts-ignore: decorator
861+
@inline
862+
export function parse(value: string, radix: i32 = 0): isize {
863+
return <isize>strtol<i64>(value, radix);
864+
}
833865
}
834866

835867
// @ts-ignore: decorator
@@ -845,6 +877,12 @@ export namespace u8 {
845877
// @ts-ignore: decorator
846878
@lazy
847879
export const MAX_VALUE: u8 = 255;
880+
881+
// @ts-ignore: decorator
882+
@inline
883+
export function parse(value: string, radix: i32 = 0): u8 {
884+
return <u8>strtol<i32>(value, radix);
885+
}
848886
}
849887

850888
// @ts-ignore: decorator
@@ -860,6 +898,12 @@ export namespace u16 {
860898
// @ts-ignore: decorator
861899
@lazy
862900
export const MAX_VALUE: u16 = 65535;
901+
902+
// @ts-ignore: decorator
903+
@inline
904+
export function parse(value: string, radix: i32 = 0): u16 {
905+
return <u16>strtol<i32>(value, radix);
906+
}
863907
}
864908

865909
// @ts-ignore: decorator
@@ -875,6 +919,12 @@ export namespace u32 {
875919
// @ts-ignore: decorator
876920
@lazy
877921
export const MAX_VALUE: u32 = 4294967295;
922+
923+
// @ts-ignore: decorator
924+
@inline
925+
export function parse(value: string, radix: i32 = 0): u32 {
926+
return <u32>strtol<i32>(value, radix);
927+
}
878928
}
879929

880930
// @ts-ignore: decorator
@@ -890,6 +940,12 @@ export namespace u64 {
890940
// @ts-ignore: decorator
891941
@lazy
892942
export const MAX_VALUE: u64 = 18446744073709551615;
943+
944+
// @ts-ignore: decorator
945+
@inline
946+
export function parse(value: string, radix: i32 = 0): u64 {
947+
return <u64>strtol<i64>(value, radix);
948+
}
893949
}
894950

895951
// @ts-ignore: decorator
@@ -907,6 +963,12 @@ export namespace usize {
907963
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>()
908964
? 4294967295
909965
: <usize>18446744073709551615;
966+
967+
// @ts-ignore: decorator
968+
@inline
969+
export function parse(value: string, radix: i32 = 0): usize {
970+
return <usize>strtol<i64>(value, radix);
971+
}
910972
}
911973

912974
// @ts-ignore: decorator
@@ -922,6 +984,12 @@ export namespace bool {
922984
// @ts-ignore: decorator
923985
@lazy
924986
export const MAX_VALUE: bool = true;
987+
988+
// @ts-ignore: decorator
989+
@inline
990+
export function parse(value: string): bool {
991+
return strtob(value);
992+
}
925993
}
926994

927995
// @ts-ignore: decorator
@@ -966,6 +1034,12 @@ export namespace f32 {
9661034
@lazy
9671035
export const NaN: f32 = 0.0 / 0.0;
9681036

1037+
// @ts-ignore: decorator
1038+
@inline
1039+
export function parse(value: string): f32 {
1040+
return <f32>strtod(value);
1041+
}
1042+
9691043
// @ts-ignore: decorator
9701044
@builtin
9711045
export declare function abs(value: f32): f32;
@@ -1081,6 +1155,12 @@ export namespace f64 {
10811155
@lazy
10821156
export const NaN: f64 = 0.0 / 0.0;
10831157

1158+
// @ts-ignore: decorator
1159+
@inline
1160+
export function parse(value: string): f64 {
1161+
return strtod(value);
1162+
}
1163+
10841164
// @ts-ignore: decorator
10851165
@builtin
10861166
export declare function abs(value: f64): f64;

Diff for: std/assembly/index.d.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ declare namespace i32 {
310310
export const MIN_VALUE: i32;
311311
/** Largest representable value. */
312312
export const MAX_VALUE: i32;
313+
/** Converts a string to an i32 of this type. */
314+
export function parse(value: string, radix?: i32): i32;
313315
/** Loads an 8-bit signed integer value from memory and returns it as a 32-bit integer. */
314316
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i32;
315317
/** Loads an 8-bit unsigned integer value from memory and returns it as a 32-bit integer. */
@@ -433,6 +435,8 @@ declare namespace i64 {
433435
export const MIN_VALUE: i64;
434436
/** Largest representable value. */
435437
export const MAX_VALUE: i64;
438+
/** Converts a string to an i64 of this type. */
439+
export function parse(value: string, radix?: i32): i64;
436440
/** Loads an 8-bit signed integer value from memory and returns it as a 64-bit integer. */
437441
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i64;
438442
/** Loads an 8-bit unsigned integer value from memory and returns it as a 64-bit integer. */
@@ -585,6 +589,8 @@ declare namespace u8 {
585589
export const MIN_VALUE: u8;
586590
/** Largest representable value. */
587591
export const MAX_VALUE: u8;
592+
/** Converts a string to an u8 of this type. */
593+
export function parse(value: string, radix?: i32): u8;
588594
}
589595
/** Converts any other numeric value to a 16-bit unsigned integer. */
590596
declare function u16(value: any): u16;
@@ -593,6 +599,8 @@ declare namespace u16 {
593599
export const MIN_VALUE: u16;
594600
/** Largest representable value. */
595601
export const MAX_VALUE: u16;
602+
/** Converts a string to an u16 of this type. */
603+
export function parse(value: string, radix?: i32): u16;
596604
}
597605
/** Converts any other numeric value to a 32-bit unsigned integer. */
598606
declare function u32(value: any): u32;
@@ -601,6 +609,8 @@ declare namespace u32 {
601609
export const MIN_VALUE: u32;
602610
/** Largest representable value. */
603611
export const MAX_VALUE: u32;
612+
/** Converts a string to an u32 of this type. */
613+
export function parse(value: string, radix?: i32): u32;
604614
}
605615
/** Converts any other numeric value to a 64-bit unsigned integer. */
606616
declare function u64(value: any): u64;
@@ -609,6 +619,8 @@ declare namespace u64 {
609619
export const MIN_VALUE: u64;
610620
/** Largest representable value. */
611621
export const MAX_VALUE: u64;
622+
/** Converts a string to an u64 of this type. */
623+
export function parse(value: string, radix?: i32): u64;
612624
}
613625
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
614626
declare var usize: typeof u32 | typeof u64;
@@ -619,6 +631,8 @@ declare namespace bool {
619631
export const MIN_VALUE: bool;
620632
/** Largest representable value. */
621633
export const MAX_VALUE: bool;
634+
/** Converts a string to an bool of this type. */
635+
export function parse(value: string): bool;
622636
}
623637
/** Converts any other numeric value to a 32-bit float. */
624638
declare function f32(value: any): f32;
@@ -641,6 +655,8 @@ declare namespace f32 {
641655
export const NaN: f32;
642656
/** Difference between 1 and the smallest representable value greater than 1. */
643657
export const EPSILON: f32;
658+
/** Converts a string to an f32 of this type. */
659+
export function parse(value: string, radix?: i32): f32;
644660
/** Loads a 32-bit float from memory. */
645661
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f32;
646662
/** Stores a 32-bit float to memory. */
@@ -699,6 +715,8 @@ declare namespace f64 {
699715
export const NaN: f64;
700716
/** Difference between 1 and the smallest representable value greater than 1. */
701717
export const EPSILON: f64;
718+
/** Converts a string to an f64 of this type. */
719+
export function parse(value: string, radix?: i32): f64;
702720
/** Loads a 64-bit float from memory. */
703721
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f64;
704722
/** Stores a 64-bit float to memory. */
@@ -1390,7 +1408,7 @@ declare class _Integer {
13901408
static readonly MIN_VALUE: number;
13911409
/** Largest representable value. */
13921410
static readonly MAX_VALUE: number;
1393-
/** Converts a string to an integer of this type. */
1411+
/** @deprecated Converts a string to an integer of this type. Please use "i32.parse" method. */
13941412
static parseInt(value: string, radix?: number): number;
13951413
/** Converts this integer to a string. */
13961414
toString(radix?: number): string;
@@ -1423,9 +1441,9 @@ declare class _Float {
14231441
static isSafeInteger(value: f32 | f64): bool;
14241442
/** Returns true if the value passed is an integer, false otherwise. */
14251443
static isInteger(value: f32 | f64): bool;
1426-
/** Converts a string to an integer. */
1444+
/** @deprecated Converts a string to an integer. Please use "i32.parse" / "i64.parse" methods. */
14271445
static parseInt(value: string, radix?: i32): f32 | f64;
1428-
/** Converts a string to a floating-point number. */
1446+
/** @deprecated Converts a string to a floating-point number. Please use "f32.parse" / "f64.parse" methods. */
14291447
static parseFloat(value: string): f32 | f64;
14301448
/** Converts this floating-point number to a string. */
14311449
toString(radix?: number): string;

0 commit comments

Comments
 (0)