Skip to content
Closed
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d4c218c
ParseInt Implementation
MaxGraey Jan 10, 2018
52ca2d1
Move constans out of function
MaxGraey Jan 10, 2018
2dc07c6
Drop charCodeAt for char constant and use immediate values
MaxGraey Jan 10, 2018
d6b31e5
Fix tests
MaxGraey Jan 10, 2018
115c9d5
Explicit u16 type for constants
MaxGraey Jan 10, 2018
50062f8
Some simplifications
MaxGraey Jan 10, 2018
e73009f
Rename index -> pos
MaxGraey Jan 10, 2018
0f61d48
Some type hints
MaxGraey Jan 10, 2018
e918e20
Use break from loop instead early return with NaN
MaxGraey Jan 10, 2018
3966f36
break instead NaN other range checking
MaxGraey Jan 10, 2018
ed1c0a5
Manage '+' & '-' only signs without digits
MaxGraey Jan 11, 2018
2ef3491
Father optimizations
MaxGraey Jan 11, 2018
a7e5786
Fix some issues with partially valid numbers
MaxGraey Jan 11, 2018
2af303b
Additional fixes
MaxGraey Jan 11, 2018
3b83121
Minor fix
MaxGraey Jan 11, 2018
40103c6
Simplification
MaxGraey Jan 11, 2018
8c30dc0
Minor fixes
MaxGraey Jan 11, 2018
c5df495
Basic test peparations
MaxGraey Jan 11, 2018
a7a3416
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 12, 2018
95fb2c3
/implement-parse-int: Auto stash before merge of "std/implement-parse…
MaxGraey Jan 12, 2018
b2aaa41
Remove @global from parseInt
MaxGraey Jan 12, 2018
eaff433
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 12, 2018
d8cb0c3
Fix tests
MaxGraey Jan 12, 2018
3a841a8
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 17, 2018
e4e0880
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 19, 2018
02d0ba7
Replace var -> let
MaxGraey Jan 19, 2018
56f434a
Revert "Replace var -> let"
MaxGraey Jan 19, 2018
ece3ff7
Fix tests
MaxGraey Jan 19, 2018
d1f7d96
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 23, 2018
61da223
Update
MaxGraey Jan 23, 2018
eefade5
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 27, 2018
fb455f2
Update test fixture
MaxGraey Jan 27, 2018
142c201
Add tests for parseInt
MaxGraey Jan 27, 2018
f4b7706
Minor fixes
MaxGraey Jan 27, 2018
b42d894
Merge branch 'master' into std/implement-parse-int
MaxGraey Jan 28, 2018
37b6288
Update tests
MaxGraey Jan 28, 2018
309e232
Update u32 -> i32 in parseInt
MaxGraey Jan 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/asc
100644 → 100755
Empty file.
912 changes: 912 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

78 changes: 76 additions & 2 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const EMPTY: String = changetype<String>("");

const cp: u16 = 43; // "+"
const cn: u16 = 45; // "-"
const cx: u16 = 120; // "x"
const cX: u16 = 88; // "X"
const c0: u16 = 48; // "0"
const c9: u16 = 57; // "9"
const ca: u16 = 97; // "a"
const cz: u16 = 122; // "z"
const cA: u16 = 65; // "A"
const cZ: u16 = 90; // "Z"

export class String {

private __memory: usize;
Expand Down Expand Up @@ -189,8 +200,71 @@ function isWhiteSpaceOrLineTerminator(c: u16): bool {
}

// @binding(CALL, [ STRING, PASS_THRU ], PASS_THRU)
export function parseInt(str: string, radix: i32 = 10): f64 {
throw new Error("not implemented");
export function parseInt(str: String, radix: i32 = 0): f64 {
var len = <u32>str.length;
if (len == 0 || (radix != 0 && radix < 2) || radix > 36)
return NaN;

var s0: u16 = str.charCodeAt(0);
var pos: u32 = 0;
var neg = (s0 == cn);

if (s0 == cp || neg) {
if (len == 1)
return NaN;

pos = 1;
}

if (radix == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be sufficiently covered by a radix: i32 = 10 parameter above?

Copy link
Member Author

@MaxGraey MaxGraey Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are fully justified changes. You can try in js:

console.log(parseInt('10')) // => 10
console.log(parseInt('10', 10)) // => 10

It's clear. But what about specified radix with hex prefix?

console.log(parseInt('0x10', 10)) // => 0
console.log(parseInt('0x10')) // => 16
console.log(parseInt('0x10', 0)) // => 16

So radix with zero or unspecified value mean actually auto radix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, makes sense.

if (str.charCodeAt(pos) == c0) {
var s1 = str.charCodeAt(pos + 1);
if (len > 1 && (s1 == cx || s1 == cX)) {
if (len < pos + 3)
return NaN;

radix = 16;
pos += 2;

} else {
// radix = 8;
// pos += 1;
radix = 10;
}
} else {
radix = 10;
}
}

var valid = false;
var result: f64 = 0;

for (; pos < len; ++pos) {
var digit: i32, c: i32 = str.charCodeAt(pos);

if (c0 <= c && c <= c9) digit = c - c0;
else if (ca <= c && c <= cz) digit = c - ca + 10;
else if (cA <= c && c <= cZ) digit = c - cA + 10;
else {
if (valid) break;
return NaN;
}

if (digit >= radix) {
if (valid) break;
return NaN;
}

valid = true;

result *= radix;
result += digit;
}

if (!valid && len > 2)
return NaN;

return neg ? -result : result;
}

// @binding(CALL, [ STRING ], PASS_THRU)
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/array.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4068,6 +4068,16 @@
CLASS_PROTOTYPE: Set
PROPERTY: std:set/Set#size
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
CLASS_PROTOTYPE: std:string/String
CLASS_PROTOTYPE: String
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/carray.wast
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@
CLASS_PROTOTYPE: Set
PROPERTY: std:set/Set#size
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
CLASS_PROTOTYPE: std:string/String
CLASS_PROTOTYPE: String
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/heap.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2864,6 +2864,16 @@
CLASS_PROTOTYPE: Set
PROPERTY: std:set/Set#size
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
CLASS_PROTOTYPE: std:string/String
CLASS_PROTOTYPE: String
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/std/set.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,16 @@
CLASS_PROTOTYPE: Set
PROPERTY: std:set/Set#size
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
CLASS_PROTOTYPE: std:string/String
CLASS_PROTOTYPE: String
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
Expand Down
4 changes: 4 additions & 0 deletions tests/compiler/std/string.optimized.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(memory $0 1)
(export "memory" (memory $0))
)
4 changes: 4 additions & 0 deletions tests/compiler/std/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// const _123: String = changetype<String>("123");

// parseInt(_123);
// parseInt(new String(0, 0));
130 changes: 130 additions & 0 deletions tests/compiler/std/string.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
(module
(global $HEAP_BASE i32 (i32.const 4))
(memory $0 1)
(export "memory" (memory $0))
)
(;
[program.elements]
GLOBAL: NaN
GLOBAL: Infinity
FUNCTION_PROTOTYPE: isNaN
FUNCTION_PROTOTYPE: isFinite
FUNCTION_PROTOTYPE: clz
FUNCTION_PROTOTYPE: ctz
FUNCTION_PROTOTYPE: popcnt
FUNCTION_PROTOTYPE: rotl
FUNCTION_PROTOTYPE: rotr
FUNCTION_PROTOTYPE: abs
FUNCTION_PROTOTYPE: max
FUNCTION_PROTOTYPE: min
FUNCTION_PROTOTYPE: ceil
FUNCTION_PROTOTYPE: floor
FUNCTION_PROTOTYPE: copysign
FUNCTION_PROTOTYPE: nearest
FUNCTION_PROTOTYPE: reinterpret
FUNCTION_PROTOTYPE: sqrt
FUNCTION_PROTOTYPE: trunc
FUNCTION_PROTOTYPE: load
FUNCTION_PROTOTYPE: store
FUNCTION_PROTOTYPE: sizeof
FUNCTION_PROTOTYPE: select
FUNCTION_PROTOTYPE: unreachable
FUNCTION_PROTOTYPE: current_memory
FUNCTION_PROTOTYPE: grow_memory
FUNCTION_PROTOTYPE: changetype
FUNCTION_PROTOTYPE: assert
FUNCTION_PROTOTYPE: i8
FUNCTION_PROTOTYPE: i16
FUNCTION_PROTOTYPE: i32
FUNCTION_PROTOTYPE: i64
FUNCTION_PROTOTYPE: u8
FUNCTION_PROTOTYPE: u16
FUNCTION_PROTOTYPE: u32
FUNCTION_PROTOTYPE: u64
FUNCTION_PROTOTYPE: bool
FUNCTION_PROTOTYPE: f32
FUNCTION_PROTOTYPE: f64
FUNCTION_PROTOTYPE: isize
FUNCTION_PROTOTYPE: usize
GLOBAL: HEAP_BASE
CLASS_PROTOTYPE: std:array/Array
CLASS_PROTOTYPE: Array
PROPERTY: std:array/Array#length
CLASS_PROTOTYPE: std:array/CArray
CLASS_PROTOTYPE: CArray
CLASS_PROTOTYPE: std:error/Error
CLASS_PROTOTYPE: Error
CLASS_PROTOTYPE: std:error/RangeError
CLASS_PROTOTYPE: RangeError
GLOBAL: std:heap/ALIGN_LOG2
GLOBAL: std:heap/ALIGN_SIZE
GLOBAL: std:heap/ALIGN_MASK
GLOBAL: std:heap/HEAP_OFFSET
FUNCTION_PROTOTYPE: std:heap/allocate_memory
FUNCTION_PROTOTYPE: allocate_memory
FUNCTION_PROTOTYPE: std:heap/free_memory
FUNCTION_PROTOTYPE: free_memory
FUNCTION_PROTOTYPE: std:heap/copy_memory
FUNCTION_PROTOTYPE: std:heap/move_memory
FUNCTION_PROTOTYPE: move_memory
FUNCTION_PROTOTYPE: std:heap/set_memory
FUNCTION_PROTOTYPE: set_memory
FUNCTION_PROTOTYPE: std:heap/compare_memory
FUNCTION_PROTOTYPE: compare_memory
CLASS_PROTOTYPE: std:map/Map
CLASS_PROTOTYPE: Map
CLASS_PROTOTYPE: std:regexp/RegExp
CLASS_PROTOTYPE: RegExp
CLASS_PROTOTYPE: std:set/Set
CLASS_PROTOTYPE: Set
PROPERTY: std:set/Set#size
GLOBAL: std:string/EMPTY
GLOBAL: std:string/cp
GLOBAL: std:string/cn
GLOBAL: std:string/cx
GLOBAL: std:string/cX
GLOBAL: std:string/c0
GLOBAL: std:string/c9
GLOBAL: std:string/ca
GLOBAL: std:string/cz
GLOBAL: std:string/cA
GLOBAL: std:string/cZ
CLASS_PROTOTYPE: std:string/String
CLASS_PROTOTYPE: String
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
FUNCTION_PROTOTYPE: std:string/parseInt
FUNCTION_PROTOTYPE: parseInt
FUNCTION_PROTOTYPE: std:string/parseFloat
FUNCTION_PROTOTYPE: parseFloat
[program.exports]
CLASS_PROTOTYPE: Array
CLASS_PROTOTYPE: std:array/Array
CLASS_PROTOTYPE: CArray
CLASS_PROTOTYPE: std:array/CArray
CLASS_PROTOTYPE: Error
CLASS_PROTOTYPE: std:error/Error
CLASS_PROTOTYPE: RangeError
CLASS_PROTOTYPE: std:error/RangeError
FUNCTION_PROTOTYPE: allocate_memory
FUNCTION_PROTOTYPE: std:heap/allocate_memory
FUNCTION_PROTOTYPE: free_memory
FUNCTION_PROTOTYPE: std:heap/free_memory
FUNCTION_PROTOTYPE: move_memory
FUNCTION_PROTOTYPE: std:heap/move_memory
FUNCTION_PROTOTYPE: set_memory
FUNCTION_PROTOTYPE: std:heap/set_memory
FUNCTION_PROTOTYPE: compare_memory
FUNCTION_PROTOTYPE: std:heap/compare_memory
CLASS_PROTOTYPE: Map
CLASS_PROTOTYPE: std:map/Map
CLASS_PROTOTYPE: RegExp
CLASS_PROTOTYPE: std:regexp/RegExp
CLASS_PROTOTYPE: Set
CLASS_PROTOTYPE: std:set/Set
CLASS_PROTOTYPE: String
CLASS_PROTOTYPE: std:string/String
FUNCTION_PROTOTYPE: parseInt
FUNCTION_PROTOTYPE: std:string/parseInt
FUNCTION_PROTOTYPE: parseFloat
FUNCTION_PROTOTYPE: std:string/parseFloat
;)