Fix parseInt routines after refactoring (#655)

This commit is contained in:
Max Graey
2019-06-12 11:17:01 +03:00
committed by Daniel Wirtz
parent 40dac8269d
commit f9a77b132f
9 changed files with 3142 additions and 1927 deletions

View File

@ -830,7 +830,7 @@ export class Program extends DiagnosticEmitter {
if (!name.length) continue; // explicitly disabled
let firstChar = name.charCodeAt(0);
if (firstChar >= CharCode._0 && firstChar <= CharCode._9) {
this.registerConstantInteger(alias, Type.i32, i64_new(parseI32(name, 10)));
this.registerConstantInteger(alias, Type.i32, i64_new(<i32>parseInt(name, 10)));
} else {
let elementsByName = this.elementsByName;
let element = elementsByName.get(name);

View File

@ -158,10 +158,6 @@ declare function isManaged<T>(value?: any): bool;
declare function assert<T>(isTrueish: T, message?: string): T & object; // any better way to model `: T != null`?
/** Parses an integer string to a 64-bit float. */
declare function parseInt(str: string, radix?: i32): f64;
/** Parses an integer string to a 32-bit integer. */
declare function parseI32(str: string, radix?: i32): i32;
/** Parses an integer string to a 64-bit integer. */
declare function parseI64(str: string, radix?: i32): i64;
/** Parses a string to a 64-bit float. */
declare function parseFloat(str: string): f64;
/** Returns the 64-bit floating-point remainder of `x/y`. */

View File

@ -1,4 +1,5 @@
import { itoa, dtoa } from "./util/number";
import { strtol } from "./util/string";
// @ts-ignore: decorator
@builtin @inline
@ -34,7 +35,7 @@ export abstract class I8 {
static readonly MAX_VALUE: i8 = i8.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): i8 {
return <i8>parseI32(value, radix);
return <i8>strtol<i32>(value, radix);
}
toString(this: i8): String {
@ -55,7 +56,7 @@ export abstract class I16 {
static readonly MAX_VALUE: i16 = i16.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): i16 {
return <i16>parseI32(value, radix);
return <i16>strtol<i32>(value, radix);
}
toString(this: i16): String {
@ -76,7 +77,7 @@ export abstract class I32 {
static readonly MAX_VALUE: i32 = i32.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): i32 {
return <i32>parseI32(value, radix);
return <i32>strtol<i32>(value, radix);
}
toString(this: i32): String {
@ -97,7 +98,7 @@ export abstract class I64 {
static readonly MAX_VALUE: i64 = i64.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): i64 {
return <i64>parseI64(value, radix);
return strtol<i64>(value, radix);
}
toString(this: i64): String {
@ -118,7 +119,7 @@ export abstract class Isize {
static readonly MAX_VALUE: isize = isize.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): isize {
return <isize>parseI64(value, radix);
return <isize>strtol<i64>(value, radix);
}
toString(this: isize): String {
@ -139,7 +140,7 @@ export abstract class U8 {
static readonly MAX_VALUE: u8 = u8.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): u8 {
return <u8>parseI32(value, radix);
return <u8>strtol<i32>(value, radix);
}
toString(this: u8): String {
@ -160,7 +161,7 @@ export abstract class U16 {
static readonly MAX_VALUE: u16 = u16.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): u16 {
return <u16>parseI32(value, radix);
return <u16>strtol<i32>(value, radix);
}
toString(this: u16): String {
@ -181,7 +182,7 @@ export abstract class U32 {
static readonly MAX_VALUE: u32 = u32.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): u32 {
return <u32>parseI32(value, radix);
return <u32>strtol<i32>(value, radix);
}
toString(this: u32): String {
@ -202,7 +203,7 @@ export abstract class U64 {
static readonly MAX_VALUE: u64 = u64.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): u64 {
return <u64>parseI64(value, radix);
return <u64>strtol<i64>(value, radix);
}
toString(this: u64): String {
@ -223,7 +224,7 @@ export abstract class Usize {
static readonly MAX_VALUE: usize = usize.MAX_VALUE;
static parseInt(value: string, radix: i32 = 0): usize {
return <usize>parseI64(value, radix);
return <usize>strtol<i64>(value, radix);
}
toString(this: usize): String {
@ -303,7 +304,7 @@ export abstract class F32 {
}
static parseInt(value: string, radix: i32 = 0): f32 {
return <f32>parseI64(value, radix);
return <f32>strtol<i64>(value, radix);
}
static parseFloat(value: string): f32 {
@ -368,7 +369,7 @@ export abstract class F64 {
}
static parseInt(value: string, radix: i32 = 0): f64 {
return <f64>parseI64(value, radix);
return <f64>strtol<i64>(value, radix);
}
static parseFloat(value: string): f64 {

View File

@ -66,7 +66,8 @@ export function strtol<T>(str: string, radix: i32 = 0): T {
var code = <i32>load<u16>(ptr);
// determine sign
var sign: T;
// @ts-ignore: cast
var sign: T = 1;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
code = <i32>load<u16>(ptr += 2);
@ -82,11 +83,6 @@ export function strtol<T>(str: string, radix: i32 = 0): T {
// @ts-ignore: cast
if (!--len) return <T>NaN;
code = <i32>load<u16>(ptr += 2);
// @ts-ignore: type
sign = 1;
} else {
// @ts-ignore: type
sign = 1;
}
// determine radix
@ -133,7 +129,7 @@ export function strtol<T>(str: string, radix: i32 = 0): T {
} else break;
if (code >= radix) break;
// @ts-ignore: type
num = (num * radix) + code;
num = num * radix + code;
ptr += 2;
}
// @ts-ignore: type
@ -149,7 +145,7 @@ export function strtod(str: string): f64 {
var code = <i32>load<u16>(ptr);
// determine sign
var sign: f64;
var sign = 1.0;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
code = <i32>load<u16>(ptr += 2);
@ -158,22 +154,19 @@ export function strtod(str: string): f64 {
if (code == CharCode.MINUS) {
if (!--len) return NaN;
code = <i32>load<u16>(ptr += 2);
sign = -1;
sign = -1.0;
} else if (code == CharCode.PLUS) {
if (!--len) return NaN;
code = <i32>load<u16>(ptr += 2);
sign = 1;
} else {
sign = 1;
}
// calculate value
var num: f64 = 0;
var num = 0.0;
while (len--) {
code = <i32>load<u16>(ptr);
if (code == CharCode.DOT) {
ptr += 2;
let fac: f64 = 0.1; // precision :(
let fac = 0.1; // precision :(
while (len--) {
code = <i32>load<u16>(ptr);
if (code == CharCode.E || code == CharCode.e) {
@ -189,7 +182,7 @@ export function strtod(str: string): f64 {
}
code -= CharCode._0;
if (<u32>code >= 10) break;
num = (num * 10) + code;
num = num * 10 + code;
ptr += 2;
}
return sign * num;

View File

@ -120,8 +120,6 @@ declare function isConstant(expression: any): bool;
declare function assert<T>(isTrueish: T | null, message?: string): T;
/** Parses an integer string to a 64-bit float. */
declare function parseInt(str: string, radix?: i32): f64;
/** Parses an integer string to a 32-bit integer. */
declare function parseI32(str: string, radix?: i32): i32;
/** Parses a floating point string to a 64-bit float. */
declare function parseFloat(str: string): f64;
/** Returns the 64-bit floating-point remainder of `x/y`. */

View File

@ -188,10 +188,6 @@ globalScope["changetype"] = function changetype(value) {
return value;
};
globalScope["parseI32"] = function parseI32(str, radix) {
return parseInt(str, undefined) | 0;
};
String["fromCharCodes"] = function fromCharCodes(arr) {
return String.fromCharCode.apply(String, arr);
};

File diff suppressed because it is too large Load Diff

View File

@ -79,6 +79,9 @@ assert(parseInt("0x1g") == 1); // not valid
assert(parseInt(" \t\n1") == 1);
assert(parseInt(" \t\n0x02") == 2);
assert(I32.parseInt("0x7FFFFFFF") == I32.MAX_VALUE);
assert(I64.parseInt("0x7FFFFFFFFFFFFFFF") == I64.MAX_VALUE);
assert(parseFloat("0") == 0);
assert(parseFloat("1") == 1);
assert(parseFloat("0.1") == 0.1);

File diff suppressed because it is too large Load Diff