wined3d: Use rendertarget views for color output instead of surfaces.
[wine.git] / dlls / jscript / math.c
blobef906f54390527ce3c97c16714ac0060bd6fb243
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 /* ECMA-262 3rd Edition 15.8.2.12 */
61 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
62 jsval_t *r)
64 double d;
65 HRESULT hres;
67 TRACE("\n");
69 if(!argc) {
70 if(r)
71 *r = jsval_number(NAN);
72 return S_OK;
75 hres = to_number(ctx, argv[0], &d);
76 if(FAILED(hres))
77 return hres;
79 if(r)
80 *r = jsval_number(d < 0.0 ? -d : d);
81 return S_OK;
84 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
85 jsval_t *r)
87 double x;
88 HRESULT hres;
90 TRACE("\n");
92 if(!argc) {
93 if(r)
94 *r = jsval_number(NAN);
95 return S_OK;
98 hres = to_number(ctx, argv[0], &x);
99 if(FAILED(hres))
100 return hres;
102 if(r)
103 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
104 return S_OK;
107 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
108 jsval_t *r)
110 double x;
111 HRESULT hres;
113 TRACE("\n");
115 if(!argc) {
116 if(r)
117 *r = jsval_number(NAN);
118 return S_OK;
121 hres = to_number(ctx, argv[0], &x);
122 if(FAILED(hres))
123 return hres;
125 if(r)
126 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
127 return S_OK;
130 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
131 jsval_t *r)
133 double x;
134 HRESULT hres;
136 TRACE("\n");
138 if(!argc) {
139 if(r)
140 *r = jsval_number(NAN);
141 return S_OK;
144 hres = to_number(ctx, argv[0], &x);
145 if(FAILED(hres))
146 return hres;
148 if(r)
149 *r = jsval_number(atan(x));
150 return S_OK;
153 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
154 jsval_t *r)
156 double x, y;
157 HRESULT hres;
159 TRACE("\n");
161 if(argc<2) {
162 if(r)
163 *r = jsval_number(NAN);
164 return S_OK;
167 hres = to_number(ctx, argv[0], &y);
168 if(FAILED(hres))
169 return hres;
171 hres = to_number(ctx, argv[1], &x);
172 if(FAILED(hres))
173 return hres;
175 if(r)
176 *r = jsval_number(atan2(y, x));
177 return S_OK;
180 /* ECMA-262 3rd Edition 15.8.2.6 */
181 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
182 jsval_t *r)
184 double x;
185 HRESULT hres;
187 TRACE("\n");
189 if(!argc) {
190 if(r)
191 *r = jsval_number(NAN);
192 return S_OK;
195 hres = to_number(ctx, argv[0], &x);
196 if(FAILED(hres))
197 return hres;
199 if(r)
200 *r = jsval_number(ceil(x));
201 return S_OK;
204 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
205 jsval_t *r)
207 double x;
208 HRESULT hres;
210 TRACE("\n");
212 if(!argc) {
213 if(r)
214 *r = jsval_number(NAN);
215 return S_OK;
218 hres = to_number(ctx, argv[0], &x);
219 if(FAILED(hres))
220 return hres;
222 if(r)
223 *r = jsval_number(cos(x));
224 return S_OK;
227 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
228 jsval_t *r)
230 double x;
231 HRESULT hres;
233 TRACE("\n");
235 if(!argc) {
236 if(r)
237 *r = jsval_number(NAN);
238 return S_OK;
241 hres = to_number(ctx, argv[0], &x);
242 if(FAILED(hres))
243 return hres;
245 if(r)
246 *r = jsval_number(exp(x));
247 return S_OK;
250 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
251 jsval_t *r)
253 double x;
254 HRESULT hres;
256 TRACE("\n");
258 if(!argc) {
259 if(r)
260 *r = jsval_number(NAN);
261 return S_OK;
264 hres = to_number(ctx, argv[0], &x);
265 if(FAILED(hres))
266 return hres;
268 if(r)
269 *r = jsval_number(floor(x));
270 return S_OK;
273 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
274 jsval_t *r)
276 double x;
277 HRESULT hres;
279 TRACE("\n");
281 if(!argc) {
282 if(r)
283 *r = jsval_number(NAN);
284 return S_OK;
287 hres = to_number(ctx, argv[0], &x);
288 if(FAILED(hres))
289 return hres;
291 if(r)
292 *r = jsval_number(x < -0.0 ? NAN : log(x));
293 return S_OK;
296 /* ECMA-262 3rd Edition 15.8.2.11 */
297 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
298 jsval_t *r)
300 DOUBLE max, d;
301 DWORD i;
302 HRESULT hres;
304 TRACE("\n");
306 if(!argc) {
307 if(r)
308 *r = jsval_number(-INFINITY);
309 return S_OK;
312 hres = to_number(ctx, argv[0], &max);
313 if(FAILED(hres))
314 return hres;
316 for(i=1; i < argc; i++) {
317 hres = to_number(ctx, argv[i], &d);
318 if(FAILED(hres))
319 return hres;
321 if(d > max || isnan(d))
322 max = d;
325 if(r)
326 *r = jsval_number(max);
327 return S_OK;
330 /* ECMA-262 3rd Edition 15.8.2.12 */
331 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
332 jsval_t *r)
334 DOUBLE min, d;
335 DWORD i;
336 HRESULT hres;
338 TRACE("\n");
340 if(!argc) {
341 if(r)
342 *r = jsval_number(INFINITY);
343 return S_OK;
346 hres = to_number(ctx, argv[0], &min);
347 if(FAILED(hres))
348 return hres;
350 for(i=1; i < argc; i++) {
351 hres = to_number(ctx, argv[i], &d);
352 if(FAILED(hres))
353 return hres;
355 if(d < min || isnan(d))
356 min = d;
359 if(r)
360 *r = jsval_number(min);
361 return S_OK;
364 /* ECMA-262 3rd Edition 15.8.2.13 */
365 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
366 jsval_t *r)
368 double x, y;
369 HRESULT hres;
371 TRACE("\n");
373 if(argc < 2) {
374 if(r)
375 *r = jsval_number(NAN);
376 return S_OK;
379 hres = to_number(ctx, argv[0], &x);
380 if(FAILED(hres))
381 return hres;
383 hres = to_number(ctx, argv[1], &y);
384 if(FAILED(hres))
385 return hres;
387 if(r)
388 *r = jsval_number(pow(x, y));
389 return S_OK;
392 /* ECMA-262 3rd Edition 15.8.2.14 */
393 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
394 jsval_t *r)
396 UINT x;
398 TRACE("\n");
400 if(!RtlGenRandom(&x, sizeof(x)))
401 return E_UNEXPECTED;
403 if(r)
404 *r = jsval_number((double)x/(double)UINT_MAX);
405 return S_OK;
408 /* ECMA-262 3rd Edition 15.8.2.15 */
409 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
410 jsval_t *r)
412 double x;
413 HRESULT hres;
415 TRACE("\n");
417 if(!argc) {
418 if(r)
419 *r = jsval_number(NAN);
420 return S_OK;
423 hres = to_number(ctx, argv[0], &x);
424 if(FAILED(hres))
425 return hres;
427 if(r)
428 *r = jsval_number(floor(x+0.5));
429 return S_OK;
432 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
433 jsval_t *r)
435 double x;
436 HRESULT hres;
438 TRACE("\n");
440 if(!argc) {
441 if(r)
442 *r = jsval_number(NAN);
443 return S_OK;
446 hres = to_number(ctx, argv[0], &x);
447 if(FAILED(hres))
448 return hres;
450 if(r)
451 *r = jsval_number(sin(x));
452 return S_OK;
455 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
456 jsval_t *r)
458 double x;
459 HRESULT hres;
461 TRACE("\n");
463 if(!argc) {
464 if(r)
465 *r = jsval_number(NAN);
466 return S_OK;
469 hres = to_number(ctx, argv[0], &x);
470 if(FAILED(hres))
471 return hres;
473 if(r)
474 *r = jsval_number(sqrt(x));
475 return S_OK;
478 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
479 jsval_t *r)
481 double x;
482 HRESULT hres;
484 TRACE("\n");
486 if(!argc) {
487 if(r)
488 *r = jsval_number(NAN);
489 return S_OK;
492 hres = to_number(ctx, argv[0], &x);
493 if(FAILED(hres))
494 return hres;
496 if(r)
497 *r = jsval_number(tan(x));
498 return S_OK;
501 static const builtin_prop_t Math_props[] = {
502 {absW, Math_abs, PROPF_METHOD|1},
503 {acosW, Math_acos, PROPF_METHOD|1},
504 {asinW, Math_asin, PROPF_METHOD|1},
505 {atanW, Math_atan, PROPF_METHOD|1},
506 {atan2W, Math_atan2, PROPF_METHOD|2},
507 {ceilW, Math_ceil, PROPF_METHOD|1},
508 {cosW, Math_cos, PROPF_METHOD|1},
509 {expW, Math_exp, PROPF_METHOD|1},
510 {floorW, Math_floor, PROPF_METHOD|1},
511 {logW, Math_log, PROPF_METHOD|1},
512 {maxW, Math_max, PROPF_METHOD|2},
513 {minW, Math_min, PROPF_METHOD|2},
514 {powW, Math_pow, PROPF_METHOD|2},
515 {randomW, Math_random, PROPF_METHOD},
516 {roundW, Math_round, PROPF_METHOD|1},
517 {sinW, Math_sin, PROPF_METHOD|1},
518 {sqrtW, Math_sqrt, PROPF_METHOD|1},
519 {tanW, Math_tan, PROPF_METHOD|1}
522 static const builtin_info_t Math_info = {
523 JSCLASS_MATH,
524 {NULL, NULL, 0},
525 sizeof(Math_props)/sizeof(*Math_props),
526 Math_props,
527 NULL,
528 NULL
531 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
533 jsdisp_t *math;
534 unsigned i;
535 HRESULT hres;
537 struct {
538 const WCHAR *name;
539 DOUBLE val;
540 }constants[] = {
541 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
542 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
543 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
544 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
545 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
546 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
547 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
548 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
551 math = heap_alloc_zero(sizeof(jsdisp_t));
552 if(!math)
553 return E_OUTOFMEMORY;
555 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
556 if(FAILED(hres)) {
557 heap_free(math);
558 return hres;
561 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
562 hres = jsdisp_propput_const(math, constants[i].name, jsval_number(constants[i].val));
563 if(FAILED(hres)) {
564 jsdisp_release(math);
565 return hres;
569 *ret = math;
570 return S_OK;