From 1cffc0eb73dc479ea83da847c342926d55028b45 Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Wed, 15 Jul 2009 12:51:21 +0200 Subject: [PATCH] jscript: Improve Number_toString implementation. --- dlls/jscript/number.c | 113 ++++++++++++++++++++++++++++++++++++-- dlls/jscript/tests/api.js | 50 +++++++++++++++-- 2 files changed, 151 insertions(+), 12 deletions(-) diff --git a/dlls/jscript/number.c b/dlls/jscript/number.c index d6591d06317..5399c91615a 100644 --- a/dlls/jscript/number.c +++ b/dlls/jscript/number.c @@ -16,6 +16,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include + #include "jscript.h" #include "wine/debug.h" @@ -39,11 +41,14 @@ static const WCHAR propertyIsEnumerableW[] = {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0}; static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0}; +#define NUMBER_TOSTRING_BUF_SIZE 64 /* ECMA-262 3rd Edition 15.7.4.2 */ static HRESULT Number_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { NumberInstance *number; + INT radix = 10; + DOUBLE val; BSTR str; HRESULT hres; @@ -56,14 +61,110 @@ static HRESULT Number_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPA number = (NumberInstance*)dispex; - if(arg_cnt(dp) != 0) { - FIXME("unsupported args\n"); - return E_NOTIMPL; + if(arg_cnt(dp)) { + hres = to_int32(dispex->ctx, get_arg(dp, 0), ei, &radix); + if(FAILED(hres)) + return hres; + + if(radix<2 || radix>36) { + FIXME("throw TypeError\n"); + return E_FAIL; + } } - hres = to_string(dispex->ctx, &number->num, ei, &str); - if(FAILED(hres)) - return hres; + if(V_VT(&number->num) == VT_I4) + val = V_I4(&number->num); + else + val = V_R8(&number->num); + + if(radix==10 || isnan(val) || isinf(val)) { + hres = to_string(dispex->ctx, &number->num, ei, &str); + if(FAILED(hres)) + return hres; + } + else { + INT idx = 0; + DOUBLE integ, frac, log_radix = 0; + WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16]; + BOOL exp = FALSE; + + if(val<0) { + val = -val; + buf[idx++] = '-'; + } + + while(1) { + integ = floor(val); + frac = val-integ; + + if(integ == 0) + buf[idx++] = '0'; + while(integ>=1 && idx beg) { + wch = buf[beg]; + buf[beg++] = buf[end]; + buf[end--] = wch; + } + } + + if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.'; + + while(frac>0 && idx