-
-
Notifications
You must be signed in to change notification settings - Fork 673
ParseInt Implementation #8
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
Conversation
The tokenizer also has this one derived from tsc. Might make sense to bundle src/util/charcode with stdlib as well so there's a source for the constants. Otherwise, declaring a |
One way to test it, btw., could be to use the charcodes (u16) as the module's memory and then |
Got it. May be tomorrow I switch |
Unfortunately I can't do that because |
Yeah, That doesn't lead to something that "just works", of course, it's just the only test case possible atm. |
Hmm, sounds promising |
Just tested in node.js: > parseInt("0011", 2)
3
> parseInt("0011", 8)
9 |
Right, current implementation support any radix from 2 to 36 the same as native JS method. > parseInt("0x10") // expecting 16
16
> parseInt("0o10") // expecting 8
0
> parseInt("0b10") // expecting 2
0 |
Hmm, after I added additional file in test directory script |
Yeah, config is tested against tsc, and it doesn't like Workaround I have in mind currently is to implicitly make all exports from stdlib files globals so functions don't need a |
I don't think so problem in |
The other differences in the the latest log are expected as well because there have been changes to other parts of the standard library meanwhile. For example the You can regenerate the fixtures by running |
So can I somehow fix that? |
Yes, just regenerate the outdated fixture your code is compared to using the command above. Fixtures are there to visually confirm that the output of a known-to-be-good test didn't change due to a possibly unrelated change. Hence, when editing code that affects a fixture, the intended procedure is to regenerate the fixture and then manually validate, by means of looking at the diff, that the changes are expected. |
Got it! It's kind of snapshotting |
pos = 1; | ||
} | ||
|
||
if (radix == 0) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, makes sense.
I added tests for parseInt. Only one case produce unreachable issue. Need more investigation |
var strHexInvalid: string = "0x"; | ||
var strHexnInvalid: string = "-0x"; | ||
var strHexnnInv: string = "--0x10"; | ||
// var strHexInvChar: string = "0x1g"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case we get unreachable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's strange. Probably hitting an assertion somewhere then. Might make sense to hook assert
up to an import on failure now, so we know where that is :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It always print:
Testing compiler/std/string.ts
parse incl. I/O: 4.978ms / compile: 4.763ms
validate OK
[trap unreachable]
interpret ERROR:undefined
Created
Created optimized
And I can't figure out what is problem
To debug this further, you can now import an @global
declare function abort(message: string | null, filename: string, line: i32, column: i32): void;
... On the JS side, that'd look like: function abort(msg, file, line, column) {
throw Error("Assertion failed: '" + (msg ? getString(msg) : "") + "' at " + getString(file) + ":" + line + ":" + column);
}
function getString(ptr) {
var len = new Uint32Array(exports.memory.buffer, ptr)[0];
var str = new Uint16Array(exports.memory.buffer, ptr + 4).subarray(0, len);
return String.fromCharCode.apply(String, str);
} with |
I created something above that might help as a starting point. It possibly doesn't catch all the corner cases yet and has only rudimentary tests. Most important difference is that it outputs i64s, not sure how far we want to go with compatibility there. |
How do you like to proceed with this one? Was probably a bit insensible of me to implement something on my own though you were working on something already, sorry for that. |
It's ok, you already implement this so I close this |
Add diff to ./test.sh based on snapshots of output
…ovments update AssemblyScript & as-pecr. Fix string comparisions
Hi dcode! I'm trying to help you with std functions.
There is parseInt function. I tested this part on pure typescript and it's behave exactly the same as original JS method. For real tests in AS environment need some parser functionality (String literals) which missing yet.
TODO