diff --git a/jsbuiltin.c b/jsbuiltin.c index 68410db..7edb18e 100644 --- a/jsbuiltin.c +++ b/jsbuiltin.c @@ -44,6 +44,7 @@ static int jsB_parseInt(js_State *J, int argc) { const char *s = js_tostring(J, 1); double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10; + while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; js_pushnumber(J, strtol(s, NULL, radix == 0 ? 10 : radix)); return 1; } @@ -51,6 +52,7 @@ static int jsB_parseInt(js_State *J, int argc) static int jsB_parseFloat(js_State *J, int argc) { const char *s = js_tostring(J, 1); + while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; js_pushnumber(J, strtod(s, NULL)); return 1; } diff --git a/jsvalue.c b/jsvalue.c index 7f0d3bf..2bd091d 100644 --- a/jsvalue.c +++ b/jsvalue.c @@ -1,4 +1,5 @@ #include "jsi.h" +#include "jslex.h" #include "jscompile.h" #include "jsvalue.h" #include "utf.h" @@ -113,8 +114,16 @@ int jsV_toboolean(js_State *J, const js_Value *v) /* ToNumber() on a string */ double jsV_stringtonumber(js_State *J, const char *s) { - /* TODO: use lexer to parse string grammar */ - return strtod(s, NULL); + char *e; + double n; + while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s; + if (s[0] == '0' && s[1] == 'x' && s[2] != 0) + n = strtol(s + 2, &e, 16); + else + n = strtod(s, &e); + while (jsY_iswhite(*e) || jsY_isnewline(*e)) ++e; + if (*e) return NAN; + return n; } /* ToNumber() on a value */