mssign32: Add a trailing '\n' to a few FIXME() messages.
[wine/multimedia.git] / dlls / jscript / math.c
blobb28431eec0ddaf26d3f0ec3e0206d6f597546b0c
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, DISPPARAMS *dp,
62 VARIANT *retv, jsexcept_t *ei)
64 VARIANT v;
65 DOUBLE d;
66 HRESULT hres;
68 TRACE("\n");
70 if(!arg_cnt(dp)) {
71 if(retv)
72 num_set_nan(retv);
73 return S_OK;
76 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
77 if(FAILED(hres))
78 return hres;
80 d = num_val(&v);
81 if(retv)
82 num_set_val(retv, d < 0.0 ? -d : d);
83 return S_OK;
86 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
87 VARIANT *retv, jsexcept_t *ei)
89 VARIANT v;
90 HRESULT hres;
92 TRACE("\n");
94 if(!arg_cnt(dp)) {
95 if(retv) num_set_nan(retv);
96 return S_OK;
99 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
100 if(FAILED(hres))
101 return hres;
103 if(retv) {
104 DOUBLE x = num_val(&v);
105 if(x < -1.0 || x > 1.0)
106 num_set_nan(retv);
107 else
108 num_set_val(retv, acos(x));
110 return S_OK;
113 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
114 VARIANT *retv, jsexcept_t *ei)
116 VARIANT v;
117 HRESULT hres;
119 TRACE("\n");
121 if(!arg_cnt(dp)) {
122 if(retv) num_set_nan(retv);
123 return S_OK;
126 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
127 if(FAILED(hres))
128 return hres;
130 if(retv) {
131 DOUBLE x = num_val(&v);
132 if(x < -1.0 || x > 1.0)
133 num_set_nan(retv);
134 else
135 num_set_val(retv, asin(x));
137 return S_OK;
140 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
141 VARIANT *retv, jsexcept_t *ei)
143 VARIANT v;
144 HRESULT hres;
146 TRACE("\n");
148 if(!arg_cnt(dp)) {
149 if(retv) num_set_nan(retv);
150 return S_OK;
153 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
154 if(FAILED(hres))
155 return hres;
157 if(retv) num_set_val(retv, atan(num_val(&v)));
158 return S_OK;
161 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
162 VARIANT *retv, jsexcept_t *ei)
164 VARIANT v1, v2;
165 HRESULT hres;
167 TRACE("\n");
169 if(arg_cnt(dp)<2) {
170 if(retv) num_set_nan(retv);
171 return S_OK;
174 hres = to_number(ctx, get_arg(dp, 0), ei, &v1);
175 if(FAILED(hres))
176 return hres;
178 hres = to_number(ctx, get_arg(dp, 1), ei, &v2);
179 if(FAILED(hres))
180 return hres;
182 if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
183 return S_OK;
186 /* ECMA-262 3rd Edition 15.8.2.6 */
187 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
188 VARIANT *retv, jsexcept_t *ei)
190 VARIANT v;
191 HRESULT hres;
193 TRACE("\n");
195 if(!arg_cnt(dp)) {
196 if(retv)
197 num_set_nan(retv);
198 return S_OK;
201 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
202 if(FAILED(hres))
203 return hres;
205 if(retv)
206 num_set_val(retv, ceil(num_val(&v)));
207 return S_OK;
210 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei)
213 VARIANT v;
214 HRESULT hres;
216 TRACE("\n");
218 if(!arg_cnt(dp)) {
219 if(retv) num_set_nan(retv);
220 return S_OK;
223 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
224 if(FAILED(hres))
225 return hres;
227 if(retv) num_set_val(retv, cos(num_val(&v)));
228 return S_OK;
231 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
232 VARIANT *retv, jsexcept_t *ei)
234 VARIANT v;
235 HRESULT hres;
237 TRACE("\n");
239 if(!arg_cnt(dp)) {
240 if(retv) num_set_nan(retv);
241 return S_OK;
244 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
245 if(FAILED(hres))
246 return hres;
248 if(retv) num_set_val(retv, exp(num_val(&v)));
249 return S_OK;
252 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
253 VARIANT *retv, jsexcept_t *ei)
255 VARIANT v;
256 HRESULT hres;
258 TRACE("\n");
260 if(!arg_cnt(dp)) {
261 if(retv)
262 num_set_nan(retv);
263 return S_OK;
266 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
267 if(FAILED(hres))
268 return hres;
270 if(retv)
271 num_set_val(retv, floor(num_val(&v)));
272 return S_OK;
275 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
276 VARIANT *retv, jsexcept_t *ei)
278 VARIANT v;
279 HRESULT hres;
281 TRACE("\n");
283 if(!arg_cnt(dp)) {
284 if(retv)
285 num_set_nan(retv);
286 return S_OK;
289 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
290 if(FAILED(hres))
291 return hres;
293 if(retv) {
294 DOUBLE x = num_val(&v);
295 if(x < -0.0)
296 num_set_nan(retv);
297 else
298 num_set_val(retv, log(x));
300 return S_OK;
303 /* ECMA-262 3rd Edition 15.8.2.11 */
304 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
305 VARIANT *retv, jsexcept_t *ei)
307 DOUBLE max, d;
308 VARIANT v;
309 DWORD i;
310 HRESULT hres;
312 TRACE("\n");
314 if(!arg_cnt(dp)) {
315 if(retv)
316 num_set_inf(retv, FALSE);
317 return S_OK;
320 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
321 if(FAILED(hres))
322 return hres;
324 max = num_val(&v);
325 for(i=1; i < arg_cnt(dp); i++) {
326 hres = to_number(ctx, get_arg(dp, i), ei, &v);
327 if(FAILED(hres))
328 return hres;
330 d = num_val(&v);
331 if(d > max || isnan(d))
332 max = d;
335 if(retv)
336 num_set_val(retv, max);
337 return S_OK;
340 /* ECMA-262 3rd Edition 15.8.2.12 */
341 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
342 VARIANT *retv, jsexcept_t *ei)
344 DOUBLE min, d;
345 VARIANT v;
346 DWORD i;
347 HRESULT hres;
349 TRACE("\n");
351 if(!arg_cnt(dp)) {
352 if(retv)
353 num_set_inf(retv, TRUE);
354 return S_OK;
357 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
358 if(FAILED(hres))
359 return hres;
361 min = num_val(&v);
362 for(i=1; i < arg_cnt(dp); i++) {
363 hres = to_number(ctx, get_arg(dp, i), ei, &v);
364 if(FAILED(hres))
365 return hres;
367 d = num_val(&v);
368 if(d < min || isnan(d))
369 min = d;
372 if(retv)
373 num_set_val(retv, min);
374 return S_OK;
377 /* ECMA-262 3rd Edition 15.8.2.13 */
378 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
379 VARIANT *retv, jsexcept_t *ei)
381 VARIANT x, y;
382 HRESULT hres;
384 TRACE("\n");
386 if(arg_cnt(dp) < 2) {
387 if(retv) num_set_nan(retv);
388 return S_OK;
391 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
392 if(FAILED(hres))
393 return hres;
395 hres = to_number(ctx, get_arg(dp, 1), ei, &y);
396 if(FAILED(hres))
397 return hres;
399 if(retv)
400 num_set_val(retv, pow(num_val(&x), num_val(&y)));
401 return S_OK;
404 /* ECMA-262 3rd Edition 15.8.2.14 */
405 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
406 VARIANT *retv, jsexcept_t *ei)
408 UINT r;
410 TRACE("\n");
412 if(!RtlGenRandom(&r, sizeof(r)))
413 return E_UNEXPECTED;
415 if(retv)
416 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
418 return S_OK;
421 /* ECMA-262 3rd Edition 15.8.2.15 */
422 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
423 VARIANT *retv, jsexcept_t *ei)
425 VARIANT v;
426 HRESULT hres;
428 TRACE("\n");
430 if(!arg_cnt(dp)) {
431 num_set_nan(retv);
432 return S_OK;
435 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
436 if(FAILED(hres))
437 return hres;
439 if(retv)
440 num_set_val(retv, floor(num_val(&v)+0.5));
441 return S_OK;
444 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
445 VARIANT *retv, jsexcept_t *ei)
447 VARIANT v;
448 HRESULT hres;
450 TRACE("\n");
452 if(!arg_cnt(dp)) {
453 if(retv) num_set_nan(retv);
454 return S_OK;
457 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
458 if(FAILED(hres))
459 return hres;
461 if(retv) num_set_val(retv, sin(num_val(&v)));
462 return S_OK;
465 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
466 VARIANT *retv, jsexcept_t *ei)
468 VARIANT v;
469 HRESULT hres;
471 TRACE("\n");
473 if(!arg_cnt(dp)) {
474 if(retv) num_set_nan(retv);
475 return S_OK;
478 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
479 if(FAILED(hres))
480 return hres;
482 if(retv) num_set_val(retv, sqrt(num_val(&v)));
483 return S_OK;
486 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
487 VARIANT *retv, jsexcept_t *ei)
489 VARIANT v;
490 HRESULT hres;
492 TRACE("\n");
494 if(!arg_cnt(dp)) {
495 if(retv) num_set_nan(retv);
496 return S_OK;
499 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
500 if(FAILED(hres))
501 return hres;
503 if(retv) num_set_val(retv, tan(num_val(&v)));
504 return S_OK;
507 static const builtin_prop_t Math_props[] = {
508 {absW, Math_abs, PROPF_METHOD|1},
509 {acosW, Math_acos, PROPF_METHOD|1},
510 {asinW, Math_asin, PROPF_METHOD|1},
511 {atanW, Math_atan, PROPF_METHOD|1},
512 {atan2W, Math_atan2, PROPF_METHOD|2},
513 {ceilW, Math_ceil, PROPF_METHOD|1},
514 {cosW, Math_cos, PROPF_METHOD|1},
515 {expW, Math_exp, PROPF_METHOD|1},
516 {floorW, Math_floor, PROPF_METHOD|1},
517 {logW, Math_log, PROPF_METHOD|1},
518 {maxW, Math_max, PROPF_METHOD|2},
519 {minW, Math_min, PROPF_METHOD|2},
520 {powW, Math_pow, PROPF_METHOD|2},
521 {randomW, Math_random, PROPF_METHOD},
522 {roundW, Math_round, PROPF_METHOD|1},
523 {sinW, Math_sin, PROPF_METHOD|1},
524 {sqrtW, Math_sqrt, PROPF_METHOD|1},
525 {tanW, Math_tan, PROPF_METHOD|1}
528 static const builtin_info_t Math_info = {
529 JSCLASS_MATH,
530 {NULL, NULL, 0},
531 sizeof(Math_props)/sizeof(*Math_props),
532 Math_props,
533 NULL,
534 NULL
537 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
539 jsdisp_t *math;
540 unsigned i;
541 VARIANT v;
542 HRESULT hres;
544 struct {
545 const WCHAR *name;
546 DOUBLE val;
547 }constants[] = {
548 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
549 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
550 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
551 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
552 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
553 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
554 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
555 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
558 math = heap_alloc_zero(sizeof(jsdisp_t));
559 if(!math)
560 return E_OUTOFMEMORY;
562 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
563 if(FAILED(hres)) {
564 heap_free(math);
565 return hres;
568 V_VT(&v) = VT_R8;
569 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
570 V_R8(&v) = constants[i].val;
571 hres = jsdisp_propput_const(math, constants[i].name, &v);
572 if(FAILED(hres)) {
573 jsdisp_release(math);
574 return hres;
578 *ret = math;
579 return S_OK;