user32: Fix checking of show count in ShowCursor.
[wine/multimedia.git] / dlls / jscript / math.c
blob8de9df91bd610bacca009243c35e6ed231d0985b
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <math.h>
24 #include <limits.h>
26 #include "jscript.h"
27 #include "ntsecapi.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
33 static const WCHAR EW[] = {'E',0};
34 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
35 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
36 static const WCHAR LN2W[] = {'L','N','2',0};
37 static const WCHAR LN10W[] = {'L','N','1','0',0};
38 static const WCHAR PIW[] = {'P','I',0};
39 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
40 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
41 static const WCHAR absW[] = {'a','b','s',0};
42 static const WCHAR acosW[] = {'a','c','o','s',0};
43 static const WCHAR asinW[] = {'a','s','i','n',0};
44 static const WCHAR atanW[] = {'a','t','a','n',0};
45 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
46 static const WCHAR ceilW[] = {'c','e','i','l',0};
47 static const WCHAR cosW[] = {'c','o','s',0};
48 static const WCHAR expW[] = {'e','x','p',0};
49 static const WCHAR floorW[] = {'f','l','o','o','r',0};
50 static const WCHAR logW[] = {'l','o','g',0};
51 static const WCHAR maxW[] = {'m','a','x',0};
52 static const WCHAR minW[] = {'m','i','n',0};
53 static const WCHAR powW[] = {'p','o','w',0};
54 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
55 static const WCHAR roundW[] = {'r','o','u','n','d',0};
56 static const WCHAR sinW[] = {'s','i','n',0};
57 static const WCHAR sqrtW[] = {'s','q','r','t',0};
58 static const WCHAR tanW[] = {'t','a','n',0};
60 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
62 switch(flags) {
63 case DISPATCH_PROPERTYGET:
64 V_VT(retv) = VT_R8;
65 V_R8(retv) = val;
66 return S_OK;
67 case DISPATCH_PROPERTYPUT:
68 return S_OK;
71 FIXME("unhandled flags %x\n", flags);
72 return E_NOTIMPL;
75 /* ECMA-262 3rd Edition 15.8.1.1 */
76 static HRESULT Math_E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
77 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
79 TRACE("\n");
80 return math_constant(M_E, flags, retv);
83 /* ECMA-262 3rd Edition 15.8.1.4 */
84 static HRESULT Math_LOG2E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
85 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 TRACE("\n");
88 return math_constant(M_LOG2E, flags, retv);
91 /* ECMA-262 3rd Edition 15.8.1.4 */
92 static HRESULT Math_LOG10E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
93 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
95 TRACE("\n");
96 return math_constant(M_LOG10E, flags, retv);
99 static HRESULT Math_LN2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
100 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
102 TRACE("\n");
103 return math_constant(M_LN2, flags, retv);
106 static HRESULT Math_LN10(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
107 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 TRACE("\n");
110 return math_constant(M_LN10, flags, retv);
113 /* ECMA-262 3rd Edition 15.8.1.6 */
114 static HRESULT Math_PI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
115 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 TRACE("\n");
118 return math_constant(M_PI, flags, retv);
121 static HRESULT Math_SQRT2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
122 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
124 TRACE("\n");
125 return math_constant(M_SQRT2, flags, retv);
128 static HRESULT Math_SQRT1_2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
129 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
131 TRACE("\n");
132 return math_constant(M_SQRT1_2, flags, retv);
135 /* ECMA-262 3rd Edition 15.8.2.12 */
136 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
137 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
139 VARIANT v;
140 DOUBLE d;
141 HRESULT hres;
143 TRACE("\n");
145 if(!arg_cnt(dp)) {
146 if(retv)
147 num_set_nan(retv);
148 return S_OK;
151 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
152 if(FAILED(hres))
153 return hres;
155 d = num_val(&v);
156 if(retv)
157 num_set_val(retv, d < 0.0 ? -d : d);
158 return S_OK;
161 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
162 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
164 VARIANT v;
165 HRESULT hres;
167 TRACE("\n");
169 if(!arg_cnt(dp)) {
170 if(retv) num_set_nan(retv);
171 return S_OK;
174 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
175 if(FAILED(hres))
176 return hres;
178 if(retv) num_set_val(retv, acos(num_val(&v)));
179 return S_OK;
182 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
183 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 VARIANT v;
186 HRESULT hres;
188 TRACE("\n");
190 if(!arg_cnt(dp)) {
191 if(retv) num_set_nan(retv);
192 return S_OK;
195 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
196 if(FAILED(hres))
197 return hres;
199 if(retv) num_set_val(retv, asin(num_val(&v)));
200 return S_OK;
203 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
206 VARIANT v;
207 HRESULT hres;
209 TRACE("\n");
211 if(!arg_cnt(dp)) {
212 if(retv) num_set_nan(retv);
213 return S_OK;
216 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
217 if(FAILED(hres))
218 return hres;
220 if(retv) num_set_val(retv, atan(num_val(&v)));
221 return S_OK;
224 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
227 VARIANT v1, v2;
228 HRESULT hres;
230 TRACE("\n");
232 if(arg_cnt(dp)<2) {
233 if(retv) num_set_nan(retv);
234 return S_OK;
237 hres = to_number(ctx, get_arg(dp, 0), ei, &v1);
238 if(FAILED(hres))
239 return hres;
241 hres = to_number(ctx, get_arg(dp, 1), ei, &v2);
242 if(FAILED(hres))
243 return hres;
245 if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
246 return S_OK;
249 /* ECMA-262 3rd Edition 15.8.2.6 */
250 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
251 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
253 VARIANT v;
254 HRESULT hres;
256 TRACE("\n");
258 if(!arg_cnt(dp)) {
259 if(retv)
260 num_set_nan(retv);
261 return S_OK;
264 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
265 if(FAILED(hres))
266 return hres;
268 if(retv)
269 num_set_val(retv, ceil(num_val(&v)));
270 return S_OK;
273 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
274 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
276 VARIANT v;
277 HRESULT hres;
279 TRACE("\n");
281 if(!arg_cnt(dp)) {
282 if(retv) num_set_nan(retv);
283 return S_OK;
286 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
287 if(FAILED(hres))
288 return hres;
290 if(retv) num_set_val(retv, cos(num_val(&v)));
291 return S_OK;
294 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
295 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 VARIANT v;
298 HRESULT hres;
300 TRACE("\n");
302 if(!arg_cnt(dp)) {
303 if(retv) num_set_nan(retv);
304 return S_OK;
307 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
308 if(FAILED(hres))
309 return hres;
311 if(retv) num_set_val(retv, exp(num_val(&v)));
312 return S_OK;
315 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
316 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
318 VARIANT v;
319 HRESULT hres;
321 TRACE("\n");
323 if(!arg_cnt(dp)) {
324 if(retv)
325 num_set_nan(retv);
326 return S_OK;
329 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
330 if(FAILED(hres))
331 return hres;
333 if(retv)
334 num_set_val(retv, floor(num_val(&v)));
335 return S_OK;
338 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
339 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
341 VARIANT v;
342 HRESULT hres;
344 TRACE("\n");
346 if(!arg_cnt(dp)) {
347 if(retv)
348 num_set_nan(retv);
349 return S_OK;
352 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
353 if(FAILED(hres))
354 return hres;
356 if(retv)
357 num_set_val(retv, log(num_val(&v)));
358 return S_OK;
361 /* ECMA-262 3rd Edition 15.8.2.11 */
362 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
363 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
365 DOUBLE max, d;
366 VARIANT v;
367 DWORD i;
368 HRESULT hres;
370 TRACE("\n");
372 if(!arg_cnt(dp)) {
373 if(retv)
374 num_set_inf(retv, FALSE);
375 return S_OK;
378 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
379 if(FAILED(hres))
380 return hres;
382 max = num_val(&v);
383 for(i=1; i < arg_cnt(dp); i++) {
384 hres = to_number(ctx, get_arg(dp, i), ei, &v);
385 if(FAILED(hres))
386 return hres;
388 d = num_val(&v);
389 if(d > max || isnan(d))
390 max = d;
393 if(retv)
394 num_set_val(retv, max);
395 return S_OK;
398 /* ECMA-262 3rd Edition 15.8.2.12 */
399 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
400 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
402 DOUBLE min, d;
403 VARIANT v;
404 DWORD i;
405 HRESULT hres;
407 TRACE("\n");
409 if(!arg_cnt(dp)) {
410 if(retv)
411 num_set_inf(retv, TRUE);
412 return S_OK;
415 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
416 if(FAILED(hres))
417 return hres;
419 min = num_val(&v);
420 for(i=1; i < arg_cnt(dp); i++) {
421 hres = to_number(ctx, get_arg(dp, i), ei, &v);
422 if(FAILED(hres))
423 return hres;
425 d = num_val(&v);
426 if(d < min || isnan(d))
427 min = d;
430 if(retv)
431 num_set_val(retv, min);
432 return S_OK;
435 /* ECMA-262 3rd Edition 15.8.2.13 */
436 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
437 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
439 VARIANT x, y;
440 HRESULT hres;
442 TRACE("\n");
444 if(arg_cnt(dp) < 2) {
445 if(retv) num_set_nan(retv);
446 return S_OK;
449 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
450 if(FAILED(hres))
451 return hres;
453 hres = to_number(ctx, get_arg(dp, 1), ei, &y);
454 if(FAILED(hres))
455 return hres;
457 if(retv)
458 num_set_val(retv, pow(num_val(&x), num_val(&y)));
459 return S_OK;
462 /* ECMA-262 3rd Edition 15.8.2.14 */
463 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
464 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
466 UINT r;
468 TRACE("\n");
470 if(!RtlGenRandom(&r, sizeof(r)))
471 return E_UNEXPECTED;
473 if(retv)
474 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
476 return S_OK;
479 /* ECMA-262 3rd Edition 15.8.2.15 */
480 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
481 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
483 VARIANT v;
484 HRESULT hres;
486 TRACE("\n");
488 if(!arg_cnt(dp)) {
489 num_set_nan(retv);
490 return S_OK;
493 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
494 if(FAILED(hres))
495 return hres;
497 if(retv)
498 num_set_val(retv, floor(num_val(&v)+0.5));
499 return S_OK;
502 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
503 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
505 VARIANT v;
506 HRESULT hres;
508 TRACE("\n");
510 if(!arg_cnt(dp)) {
511 if(retv) num_set_nan(retv);
512 return S_OK;
515 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
516 if(FAILED(hres))
517 return hres;
519 if(retv) num_set_val(retv, sin(num_val(&v)));
520 return S_OK;
523 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
524 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
526 VARIANT v;
527 HRESULT hres;
529 TRACE("\n");
531 if(!arg_cnt(dp)) {
532 if(retv) num_set_nan(retv);
533 return S_OK;
536 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
537 if(FAILED(hres))
538 return hres;
540 if(retv) num_set_val(retv, sqrt(num_val(&v)));
541 return S_OK;
544 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
545 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
547 VARIANT v;
548 HRESULT hres;
550 TRACE("\n");
552 if(!arg_cnt(dp)) {
553 if(retv) num_set_nan(retv);
554 return S_OK;
557 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
558 if(FAILED(hres))
559 return hres;
561 if(retv) num_set_val(retv, tan(num_val(&v)));
562 return S_OK;
565 static const builtin_prop_t Math_props[] = {
566 {EW, Math_E, 0},
567 {LN10W, Math_LN10, 0},
568 {LN2W, Math_LN2, 0},
569 {LOG10EW, Math_LOG10E, 0},
570 {LOG2EW, Math_LOG2E, 0},
571 {PIW, Math_PI, 0},
572 {SQRT1_2W, Math_SQRT1_2, 0},
573 {SQRT2W, Math_SQRT2, 0},
574 {absW, Math_abs, PROPF_METHOD|1},
575 {acosW, Math_acos, PROPF_METHOD|1},
576 {asinW, Math_asin, PROPF_METHOD|1},
577 {atanW, Math_atan, PROPF_METHOD|1},
578 {atan2W, Math_atan2, PROPF_METHOD|2},
579 {ceilW, Math_ceil, PROPF_METHOD|1},
580 {cosW, Math_cos, PROPF_METHOD|1},
581 {expW, Math_exp, PROPF_METHOD|1},
582 {floorW, Math_floor, PROPF_METHOD|1},
583 {logW, Math_log, PROPF_METHOD|1},
584 {maxW, Math_max, PROPF_METHOD|2},
585 {minW, Math_min, PROPF_METHOD|2},
586 {powW, Math_pow, PROPF_METHOD|2},
587 {randomW, Math_random, PROPF_METHOD},
588 {roundW, Math_round, PROPF_METHOD|1},
589 {sinW, Math_sin, PROPF_METHOD|1},
590 {sqrtW, Math_sqrt, PROPF_METHOD|1},
591 {tanW, Math_tan, PROPF_METHOD|1}
594 static const builtin_info_t Math_info = {
595 JSCLASS_MATH,
596 {NULL, NULL, 0},
597 sizeof(Math_props)/sizeof(*Math_props),
598 Math_props,
599 NULL,
600 NULL
603 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
605 DispatchEx *math;
606 HRESULT hres;
608 math = heap_alloc_zero(sizeof(DispatchEx));
609 if(!math)
610 return E_OUTOFMEMORY;
612 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
613 if(FAILED(hres)) {
614 heap_free(math);
615 return hres;
618 *ret = math;
619 return S_OK;