[PSAPI_WINETEST] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / dll / win32 / jscript / math.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "jscript.h"
21
22 #include <ntsecapi.h>
23
24 static const WCHAR EW[] = {'E',0};
25 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
26 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
27 static const WCHAR LN2W[] = {'L','N','2',0};
28 static const WCHAR LN10W[] = {'L','N','1','0',0};
29 static const WCHAR PIW[] = {'P','I',0};
30 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
31 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
32 static const WCHAR absW[] = {'a','b','s',0};
33 static const WCHAR acosW[] = {'a','c','o','s',0};
34 static const WCHAR asinW[] = {'a','s','i','n',0};
35 static const WCHAR atanW[] = {'a','t','a','n',0};
36 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
37 static const WCHAR ceilW[] = {'c','e','i','l',0};
38 static const WCHAR cosW[] = {'c','o','s',0};
39 static const WCHAR expW[] = {'e','x','p',0};
40 static const WCHAR floorW[] = {'f','l','o','o','r',0};
41 static const WCHAR logW[] = {'l','o','g',0};
42 static const WCHAR maxW[] = {'m','a','x',0};
43 static const WCHAR minW[] = {'m','i','n',0};
44 static const WCHAR powW[] = {'p','o','w',0};
45 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
46 static const WCHAR roundW[] = {'r','o','u','n','d',0};
47 static const WCHAR sinW[] = {'s','i','n',0};
48 static const WCHAR sqrtW[] = {'s','q','r','t',0};
49 static const WCHAR tanW[] = {'t','a','n',0};
50
51 /* ECMA-262 3rd Edition 15.8.2.12 */
52 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
53 jsval_t *r)
54 {
55 double d;
56 HRESULT hres;
57
58 TRACE("\n");
59
60 if(!argc) {
61 if(r)
62 *r = jsval_number(NAN);
63 return S_OK;
64 }
65
66 hres = to_number(ctx, argv[0], &d);
67 if(FAILED(hres))
68 return hres;
69
70 if(r)
71 *r = jsval_number(d < 0.0 ? -d : d);
72 return S_OK;
73 }
74
75 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
76 jsval_t *r)
77 {
78 double x;
79 HRESULT hres;
80
81 TRACE("\n");
82
83 if(!argc) {
84 if(r)
85 *r = jsval_number(NAN);
86 return S_OK;
87 }
88
89 hres = to_number(ctx, argv[0], &x);
90 if(FAILED(hres))
91 return hres;
92
93 if(r)
94 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
95 return S_OK;
96 }
97
98 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
99 jsval_t *r)
100 {
101 double x;
102 HRESULT hres;
103
104 TRACE("\n");
105
106 if(!argc) {
107 if(r)
108 *r = jsval_number(NAN);
109 return S_OK;
110 }
111
112 hres = to_number(ctx, argv[0], &x);
113 if(FAILED(hres))
114 return hres;
115
116 if(r)
117 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
118 return S_OK;
119 }
120
121 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
122 jsval_t *r)
123 {
124 double x;
125 HRESULT hres;
126
127 TRACE("\n");
128
129 if(!argc) {
130 if(r)
131 *r = jsval_number(NAN);
132 return S_OK;
133 }
134
135 hres = to_number(ctx, argv[0], &x);
136 if(FAILED(hres))
137 return hres;
138
139 if(r)
140 *r = jsval_number(atan(x));
141 return S_OK;
142 }
143
144 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
145 jsval_t *r)
146 {
147 double x, y;
148 HRESULT hres;
149
150 TRACE("\n");
151
152 if(argc<2) {
153 if(r)
154 *r = jsval_number(NAN);
155 return S_OK;
156 }
157
158 hres = to_number(ctx, argv[0], &y);
159 if(FAILED(hres))
160 return hres;
161
162 hres = to_number(ctx, argv[1], &x);
163 if(FAILED(hres))
164 return hres;
165
166 if(r)
167 *r = jsval_number(atan2(y, x));
168 return S_OK;
169 }
170
171 /* ECMA-262 3rd Edition 15.8.2.6 */
172 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
173 jsval_t *r)
174 {
175 double x;
176 HRESULT hres;
177
178 TRACE("\n");
179
180 if(!argc) {
181 if(r)
182 *r = jsval_number(NAN);
183 return S_OK;
184 }
185
186 hres = to_number(ctx, argv[0], &x);
187 if(FAILED(hres))
188 return hres;
189
190 if(r)
191 *r = jsval_number(ceil(x));
192 return S_OK;
193 }
194
195 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
196 jsval_t *r)
197 {
198 double x;
199 HRESULT hres;
200
201 TRACE("\n");
202
203 if(!argc) {
204 if(r)
205 *r = jsval_number(NAN);
206 return S_OK;
207 }
208
209 hres = to_number(ctx, argv[0], &x);
210 if(FAILED(hres))
211 return hres;
212
213 if(r)
214 *r = jsval_number(cos(x));
215 return S_OK;
216 }
217
218 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
219 jsval_t *r)
220 {
221 double x;
222 HRESULT hres;
223
224 TRACE("\n");
225
226 if(!argc) {
227 if(r)
228 *r = jsval_number(NAN);
229 return S_OK;
230 }
231
232 hres = to_number(ctx, argv[0], &x);
233 if(FAILED(hres))
234 return hres;
235
236 if(r)
237 *r = jsval_number(exp(x));
238 return S_OK;
239 }
240
241 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
242 jsval_t *r)
243 {
244 double x;
245 HRESULT hres;
246
247 TRACE("\n");
248
249 if(!argc) {
250 if(r)
251 *r = jsval_number(NAN);
252 return S_OK;
253 }
254
255 hres = to_number(ctx, argv[0], &x);
256 if(FAILED(hres))
257 return hres;
258
259 if(r)
260 *r = jsval_number(floor(x));
261 return S_OK;
262 }
263
264 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
265 jsval_t *r)
266 {
267 double x;
268 HRESULT hres;
269
270 TRACE("\n");
271
272 if(!argc) {
273 if(r)
274 *r = jsval_number(NAN);
275 return S_OK;
276 }
277
278 hres = to_number(ctx, argv[0], &x);
279 if(FAILED(hres))
280 return hres;
281
282 if(r)
283 *r = jsval_number(x < -0.0 ? NAN : log(x));
284 return S_OK;
285 }
286
287 /* ECMA-262 3rd Edition 15.8.2.11 */
288 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
289 jsval_t *r)
290 {
291 DOUBLE max, d;
292 DWORD i;
293 HRESULT hres;
294
295 TRACE("\n");
296
297 if(!argc) {
298 if(r)
299 *r = jsval_number(-INFINITY);
300 return S_OK;
301 }
302
303 hres = to_number(ctx, argv[0], &max);
304 if(FAILED(hres))
305 return hres;
306
307 for(i=1; i < argc; i++) {
308 hres = to_number(ctx, argv[i], &d);
309 if(FAILED(hres))
310 return hres;
311
312 if(d > max || isnan(d))
313 max = d;
314 }
315
316 if(r)
317 *r = jsval_number(max);
318 return S_OK;
319 }
320
321 /* ECMA-262 3rd Edition 15.8.2.12 */
322 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
323 jsval_t *r)
324 {
325 DOUBLE min, d;
326 DWORD i;
327 HRESULT hres;
328
329 TRACE("\n");
330
331 if(!argc) {
332 if(r)
333 *r = jsval_number(INFINITY);
334 return S_OK;
335 }
336
337 hres = to_number(ctx, argv[0], &min);
338 if(FAILED(hres))
339 return hres;
340
341 for(i=1; i < argc; i++) {
342 hres = to_number(ctx, argv[i], &d);
343 if(FAILED(hres))
344 return hres;
345
346 if(d < min || isnan(d))
347 min = d;
348 }
349
350 if(r)
351 *r = jsval_number(min);
352 return S_OK;
353 }
354
355 /* ECMA-262 3rd Edition 15.8.2.13 */
356 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
357 jsval_t *r)
358 {
359 double x, y;
360 HRESULT hres;
361
362 TRACE("\n");
363
364 if(argc < 2) {
365 if(r)
366 *r = jsval_number(NAN);
367 return S_OK;
368 }
369
370 hres = to_number(ctx, argv[0], &x);
371 if(FAILED(hres))
372 return hres;
373
374 hres = to_number(ctx, argv[1], &y);
375 if(FAILED(hres))
376 return hres;
377
378 if(r)
379 *r = jsval_number(pow(x, y));
380 return S_OK;
381 }
382
383 /* ECMA-262 3rd Edition 15.8.2.14 */
384 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
385 jsval_t *r)
386 {
387 UINT x;
388
389 TRACE("\n");
390
391 if(!RtlGenRandom(&x, sizeof(x)))
392 return E_UNEXPECTED;
393
394 if(r)
395 *r = jsval_number((double)x/(double)UINT_MAX);
396 return S_OK;
397 }
398
399 /* ECMA-262 3rd Edition 15.8.2.15 */
400 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
401 jsval_t *r)
402 {
403 double x;
404 HRESULT hres;
405
406 TRACE("\n");
407
408 if(!argc) {
409 if(r)
410 *r = jsval_number(NAN);
411 return S_OK;
412 }
413
414 hres = to_number(ctx, argv[0], &x);
415 if(FAILED(hres))
416 return hres;
417
418 if(r)
419 *r = jsval_number(floor(x+0.5));
420 return S_OK;
421 }
422
423 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
424 jsval_t *r)
425 {
426 double x;
427 HRESULT hres;
428
429 TRACE("\n");
430
431 if(!argc) {
432 if(r)
433 *r = jsval_number(NAN);
434 return S_OK;
435 }
436
437 hres = to_number(ctx, argv[0], &x);
438 if(FAILED(hres))
439 return hres;
440
441 if(r)
442 *r = jsval_number(sin(x));
443 return S_OK;
444 }
445
446 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
447 jsval_t *r)
448 {
449 double x;
450 HRESULT hres;
451
452 TRACE("\n");
453
454 if(!argc) {
455 if(r)
456 *r = jsval_number(NAN);
457 return S_OK;
458 }
459
460 hres = to_number(ctx, argv[0], &x);
461 if(FAILED(hres))
462 return hres;
463
464 if(r)
465 *r = jsval_number(sqrt(x));
466 return S_OK;
467 }
468
469 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
470 jsval_t *r)
471 {
472 double x;
473 HRESULT hres;
474
475 TRACE("\n");
476
477 if(!argc) {
478 if(r)
479 *r = jsval_number(NAN);
480 return S_OK;
481 }
482
483 hres = to_number(ctx, argv[0], &x);
484 if(FAILED(hres))
485 return hres;
486
487 if(r)
488 *r = jsval_number(tan(x));
489 return S_OK;
490 }
491
492 static const builtin_prop_t Math_props[] = {
493 {absW, Math_abs, PROPF_METHOD|1},
494 {acosW, Math_acos, PROPF_METHOD|1},
495 {asinW, Math_asin, PROPF_METHOD|1},
496 {atanW, Math_atan, PROPF_METHOD|1},
497 {atan2W, Math_atan2, PROPF_METHOD|2},
498 {ceilW, Math_ceil, PROPF_METHOD|1},
499 {cosW, Math_cos, PROPF_METHOD|1},
500 {expW, Math_exp, PROPF_METHOD|1},
501 {floorW, Math_floor, PROPF_METHOD|1},
502 {logW, Math_log, PROPF_METHOD|1},
503 {maxW, Math_max, PROPF_METHOD|2},
504 {minW, Math_min, PROPF_METHOD|2},
505 {powW, Math_pow, PROPF_METHOD|2},
506 {randomW, Math_random, PROPF_METHOD},
507 {roundW, Math_round, PROPF_METHOD|1},
508 {sinW, Math_sin, PROPF_METHOD|1},
509 {sqrtW, Math_sqrt, PROPF_METHOD|1},
510 {tanW, Math_tan, PROPF_METHOD|1}
511 };
512
513 static const builtin_info_t Math_info = {
514 JSCLASS_MATH,
515 {NULL, NULL, 0},
516 sizeof(Math_props)/sizeof(*Math_props),
517 Math_props,
518 NULL,
519 NULL
520 };
521
522 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
523 {
524 jsdisp_t *math;
525 unsigned i;
526 HRESULT hres;
527
528 struct {
529 const WCHAR *name;
530 DOUBLE val;
531 }constants[] = {
532 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
533 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
534 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
535 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
536 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
537 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
538 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
539 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
540 };
541
542 math = heap_alloc_zero(sizeof(jsdisp_t));
543 if(!math)
544 return E_OUTOFMEMORY;
545
546 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
547 if(FAILED(hres)) {
548 heap_free(math);
549 return hres;
550 }
551
552 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
553 hres = jsdisp_propput_const(math, constants[i].name, jsval_number(constants[i].val));
554 if(FAILED(hres)) {
555 jsdisp_release(math);
556 return hres;
557 }
558 }
559
560 *ret = math;
561 return S_OK;
562 }