Skip to content

Commit

Permalink
Add parse methods to portable. Also fix couple type definitions (#2627)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey committed Jan 22, 2023
1 parent eb60348 commit 4535263
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
26 changes: 15 additions & 11 deletions std/assembly/index.d.ts
Expand Up @@ -304,6 +304,8 @@ declare namespace i8 {
export const MIN_VALUE: i8;
/** Largest representable value. */
export const MAX_VALUE: i8;
/** Parses a string as an i8. */
export function parse(value: string, radix?: i32): i8;
}
/** Converts any other numeric value to a 16-bit signed integer. */
declare function i16(value: any): i16;
Expand All @@ -312,6 +314,8 @@ declare namespace i16 {
export const MIN_VALUE: i16;
/** Largest representable value. */
export const MAX_VALUE: i16;
/** Parses a string as an i16. */
export function parse(value: string, radix?: i32): i16;
}
/** Converts any other numeric value to a 32-bit signed integer. */
declare function i32(value: any): i32;
Expand All @@ -320,7 +324,7 @@ 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. */
/** Parses a string as an i32. */
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;
Expand Down Expand Up @@ -445,7 +449,7 @@ 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. */
/** Parses a string as an i64. */
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;
Expand Down Expand Up @@ -599,7 +603,7 @@ 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. */
/** Parses a string as an u8. */
export function parse(value: string, radix?: i32): u8;
}
/** Converts any other numeric value to a 16-bit unsigned integer. */
Expand All @@ -609,7 +613,7 @@ 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. */
/** Parses a string as an u16. */
export function parse(value: string, radix?: i32): u16;
}
/** Converts any other numeric value to a 32-bit unsigned integer. */
Expand All @@ -619,7 +623,7 @@ 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. */
/** Parses a string as an u32. */
export function parse(value: string, radix?: i32): u32;
}
/** Converts any other numeric value to a 64-bit unsigned integer. */
Expand All @@ -629,7 +633,7 @@ 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. */
/** Parses a string as an u64. */
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. */
Expand All @@ -641,7 +645,7 @@ 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. */
/** Parses a string as a bool. */
export function parse(value: string): bool;
}
/** Converts any other numeric value to a 32-bit float. */
Expand All @@ -665,8 +669,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;
/** Parses a string as an f32. */
export function parse(value: string): 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 @@ -725,8 +729,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;
/** Parses a string as an f64. */
export function parse(value: string): 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
42 changes: 32 additions & 10 deletions std/portable/index.d.ts
Expand Up @@ -131,8 +131,10 @@ declare namespace i8 {
export const MAX_VALUE: i8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i8;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): i8;
/** Parses a string as an i8. */
export function parse(value: string, radix?: i32): i8;
}
/** Converts any other numeric value to a 16-bit signed integer. */
declare function i16(value: any): i16;
Expand All @@ -143,8 +145,10 @@ declare namespace i16 {
export const MAX_VALUE: i16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i16;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): i16;
/** Parses a string as an i16. */
export function parse(value: string, radix?: i32): i16;
}
/** Converts any other numeric value to a 32-bit signed integer. */
declare function i32(value: any): i32;
Expand All @@ -155,8 +159,10 @@ declare namespace i32 {
export const MAX_VALUE: i32;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): i32;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): i32;
/** Parses a string as an i32. */
export function parse(value: string, radix?: i32): i32;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
declare function isize(value: any): isize;
Expand All @@ -167,8 +173,10 @@ declare namespace isize {
export const MAX_VALUE: isize;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): isize;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): isize;
/** Parses a string as an iszie. */
export function parse(value: string, radix?: i32): isize;
}
/** Converts any other numeric value to an 8-bit unsigned integer. */
declare function u8(value: any): u8;
Expand All @@ -179,8 +187,10 @@ declare namespace u8 {
export const MAX_VALUE: u8;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u8;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): u8;
/** Parses a string as an u8. */
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 @@ -191,8 +201,10 @@ declare namespace u16 {
export const MAX_VALUE: u16;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u16;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): u16;
/** Parses a string as an u16. */
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 @@ -203,8 +215,10 @@ declare namespace u32 {
export const MAX_VALUE: u32;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): u32;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): u32;
/** Parses a string as an u32. */
export function parse(value: string, radix?: i32): u32;
}
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
declare function usize(value: any): isize;
Expand All @@ -215,8 +229,10 @@ declare namespace usize {
export const MAX_VALUE: usize;
/** Converts a string to a floating-point number and cast to target integer after. */
export function parseFloat(string: string): usize;
/** Converts A string to an integer. */
/** Parses a string as an integer. */
export function parseInt(string: string, radix?: i32): usize;
/** Parses a string as an usize. */
export function parse(value: string, radix?: i32): usize;
}
/** Converts any other numeric value to a 1-bit unsigned integer. */
declare function bool(value: any): bool;
Expand All @@ -225,6 +241,8 @@ declare namespace bool {
export const MIN_VALUE: bool;
/** Largest representable value. */
export const MAX_VALUE: bool;
/** Parses a string as a bool. */
export function parse(value: string): bool;
}
/** Converts any other numeric value to a 32-bit float. */
declare function f32(value: any): f32;
Expand Down Expand Up @@ -258,8 +276,10 @@ declare namespace f32 {
export function isInteger(value: f32): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f32;
/** Converts A string to an integer. */
/** Parses a string as an integer and convert to an f32. */
export function parseInt(string: string, radix?: i32): f32;
/** Parses a string as an f32. */
export function parse(value: string): f32;
}
/** Converts any other numeric value to a 64-bit float. */
declare function f64(value: any): f64;
Expand Down Expand Up @@ -293,8 +313,10 @@ declare namespace f64 {
export function isInteger(value: f64): bool;
/** Converts a string to a floating-point number. */
export function parseFloat(string: string): f64;
/** Converts A string to an integer. */
/** Parses a string as an integer and convert to an f64. */
export function parseInt(string: string, radix?: i32): f64;
/** Parses a string as an f64. */
export function parse(value: string): f64;
}

// Standard library
Expand Down
36 changes: 27 additions & 9 deletions std/portable/index.js
Expand Up @@ -22,55 +22,69 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
globalScope["i8"] = function i8(value) { return value << 24 >> 24; },
{
"MIN_VALUE": { value: -128 },
"MAX_VALUE": { value: 127 }
"MAX_VALUE": { value: 127 },

parse(str, radix) { return parseInt(str, radix) << 24 >> 24; }
}
);

Object.defineProperties(
globalScope["i16"] = function i16(value) { return value << 16 >> 16; },
{
"MIN_VALUE": { value: -32768 },
"MAX_VALUE": { value: 32767 }
"MAX_VALUE": { value: 32767 },

parse(str, radix) { return parseInt(str, radix) << 16 >> 16; }
}
);

Object.defineProperties(
globalScope["i32"] = globalScope["isize"] = function i32(value) { return value | 0; },
{
"MIN_VALUE": { value: -2147483648 },
"MAX_VALUE": { value: 2147483647 }
"MAX_VALUE": { value: 2147483647 },

parse(str, radix) { return parseInt(str, radix) | 0; }
}
);

Object.defineProperties(
globalScope["u8"] = function u8(value) { return value & 0xff; },
{
"MIN_VALUE": { value: 0 },
"MAX_VALUE": { value: 255 }
"MAX_VALUE": { value: 255 },

parse(str, radix) { return parseInt(str, radix) & 0xff; }
}
);

Object.defineProperties(
globalScope["u16"] = function u16(value) { return value & 0xffff; },
{
"MIN_VALUE": { value: 0 },
"MAX_VALUE": { value: 65535 }
"MAX_VALUE": { value: 65535 },

parse(str, radix) { return parseInt(str, radix) & 0xffff; }
}
);

Object.defineProperties(
globalScope["u32"] = globalScope["usize"] = function u32(value) { return value >>> 0; },
{
"MIN_VALUE": { value: 0 },
"MAX_VALUE": { value: 4294967295 }
"MAX_VALUE": { value: 4294967295 },

parse(str, radix) { return parseInt(str, radix) >>> 0; }
}
);

Object.defineProperties(
globalScope["bool"] = function bool(value) { return !!value; },
{
"MIN_VALUE": { value: false },
"MAX_VALUE": { value: true }
"MAX_VALUE": { value: true },

parse(str) { return str.trim() === "true"; }
}
);

Expand All @@ -85,7 +99,9 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
"MAX_SAFE_INTEGER": { value: 16777215 },
"POSITIVE_INFINITY": { value: Infinity },
"NEGATIVE_INFINITY": { value: -Infinity },
"NaN": { value: NaN }
"NaN": { value: NaN },

parse(str) { return Math.fround(parseFloat(str)); }
}
);

Expand All @@ -100,7 +116,9 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
"MAX_SAFE_INTEGER": { value: 9007199254740991 },
"POSITIVE_INFINITY": { value: Infinity },
"NEGATIVE_INFINITY": { value: -Infinity },
"NaN": { value: NaN }
"NaN": { value: NaN },

parse(str) { return parseFloat(str); }
}
);

Expand Down

0 comments on commit 4535263

Please sign in to comment.