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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
34 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
35 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
36 static const WCHAR toFixedW
[] = {'t','o','F','i','x','e','d',0};
37 static const WCHAR toExponentialW
[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
38 static const WCHAR toPrecisionW
[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
39 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
41 #define NUMBER_TOSTRING_BUF_SIZE 64
42 #define NUMBER_DTOA_SIZE 18
44 static inline NumberInstance
*number_from_jsdisp(jsdisp_t
*jsdisp
)
46 return CONTAINING_RECORD(jsdisp
, NumberInstance
, dispex
);
49 static inline NumberInstance
*number_from_vdisp(vdisp_t
*vdisp
)
51 return number_from_jsdisp(vdisp
->u
.jsdisp
);
54 static inline NumberInstance
*number_this(vdisp_t
*jsthis
)
56 return is_vclass(jsthis
, JSCLASS_NUMBER
) ? number_from_vdisp(jsthis
) : NULL
;
59 static inline void number_to_str(double d
, WCHAR
*buf
, int size
, int *dec_point
)
64 /* TODO: this function should print doubles with bigger precision */
65 assert(size
>=2 && size
<=NUMBER_DTOA_SIZE
&& d
>=0);
70 *dec_point
= floor(log10(d
));
71 l
= d
*pow(10, size
-*dec_point
-1);
79 for(i
=size
-2; i
>=0; i
--) {
84 /* log10 was wrong by 1 or rounding changed number of digits */
87 memmove(buf
+1, buf
, size
-2);
89 }else if(buf
[0]=='0' && buf
[1]>='1' && buf
[1]<='9') {
91 memmove(buf
, buf
+1, size
-2);
96 static inline jsstr_t
*number_to_fixed(double val
, int prec
)
98 WCHAR buf
[NUMBER_DTOA_SIZE
];
99 int dec_point
, size
, buf_size
, buf_pos
;
104 TRACE("%lf %d\n", val
, prec
);
112 buf_size
= log10(val
)+prec
+2;
114 buf_size
= prec
? prec
+1 : 2;
115 if(buf_size
> NUMBER_DTOA_SIZE
)
116 buf_size
= NUMBER_DTOA_SIZE
;
118 number_to_str(val
, buf
, buf_size
, &dec_point
);
130 ret
= jsstr_alloc_buf(size
, &str
);
138 for(;buf_pos
<buf_size
-1 && dec_point
; dec_point
--)
139 str
[size
++] = buf
[buf_pos
++];
143 for(; dec_point
>0; dec_point
--)
148 for(; dec_point
<0 && prec
; dec_point
++, prec
--)
150 for(; buf_pos
<buf_size
-1 && prec
; prec
--)
151 str
[size
++] = buf
[buf_pos
++];
152 for(; prec
; prec
--) {
160 static inline jsstr_t
*number_to_exponential(double val
, int prec
)
162 WCHAR buf
[NUMBER_DTOA_SIZE
], *pbuf
;
163 int dec_point
, size
, buf_size
, exp_size
= 1;
174 if(buf_size
<2 || buf_size
>NUMBER_DTOA_SIZE
)
175 buf_size
= NUMBER_DTOA_SIZE
;
176 number_to_str(val
, buf
, buf_size
, &dec_point
);
179 for(; buf_size
>1 && buf
[buf_size
-1]=='0'; buf_size
--)
183 while(dec_point
>=size
|| dec_point
<=-size
) {
189 size
= buf_size
+2+exp_size
; /* 2 = strlen(e+) */
191 size
= buf_size
+3+exp_size
; /* 3 = strlen(.e+) */
193 size
= prec
+4+exp_size
; /* 4 = strlen(0.e+) */
197 ret
= jsstr_alloc_buf(size
, &str
);
205 str
[size
++] = *pbuf
++;
209 str
[size
++] = *pbuf
++;
210 for(; prec
>buf_size
-1; prec
--)
218 dec_point
= -dec_point
;
222 str
[--size
] = '0'+dec_point
%10;
231 /* ECMA-262 3rd Edition 15.7.4.2 */
232 static HRESULT
Number_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
235 NumberInstance
*number
;
243 if(!(number
= number_this(jsthis
)))
244 return throw_type_error(ctx
, JS_E_NUMBER_EXPECTED
, NULL
);
247 hres
= to_int32(ctx
, argv
[0], &radix
);
251 if(radix
<2 || radix
>36)
252 return throw_type_error(ctx
, JS_E_INVALIDARG
, NULL
);
257 if(radix
==10 || !is_finite(val
)) {
258 hres
= to_string(ctx
, jsval_number(val
), &str
);
263 DOUBLE integ
, frac
, log_radix
= 0;
264 WCHAR buf
[NUMBER_TOSTRING_BUF_SIZE
+16];
278 while(integ
>=1 && idx
<NUMBER_TOSTRING_BUF_SIZE
) {
279 buf
[idx
] = fmod(integ
, radix
);
280 if(buf
[idx
]<10) buf
[idx
] += '0';
281 else buf
[idx
] += 'a'-10;
286 if(idx
<NUMBER_TOSTRING_BUF_SIZE
) {
287 INT beg
= buf
[0]=='-'?1:0;
293 buf
[beg
++] = buf
[end
];
298 if(idx
!= NUMBER_TOSTRING_BUF_SIZE
) buf
[idx
++] = '.';
300 while(frac
>0 && idx
<NUMBER_TOSTRING_BUF_SIZE
) {
302 buf
[idx
] = fmod(frac
, radix
);
304 if(buf
[idx
]<10) buf
[idx
] += '0';
305 else buf
[idx
] += 'a'-10;
309 if(idx
==NUMBER_TOSTRING_BUF_SIZE
&& !exp
) {
311 idx
= (buf
[0]=='-') ? 1 : 0;
312 log_radix
= floor(log(val
)/log(radix
));
313 val
*= pow(radix
, -log_radix
);
320 while(buf
[idx
-1] == '0') idx
--;
321 if(buf
[idx
-1] == '.') idx
--;
327 static const WCHAR formatW
[] = {'(','e','%','c','%','d',')',0};
331 log_radix
= -log_radix
;
335 sprintfW(&buf
[idx
], formatW
, ch
, (int)log_radix
);
338 else buf
[idx
] = '\0';
340 str
= jsstr_alloc(buf
);
342 return E_OUTOFMEMORY
;
346 *r
= jsval_string(str
);
352 static HRESULT
Number_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
359 static HRESULT
Number_toFixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
362 NumberInstance
*number
;
370 if(!(number
= number_this(jsthis
)))
371 return throw_type_error(ctx
, JS_E_NUMBER_EXPECTED
, NULL
);
374 hres
= to_int32(ctx
, argv
[0], &prec
);
378 if(prec
<0 || prec
>20)
379 return throw_range_error(ctx
, JS_E_FRACTION_DIGITS_OUT_OF_RANGE
, NULL
);
383 if(!is_finite(val
)) {
384 hres
= to_string(ctx
, jsval_number(val
), &str
);
388 str
= number_to_fixed(val
, prec
);
390 return E_OUTOFMEMORY
;
394 *r
= jsval_string(str
);
400 static HRESULT
Number_toExponential(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
403 NumberInstance
*number
;
411 if(!(number
= number_this(jsthis
)))
412 return throw_type_error(ctx
, JS_E_NUMBER_EXPECTED
, NULL
);
415 hres
= to_int32(ctx
, argv
[0], &prec
);
419 if(prec
<0 || prec
>20)
420 return throw_range_error(ctx
, JS_E_FRACTION_DIGITS_OUT_OF_RANGE
, NULL
);
424 if(!is_finite(val
)) {
425 hres
= to_string(ctx
, jsval_number(val
), &str
);
431 str
= number_to_exponential(val
, prec
);
433 return E_OUTOFMEMORY
;
437 *r
= jsval_string(str
);
443 static HRESULT
Number_toPrecision(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
446 NumberInstance
*number
;
452 if(!(number
= number_this(jsthis
)))
453 return throw_type_error(ctx
, JS_E_NUMBER_EXPECTED
, NULL
);
456 hres
= to_int32(ctx
, argv
[0], &prec
);
460 if(prec
<1 || prec
>21)
461 return throw_range_error(ctx
, JS_E_PRECISION_OUT_OF_RANGE
, NULL
);
465 if(!is_finite(val
) || !prec
) {
466 hres
= to_string(ctx
, jsval_number(val
), &str
);
471 size
= floor(log10(val
>0 ? val
: -val
)) + 1;
476 str
= number_to_exponential(val
, prec
-1);
478 str
= number_to_fixed(val
, prec
-size
);
480 return E_OUTOFMEMORY
;
484 *r
= jsval_string(str
);
490 static HRESULT
Number_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
493 NumberInstance
*number
;
497 if(!(number
= number_this(jsthis
)))
498 return throw_type_error(ctx
, JS_E_NUMBER_EXPECTED
, NULL
);
501 *r
= jsval_number(number
->value
);
505 static HRESULT
Number_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
507 NumberInstance
*number
= number_from_jsdisp(jsthis
);
509 TRACE("(%p)\n", number
);
511 *r
= jsval_number(number
->value
);
515 static const builtin_prop_t Number_props
[] = {
516 {toExponentialW
, Number_toExponential
, PROPF_METHOD
|1},
517 {toFixedW
, Number_toFixed
, PROPF_METHOD
},
518 {toLocaleStringW
, Number_toLocaleString
, PROPF_METHOD
},
519 {toPrecisionW
, Number_toPrecision
, PROPF_METHOD
|1},
520 {toStringW
, Number_toString
, PROPF_METHOD
|1},
521 {valueOfW
, Number_valueOf
, PROPF_METHOD
}
524 static const builtin_info_t Number_info
= {
526 {NULL
, NULL
,0, Number_get_value
},
527 sizeof(Number_props
)/sizeof(*Number_props
),
533 static const builtin_info_t NumberInst_info
= {
535 {NULL
, NULL
,0, Number_get_value
},
541 static HRESULT
NumberConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
553 *r
= jsval_number(0);
557 hres
= to_number(ctx
, argv
[0], &n
);
562 *r
= jsval_number(n
);
565 case DISPATCH_CONSTRUCT
: {
569 hres
= to_number(ctx
, argv
[0], &n
);
576 hres
= create_number(ctx
, n
, &obj
);
584 FIXME("unimplemented flags %x\n", flags
);
591 static HRESULT
alloc_number(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, NumberInstance
**ret
)
593 NumberInstance
*number
;
596 number
= heap_alloc_zero(sizeof(NumberInstance
));
598 return E_OUTOFMEMORY
;
601 hres
= init_dispex(&number
->dispex
, ctx
, &Number_info
, object_prototype
);
603 hres
= init_dispex_from_constr(&number
->dispex
, ctx
, &NumberInst_info
, ctx
->number_constr
);
613 HRESULT
create_number_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
615 NumberInstance
*number
;
618 static const WCHAR NumberW
[] = {'N','u','m','b','e','r',0};
620 hres
= alloc_number(ctx
, object_prototype
, &number
);
625 hres
= create_builtin_constructor(ctx
, NumberConstr_value
, NumberW
, NULL
,
626 PROPF_CONSTR
|1, &number
->dispex
, ret
);
628 jsdisp_release(&number
->dispex
);
632 HRESULT
create_number(script_ctx_t
*ctx
, double value
, jsdisp_t
**ret
)
634 NumberInstance
*number
;
637 hres
= alloc_number(ctx
, NULL
, &number
);
641 number
->value
= value
;
643 *ret
= &number
->dispex
;