mshtml.idl: Added IHTMLTableCell declaration.
[wine/multimedia.git] / dlls / jscript / math.c
blob9c17967edd8306bb0f33bfde496eb69c0e03306e
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 double d;
65 HRESULT hres;
67 TRACE("\n");
69 if(!arg_cnt(dp)) {
70 if(retv)
71 num_set_nan(retv);
72 return S_OK;
75 hres = to_number(ctx, get_arg(dp, 0), ei, &d);
76 if(FAILED(hres))
77 return hres;
79 if(retv)
80 num_set_val(retv, d < 0.0 ? -d : d);
81 return S_OK;
84 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
85 VARIANT *retv, jsexcept_t *ei)
87 double x;
88 HRESULT hres;
90 TRACE("\n");
92 if(!arg_cnt(dp)) {
93 if(retv) num_set_nan(retv);
94 return S_OK;
97 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
98 if(FAILED(hres))
99 return hres;
101 if(retv) {
102 if(x < -1.0 || x > 1.0)
103 num_set_nan(retv);
104 else
105 num_set_val(retv, acos(x));
107 return S_OK;
110 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
111 VARIANT *retv, jsexcept_t *ei)
113 double x;
114 HRESULT hres;
116 TRACE("\n");
118 if(!arg_cnt(dp)) {
119 if(retv) num_set_nan(retv);
120 return S_OK;
123 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
124 if(FAILED(hres))
125 return hres;
127 if(retv) {
128 if(x < -1.0 || x > 1.0)
129 num_set_nan(retv);
130 else
131 num_set_val(retv, asin(x));
133 return S_OK;
136 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
137 VARIANT *retv, jsexcept_t *ei)
139 double x;
140 HRESULT hres;
142 TRACE("\n");
144 if(!arg_cnt(dp)) {
145 if(retv) num_set_nan(retv);
146 return S_OK;
149 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
150 if(FAILED(hres))
151 return hres;
153 if(retv) num_set_val(retv, atan(x));
154 return S_OK;
157 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
158 VARIANT *retv, jsexcept_t *ei)
160 double x, y;
161 HRESULT hres;
163 TRACE("\n");
165 if(arg_cnt(dp)<2) {
166 if(retv) num_set_nan(retv);
167 return S_OK;
170 hres = to_number(ctx, get_arg(dp, 0), ei, &y);
171 if(FAILED(hres))
172 return hres;
174 hres = to_number(ctx, get_arg(dp, 1), ei, &x);
175 if(FAILED(hres))
176 return hres;
178 if(retv) num_set_val(retv, atan2(y, x));
179 return S_OK;
182 /* ECMA-262 3rd Edition 15.8.2.6 */
183 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
184 VARIANT *retv, jsexcept_t *ei)
186 double x;
187 HRESULT hres;
189 TRACE("\n");
191 if(!arg_cnt(dp)) {
192 if(retv)
193 num_set_nan(retv);
194 return S_OK;
197 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
198 if(FAILED(hres))
199 return hres;
201 if(retv)
202 num_set_val(retv, ceil(x));
203 return S_OK;
206 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei)
209 double x;
210 HRESULT hres;
212 TRACE("\n");
214 if(!arg_cnt(dp)) {
215 if(retv) num_set_nan(retv);
216 return S_OK;
219 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
220 if(FAILED(hres))
221 return hres;
223 if(retv) num_set_val(retv, cos(x));
224 return S_OK;
227 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
228 VARIANT *retv, jsexcept_t *ei)
230 double x;
231 HRESULT hres;
233 TRACE("\n");
235 if(!arg_cnt(dp)) {
236 if(retv) num_set_nan(retv);
237 return S_OK;
240 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
241 if(FAILED(hres))
242 return hres;
244 if(retv) num_set_val(retv, exp(x));
245 return S_OK;
248 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
249 VARIANT *retv, jsexcept_t *ei)
251 double x;
252 HRESULT hres;
254 TRACE("\n");
256 if(!arg_cnt(dp)) {
257 if(retv)
258 num_set_nan(retv);
259 return S_OK;
262 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
263 if(FAILED(hres))
264 return hres;
266 if(retv)
267 num_set_val(retv, floor(x));
268 return S_OK;
271 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
272 VARIANT *retv, jsexcept_t *ei)
274 double x;
275 HRESULT hres;
277 TRACE("\n");
279 if(!arg_cnt(dp)) {
280 if(retv)
281 num_set_nan(retv);
282 return S_OK;
285 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
286 if(FAILED(hres))
287 return hres;
289 if(retv) {
290 if(x < -0.0)
291 num_set_nan(retv);
292 else
293 num_set_val(retv, log(x));
295 return S_OK;
298 /* ECMA-262 3rd Edition 15.8.2.11 */
299 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
300 VARIANT *retv, jsexcept_t *ei)
302 DOUBLE max, d;
303 DWORD i;
304 HRESULT hres;
306 TRACE("\n");
308 if(!arg_cnt(dp)) {
309 if(retv)
310 num_set_inf(retv, FALSE);
311 return S_OK;
314 hres = to_number(ctx, get_arg(dp, 0), ei, &max);
315 if(FAILED(hres))
316 return hres;
318 for(i=1; i < arg_cnt(dp); i++) {
319 hres = to_number(ctx, get_arg(dp, i), ei, &d);
320 if(FAILED(hres))
321 return hres;
323 if(d > max || isnan(d))
324 max = d;
327 if(retv)
328 num_set_val(retv, max);
329 return S_OK;
332 /* ECMA-262 3rd Edition 15.8.2.12 */
333 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
334 VARIANT *retv, jsexcept_t *ei)
336 DOUBLE min, d;
337 DWORD i;
338 HRESULT hres;
340 TRACE("\n");
342 if(!arg_cnt(dp)) {
343 if(retv)
344 num_set_inf(retv, TRUE);
345 return S_OK;
348 hres = to_number(ctx, get_arg(dp, 0), ei, &min);
349 if(FAILED(hres))
350 return hres;
352 for(i=1; i < arg_cnt(dp); i++) {
353 hres = to_number(ctx, get_arg(dp, i), ei, &d);
354 if(FAILED(hres))
355 return hres;
357 if(d < min || isnan(d))
358 min = d;
361 if(retv)
362 num_set_val(retv, min);
363 return S_OK;
366 /* ECMA-262 3rd Edition 15.8.2.13 */
367 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
368 VARIANT *retv, jsexcept_t *ei)
370 double x, y;
371 HRESULT hres;
373 TRACE("\n");
375 if(arg_cnt(dp) < 2) {
376 if(retv) num_set_nan(retv);
377 return S_OK;
380 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
381 if(FAILED(hres))
382 return hres;
384 hres = to_number(ctx, get_arg(dp, 1), ei, &y);
385 if(FAILED(hres))
386 return hres;
388 if(retv)
389 num_set_val(retv, pow(x, y));
390 return S_OK;
393 /* ECMA-262 3rd Edition 15.8.2.14 */
394 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
395 VARIANT *retv, jsexcept_t *ei)
397 UINT r;
399 TRACE("\n");
401 if(!RtlGenRandom(&r, sizeof(r)))
402 return E_UNEXPECTED;
404 if(retv)
405 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
407 return S_OK;
410 /* ECMA-262 3rd Edition 15.8.2.15 */
411 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
412 VARIANT *retv, jsexcept_t *ei)
414 double x;
415 HRESULT hres;
417 TRACE("\n");
419 if(!arg_cnt(dp)) {
420 num_set_nan(retv);
421 return S_OK;
424 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
425 if(FAILED(hres))
426 return hres;
428 if(retv)
429 num_set_val(retv, floor(x+0.5));
430 return S_OK;
433 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
434 VARIANT *retv, jsexcept_t *ei)
436 double x;
437 HRESULT hres;
439 TRACE("\n");
441 if(!arg_cnt(dp)) {
442 if(retv) num_set_nan(retv);
443 return S_OK;
446 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
447 if(FAILED(hres))
448 return hres;
450 if(retv) num_set_val(retv, sin(x));
451 return S_OK;
454 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
455 VARIANT *retv, jsexcept_t *ei)
457 double x;
458 HRESULT hres;
460 TRACE("\n");
462 if(!arg_cnt(dp)) {
463 if(retv) num_set_nan(retv);
464 return S_OK;
467 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
468 if(FAILED(hres))
469 return hres;
471 if(retv) num_set_val(retv, sqrt(x));
472 return S_OK;
475 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
476 VARIANT *retv, jsexcept_t *ei)
478 double x;
479 HRESULT hres;
481 TRACE("\n");
483 if(!arg_cnt(dp)) {
484 if(retv) num_set_nan(retv);
485 return S_OK;
488 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
489 if(FAILED(hres))
490 return hres;
492 if(retv) num_set_val(retv, tan(x));
493 return S_OK;
496 static const builtin_prop_t Math_props[] = {
497 {absW, Math_abs, PROPF_METHOD|1},
498 {acosW, Math_acos, PROPF_METHOD|1},
499 {asinW, Math_asin, PROPF_METHOD|1},
500 {atanW, Math_atan, PROPF_METHOD|1},
501 {atan2W, Math_atan2, PROPF_METHOD|2},
502 {ceilW, Math_ceil, PROPF_METHOD|1},
503 {cosW, Math_cos, PROPF_METHOD|1},
504 {expW, Math_exp, PROPF_METHOD|1},
505 {floorW, Math_floor, PROPF_METHOD|1},
506 {logW, Math_log, PROPF_METHOD|1},
507 {maxW, Math_max, PROPF_METHOD|2},
508 {minW, Math_min, PROPF_METHOD|2},
509 {powW, Math_pow, PROPF_METHOD|2},
510 {randomW, Math_random, PROPF_METHOD},
511 {roundW, Math_round, PROPF_METHOD|1},
512 {sinW, Math_sin, PROPF_METHOD|1},
513 {sqrtW, Math_sqrt, PROPF_METHOD|1},
514 {tanW, Math_tan, PROPF_METHOD|1}
517 static const builtin_info_t Math_info = {
518 JSCLASS_MATH,
519 {NULL, NULL, 0},
520 sizeof(Math_props)/sizeof(*Math_props),
521 Math_props,
522 NULL,
523 NULL
526 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
528 jsdisp_t *math;
529 unsigned i;
530 VARIANT v;
531 HRESULT hres;
533 struct {
534 const WCHAR *name;
535 DOUBLE val;
536 }constants[] = {
537 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
538 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
539 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
540 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
541 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
542 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
543 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
544 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
547 math = heap_alloc_zero(sizeof(jsdisp_t));
548 if(!math)
549 return E_OUTOFMEMORY;
551 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
552 if(FAILED(hres)) {
553 heap_free(math);
554 return hres;
557 V_VT(&v) = VT_R8;
558 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
559 V_R8(&v) = constants[i].val;
560 hres = jsdisp_propput_const(math, constants[i].name, &v);
561 if(FAILED(hres)) {
562 jsdisp_release(math);
563 return hres;
567 *ret = math;
568 return S_OK;