ddraw: Use a less offensive handle table implementation for surfaces.
[wine/hramrach.git] / dlls / jscript / math.c
blob88f0210656f5654dee6e9ad74c1854e7cb13dd90
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, IServiceProvider *sp)
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, IServiceProvider *sp)
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) num_set_val(retv, acos(num_val(&v)));
104 return S_OK;
107 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
108 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
110 VARIANT v;
111 HRESULT hres;
113 TRACE("\n");
115 if(!arg_cnt(dp)) {
116 if(retv) num_set_nan(retv);
117 return S_OK;
120 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
121 if(FAILED(hres))
122 return hres;
124 if(retv) num_set_val(retv, asin(num_val(&v)));
125 return S_OK;
128 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
129 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
131 VARIANT v;
132 HRESULT hres;
134 TRACE("\n");
136 if(!arg_cnt(dp)) {
137 if(retv) num_set_nan(retv);
138 return S_OK;
141 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
142 if(FAILED(hres))
143 return hres;
145 if(retv) num_set_val(retv, atan(num_val(&v)));
146 return S_OK;
149 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
150 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
152 VARIANT v1, v2;
153 HRESULT hres;
155 TRACE("\n");
157 if(arg_cnt(dp)<2) {
158 if(retv) num_set_nan(retv);
159 return S_OK;
162 hres = to_number(ctx, get_arg(dp, 0), ei, &v1);
163 if(FAILED(hres))
164 return hres;
166 hres = to_number(ctx, get_arg(dp, 1), ei, &v2);
167 if(FAILED(hres))
168 return hres;
170 if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
171 return S_OK;
174 /* ECMA-262 3rd Edition 15.8.2.6 */
175 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
176 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
178 VARIANT v;
179 HRESULT hres;
181 TRACE("\n");
183 if(!arg_cnt(dp)) {
184 if(retv)
185 num_set_nan(retv);
186 return S_OK;
189 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
190 if(FAILED(hres))
191 return hres;
193 if(retv)
194 num_set_val(retv, ceil(num_val(&v)));
195 return S_OK;
198 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
199 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
201 VARIANT v;
202 HRESULT hres;
204 TRACE("\n");
206 if(!arg_cnt(dp)) {
207 if(retv) num_set_nan(retv);
208 return S_OK;
211 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
212 if(FAILED(hres))
213 return hres;
215 if(retv) num_set_val(retv, cos(num_val(&v)));
216 return S_OK;
219 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
220 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
222 VARIANT v;
223 HRESULT hres;
225 TRACE("\n");
227 if(!arg_cnt(dp)) {
228 if(retv) num_set_nan(retv);
229 return S_OK;
232 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
233 if(FAILED(hres))
234 return hres;
236 if(retv) num_set_val(retv, exp(num_val(&v)));
237 return S_OK;
240 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
241 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
243 VARIANT v;
244 HRESULT hres;
246 TRACE("\n");
248 if(!arg_cnt(dp)) {
249 if(retv)
250 num_set_nan(retv);
251 return S_OK;
254 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
255 if(FAILED(hres))
256 return hres;
258 if(retv)
259 num_set_val(retv, floor(num_val(&v)));
260 return S_OK;
263 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
266 VARIANT v;
267 HRESULT hres;
269 TRACE("\n");
271 if(!arg_cnt(dp)) {
272 if(retv)
273 num_set_nan(retv);
274 return S_OK;
277 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
278 if(FAILED(hres))
279 return hres;
281 if(retv)
282 num_set_val(retv, log(num_val(&v)));
283 return S_OK;
286 /* ECMA-262 3rd Edition 15.8.2.11 */
287 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
288 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
290 DOUBLE max, d;
291 VARIANT v;
292 DWORD i;
293 HRESULT hres;
295 TRACE("\n");
297 if(!arg_cnt(dp)) {
298 if(retv)
299 num_set_inf(retv, FALSE);
300 return S_OK;
303 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
304 if(FAILED(hres))
305 return hres;
307 max = num_val(&v);
308 for(i=1; i < arg_cnt(dp); i++) {
309 hres = to_number(ctx, get_arg(dp, i), ei, &v);
310 if(FAILED(hres))
311 return hres;
313 d = num_val(&v);
314 if(d > max || isnan(d))
315 max = d;
318 if(retv)
319 num_set_val(retv, max);
320 return S_OK;
323 /* ECMA-262 3rd Edition 15.8.2.12 */
324 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
325 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
327 DOUBLE min, d;
328 VARIANT v;
329 DWORD i;
330 HRESULT hres;
332 TRACE("\n");
334 if(!arg_cnt(dp)) {
335 if(retv)
336 num_set_inf(retv, TRUE);
337 return S_OK;
340 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
341 if(FAILED(hres))
342 return hres;
344 min = num_val(&v);
345 for(i=1; i < arg_cnt(dp); i++) {
346 hres = to_number(ctx, get_arg(dp, i), ei, &v);
347 if(FAILED(hres))
348 return hres;
350 d = num_val(&v);
351 if(d < min || isnan(d))
352 min = d;
355 if(retv)
356 num_set_val(retv, min);
357 return S_OK;
360 /* ECMA-262 3rd Edition 15.8.2.13 */
361 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
362 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 VARIANT x, y;
365 HRESULT hres;
367 TRACE("\n");
369 if(arg_cnt(dp) < 2) {
370 if(retv) num_set_nan(retv);
371 return S_OK;
374 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
375 if(FAILED(hres))
376 return hres;
378 hres = to_number(ctx, get_arg(dp, 1), ei, &y);
379 if(FAILED(hres))
380 return hres;
382 if(retv)
383 num_set_val(retv, pow(num_val(&x), num_val(&y)));
384 return S_OK;
387 /* ECMA-262 3rd Edition 15.8.2.14 */
388 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
389 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
391 UINT r;
393 TRACE("\n");
395 if(!RtlGenRandom(&r, sizeof(r)))
396 return E_UNEXPECTED;
398 if(retv)
399 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
401 return S_OK;
404 /* ECMA-262 3rd Edition 15.8.2.15 */
405 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
406 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
408 VARIANT v;
409 HRESULT hres;
411 TRACE("\n");
413 if(!arg_cnt(dp)) {
414 num_set_nan(retv);
415 return S_OK;
418 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
419 if(FAILED(hres))
420 return hres;
422 if(retv)
423 num_set_val(retv, floor(num_val(&v)+0.5));
424 return S_OK;
427 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
428 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
430 VARIANT v;
431 HRESULT hres;
433 TRACE("\n");
435 if(!arg_cnt(dp)) {
436 if(retv) num_set_nan(retv);
437 return S_OK;
440 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
441 if(FAILED(hres))
442 return hres;
444 if(retv) num_set_val(retv, sin(num_val(&v)));
445 return S_OK;
448 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
449 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
451 VARIANT v;
452 HRESULT hres;
454 TRACE("\n");
456 if(!arg_cnt(dp)) {
457 if(retv) num_set_nan(retv);
458 return S_OK;
461 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
462 if(FAILED(hres))
463 return hres;
465 if(retv) num_set_val(retv, sqrt(num_val(&v)));
466 return S_OK;
469 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
470 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
472 VARIANT v;
473 HRESULT hres;
475 TRACE("\n");
477 if(!arg_cnt(dp)) {
478 if(retv) num_set_nan(retv);
479 return S_OK;
482 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
483 if(FAILED(hres))
484 return hres;
486 if(retv) num_set_val(retv, tan(num_val(&v)));
487 return S_OK;
490 static const builtin_prop_t Math_props[] = {
491 {absW, Math_abs, PROPF_METHOD|1},
492 {acosW, Math_acos, PROPF_METHOD|1},
493 {asinW, Math_asin, PROPF_METHOD|1},
494 {atanW, Math_atan, PROPF_METHOD|1},
495 {atan2W, Math_atan2, PROPF_METHOD|2},
496 {ceilW, Math_ceil, PROPF_METHOD|1},
497 {cosW, Math_cos, PROPF_METHOD|1},
498 {expW, Math_exp, PROPF_METHOD|1},
499 {floorW, Math_floor, PROPF_METHOD|1},
500 {logW, Math_log, PROPF_METHOD|1},
501 {maxW, Math_max, PROPF_METHOD|2},
502 {minW, Math_min, PROPF_METHOD|2},
503 {powW, Math_pow, PROPF_METHOD|2},
504 {randomW, Math_random, PROPF_METHOD},
505 {roundW, Math_round, PROPF_METHOD|1},
506 {sinW, Math_sin, PROPF_METHOD|1},
507 {sqrtW, Math_sqrt, PROPF_METHOD|1},
508 {tanW, Math_tan, PROPF_METHOD|1}
511 static const builtin_info_t Math_info = {
512 JSCLASS_MATH,
513 {NULL, NULL, 0},
514 sizeof(Math_props)/sizeof(*Math_props),
515 Math_props,
516 NULL,
517 NULL
520 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
522 DispatchEx *math;
523 unsigned i;
524 VARIANT v;
525 HRESULT hres;
527 struct {
528 const WCHAR *name;
529 DOUBLE val;
530 }constants[] = {
531 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
532 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
533 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
534 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
535 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
536 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
537 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
538 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
541 math = heap_alloc_zero(sizeof(DispatchEx));
542 if(!math)
543 return E_OUTOFMEMORY;
545 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
546 if(FAILED(hres)) {
547 heap_free(math);
548 return hres;
551 V_VT(&v) = VT_R8;
552 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
553 V_R8(&v) = constants[i].val;
554 hres = jsdisp_propput_const(math, constants[i].name, &v);
555 if(FAILED(hres)) {
556 jsdisp_release(math);
557 return hres;
561 *ret = math;
562 return S_OK;