rpcrt4/tests: Fix a few more broken tests on NT4.
[wine.git] / dlls / jscript / math.c
blob9d609e5d8c0298fddbbfa75f52d9cfe824b0c6c5
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 <limits.h>
25 #include "jscript.h"
26 #include "ntsecapi.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 static const WCHAR EW[] = {'E',0};
33 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
34 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
35 static const WCHAR LN2W[] = {'L','N','2',0};
36 static const WCHAR LN10W[] = {'L','N','1','0',0};
37 static const WCHAR PIW[] = {'P','I',0};
38 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
39 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
40 static const WCHAR absW[] = {'a','b','s',0};
41 static const WCHAR acosW[] = {'a','c','o','s',0};
42 static const WCHAR asinW[] = {'a','s','i','n',0};
43 static const WCHAR atanW[] = {'a','t','a','n',0};
44 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
45 static const WCHAR ceilW[] = {'c','e','i','l',0};
46 static const WCHAR cosW[] = {'c','o','s',0};
47 static const WCHAR expW[] = {'e','x','p',0};
48 static const WCHAR floorW[] = {'f','l','o','o','r',0};
49 static const WCHAR logW[] = {'l','o','g',0};
50 static const WCHAR maxW[] = {'m','a','x',0};
51 static const WCHAR minW[] = {'m','i','n',0};
52 static const WCHAR powW[] = {'p','o','w',0};
53 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
54 static const WCHAR roundW[] = {'r','o','u','n','d',0};
55 static const WCHAR sinW[] = {'s','i','n',0};
56 static const WCHAR sqrtW[] = {'s','q','r','t',0};
57 static const WCHAR tanW[] = {'t','a','n',0};
59 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
61 switch(flags) {
62 case DISPATCH_PROPERTYGET:
63 V_VT(retv) = VT_R8;
64 V_R8(retv) = val;
65 return S_OK;
66 case DISPATCH_PROPERTYPUT:
67 return S_OK;
70 FIXME("unhandled flags %x\n", flags);
71 return E_NOTIMPL;
74 /* ECMA-262 3rd Edition 15.8.1.1 */
75 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
78 TRACE("\n");
79 return math_constant(M_E, flags, retv);
82 /* ECMA-262 3rd Edition 15.8.1.4 */
83 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
84 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
86 TRACE("\n");
87 return math_constant(M_LOG2E, flags, retv);
90 /* ECMA-262 3rd Edition 15.8.1.4 */
91 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
92 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
94 TRACE("\n");
95 return math_constant(M_LOG10E, flags, retv);
98 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 TRACE("\n");
102 return math_constant(M_LN2, flags, retv);
105 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 TRACE("\n");
109 return math_constant(M_LN10, flags, retv);
112 /* ECMA-262 3rd Edition 15.8.1.6 */
113 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
116 TRACE("\n");
117 return math_constant(M_PI, flags, retv);
120 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 TRACE("\n");
124 return math_constant(M_SQRT2, flags, retv);
127 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
130 TRACE("\n");
131 return math_constant(M_SQRT1_2, flags, retv);
134 /* ECMA-262 3rd Edition 15.8.2.12 */
135 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
136 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
138 VARIANT v;
139 DOUBLE d;
140 HRESULT hres;
142 TRACE("\n");
144 if(!arg_cnt(dp)) {
145 if(retv)
146 num_set_nan(retv);
147 return S_OK;
150 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
151 if(FAILED(hres))
152 return hres;
154 d = num_val(&v);
155 if(retv)
156 num_set_val(retv, d < 0.0 ? -d : d);
157 return S_OK;
160 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 VARIANT v;
164 HRESULT hres;
166 TRACE("\n");
168 if(!arg_cnt(dp)) {
169 if(retv) num_set_nan(retv);
170 return S_OK;
173 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
174 if(FAILED(hres))
175 return hres;
177 if(retv) num_set_val(retv, acos(num_val(&v)));
178 return S_OK;
181 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 VARIANT v;
185 HRESULT hres;
187 TRACE("\n");
189 if(!arg_cnt(dp)) {
190 if(retv) num_set_nan(retv);
191 return S_OK;
194 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
195 if(FAILED(hres))
196 return hres;
198 if(retv) num_set_val(retv, asin(num_val(&v)));
199 return S_OK;
202 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
203 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 VARIANT v;
206 HRESULT hres;
208 TRACE("\n");
210 if(!arg_cnt(dp)) {
211 if(retv) num_set_nan(retv);
212 return S_OK;
215 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
216 if(FAILED(hres))
217 return hres;
219 if(retv) num_set_val(retv, atan(num_val(&v)));
220 return S_OK;
223 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
224 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 VARIANT v1, v2;
227 HRESULT hres;
229 TRACE("\n");
231 if(arg_cnt(dp)<2) {
232 if(retv) num_set_nan(retv);
233 return S_OK;
236 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v1);
237 if(FAILED(hres))
238 return hres;
240 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v2);
241 if(FAILED(hres))
242 return hres;
244 if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
245 return S_OK;
248 /* ECMA-262 3rd Edition 15.8.2.6 */
249 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 VARIANT v;
253 HRESULT hres;
255 TRACE("\n");
257 if(!arg_cnt(dp)) {
258 if(retv)
259 num_set_nan(retv);
260 return S_OK;
263 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
264 if(FAILED(hres))
265 return hres;
267 if(retv)
268 num_set_val(retv, ceil(num_val(&v)));
269 return S_OK;
272 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 VARIANT v;
276 HRESULT hres;
278 TRACE("\n");
280 if(!arg_cnt(dp)) {
281 if(retv) num_set_nan(retv);
282 return S_OK;
285 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
286 if(FAILED(hres))
287 return hres;
289 if(retv) num_set_val(retv, cos(num_val(&v)));
290 return S_OK;
293 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
294 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
296 VARIANT v;
297 HRESULT hres;
299 TRACE("\n");
301 if(!arg_cnt(dp)) {
302 if(retv) num_set_nan(retv);
303 return S_OK;
306 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
307 if(FAILED(hres))
308 return hres;
310 if(retv) num_set_val(retv, exp(num_val(&v)));
311 return S_OK;
314 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
315 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
317 VARIANT v;
318 HRESULT hres;
320 TRACE("\n");
322 if(!arg_cnt(dp)) {
323 if(retv)
324 num_set_nan(retv);
325 return S_OK;
328 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
329 if(FAILED(hres))
330 return hres;
332 if(retv)
333 num_set_val(retv, floor(num_val(&v)));
334 return S_OK;
337 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 VARIANT v;
341 HRESULT hres;
343 TRACE("\n");
345 if(!arg_cnt(dp)) {
346 if(retv)
347 num_set_nan(retv);
348 return S_OK;
351 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
352 if(FAILED(hres))
353 return hres;
355 if(retv)
356 num_set_val(retv, log(num_val(&v)));
357 return S_OK;
360 /* ECMA-262 3rd Edition 15.8.2.11 */
361 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
362 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 DOUBLE max, d;
365 VARIANT v;
366 DWORD i;
367 HRESULT hres;
369 TRACE("\n");
371 if(!arg_cnt(dp)) {
372 if(retv)
373 num_set_inf(retv, FALSE);
374 return S_OK;
377 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
378 if(FAILED(hres))
379 return hres;
381 max = num_val(&v);
382 for(i=1; i < arg_cnt(dp); i++) {
383 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
384 if(FAILED(hres))
385 return hres;
387 d = num_val(&v);
388 if(d > max || isnan(d))
389 max = d;
392 if(retv)
393 num_set_val(retv, max);
394 return S_OK;
397 /* ECMA-262 3rd Edition 15.8.2.12 */
398 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
399 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
401 DOUBLE min, d;
402 VARIANT v;
403 DWORD i;
404 HRESULT hres;
406 TRACE("\n");
408 if(!arg_cnt(dp)) {
409 if(retv)
410 num_set_inf(retv, TRUE);
411 return S_OK;
414 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
415 if(FAILED(hres))
416 return hres;
418 min = num_val(&v);
419 for(i=1; i < arg_cnt(dp); i++) {
420 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
421 if(FAILED(hres))
422 return hres;
424 d = num_val(&v);
425 if(d < min || isnan(d))
426 min = d;
429 if(retv)
430 num_set_val(retv, min);
431 return S_OK;
434 /* ECMA-262 3rd Edition 15.8.2.13 */
435 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
436 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
438 VARIANT x, y;
439 HRESULT hres;
441 TRACE("\n");
443 if(arg_cnt(dp) < 2) {
444 if(retv) num_set_nan(retv);
445 return S_OK;
448 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
449 if(FAILED(hres))
450 return hres;
452 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
453 if(FAILED(hres))
454 return hres;
456 if(retv)
457 num_set_val(retv, pow(num_val(&x), num_val(&y)));
458 return S_OK;
461 /* ECMA-262 3rd Edition 15.8.2.14 */
462 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
463 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
465 UINT r;
467 TRACE("\n");
469 if(!RtlGenRandom(&r, sizeof(r)))
470 return E_UNEXPECTED;
472 if(retv)
473 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
475 return S_OK;
478 /* ECMA-262 3rd Edition 15.8.2.15 */
479 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
480 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
482 VARIANT v;
483 HRESULT hres;
485 TRACE("\n");
487 if(!arg_cnt(dp)) {
488 num_set_nan(retv);
489 return S_OK;
492 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
493 if(FAILED(hres))
494 return hres;
496 if(retv)
497 num_set_val(retv, floor(num_val(&v)+0.5));
498 return S_OK;
501 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
502 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
504 VARIANT v;
505 HRESULT hres;
507 TRACE("\n");
509 if(!arg_cnt(dp)) {
510 if(retv) num_set_nan(retv);
511 return S_OK;
514 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
515 if(FAILED(hres))
516 return hres;
518 if(retv) num_set_val(retv, sin(num_val(&v)));
519 return S_OK;
522 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
523 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
525 VARIANT v;
526 HRESULT hres;
528 TRACE("\n");
530 if(!arg_cnt(dp)) {
531 if(retv) num_set_nan(retv);
532 return S_OK;
535 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
536 if(FAILED(hres))
537 return hres;
539 if(retv) num_set_val(retv, sqrt(num_val(&v)));
540 return S_OK;
543 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
544 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
546 VARIANT v;
547 HRESULT hres;
549 TRACE("\n");
551 if(!arg_cnt(dp)) {
552 if(retv) num_set_nan(retv);
553 return S_OK;
556 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
557 if(FAILED(hres))
558 return hres;
560 if(retv) num_set_val(retv, tan(num_val(&v)));
561 return S_OK;
564 static const builtin_prop_t Math_props[] = {
565 {EW, Math_E, 0},
566 {LN10W, Math_LN10, 0},
567 {LN2W, Math_LN2, 0},
568 {LOG10EW, Math_LOG10E, 0},
569 {LOG2EW, Math_LOG2E, 0},
570 {PIW, Math_PI, 0},
571 {SQRT1_2W, Math_SQRT1_2, 0},
572 {SQRT2W, Math_SQRT2, 0},
573 {absW, Math_abs, PROPF_METHOD},
574 {acosW, Math_acos, PROPF_METHOD},
575 {asinW, Math_asin, PROPF_METHOD},
576 {atanW, Math_atan, PROPF_METHOD},
577 {atan2W, Math_atan2, PROPF_METHOD},
578 {ceilW, Math_ceil, PROPF_METHOD},
579 {cosW, Math_cos, PROPF_METHOD},
580 {expW, Math_exp, PROPF_METHOD},
581 {floorW, Math_floor, PROPF_METHOD},
582 {logW, Math_log, PROPF_METHOD},
583 {maxW, Math_max, PROPF_METHOD},
584 {minW, Math_min, PROPF_METHOD},
585 {powW, Math_pow, PROPF_METHOD},
586 {randomW, Math_random, PROPF_METHOD},
587 {roundW, Math_round, PROPF_METHOD},
588 {sinW, Math_sin, PROPF_METHOD},
589 {sqrtW, Math_sqrt, PROPF_METHOD},
590 {tanW, Math_tan, PROPF_METHOD}
593 static const builtin_info_t Math_info = {
594 JSCLASS_MATH,
595 {NULL, NULL, 0},
596 sizeof(Math_props)/sizeof(*Math_props),
597 Math_props,
598 NULL,
599 NULL
602 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
604 return create_dispex(ctx, &Math_info, NULL, ret);