2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
37 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
38 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
39 static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
40 static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
41 static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
42 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
44 #define NUMBER_TOSTRING_BUF_SIZE 64
45 #define NUMBER_DTOA_SIZE 18
47 static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
49 return (NumberInstance*)vdisp->u.jsdisp;
52 static inline NumberInstance *number_this(vdisp_t *jsthis)
54 return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
57 static inline void dtoa(double d, WCHAR *buf, int size, int *dec_point)
62 /* TODO: this function should print doubles with bigger precision */
63 assert(size>=2 && size<=NUMBER_DTOA_SIZE && d>=0);
68 *dec_point = floor(log10(d));
69 l = d*pow(10, size-*dec_point-1);
77 for(i=size-2; i>=0; i--) {
82 /* log10 was wrong by 1 or rounding changed number of digits */
85 memmove(buf+1, buf, size-2);
87 }else if(buf[0]=='0' && buf[1]>='1' && buf[1]<='9') {
89 memmove(buf, buf+1, size-2);
94 static inline void number_to_fixed(double val, int prec, BSTR *out)
96 WCHAR buf[NUMBER_DTOA_SIZE];
97 int dec_point, size, buf_size, buf_pos;
106 if(val<=-1 || val>=1)
107 buf_size = log10(val)+prec+2;
110 if(buf_size > NUMBER_DTOA_SIZE)
111 buf_size = NUMBER_DTOA_SIZE;
113 dtoa(val, buf, buf_size, &dec_point);
125 str = SysAllocStringLen(NULL, size);
130 for(;buf_pos<buf_size-1 && dec_point; dec_point--)
131 str[size++] = buf[buf_pos++];
135 for(; dec_point>0; dec_point--)
140 for(; dec_point<0 && prec; dec_point++, prec--)
142 for(; buf_pos<buf_size-1 && prec; prec--)
143 str[size++] = buf[buf_pos++];
144 for(; prec; prec--) {
153 static inline void number_to_exponential(double val, int prec, BSTR *out)
155 WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
156 int dec_point, size, buf_size, exp_size = 1;
166 if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
167 buf_size = NUMBER_DTOA_SIZE;
168 dtoa(val, buf, buf_size, &dec_point);
171 for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
175 while(dec_point>=size || dec_point<=-size) {
181 size = buf_size+2+exp_size; /* 2 = strlen(e+) */
183 size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
185 size = prec+4+exp_size; /* 4 = strlen(0.e+) */
188 str = SysAllocStringLen(NULL, size);
194 str[size++] = *pbuf++;
198 str[size++] = *pbuf++;
199 for(; prec>buf_size-1; prec--)
207 dec_point = -dec_point;
209 for(str[size]='0', size+=exp_size-1; dec_point>0; dec_point/=10)
210 str[size--] = '0'+dec_point%10;
217 /* ECMA-262 3rd Edition 15.7.4.2 */
218 static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
219 VARIANT *retv, jsexcept_t *ei)
221 NumberInstance *number;
229 if(!(number = number_this(jsthis)))
230 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
233 hres = to_int32(ctx, get_arg(dp, 0), ei, &radix);
237 if(radix<2 || radix>36)
238 return throw_type_error(ctx, ei, JS_E_INVALIDARG, NULL);
243 if(radix==10 || isnan(val) || isinf(val)) {
246 num_set_val(&v, val);
247 hres = to_string(ctx, &v, ei, &str);
252 DOUBLE integ, frac, log_radix = 0;
253 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
267 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
268 buf[idx] = fmod(integ, radix);
269 if(buf[idx]<10) buf[idx] += '0';
270 else buf[idx] += 'a'-10;
275 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
276 INT beg = buf[0]=='-'?1:0;
282 buf[beg++] = buf[end];
287 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
289 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
291 buf[idx] = fmod(frac, radix);
293 if(buf[idx]<10) buf[idx] += '0';
294 else buf[idx] += 'a'-10;
298 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
300 idx = (buf[0]=='-') ? 1 : 0;
301 log_radix = floor(log(val)/log(radix));
302 val *= pow(radix, -log_radix);
309 while(buf[idx-1] == '0') idx--;
310 if(buf[idx-1] == '.') idx--;
316 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
320 log_radix = -log_radix;
324 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
327 else buf[idx] = '\0';
329 str = SysAllocString(buf);
331 return E_OUTOFMEMORY;
335 V_VT(retv) = VT_BSTR;
343 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
344 VARIANT *retv, jsexcept_t *ei)
350 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
351 VARIANT *retv, jsexcept_t *ei)
353 NumberInstance *number;
361 if(!(number = number_this(jsthis)))
362 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
365 hres = to_int32(ctx, get_arg(dp, 0), ei, &prec);
369 if(prec<0 || prec>20)
370 return throw_range_error(ctx, ei, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
374 if(isinf(val) || isnan(val)) {
377 num_set_val(&v, val);
378 hres = to_string(ctx, &v, ei, &str);
382 number_to_fixed(val, prec, &str);
386 V_VT(retv) = VT_BSTR;
394 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
395 VARIANT *retv, jsexcept_t *ei)
397 NumberInstance *number;
405 if(!(number = number_this(jsthis)))
406 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
409 hres = to_int32(ctx, get_arg(dp, 0), ei, &prec);
413 if(prec<0 || prec>20)
414 return throw_range_error(ctx, ei, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
418 if(isinf(val) || isnan(val)) {
421 num_set_val(&v, val);
422 hres = to_string(ctx, &v, ei, &str);
428 number_to_exponential(val, prec, &str);
432 V_VT(retv) = VT_BSTR;
440 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
441 VARIANT *retv, jsexcept_t *ei)
443 NumberInstance *number;
449 if(!(number = number_this(jsthis)))
450 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
453 hres = to_int32(ctx, get_arg(dp, 0), ei, &prec);
457 if(prec<1 || prec>21)
458 return throw_range_error(ctx, ei, JS_E_PRECISION_OUT_OF_RANGE, NULL);
462 if(isinf(val) || isnan(val) || !prec) {
465 num_set_val(&v, val);
466 hres = to_string(ctx, &v, ei, &str);
471 size = floor(log10(val>0 ? val : -val)) + 1;
476 number_to_exponential(val, prec-1, &str);
478 number_to_fixed(val, prec-size, &str);
482 V_VT(retv) = VT_BSTR;
490 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
491 VARIANT *retv, jsexcept_t *ei)
493 NumberInstance *number;
497 if(!(number = number_this(jsthis)))
498 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
501 num_set_val(retv, number->value);
505 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
506 VARIANT *retv, jsexcept_t *ei)
508 NumberInstance *number = number_from_vdisp(jsthis);
512 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
513 case DISPATCH_PROPERTYGET:
514 num_set_val(retv, number->value);
518 FIXME("flags %x\n", flags);
525 static const builtin_prop_t Number_props[] = {
526 {toExponentialW, Number_toExponential, PROPF_METHOD|1},
527 {toFixedW, Number_toFixed, PROPF_METHOD},
528 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
529 {toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
530 {toStringW, Number_toString, PROPF_METHOD|1},
531 {valueOfW, Number_valueOf, PROPF_METHOD}
534 static const builtin_info_t Number_info = {
536 {NULL, Number_value, 0},
537 sizeof(Number_props)/sizeof(*Number_props),
543 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
544 VARIANT *retv, jsexcept_t *ei)
561 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
566 num_set_val(retv, n);
569 case DISPATCH_CONSTRUCT: {
573 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
580 hres = create_number(ctx, n, &obj);
584 var_set_jsdisp(retv, obj);
588 FIXME("unimplemented flags %x\n", flags);
595 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
597 NumberInstance *number;
600 number = heap_alloc_zero(sizeof(NumberInstance));
602 return E_OUTOFMEMORY;
605 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
607 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
615 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
617 NumberInstance *number;
620 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
622 hres = alloc_number(ctx, object_prototype, &number);
627 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
628 PROPF_CONSTR|1, &number->dispex, ret);
630 jsdisp_release(&number->dispex);
634 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
636 NumberInstance *number;
639 hres = alloc_number(ctx, NULL, &number);
643 number->value = value;
645 *ret = &number->dispex;