netstat: Fix compilation on systems that don't support nameless unions.
[wine.git] / dlls / jscript / number.c
blob351e91531946293535c0528a2f7dc8dbde611f69
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
23 #include <assert.h>
25 #include "jscript.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 typedef struct {
32 jsdisp_t dispex;
34 double value;
35 } NumberInstance;
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)
59 ULONGLONG l;
60 int i;
62 /* TODO: this function should print doubles with bigger precision */
63 assert(size>=2 && size<=NUMBER_DTOA_SIZE && d>=0);
65 if(d == 0)
66 *dec_point = 0;
67 else
68 *dec_point = floor(log10(d));
69 l = d*pow(10, size-*dec_point-1);
71 if(l%10 >= 5)
72 l = l/10+1;
73 else
74 l /= 10;
76 buf[size-1] = 0;
77 for(i=size-2; i>=0; i--) {
78 buf[i] = '0'+l%10;
79 l /= 10;
82 /* log10 was wrong by 1 or rounding changed number of digits */
83 if(l) {
84 (*dec_point)++;
85 memmove(buf+1, buf, size-2);
86 buf[0] = '0'+l;
87 }else if(buf[0]=='0' && buf[1]>='1' && buf[1]<='9') {
88 (*dec_point)--;
89 memmove(buf, buf+1, size-2);
90 buf[size-2] = '0';
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;
98 BOOL neg = FALSE;
99 jsstr_t *ret;
100 WCHAR *str;
102 TRACE("%lf %d\n", val, prec);
104 if(val < 0) {
105 neg = TRUE;
106 val = -val;
109 if(val >= 1)
110 buf_size = log10(val)+prec+2;
111 else
112 buf_size = prec ? prec+1 : 2;
113 if(buf_size > NUMBER_DTOA_SIZE)
114 buf_size = NUMBER_DTOA_SIZE;
116 dtoa(val, buf, buf_size, &dec_point);
117 dec_point++;
118 size = 0;
119 if(neg)
120 size++;
121 if(dec_point > 0)
122 size += dec_point;
123 else
124 size++;
125 if(prec)
126 size += prec+1;
128 ret = jsstr_alloc_buf(size);
129 if(!ret)
130 return NULL;
132 str = ret->str;
133 size = buf_pos = 0;
134 if(neg)
135 str[size++] = '-';
136 if(dec_point > 0) {
137 for(;buf_pos<buf_size-1 && dec_point; dec_point--)
138 str[size++] = buf[buf_pos++];
139 }else {
140 str[size++] = '0';
142 for(; dec_point>0; dec_point--)
143 str[size++] = '0';
144 if(prec) {
145 str[size++] = '.';
147 for(; dec_point<0 && prec; dec_point++, prec--)
148 str[size++] = '0';
149 for(; buf_pos<buf_size-1 && prec; prec--)
150 str[size++] = buf[buf_pos++];
151 for(; prec; prec--) {
152 str[size++] = '0';
155 str[size++] = 0;
156 return ret;
159 static inline jsstr_t *number_to_exponential(double val, int prec)
161 WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
162 int dec_point, size, buf_size, exp_size = 1;
163 BOOL neg = FALSE;
164 jsstr_t *ret;
165 WCHAR *str;
167 if(val < 0) {
168 neg = TRUE;
169 val = -val;
172 buf_size = prec+2;
173 if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
174 buf_size = NUMBER_DTOA_SIZE;
175 dtoa(val, buf, buf_size, &dec_point);
176 buf_size--;
177 if(prec == -1)
178 for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
179 buf[buf_size-1] = 0;
181 size = 10;
182 while(dec_point>=size || dec_point<=-size) {
183 size *= 10;
184 exp_size++;
187 if(buf_size == 1)
188 size = buf_size+2+exp_size; /* 2 = strlen(e+) */
189 else if(prec == -1)
190 size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
191 else
192 size = prec+4+exp_size; /* 4 = strlen(0.e+) */
193 if(neg)
194 size++;
196 ret = jsstr_alloc_buf(size);
197 if(!ret)
198 return NULL;
200 str = ret->str;
201 size = 0;
202 pbuf = buf;
203 if(neg)
204 str[size++] = '-';
205 str[size++] = *pbuf++;
206 if(buf_size != 1) {
207 str[size++] = '.';
208 while(*pbuf)
209 str[size++] = *pbuf++;
210 for(; prec>buf_size-1; prec--)
211 str[size++] = '0';
213 str[size++] = 'e';
214 if(dec_point >= 0) {
215 str[size++] = '+';
216 }else {
217 str[size++] = '-';
218 dec_point = -dec_point;
220 size += exp_size;
221 do {
222 str[--size] = '0'+dec_point%10;
223 dec_point /= 10;
224 }while(dec_point>0);
225 size += exp_size;
226 str[size] = 0;
228 return ret;
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,
233 jsval_t *r)
235 NumberInstance *number;
236 INT radix = 10;
237 DOUBLE val;
238 jsstr_t *str;
239 HRESULT hres;
241 TRACE("\n");
243 if(!(number = number_this(jsthis)))
244 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
246 if(argc) {
247 hres = to_int32(ctx, argv[0], &radix);
248 if(FAILED(hres))
249 return hres;
251 if(radix<2 || radix>36)
252 return throw_type_error(ctx, JS_E_INVALIDARG, NULL);
255 val = number->value;
257 if(radix==10 || isnan(val) || isinf(val)) {
258 hres = to_string(ctx, jsval_number(val), &str);
259 if(FAILED(hres))
260 return hres;
261 }else {
262 INT idx = 0;
263 DOUBLE integ, frac, log_radix = 0;
264 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
265 BOOL exp = FALSE;
267 if(val<0) {
268 val = -val;
269 buf[idx++] = '-';
272 while(1) {
273 integ = floor(val);
274 frac = val-integ;
276 if(integ == 0)
277 buf[idx++] = '0';
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;
282 integ /= radix;
283 idx++;
286 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
287 INT beg = buf[0]=='-'?1:0;
288 INT end = idx-1;
289 WCHAR wch;
291 while(end > beg) {
292 wch = buf[beg];
293 buf[beg++] = buf[end];
294 buf[end--] = wch;
298 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
300 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
301 frac *= radix;
302 buf[idx] = fmod(frac, radix);
303 frac -= buf[idx];
304 if(buf[idx]<10) buf[idx] += '0';
305 else buf[idx] += 'a'-10;
306 idx++;
309 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
310 exp = TRUE;
311 idx = (buf[0]=='-') ? 1 : 0;
312 log_radix = floor(log(val)/log(radix));
313 val *= pow(radix, -log_radix);
314 continue;
317 break;
320 while(buf[idx-1] == '0') idx--;
321 if(buf[idx-1] == '.') idx--;
323 if(exp) {
324 if(log_radix==0)
325 buf[idx] = 0;
326 else {
327 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
328 WCHAR ch;
330 if(log_radix<0) {
331 log_radix = -log_radix;
332 ch = '-';
334 else ch = '+';
335 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
338 else buf[idx] = '\0';
340 str = jsstr_alloc(buf);
341 if(!str)
342 return E_OUTOFMEMORY;
345 if(r)
346 *r = jsval_string(str);
347 else
348 jsstr_release(str);
349 return S_OK;
352 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
353 jsval_t *r)
355 FIXME("\n");
356 return E_NOTIMPL;
359 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
360 jsval_t *r)
362 NumberInstance *number;
363 DOUBLE val;
364 INT prec = 0;
365 jsstr_t *str;
366 HRESULT hres;
368 TRACE("\n");
370 if(!(number = number_this(jsthis)))
371 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
373 if(argc) {
374 hres = to_int32(ctx, argv[0], &prec);
375 if(FAILED(hres))
376 return hres;
378 if(prec<0 || prec>20)
379 return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
382 val = number->value;
383 if(isinf(val) || isnan(val)) {
384 hres = to_string(ctx, jsval_number(val), &str);
385 if(FAILED(hres))
386 return hres;
387 }else {
388 str = number_to_fixed(val, prec);
389 if(!str)
390 return E_OUTOFMEMORY;
393 if(r)
394 *r = jsval_string(str);
395 else
396 jsstr_release(str);
397 return S_OK;
400 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
401 jsval_t *r)
403 NumberInstance *number;
404 DOUBLE val;
405 INT prec = 0;
406 jsstr_t *str;
407 HRESULT hres;
409 TRACE("\n");
411 if(!(number = number_this(jsthis)))
412 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
414 if(argc) {
415 hres = to_int32(ctx, argv[0], &prec);
416 if(FAILED(hres))
417 return hres;
419 if(prec<0 || prec>20)
420 return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
423 val = number->value;
424 if(isinf(val) || isnan(val)) {
425 hres = to_string(ctx, jsval_number(val), &str);
426 if(FAILED(hres))
427 return hres;
428 }else {
429 if(!prec)
430 prec--;
431 str = number_to_exponential(val, prec);
432 if(!str)
433 return E_OUTOFMEMORY;
436 if(r)
437 *r = jsval_string(str);
438 else
439 jsstr_release(str);
440 return S_OK;
443 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
444 jsval_t *r)
446 NumberInstance *number;
447 INT prec = 0, size;
448 jsstr_t *str;
449 DOUBLE val;
450 HRESULT hres;
452 if(!(number = number_this(jsthis)))
453 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
455 if(argc) {
456 hres = to_int32(ctx, argv[0], &prec);
457 if(FAILED(hres))
458 return hres;
460 if(prec<1 || prec>21)
461 return throw_range_error(ctx, JS_E_PRECISION_OUT_OF_RANGE, NULL);
464 val = number->value;
465 if(isinf(val) || isnan(val) || !prec) {
466 hres = to_string(ctx, jsval_number(val), &str);
467 if(FAILED(hres))
468 return hres;
469 }else {
470 if(val != 0)
471 size = floor(log10(val>0 ? val : -val)) + 1;
472 else
473 size = 1;
475 if(size > prec)
476 str = number_to_exponential(val, prec-1);
477 else
478 str = number_to_fixed(val, prec-size);
479 if(!str)
480 return E_OUTOFMEMORY;
483 if(r)
484 *r = jsval_string(str);
485 else
486 jsstr_release(str);
487 return S_OK;
490 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
491 jsval_t *r)
493 NumberInstance *number;
495 TRACE("\n");
497 if(!(number = number_this(jsthis)))
498 return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
500 if(r)
501 *r = jsval_number(number->value);
502 return S_OK;
505 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
506 jsval_t *r)
508 NumberInstance *number = number_from_vdisp(jsthis);
510 switch(flags) {
511 case INVOKE_FUNC:
512 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
513 case DISPATCH_PROPERTYGET:
514 *r = jsval_number(number->value);
515 break;
517 default:
518 FIXME("flags %x\n", flags);
519 return E_NOTIMPL;
522 return S_OK;
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 = {
535 JSCLASS_NUMBER,
536 {NULL, Number_value, 0},
537 sizeof(Number_props)/sizeof(*Number_props),
538 Number_props,
539 NULL,
540 NULL
543 static const builtin_info_t NumberInst_info = {
544 JSCLASS_NUMBER,
545 {NULL, Number_value, 0},
546 0, NULL,
547 NULL,
548 NULL
551 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
552 jsval_t *r)
554 double n;
555 HRESULT hres;
557 TRACE("\n");
559 switch(flags) {
560 case INVOKE_FUNC:
561 if(!argc) {
562 if(r)
563 *r = jsval_number(0);
564 return S_OK;
567 hres = to_number(ctx, argv[0], &n);
568 if(FAILED(hres))
569 return hres;
571 if(r)
572 *r = jsval_number(n);
573 break;
575 case DISPATCH_CONSTRUCT: {
576 jsdisp_t *obj;
578 if(argc) {
579 hres = to_number(ctx, argv[0], &n);
580 if(FAILED(hres))
581 return hres;
582 }else {
583 n = 0;
586 hres = create_number(ctx, n, &obj);
587 if(FAILED(hres))
588 return hres;
590 *r = jsval_obj(obj);
591 break;
593 default:
594 FIXME("unimplemented flags %x\n", flags);
595 return E_NOTIMPL;
598 return S_OK;
601 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
603 NumberInstance *number;
604 HRESULT hres;
606 number = heap_alloc_zero(sizeof(NumberInstance));
607 if(!number)
608 return E_OUTOFMEMORY;
610 if(object_prototype)
611 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
612 else
613 hres = init_dispex_from_constr(&number->dispex, ctx, &NumberInst_info, ctx->number_constr);
614 if(FAILED(hres)) {
615 heap_free(number);
616 return hres;
619 *ret = number;
620 return S_OK;
623 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
625 NumberInstance *number;
626 HRESULT hres;
628 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
630 hres = alloc_number(ctx, object_prototype, &number);
631 if(FAILED(hres))
632 return hres;
634 number->value = 0;
635 hres = create_builtin_constructor(ctx, NumberConstr_value, NumberW, NULL,
636 PROPF_CONSTR|1, &number->dispex, ret);
638 jsdisp_release(&number->dispex);
639 return hres;
642 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
644 NumberInstance *number;
645 HRESULT hres;
647 hres = alloc_number(ctx, NULL, &number);
648 if(FAILED(hres))
649 return hres;
651 number->value = value;
653 *ret = &number->dispex;
654 return S_OK;