wined3d: Move the fill mode to wined3d_rasterizer_state.
[wine.git] / dlls / jscript / math.c
blobf0953d4876531eb2e665cc056cd34ab96dc30d58
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
21 #include <math.h>
22 #include <limits.h>
24 #include "jscript.h"
25 #include "ntsecapi.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 static const WCHAR EW[] = {'E',0};
32 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
33 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
34 static const WCHAR LN2W[] = {'L','N','2',0};
35 static const WCHAR LN10W[] = {'L','N','1','0',0};
36 static const WCHAR PIW[] = {'P','I',0};
37 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
38 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
39 static const WCHAR absW[] = {'a','b','s',0};
40 static const WCHAR acosW[] = {'a','c','o','s',0};
41 static const WCHAR asinW[] = {'a','s','i','n',0};
42 static const WCHAR atanW[] = {'a','t','a','n',0};
43 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
44 static const WCHAR ceilW[] = {'c','e','i','l',0};
45 static const WCHAR cosW[] = {'c','o','s',0};
46 static const WCHAR expW[] = {'e','x','p',0};
47 static const WCHAR floorW[] = {'f','l','o','o','r',0};
48 static const WCHAR logW[] = {'l','o','g',0};
49 static const WCHAR maxW[] = {'m','a','x',0};
50 static const WCHAR minW[] = {'m','i','n',0};
51 static const WCHAR powW[] = {'p','o','w',0};
52 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
53 static const WCHAR roundW[] = {'r','o','u','n','d',0};
54 static const WCHAR sinW[] = {'s','i','n',0};
55 static const WCHAR sqrtW[] = {'s','q','r','t',0};
56 static const WCHAR tanW[] = {'t','a','n',0};
58 /* ECMA-262 3rd Edition 15.8.2.12 */
59 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
60 jsval_t *r)
62 double d;
63 HRESULT hres;
65 TRACE("\n");
67 if(!argc) {
68 if(r)
69 *r = jsval_number(NAN);
70 return S_OK;
73 hres = to_number(ctx, argv[0], &d);
74 if(FAILED(hres))
75 return hres;
77 if(r)
78 *r = jsval_number(d < 0.0 ? -d : d);
79 return S_OK;
82 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
83 jsval_t *r)
85 double x;
86 HRESULT hres;
88 TRACE("\n");
90 if(!argc) {
91 if(r)
92 *r = jsval_number(NAN);
93 return S_OK;
96 hres = to_number(ctx, argv[0], &x);
97 if(FAILED(hres))
98 return hres;
100 if(r)
101 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
102 return S_OK;
105 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
106 jsval_t *r)
108 double x;
109 HRESULT hres;
111 TRACE("\n");
113 if(!argc) {
114 if(r)
115 *r = jsval_number(NAN);
116 return S_OK;
119 hres = to_number(ctx, argv[0], &x);
120 if(FAILED(hres))
121 return hres;
123 if(r)
124 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
125 return S_OK;
128 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
129 jsval_t *r)
131 double x;
132 HRESULT hres;
134 TRACE("\n");
136 if(!argc) {
137 if(r)
138 *r = jsval_number(NAN);
139 return S_OK;
142 hres = to_number(ctx, argv[0], &x);
143 if(FAILED(hres))
144 return hres;
146 if(r)
147 *r = jsval_number(atan(x));
148 return S_OK;
151 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
152 jsval_t *r)
154 double x, y;
155 HRESULT hres;
157 TRACE("\n");
159 if(argc<2) {
160 if(r)
161 *r = jsval_number(NAN);
162 return S_OK;
165 hres = to_number(ctx, argv[0], &y);
166 if(FAILED(hres))
167 return hres;
169 hres = to_number(ctx, argv[1], &x);
170 if(FAILED(hres))
171 return hres;
173 if(r)
174 *r = jsval_number(atan2(y, x));
175 return S_OK;
178 /* ECMA-262 3rd Edition 15.8.2.6 */
179 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
180 jsval_t *r)
182 double x;
183 HRESULT hres;
185 TRACE("\n");
187 if(!argc) {
188 if(r)
189 *r = jsval_number(NAN);
190 return S_OK;
193 hres = to_number(ctx, argv[0], &x);
194 if(FAILED(hres))
195 return hres;
197 if(r)
198 *r = jsval_number(ceil(x));
199 return S_OK;
202 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
203 jsval_t *r)
205 double x;
206 HRESULT hres;
208 TRACE("\n");
210 if(!argc) {
211 if(r)
212 *r = jsval_number(NAN);
213 return S_OK;
216 hres = to_number(ctx, argv[0], &x);
217 if(FAILED(hres))
218 return hres;
220 if(r)
221 *r = jsval_number(cos(x));
222 return S_OK;
225 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
226 jsval_t *r)
228 double x;
229 HRESULT hres;
231 TRACE("\n");
233 if(!argc) {
234 if(r)
235 *r = jsval_number(NAN);
236 return S_OK;
239 hres = to_number(ctx, argv[0], &x);
240 if(FAILED(hres))
241 return hres;
243 if(r)
244 *r = jsval_number(exp(x));
245 return S_OK;
248 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
249 jsval_t *r)
251 double x;
252 HRESULT hres;
254 TRACE("\n");
256 if(!argc) {
257 if(r)
258 *r = jsval_number(NAN);
259 return S_OK;
262 hres = to_number(ctx, argv[0], &x);
263 if(FAILED(hres))
264 return hres;
266 if(r)
267 *r = jsval_number(floor(x));
268 return S_OK;
271 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
272 jsval_t *r)
274 double x;
275 HRESULT hres;
277 TRACE("\n");
279 if(!argc) {
280 if(r)
281 *r = jsval_number(NAN);
282 return S_OK;
285 hres = to_number(ctx, argv[0], &x);
286 if(FAILED(hres))
287 return hres;
289 if(r)
290 *r = jsval_number(x < -0.0 ? NAN : log(x));
291 return S_OK;
294 /* ECMA-262 3rd Edition 15.8.2.11 */
295 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
296 jsval_t *r)
298 DOUBLE max, d;
299 DWORD i;
300 HRESULT hres;
302 TRACE("\n");
304 if(!argc) {
305 if(r)
306 *r = jsval_number(-INFINITY);
307 return S_OK;
310 hres = to_number(ctx, argv[0], &max);
311 if(FAILED(hres))
312 return hres;
314 for(i=1; i < argc; i++) {
315 hres = to_number(ctx, argv[i], &d);
316 if(FAILED(hres))
317 return hres;
319 if(d > max || isnan(d))
320 max = d;
323 if(r)
324 *r = jsval_number(max);
325 return S_OK;
328 /* ECMA-262 3rd Edition 15.8.2.12 */
329 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
330 jsval_t *r)
332 DOUBLE min, d;
333 DWORD i;
334 HRESULT hres;
336 TRACE("\n");
338 if(!argc) {
339 if(r)
340 *r = jsval_number(INFINITY);
341 return S_OK;
344 hres = to_number(ctx, argv[0], &min);
345 if(FAILED(hres))
346 return hres;
348 for(i=1; i < argc; i++) {
349 hres = to_number(ctx, argv[i], &d);
350 if(FAILED(hres))
351 return hres;
353 if(d < min || isnan(d))
354 min = d;
357 if(r)
358 *r = jsval_number(min);
359 return S_OK;
362 /* ECMA-262 3rd Edition 15.8.2.13 */
363 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
364 jsval_t *r)
366 double x, y;
367 HRESULT hres;
369 TRACE("\n");
371 if(argc < 2) {
372 if(r)
373 *r = jsval_number(NAN);
374 return S_OK;
377 hres = to_number(ctx, argv[0], &x);
378 if(FAILED(hres))
379 return hres;
381 hres = to_number(ctx, argv[1], &y);
382 if(FAILED(hres))
383 return hres;
385 if(r)
386 *r = jsval_number(pow(x, y));
387 return S_OK;
390 /* ECMA-262 3rd Edition 15.8.2.14 */
391 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
392 jsval_t *r)
394 UINT x;
396 TRACE("\n");
398 if(!RtlGenRandom(&x, sizeof(x)))
399 return E_UNEXPECTED;
401 if(r)
402 *r = jsval_number((double)x/(double)UINT_MAX);
403 return S_OK;
406 /* ECMA-262 3rd Edition 15.8.2.15 */
407 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
408 jsval_t *r)
410 double x;
411 HRESULT hres;
413 TRACE("\n");
415 if(!argc) {
416 if(r)
417 *r = jsval_number(NAN);
418 return S_OK;
421 hres = to_number(ctx, argv[0], &x);
422 if(FAILED(hres))
423 return hres;
425 if(r)
426 *r = jsval_number(floor(x+0.5));
427 return S_OK;
430 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
431 jsval_t *r)
433 double x;
434 HRESULT hres;
436 TRACE("\n");
438 if(!argc) {
439 if(r)
440 *r = jsval_number(NAN);
441 return S_OK;
444 hres = to_number(ctx, argv[0], &x);
445 if(FAILED(hres))
446 return hres;
448 if(r)
449 *r = jsval_number(sin(x));
450 return S_OK;
453 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
454 jsval_t *r)
456 double x;
457 HRESULT hres;
459 TRACE("\n");
461 if(!argc) {
462 if(r)
463 *r = jsval_number(NAN);
464 return S_OK;
467 hres = to_number(ctx, argv[0], &x);
468 if(FAILED(hres))
469 return hres;
471 if(r)
472 *r = jsval_number(sqrt(x));
473 return S_OK;
476 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
477 jsval_t *r)
479 double x;
480 HRESULT hres;
482 TRACE("\n");
484 if(!argc) {
485 if(r)
486 *r = jsval_number(NAN);
487 return S_OK;
490 hres = to_number(ctx, argv[0], &x);
491 if(FAILED(hres))
492 return hres;
494 if(r)
495 *r = jsval_number(tan(x));
496 return S_OK;
499 static const builtin_prop_t Math_props[] = {
500 {absW, Math_abs, PROPF_METHOD|1},
501 {acosW, Math_acos, PROPF_METHOD|1},
502 {asinW, Math_asin, PROPF_METHOD|1},
503 {atanW, Math_atan, PROPF_METHOD|1},
504 {atan2W, Math_atan2, PROPF_METHOD|2},
505 {ceilW, Math_ceil, PROPF_METHOD|1},
506 {cosW, Math_cos, PROPF_METHOD|1},
507 {expW, Math_exp, PROPF_METHOD|1},
508 {floorW, Math_floor, PROPF_METHOD|1},
509 {logW, Math_log, PROPF_METHOD|1},
510 {maxW, Math_max, PROPF_METHOD|2},
511 {minW, Math_min, PROPF_METHOD|2},
512 {powW, Math_pow, PROPF_METHOD|2},
513 {randomW, Math_random, PROPF_METHOD},
514 {roundW, Math_round, PROPF_METHOD|1},
515 {sinW, Math_sin, PROPF_METHOD|1},
516 {sqrtW, Math_sqrt, PROPF_METHOD|1},
517 {tanW, Math_tan, PROPF_METHOD|1}
520 static const builtin_info_t Math_info = {
521 JSCLASS_MATH,
522 {NULL, NULL, 0},
523 ARRAY_SIZE(Math_props),
524 Math_props,
525 NULL,
526 NULL
529 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
531 jsdisp_t *math;
532 unsigned i;
533 HRESULT hres;
535 struct {
536 const WCHAR *name;
537 DOUBLE val;
538 }constants[] = {
539 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
540 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
541 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
542 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
543 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
544 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
545 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
546 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
549 math = heap_alloc_zero(sizeof(jsdisp_t));
550 if(!math)
551 return E_OUTOFMEMORY;
553 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
554 if(FAILED(hres)) {
555 heap_free(math);
556 return hres;
559 for(i=0; i < ARRAY_SIZE(constants); i++) {
560 hres = jsdisp_define_data_property(math, constants[i].name, 0,
561 jsval_number(constants[i].val));
562 if(FAILED(hres)) {
563 jsdisp_release(math);
564 return hres;
568 *ret = math;
569 return S_OK;