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 jsstr_t *number_to_fixed(double val, int prec)
96 WCHAR buf[NUMBER_DTOA_SIZE];
97 int dec_point, size, buf_size, buf_pos;
107 if(val<=-1 || val>=1)
108 buf_size = log10(val)+prec+2;
111 if(buf_size > NUMBER_DTOA_SIZE)
112 buf_size = NUMBER_DTOA_SIZE;
114 dtoa(val, buf, buf_size, &dec_point);
126 ret = jsstr_alloc_buf(size);
135 for(;buf_pos<buf_size-1 && dec_point; dec_point--)
136 str[size++] = buf[buf_pos++];
140 for(; dec_point>0; dec_point--)
145 for(; dec_point<0 && prec; dec_point++, prec--)
147 for(; buf_pos<buf_size-1 && prec; prec--)
148 str[size++] = buf[buf_pos++];
149 for(; prec; prec--) {
157 static inline jsstr_t *number_to_exponential(double val, int prec)
159 WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
160 int dec_point, size, buf_size, exp_size = 1;
171 if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
172 buf_size = NUMBER_DTOA_SIZE;
173 dtoa(val, buf, buf_size, &dec_point);
176 for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
180 while(dec_point>=size || dec_point<=-size) {
186 size = buf_size+2+exp_size; /* 2 = strlen(e+) */
188 size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
190 size = prec+4+exp_size; /* 4 = strlen(0.e+) */
194 ret = jsstr_alloc_buf(size);
203 str[size++] = *pbuf++;
207 str[size++] = *pbuf++;
208 for(; prec>buf_size-1; prec--)
216 dec_point = -dec_point;
220 str[--size] = '0'+dec_point%10;
229 /* ECMA-262 3rd Edition 15.7.4.2 */
230 static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
233 NumberInstance *number;
241 if(!(number = number_this(jsthis)))
242 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
245 hres = to_int32(ctx, argv[0], &radix);
249 if(radix<2 || radix>36)
250 return throw_type_error(ctx, JS_E_INVALIDARG, NULL);
255 if(radix==10 || isnan(val) || isinf(val)) {
256 hres = to_string(ctx, jsval_number(val), &str);
261 DOUBLE integ, frac, log_radix = 0;
262 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
276 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
277 buf[idx] = fmod(integ, radix);
278 if(buf[idx]<10) buf[idx] += '0';
279 else buf[idx] += 'a'-10;
284 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
285 INT beg = buf[0]=='-'?1:0;
291 buf[beg++] = buf[end];
296 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
298 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
300 buf[idx] = fmod(frac, radix);
302 if(buf[idx]<10) buf[idx] += '0';
303 else buf[idx] += 'a'-10;
307 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
309 idx = (buf[0]=='-') ? 1 : 0;
310 log_radix = floor(log(val)/log(radix));
311 val *= pow(radix, -log_radix);
318 while(buf[idx-1] == '0') idx--;
319 if(buf[idx-1] == '.') idx--;
325 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
329 log_radix = -log_radix;
333 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
336 else buf[idx] = '\0';
338 str = jsstr_alloc(buf);
340 return E_OUTOFMEMORY;
344 *r = jsval_string(str);
350 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
357 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
360 NumberInstance *number;
368 if(!(number = number_this(jsthis)))
369 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
372 hres = to_int32(ctx, argv[0], &prec);
376 if(prec<0 || prec>20)
377 return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
381 if(isinf(val) || isnan(val)) {
382 hres = to_string(ctx, jsval_number(val), &str);
386 str = number_to_fixed(val, prec);
388 return E_OUTOFMEMORY;
392 *r = jsval_string(str);
398 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
401 NumberInstance *number;
409 if(!(number = number_this(jsthis)))
410 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
413 hres = to_int32(ctx, argv[0], &prec);
417 if(prec<0 || prec>20)
418 return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
422 if(isinf(val) || isnan(val)) {
423 hres = to_string(ctx, jsval_number(val), &str);
429 str = number_to_exponential(val, prec);
431 return E_OUTOFMEMORY;
435 *r = jsval_string(str);
441 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
444 NumberInstance *number;
450 if(!(number = number_this(jsthis)))
451 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
454 hres = to_int32(ctx, argv[0], &prec);
458 if(prec<1 || prec>21)
459 return throw_range_error(ctx, JS_E_PRECISION_OUT_OF_RANGE, NULL);
463 if(isinf(val) || isnan(val) || !prec) {
464 hres = to_string(ctx, jsval_number(val), &str);
469 size = floor(log10(val>0 ? val : -val)) + 1;
474 str = number_to_exponential(val, prec-1);
476 str = number_to_fixed(val, prec-size);
478 return E_OUTOFMEMORY;
482 *r = jsval_string(str);
488 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
491 NumberInstance *number;
495 if(!(number = number_this(jsthis)))
496 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
499 *r = jsval_number(number->value);
503 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
506 NumberInstance *number = number_from_vdisp(jsthis);
510 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
511 case DISPATCH_PROPERTYGET:
512 *r = jsval_number(number->value);
516 FIXME("flags %x\n", flags);
523 static const builtin_prop_t Number_props[] = {
524 {toExponentialW, Number_toExponential, PROPF_METHOD|1},
525 {toFixedW, Number_toFixed, PROPF_METHOD},
526 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
527 {toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
528 {toStringW, Number_toString, PROPF_METHOD|1},
529 {valueOfW, Number_valueOf, PROPF_METHOD}
532 static const builtin_info_t Number_info = {
534 {NULL, Number_value, 0},
535 sizeof(Number_props)/sizeof(*Number_props),
541 static const builtin_info_t NumberInst_info = {
543 {NULL, Number_value, 0},
549 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
561 *r = jsval_number(0);
565 hres = to_number(ctx, argv[0], &n);
570 *r = jsval_number(n);
573 case DISPATCH_CONSTRUCT: {
577 hres = to_number(ctx, argv[0], &n);
584 hres = create_number(ctx, n, &obj);
592 FIXME("unimplemented flags %x\n", flags);
599 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
601 NumberInstance *number;
604 number = heap_alloc_zero(sizeof(NumberInstance));
606 return E_OUTOFMEMORY;
609 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
611 hres = init_dispex_from_constr(&number->dispex, ctx, &NumberInst_info, ctx->number_constr);
621 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
623 NumberInstance *number;
626 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
628 hres = alloc_number(ctx, object_prototype, &number);
633 hres = create_builtin_constructor(ctx, NumberConstr_value, NumberW, NULL,
634 PROPF_CONSTR|1, &number->dispex, ret);
636 jsdisp_release(&number->dispex);
640 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
642 NumberInstance *number;
645 hres = alloc_number(ctx, NULL, &number);
649 number->value = value;
651 *ret = &number->dispex;