Skip to content

Commit 4535263

Browse files
authored
Add parse methods to portable. Also fix couple type definitions (#2627)
1 parent eb60348 commit 4535263

File tree

3 files changed

+74
-30
lines changed

3 files changed

+74
-30
lines changed

std/assembly/index.d.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ declare namespace i8 {
304304
export const MIN_VALUE: i8;
305305
/** Largest representable value. */
306306
export const MAX_VALUE: i8;
307+
/** Parses a string as an i8. */
308+
export function parse(value: string, radix?: i32): i8;
307309
}
308310
/** Converts any other numeric value to a 16-bit signed integer. */
309311
declare function i16(value: any): i16;
@@ -312,6 +314,8 @@ declare namespace i16 {
312314
export const MIN_VALUE: i16;
313315
/** Largest representable value. */
314316
export const MAX_VALUE: i16;
317+
/** Parses a string as an i16. */
318+
export function parse(value: string, radix?: i32): i16;
315319
}
316320
/** Converts any other numeric value to a 32-bit signed integer. */
317321
declare function i32(value: any): i32;
@@ -320,7 +324,7 @@ declare namespace i32 {
320324
export const MIN_VALUE: i32;
321325
/** Largest representable value. */
322326
export const MAX_VALUE: i32;
323-
/** Converts a string to an i32 of this type. */
327+
/** Parses a string as an i32. */
324328
export function parse(value: string, radix?: i32): i32;
325329
/** Loads an 8-bit signed integer value from memory and returns it as a 32-bit integer. */
326330
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i32;
@@ -445,7 +449,7 @@ declare namespace i64 {
445449
export const MIN_VALUE: i64;
446450
/** Largest representable value. */
447451
export const MAX_VALUE: i64;
448-
/** Converts a string to an i64 of this type. */
452+
/** Parses a string as an i64. */
449453
export function parse(value: string, radix?: i32): i64;
450454
/** Loads an 8-bit signed integer value from memory and returns it as a 64-bit integer. */
451455
export function load8_s(ptr: usize, immOffset?: usize, immAlign?: usize): i64;
@@ -599,7 +603,7 @@ declare namespace u8 {
599603
export const MIN_VALUE: u8;
600604
/** Largest representable value. */
601605
export const MAX_VALUE: u8;
602-
/** Converts a string to an u8 of this type. */
606+
/** Parses a string as an u8. */
603607
export function parse(value: string, radix?: i32): u8;
604608
}
605609
/** Converts any other numeric value to a 16-bit unsigned integer. */
@@ -609,7 +613,7 @@ declare namespace u16 {
609613
export const MIN_VALUE: u16;
610614
/** Largest representable value. */
611615
export const MAX_VALUE: u16;
612-
/** Converts a string to an u16 of this type. */
616+
/** Parses a string as an u16. */
613617
export function parse(value: string, radix?: i32): u16;
614618
}
615619
/** Converts any other numeric value to a 32-bit unsigned integer. */
@@ -619,7 +623,7 @@ declare namespace u32 {
619623
export const MIN_VALUE: u32;
620624
/** Largest representable value. */
621625
export const MAX_VALUE: u32;
622-
/** Converts a string to an u32 of this type. */
626+
/** Parses a string as an u32. */
623627
export function parse(value: string, radix?: i32): u32;
624628
}
625629
/** Converts any other numeric value to a 64-bit unsigned integer. */
@@ -629,7 +633,7 @@ declare namespace u64 {
629633
export const MIN_VALUE: u64;
630634
/** Largest representable value. */
631635
export const MAX_VALUE: u64;
632-
/** Converts a string to an u64 of this type. */
636+
/** Parses a string as an u64. */
633637
export function parse(value: string, radix?: i32): u64;
634638
}
635639
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
@@ -641,7 +645,7 @@ declare namespace bool {
641645
export const MIN_VALUE: bool;
642646
/** Largest representable value. */
643647
export const MAX_VALUE: bool;
644-
/** Converts a string to an bool of this type. */
648+
/** Parses a string as a bool. */
645649
export function parse(value: string): bool;
646650
}
647651
/** Converts any other numeric value to a 32-bit float. */
@@ -665,8 +669,8 @@ declare namespace f32 {
665669
export const NaN: f32;
666670
/** Difference between 1 and the smallest representable value greater than 1. */
667671
export const EPSILON: f32;
668-
/** Converts a string to an f32 of this type. */
669-
export function parse(value: string, radix?: i32): f32;
672+
/** Parses a string as an f32. */
673+
export function parse(value: string): f32;
670674
/** Loads a 32-bit float from memory. */
671675
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f32;
672676
/** Stores a 32-bit float to memory. */
@@ -725,8 +729,8 @@ declare namespace f64 {
725729
export const NaN: f64;
726730
/** Difference between 1 and the smallest representable value greater than 1. */
727731
export const EPSILON: f64;
728-
/** Converts a string to an f64 of this type. */
729-
export function parse(value: string, radix?: i32): f64;
732+
/** Parses a string as an f64. */
733+
export function parse(value: string): f64;
730734
/** Loads a 64-bit float from memory. */
731735
export function load(ptr: usize, immOffset?: usize, immAlign?: usize): f64;
732736
/** Stores a 64-bit float to memory. */

std/portable/index.d.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ declare namespace i8 {
131131
export const MAX_VALUE: i8;
132132
/** Converts a string to a floating-point number and cast to target integer after. */
133133
export function parseFloat(string: string): i8;
134-
/** Converts A string to an integer. */
134+
/** Parses a string as an integer. */
135135
export function parseInt(string: string, radix?: i32): i8;
136+
/** Parses a string as an i8. */
137+
export function parse(value: string, radix?: i32): i8;
136138
}
137139
/** Converts any other numeric value to a 16-bit signed integer. */
138140
declare function i16(value: any): i16;
@@ -143,8 +145,10 @@ declare namespace i16 {
143145
export const MAX_VALUE: i16;
144146
/** Converts a string to a floating-point number and cast to target integer after. */
145147
export function parseFloat(string: string): i16;
146-
/** Converts A string to an integer. */
148+
/** Parses a string as an integer. */
147149
export function parseInt(string: string, radix?: i32): i16;
150+
/** Parses a string as an i16. */
151+
export function parse(value: string, radix?: i32): i16;
148152
}
149153
/** Converts any other numeric value to a 32-bit signed integer. */
150154
declare function i32(value: any): i32;
@@ -155,8 +159,10 @@ declare namespace i32 {
155159
export const MAX_VALUE: i32;
156160
/** Converts a string to a floating-point number and cast to target integer after. */
157161
export function parseFloat(string: string): i32;
158-
/** Converts A string to an integer. */
162+
/** Parses a string as an integer. */
159163
export function parseInt(string: string, radix?: i32): i32;
164+
/** Parses a string as an i32. */
165+
export function parse(value: string, radix?: i32): i32;
160166
}
161167
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
162168
declare function isize(value: any): isize;
@@ -167,8 +173,10 @@ declare namespace isize {
167173
export const MAX_VALUE: isize;
168174
/** Converts a string to a floating-point number and cast to target integer after. */
169175
export function parseFloat(string: string): isize;
170-
/** Converts A string to an integer. */
176+
/** Parses a string as an integer. */
171177
export function parseInt(string: string, radix?: i32): isize;
178+
/** Parses a string as an iszie. */
179+
export function parse(value: string, radix?: i32): isize;
172180
}
173181
/** Converts any other numeric value to an 8-bit unsigned integer. */
174182
declare function u8(value: any): u8;
@@ -179,8 +187,10 @@ declare namespace u8 {
179187
export const MAX_VALUE: u8;
180188
/** Converts a string to a floating-point number and cast to target integer after. */
181189
export function parseFloat(string: string): u8;
182-
/** Converts A string to an integer. */
190+
/** Parses a string as an integer. */
183191
export function parseInt(string: string, radix?: i32): u8;
192+
/** Parses a string as an u8. */
193+
export function parse(value: string, radix?: i32): u8;
184194
}
185195
/** Converts any other numeric value to a 16-bit unsigned integer. */
186196
declare function u16(value: any): u16;
@@ -191,8 +201,10 @@ declare namespace u16 {
191201
export const MAX_VALUE: u16;
192202
/** Converts a string to a floating-point number and cast to target integer after. */
193203
export function parseFloat(string: string): u16;
194-
/** Converts A string to an integer. */
204+
/** Parses a string as an integer. */
195205
export function parseInt(string: string, radix?: i32): u16;
206+
/** Parses a string as an u16. */
207+
export function parse(value: string, radix?: i32): u16;
196208
}
197209
/** Converts any other numeric value to a 32-bit unsigned integer. */
198210
declare function u32(value: any): u32;
@@ -203,8 +215,10 @@ declare namespace u32 {
203215
export const MAX_VALUE: u32;
204216
/** Converts a string to a floating-point number and cast to target integer after. */
205217
export function parseFloat(string: string): u32;
206-
/** Converts A string to an integer. */
218+
/** Parses a string as an integer. */
207219
export function parseInt(string: string, radix?: i32): u32;
220+
/** Parses a string as an u32. */
221+
export function parse(value: string, radix?: i32): u32;
208222
}
209223
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
210224
declare function usize(value: any): isize;
@@ -215,8 +229,10 @@ declare namespace usize {
215229
export const MAX_VALUE: usize;
216230
/** Converts a string to a floating-point number and cast to target integer after. */
217231
export function parseFloat(string: string): usize;
218-
/** Converts A string to an integer. */
232+
/** Parses a string as an integer. */
219233
export function parseInt(string: string, radix?: i32): usize;
234+
/** Parses a string as an usize. */
235+
export function parse(value: string, radix?: i32): usize;
220236
}
221237
/** Converts any other numeric value to a 1-bit unsigned integer. */
222238
declare function bool(value: any): bool;
@@ -225,6 +241,8 @@ declare namespace bool {
225241
export const MIN_VALUE: bool;
226242
/** Largest representable value. */
227243
export const MAX_VALUE: bool;
244+
/** Parses a string as a bool. */
245+
export function parse(value: string): bool;
228246
}
229247
/** Converts any other numeric value to a 32-bit float. */
230248
declare function f32(value: any): f32;
@@ -258,8 +276,10 @@ declare namespace f32 {
258276
export function isInteger(value: f32): bool;
259277
/** Converts a string to a floating-point number. */
260278
export function parseFloat(string: string): f32;
261-
/** Converts A string to an integer. */
279+
/** Parses a string as an integer and convert to an f32. */
262280
export function parseInt(string: string, radix?: i32): f32;
281+
/** Parses a string as an f32. */
282+
export function parse(value: string): f32;
263283
}
264284
/** Converts any other numeric value to a 64-bit float. */
265285
declare function f64(value: any): f64;
@@ -293,8 +313,10 @@ declare namespace f64 {
293313
export function isInteger(value: f64): bool;
294314
/** Converts a string to a floating-point number. */
295315
export function parseFloat(string: string): f64;
296-
/** Converts A string to an integer. */
316+
/** Parses a string as an integer and convert to an f64. */
297317
export function parseInt(string: string, radix?: i32): f64;
318+
/** Parses a string as an f64. */
319+
export function parse(value: string): f64;
298320
}
299321

300322
// Standard library

std/portable/index.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,69 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
2222
globalScope["i8"] = function i8(value) { return value << 24 >> 24; },
2323
{
2424
"MIN_VALUE": { value: -128 },
25-
"MAX_VALUE": { value: 127 }
25+
"MAX_VALUE": { value: 127 },
26+
27+
parse(str, radix) { return parseInt(str, radix) << 24 >> 24; }
2628
}
2729
);
2830

2931
Object.defineProperties(
3032
globalScope["i16"] = function i16(value) { return value << 16 >> 16; },
3133
{
3234
"MIN_VALUE": { value: -32768 },
33-
"MAX_VALUE": { value: 32767 }
35+
"MAX_VALUE": { value: 32767 },
36+
37+
parse(str, radix) { return parseInt(str, radix) << 16 >> 16; }
3438
}
3539
);
3640

3741
Object.defineProperties(
3842
globalScope["i32"] = globalScope["isize"] = function i32(value) { return value | 0; },
3943
{
4044
"MIN_VALUE": { value: -2147483648 },
41-
"MAX_VALUE": { value: 2147483647 }
45+
"MAX_VALUE": { value: 2147483647 },
46+
47+
parse(str, radix) { return parseInt(str, radix) | 0; }
4248
}
4349
);
4450

4551
Object.defineProperties(
4652
globalScope["u8"] = function u8(value) { return value & 0xff; },
4753
{
4854
"MIN_VALUE": { value: 0 },
49-
"MAX_VALUE": { value: 255 }
55+
"MAX_VALUE": { value: 255 },
56+
57+
parse(str, radix) { return parseInt(str, radix) & 0xff; }
5058
}
5159
);
5260

5361
Object.defineProperties(
5462
globalScope["u16"] = function u16(value) { return value & 0xffff; },
5563
{
5664
"MIN_VALUE": { value: 0 },
57-
"MAX_VALUE": { value: 65535 }
65+
"MAX_VALUE": { value: 65535 },
66+
67+
parse(str, radix) { return parseInt(str, radix) & 0xffff; }
5868
}
5969
);
6070

6171
Object.defineProperties(
6272
globalScope["u32"] = globalScope["usize"] = function u32(value) { return value >>> 0; },
6373
{
6474
"MIN_VALUE": { value: 0 },
65-
"MAX_VALUE": { value: 4294967295 }
75+
"MAX_VALUE": { value: 4294967295 },
76+
77+
parse(str, radix) { return parseInt(str, radix) >>> 0; }
6678
}
6779
);
6880

6981
Object.defineProperties(
7082
globalScope["bool"] = function bool(value) { return !!value; },
7183
{
7284
"MIN_VALUE": { value: false },
73-
"MAX_VALUE": { value: true }
85+
"MAX_VALUE": { value: true },
86+
87+
parse(str) { return str.trim() === "true"; }
7488
}
7589
);
7690

@@ -85,7 +99,9 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
8599
"MAX_SAFE_INTEGER": { value: 16777215 },
86100
"POSITIVE_INFINITY": { value: Infinity },
87101
"NEGATIVE_INFINITY": { value: -Infinity },
88-
"NaN": { value: NaN }
102+
"NaN": { value: NaN },
103+
104+
parse(str) { return Math.fround(parseFloat(str)); }
89105
}
90106
);
91107

@@ -100,7 +116,9 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
100116
"MAX_SAFE_INTEGER": { value: 9007199254740991 },
101117
"POSITIVE_INFINITY": { value: Infinity },
102118
"NEGATIVE_INFINITY": { value: -Infinity },
103-
"NaN": { value: NaN }
119+
"NaN": { value: NaN },
120+
121+
parse(str) { return parseFloat(str); }
104122
}
105123
);
106124

0 commit comments

Comments
 (0)