Comments (6)
Considering how often Num.fromString
is used, it's surprising this bug hasn't been noticed before. Perhaps people had thought that, since strtod
returns 0
for invalid numeric strings, this was the expected behavior.
Anyway, on the face of it, it seems (as @minirop said) that all we need to do is to check whether any part of the string has been consumed by strtod
and return NULL
if it hasn't.
If we do that, there should be no need to treat an empty string as a special case or to skip past any trailing whitespace.
So that would give us:
DEF_PRIMITIVE(num_fromString)
{
if (!validateString(vm, args[1], "Argument")) return false;
ObjString* string = AS_STRING(args[1]);
errno = 0;
char* end;
double number = strtod(string->value, &end);
if (errno == ERANGE) RETURN_ERROR("Number literal is too large.");
// Check we have consumed any part of the string. Otherwise, it contains non-number
// characters and we can't parse it.
if (end == string->value) RETURN_NULL;
RETURN_NUM(number);
}
from wren.
Yes, I remember that PR.
If we introduce octal (0oxxx
) and binary (0bxxx
) literals, which I think we should, then strtod
will always return 0.0
when passed those literals in string form even though it does currently deal properly with hexadecimal strings. For consistency, we'd therefore need something like your solution to deal with all three.
from wren.
there probably should be a check to see if end == string->value
(meaning nothing has been consumed) before skipping the whitespaces.
Lines 619 to 622 in a4ae905
from wren.
I don't know why the string is strip
ed here, but it doesn't seems the sensible way to do. Instead, a strip
method should be provided instead and let the user decide to strip
or not the string before parsing to number.
from wren.
Trim is user space already fwiw
from wren.
Some time ago, I tried removing our dependency on the standard strtod
and strtoll
functions with #984.
Lines 232 to 417 in 3f5a16a
It certainly is a very extreme solution to this specific problem but thought it could be more intuitive this way and get rid of a few weird quirks of strtoXX
.
With wrenParseNum
we have easier access to different error messages than strtoXX
, for example: if there were no digits when parsing. fromString
currently discards this information but I find it could still be helpful for the compiler or if someone else wanted to use wrenParseNum
in their host C code.
I think the biggest issue of #984 is that the results of wrenParseNum
may be less accurate to strtoXX
as the double's exponents gets too high or too low.
from wren.
Related Issues (20)
- [RFC] Object.responds(_) method HOT 10
- [RFC] Add routines for degrees/radians conversions HOT 21
- [RFC] Adding a `Tuple` with language support HOT 11
- [RFC] Adding `const` versions of `Object`s. HOT 9
- Class reflection for embedding HOT 3
- [RFC] `veery` lang transpiler to `wren` lang HOT 3
- [RFC] Object method message passing syntax HOT 28
- [RFC] Add `static Object.typeOf(_)` (and deprecate `Object.type` ?) HOT 9
- How can i stop wren script running? HOT 3
- Calling wren method handle from inside a bound foreign method body HOT 5
- Whitespace bugs? HOT 4
- Serialize (suspended) fibers or serialize the vm HOT 4
- wren_debug.c should use vm->config.writeFn instead of printf HOT 1
- Should we document this aspect of for loop control variable behavior?
- How do I pass a foreign object to a function call? HOT 3
- is Wren dead? HOT 2
- Where causes code to be called twice HOT 3
- How to return other foreign class obj from a foreign class ? HOT 7
- wrenCall -> foreign call causes memory corruption HOT 11
- Compound Assignments HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wren.