Skip to content
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

Add parse methods to portable. Also fix couple type definitions #2627

Merged
merged 2 commits into from Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 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;
/** Converts a string to an i8 of this type. */
export function parse(value: string, radix?: i32): i8;
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved
}
/** 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;
/** Converts a string to an i16 of this type. */
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 Down Expand Up @@ -666,7 +670,7 @@ declare namespace 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;
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 @@ -726,7 +730,7 @@ declare namespace 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;
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
40 changes: 31 additions & 9 deletions std/portable/index.d.ts
Expand Up @@ -133,6 +133,8 @@ declare namespace i8 {
export function parseFloat(string: string): i8;
/** Converts A string to an integer. */
export function parseInt(string: string, radix?: i32): i8;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): i16;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): i32;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): isize;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): u8;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): u16;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): u32;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): usize;
/** Converts a string to 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;
/** Converts a string to an 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): f32;
/** Converts a string to 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. */
/** Converts a string to an integer. */
export function parseInt(string: string, radix?: i32): f64;
/** Converts a string to 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