widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / dlls / jscript / number.c
blob3c965323d025102c6ffd70df587ee337679c1628
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 <math.h>
20 #include <assert.h>
22 #include "jscript.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 typedef struct {
29 jsdisp_t dispex;
31 double value;
32 } NumberInstance;
34 #define NUMBER_TOSTRING_BUF_SIZE 64
35 #define NUMBER_DTOA_SIZE 18
37 static inline NumberInstance *number_from_jsdisp(jsdisp_t *jsdisp)
39 return CONTAINING_RECORD(jsdisp, NumberInstance, dispex);
42 static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
44 return number_from_jsdisp(vdisp->u.jsdisp);
47 static inline NumberInstance *number_this(vdisp_t *jsthis)
49 return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
52 static inline void number_to_str(double d, WCHAR *buf, int size, int *dec_point)
54 ULONGLONG l;
55 int i;
57 /* TODO: this function should print doubles with bigger precision */
58 assert(size>=2 && size<=NUMBER_DTOA_SIZE && d>=0);
60 if(d == 0)
61 *dec_point = 0;
62 else
63 *dec_point = floor(log10(d));
64 l = d*pow(10, size-*dec_point-1);
66 if(l%10 >= 5)
67 l = l/10+1;
68 else
69 l /= 10;
71 buf[size-1] = 0;
72 for(i=size-2; i>=0; i--) {
73 buf[i] = '0'+l%10;
74 l /= 10;
77 /* log10 was wrong by 1 or rounding changed number of digits */
78 if(l) {
79 (*dec_point)++;
80 memmove(buf+1, buf, size-2);
81 buf[0] = '0'+l;
82 }else if(buf[0]=='0' && buf[1]>='1' && buf[1]<='9') {
83 (*dec_point)--;
84 memmove(buf, buf+1, size-2);
85 buf[size-2] = '0';
89 static inline jsstr_t *number_to_fixed(double val, int prec)
91 WCHAR buf[NUMBER_DTOA_SIZE];
92 int dec_point, size, buf_size, buf_pos;
93 BOOL neg = FALSE;
94 jsstr_t *ret;
95 WCHAR *str;
97 TRACE("%lf %d\n", val, prec);
99 if(val < 0) {
100 neg = TRUE;
101 val = -val;
104 if(val >= 1)
105 buf_size = log10(val)+prec+2;
106 else
107 buf_size = prec ? prec+1 : 2;
108 if(buf_size > NUMBER_DTOA_SIZE)
109 buf_size = NUMBER_DTOA_SIZE;
111 number_to_str(val, buf, buf_size, &dec_point);
112 dec_point++;
113 size = 0;
114 if(neg)
115 size++;
116 if(dec_point > 0)
117 size += dec_point;
118 else
119 size++;
120 if(prec)
121 size += prec+1;
123 ret = jsstr_alloc_buf(size, &str);
124 if(!ret)
125 return NULL;
127 size = buf_pos = 0;
128 if(neg)
129 str[size++] = '-';
130 if(dec_point > 0) {
131 for(;buf_pos<buf_size-1 && dec_point; dec_point--)
132 str[size++] = buf[buf_pos++];
133 }else {
134 str[size++] = '0';
136 for(; dec_point>0; dec_point--)
137 str[size++] = '0';
138 if(prec) {
139 str[size++] = '.';
141 for(; dec_point<0 && prec; dec_point++, prec--)
142 str[size++] = '0';
143 for(; buf_pos<buf_size-1 && prec; prec--)
144 str[size++] = buf[buf_pos++];
145 for(; prec; prec--) {
146 str[size++] = '0';
149 str[size++] = 0;
150 return ret;
153 static inline jsstr_t *number_to_exponential(double val, int prec)
155 WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
156 int dec_point, size, buf_size, exp_size = 1;
157 BOOL neg = FALSE;
158 jsstr_t *ret;
159 WCHAR *str;
161 if(val < 0) {
162 neg = TRUE;
163 val = -val;
166 buf_size = prec+2;
167 if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
168 buf_size = NUMBER_DTOA_SIZE;
169 number_to_str(val, buf, buf_size, &dec_point);
170 buf_size--;
171 if(prec == -1)
172 for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
173 buf[buf_size-1] = 0;
175 size = 10;
176 while(dec_point>=size || dec_point<=-size) {
177 size *= 10;
178 exp_size++;
181 if(buf_size == 1)
182 size = buf_size+2+exp_size; /* 2 = strlen(e+) */
183 else if(prec == -1)
184 size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
185 else
186 size = prec+4+exp_size; /* 4 = strlen(0.e+) */
187 if(neg)
188 size++;
190 ret = jsstr_alloc_buf(size, &str);
191 if(!ret)
192 return NULL;
194 size = 0;
195 pbuf = buf;
196 if(neg)
197 str[size++] = '-';
198 str[size++] = *pbuf++;
199 if(buf_size != 1) {
200 str[size++] = '.';
201 while(*pbuf)
202 str[size++] = *pbuf++;
203 for(; prec>buf_size-1; prec--)
204 str[size++] = '0';
206 str[size++] = 'e';
207 if(dec_point >= 0) {
208 str[size++] = '+';
209 }else {
210 str[size++] = '-';
211 dec_point = -dec_point;
213 size += exp_size;
214 do {
215 str[--size] = '0'+dec_point%10;
216 dec_point /= 10;
217 }while(dec_point>0);
218 size += exp_size;
219 str[size] = 0;
221 return ret;
224 /* ECMA-262 3rd Edition 15.7.4.2 */
225 static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
226 jsval_t *r)
228 NumberInstance *number;
229 INT radix = 10;
230 DOUBLE val;
231 jsstr_t *str;
232 HRESULT hres;
234 TRACE("\n");
236 if(!(number = number_this(jsthis)))
237 return JS_E_NUMBER_EXPECTED;
239 if(argc) {
240 hres = to_int32(ctx, argv[0], &radix);
241 if(FAILED(hres))
242 return hres;
244 if(radix < 2 || radix > 36)
245 return JS_E_INVALIDARG;
248 val = number->value;
250 if(radix==10 || !isfinite(val)) {
251 hres = to_string(ctx, jsval_number(val), &str);
252 if(FAILED(hres))
253 return hres;
254 }else {
255 INT idx = 0;
256 DOUBLE integ, frac, log_radix = 0;
257 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
258 BOOL exp = FALSE;
260 if(val<0) {
261 val = -val;
262 buf[idx++] = '-';
265 while(1) {
266 integ = floor(val);
267 frac = val-integ;
269 if(integ == 0)
270 buf[idx++] = '0';
271 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
272 buf[idx] = fmod(integ, radix);
273 if(buf[idx]<10) buf[idx] += '0';
274 else buf[idx] += 'a'-10;
275 integ /= radix;
276 idx++;
279 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
280 INT beg = buf[0]=='-'?1:0;
281 INT end = idx-1;
282 WCHAR wch;
284 while(end > beg) {
285 wch = buf[beg];
286 buf[beg++] = buf[end];
287 buf[end--] = wch;
291 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
293 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
294 frac *= radix;
295 buf[idx] = fmod(frac, radix);
296 frac -= buf[idx];
297 if(buf[idx]<10) buf[idx] += '0';
298 else buf[idx] += 'a'-10;
299 idx++;
302 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
303 exp = TRUE;
304 idx = (buf[0]=='-') ? 1 : 0;
305 log_radix = floor(log(val)/log(radix));
306 val *= pow(radix, -log_radix);
307 continue;
310 break;
313 while(buf[idx-1] == '0') idx--;
314 if(buf[idx-1] == '.') idx--;
316 if(exp) {
317 if(log_radix==0)
318 buf[idx] = 0;
319 else {
320 WCHAR ch;
322 if(log_radix<0) {
323 log_radix = -log_radix;
324 ch = '-';
326 else ch = '+';
327 swprintf(&buf[idx], ARRAY_SIZE(buf) - idx, L"(e%c%d)", ch, (int)log_radix);
330 else buf[idx] = '\0';
332 str = jsstr_alloc(buf);
333 if(!str)
334 return E_OUTOFMEMORY;
337 if(r)
338 *r = jsval_string(str);
339 else
340 jsstr_release(str);
341 return S_OK;
344 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
345 jsval_t *r)
347 FIXME("\n");
348 return E_NOTIMPL;
351 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
352 jsval_t *r)
354 NumberInstance *number;
355 DOUBLE val;
356 INT prec = 0;
357 jsstr_t *str;
358 HRESULT hres;
360 TRACE("\n");
362 if(!(number = number_this(jsthis)))
363 return JS_E_NUMBER_EXPECTED;
365 if(argc) {
366 hres = to_int32(ctx, argv[0], &prec);
367 if(FAILED(hres))
368 return hres;
370 if(prec < 0 || prec > 20)
371 return JS_E_FRACTION_DIGITS_OUT_OF_RANGE;
374 val = number->value;
375 if(!isfinite(val)) {
376 hres = to_string(ctx, jsval_number(val), &str);
377 if(FAILED(hres))
378 return hres;
379 }else {
380 str = number_to_fixed(val, prec);
381 if(!str)
382 return E_OUTOFMEMORY;
385 if(r)
386 *r = jsval_string(str);
387 else
388 jsstr_release(str);
389 return S_OK;
392 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
393 jsval_t *r)
395 NumberInstance *number;
396 DOUBLE val;
397 INT prec = 0;
398 jsstr_t *str;
399 HRESULT hres;
401 TRACE("\n");
403 if(!(number = number_this(jsthis)))
404 return JS_E_NUMBER_EXPECTED;
406 if(argc) {
407 hres = to_int32(ctx, argv[0], &prec);
408 if(FAILED(hres))
409 return hres;
411 if(prec < 0 || prec > 20)
412 return JS_E_FRACTION_DIGITS_OUT_OF_RANGE;
415 val = number->value;
416 if(!isfinite(val)) {
417 hres = to_string(ctx, jsval_number(val), &str);
418 if(FAILED(hres))
419 return hres;
420 }else {
421 if(!prec)
422 prec--;
423 str = number_to_exponential(val, prec);
424 if(!str)
425 return E_OUTOFMEMORY;
428 if(r)
429 *r = jsval_string(str);
430 else
431 jsstr_release(str);
432 return S_OK;
435 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
436 jsval_t *r)
438 NumberInstance *number;
439 INT prec = 0, size;
440 jsstr_t *str;
441 DOUBLE val;
442 HRESULT hres;
444 if(!(number = number_this(jsthis)))
445 return JS_E_NUMBER_EXPECTED;
447 if(argc) {
448 hres = to_int32(ctx, argv[0], &prec);
449 if(FAILED(hres))
450 return hres;
452 if(prec<1 || prec>21)
453 return JS_E_PRECISION_OUT_OF_RANGE;
456 val = number->value;
457 if(!isfinite(val) || !prec) {
458 hres = to_string(ctx, jsval_number(val), &str);
459 if(FAILED(hres))
460 return hres;
461 }else {
462 if(val != 0)
463 size = floor(log10(val>0 ? val : -val)) + 1;
464 else
465 size = 1;
467 if(size > prec)
468 str = number_to_exponential(val, prec-1);
469 else
470 str = number_to_fixed(val, prec-size);
471 if(!str)
472 return E_OUTOFMEMORY;
475 if(r)
476 *r = jsval_string(str);
477 else
478 jsstr_release(str);
479 return S_OK;
482 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
483 jsval_t *r)
485 NumberInstance *number;
487 TRACE("\n");
489 if(!(number = number_this(jsthis)))
490 return JS_E_NUMBER_EXPECTED;
492 if(r)
493 *r = jsval_number(number->value);
494 return S_OK;
497 static HRESULT Number_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
499 NumberInstance *number = number_from_jsdisp(jsthis);
501 TRACE("(%p)\n", number);
503 *r = jsval_number(number->value);
504 return S_OK;
507 static const builtin_prop_t Number_props[] = {
508 {L"toExponential", Number_toExponential, PROPF_METHOD|1},
509 {L"toFixed", Number_toFixed, PROPF_METHOD},
510 {L"toLocaleString", Number_toLocaleString, PROPF_METHOD},
511 {L"toPrecision", Number_toPrecision, PROPF_METHOD|1},
512 {L"toString", Number_toString, PROPF_METHOD|1},
513 {L"valueOf", Number_valueOf, PROPF_METHOD}
516 static const builtin_info_t Number_info = {
517 JSCLASS_NUMBER,
518 {NULL, NULL,0, Number_get_value},
519 ARRAY_SIZE(Number_props),
520 Number_props,
521 NULL,
522 NULL
525 static const builtin_info_t NumberInst_info = {
526 JSCLASS_NUMBER,
527 {NULL, NULL,0, Number_get_value},
528 0, NULL,
529 NULL,
530 NULL
533 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
534 jsval_t *r)
536 double n;
537 HRESULT hres;
539 TRACE("\n");
541 switch(flags) {
542 case INVOKE_FUNC:
543 if(!argc) {
544 if(r)
545 *r = jsval_number(0);
546 return S_OK;
549 hres = to_number(ctx, argv[0], &n);
550 if(FAILED(hres))
551 return hres;
553 if(r)
554 *r = jsval_number(n);
555 break;
557 case DISPATCH_CONSTRUCT: {
558 jsdisp_t *obj;
560 if(argc) {
561 hres = to_number(ctx, argv[0], &n);
562 if(FAILED(hres))
563 return hres;
564 }else {
565 n = 0;
568 hres = create_number(ctx, n, &obj);
569 if(FAILED(hres))
570 return hres;
572 *r = jsval_obj(obj);
573 break;
575 default:
576 FIXME("unimplemented flags %x\n", flags);
577 return E_NOTIMPL;
580 return S_OK;
583 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
585 NumberInstance *number;
586 HRESULT hres;
588 number = heap_alloc_zero(sizeof(NumberInstance));
589 if(!number)
590 return E_OUTOFMEMORY;
592 if(object_prototype)
593 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
594 else
595 hres = init_dispex_from_constr(&number->dispex, ctx, &NumberInst_info, ctx->number_constr);
596 if(FAILED(hres)) {
597 heap_free(number);
598 return hres;
601 *ret = number;
602 return S_OK;
605 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
607 NumberInstance *number;
608 HRESULT hres;
610 hres = alloc_number(ctx, object_prototype, &number);
611 if(FAILED(hres))
612 return hres;
614 number->value = 0;
615 hres = create_builtin_constructor(ctx, NumberConstr_value, L"Number", NULL,
616 PROPF_CONSTR|1, &number->dispex, ret);
618 jsdisp_release(&number->dispex);
619 return hres;
622 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
624 NumberInstance *number;
625 HRESULT hres;
627 hres = alloc_number(ctx, NULL, &number);
628 if(FAILED(hres))
629 return hres;
631 number->value = value;
633 *ret = &number->dispex;
634 return S_OK;