Skip to content

feat: Introduce new parse methods for builtin value types. Deprecate XX.parseInt/parseFloat #2465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 26, 2022
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions std/assembly/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { strtol, strtod, strtob } from "./util/string";

type auto = i32;

// @ts-ignore: decorator
Expand Down Expand Up @@ -283,6 +285,12 @@ export namespace i8 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: i8 = 127;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): i8 {
return <i8>strtol<i32>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -298,6 +306,12 @@ export namespace i16 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: i16 = 32767;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): i16 {
return <i16>strtol<i32>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -314,6 +328,12 @@ export namespace i32 {
@lazy
export const MAX_VALUE: i32 = 2147483647;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): i32 {
return strtol<i32>(value, radix);
}

// @ts-ignore: decorator
@builtin
export declare function clz(value: i32): i32;
Expand Down Expand Up @@ -545,6 +565,12 @@ export namespace i64 {
@lazy
export const MAX_VALUE: i64 = 9223372036854775807;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): i64 {
return strtol<i64>(value, radix);
}

// @ts-ignore: decorator
@builtin
export declare function clz(value: i64): i64;
Expand Down Expand Up @@ -830,6 +856,12 @@ export namespace isize {
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>()
? 2147483647
: <isize>9223372036854775807;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): isize {
return <isize>strtol<i64>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -845,6 +877,12 @@ export namespace u8 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: u8 = 255;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): u8 {
return <u8>strtol<i32>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -860,6 +898,12 @@ export namespace u16 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: u16 = 65535;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): u16 {
return <u16>strtol<i32>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -875,6 +919,12 @@ export namespace u32 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: u32 = 4294967295;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): u32 {
return <u32>strtol<i32>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -890,6 +940,12 @@ export namespace u64 {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: u64 = 18446744073709551615;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): u64 {
return <u64>strtol<i64>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -907,6 +963,12 @@ export namespace usize {
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>()
? 4294967295
: <usize>18446744073709551615;

// @ts-ignore: decorator
@inline
export function parse(value: string, radix: i32 = 0): usize {
return <usize>strtol<i64>(value, radix);
}
}

// @ts-ignore: decorator
Expand All @@ -922,6 +984,12 @@ export namespace bool {
// @ts-ignore: decorator
@lazy
export const MAX_VALUE: bool = true;

// @ts-ignore: decorator
@inline
export function parse(value: string): bool {
return strtob(value);
}
}

// @ts-ignore: decorator
Expand Down Expand Up @@ -966,6 +1034,12 @@ export namespace f32 {
@lazy
export const NaN: f32 = 0.0 / 0.0;

// @ts-ignore: decorator
@inline
export function parse(value: string): f32 {
return <f32>strtod(value);
}

// @ts-ignore: decorator
@builtin
export declare function abs(value: f32): f32;
Expand Down Expand Up @@ -1081,6 +1155,12 @@ export namespace f64 {
@lazy
export const NaN: f64 = 0.0 / 0.0;

// @ts-ignore: decorator
@inline
export function parse(value: string): f64 {
return strtod(value);
}

// @ts-ignore: decorator
@builtin
export declare function abs(value: f64): f64;
Expand Down
24 changes: 21 additions & 3 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ declare namespace i32 {
export const MIN_VALUE: i32;
/** Largest representable value. */
export const MAX_VALUE: i32;
/** Converts a string to an i32 of this type. */
export function parse(value: string, radix?: i32): i32;
/** Loads an 8-bit signed integer value from memory and returns it as a 32-bit integer. */
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i32;
/** Loads an 8-bit unsigned integer value from memory and returns it as a 32-bit integer. */
Expand Down Expand Up @@ -433,6 +435,8 @@ declare namespace i64 {
export const MIN_VALUE: i64;
/** Largest representable value. */
export const MAX_VALUE: i64;
/** Converts a string to an i64 of this type. */
export function parse(value: string, radix?: i32): i64;
/** Loads an 8-bit signed integer value from memory and returns it as a 64-bit integer. */
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i64;
/** Loads an 8-bit unsigned integer value from memory and returns it as a 64-bit integer. */
Expand Down Expand Up @@ -585,6 +589,8 @@ declare namespace u8 {
export const MIN_VALUE: u8;
/** Largest representable value. */
export const MAX_VALUE: u8;
/** Converts a string to an u8 of this type. */
export function parse(value: string, radix?: i32): u8;
}
/** Converts any other numeric value to a 16-bit unsigned integer. */
declare function u16(value: any): u16;
Expand All @@ -593,6 +599,8 @@ declare namespace u16 {
export const MIN_VALUE: u16;
/** Largest representable value. */
export const MAX_VALUE: u16;
/** Converts a string to an u16 of this type. */
export function parse(value: string, radix?: i32): u16;
}
/** Converts any other numeric value to a 32-bit unsigned integer. */
declare function u32(value: any): u32;
Expand All @@ -601,6 +609,8 @@ declare namespace u32 {
export const MIN_VALUE: u32;
/** Largest representable value. */
export const MAX_VALUE: u32;
/** Converts a string to an u32 of this type. */
export function parse(value: string, radix?: i32): u32;
}
/** Converts any other numeric value to a 64-bit unsigned integer. */
declare function u64(value: any): u64;
Expand All @@ -609,6 +619,8 @@ declare namespace u64 {
export const MIN_VALUE: u64;
/** Largest representable value. */
export const MAX_VALUE: u64;
/** Converts a string to an u64 of this type. */
export function parse(value: string, radix?: i32): u64;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
declare var usize: typeof u32 | typeof u64;
Expand All @@ -619,6 +631,8 @@ declare namespace bool {
export const MIN_VALUE: bool;
/** Largest representable value. */
export const MAX_VALUE: bool;
/** Converts a string to an bool of this type. */
export function parse(value: string): bool;
}
/** Converts any other numeric value to a 32-bit float. */
declare function f32(value: any): f32;
Expand All @@ -641,6 +655,8 @@ declare namespace f32 {
export const NaN: f32;
/** Difference between 1 and the smallest representable value greater than 1. */
export const EPSILON: f32;
/** Converts a string to an f32 of this type. */
export function parse(value: string, radix?: i32): f32;
/** Loads a 32-bit float from memory. */
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f32;
/** Stores a 32-bit float to memory. */
Expand Down Expand Up @@ -699,6 +715,8 @@ declare namespace f64 {
export const NaN: f64;
/** Difference between 1 and the smallest representable value greater than 1. */
export const EPSILON: f64;
/** Converts a string to an f64 of this type. */
export function parse(value: string, radix?: i32): f64;
/** Loads a 64-bit float from memory. */
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f64;
/** Stores a 64-bit float to memory. */
Expand Down Expand Up @@ -1390,7 +1408,7 @@ declare class _Integer {
static readonly MIN_VALUE: number;
/** Largest representable value. */
static readonly MAX_VALUE: number;
/** Converts a string to an integer of this type. */
/** @deprecated Converts a string to an integer of this type. Please use "i32.parse" method. */
static parseInt(value: string, radix?: number): number;
/** Converts this integer to a string. */
toString(radix?: number): string;
Expand Down Expand Up @@ -1423,9 +1441,9 @@ declare class _Float {
static isSafeInteger(value: f32 | f64): bool;
/** Returns true if the value passed is an integer, false otherwise. */
static isInteger(value: f32 | f64): bool;
/** Converts a string to an integer. */
/** @deprecated Converts a string to an integer. Please use "i32.parse" / "i64.parse" methods. */
static parseInt(value: string, radix?: i32): f32 | f64;
/** Converts a string to a floating-point number. */
/** @deprecated Converts a string to a floating-point number. Please use "f32.parse" / "f64.parse" methods. */
static parseFloat(value: string): f32 | f64;
/** Converts this floating-point number to a string. */
toString(radix?: number): string;
Expand Down
Loading