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 "wine/port.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 static HRESULT
math_constant(DOUBLE val
, WORD flags
, VARIANT
*retv
)
63 case DISPATCH_PROPERTYGET
:
67 case DISPATCH_PROPERTYPUT
:
71 FIXME("unhandled flags %x\n", flags
);
75 /* ECMA-262 3rd Edition 15.8.1.1 */
76 static HRESULT
Math_E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
77 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
80 return math_constant(M_E
, flags
, retv
);
83 /* ECMA-262 3rd Edition 15.8.1.4 */
84 static HRESULT
Math_LOG2E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
85 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
88 return math_constant(M_LOG2E
, flags
, retv
);
91 /* ECMA-262 3rd Edition 15.8.1.4 */
92 static HRESULT
Math_LOG10E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
93 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
96 return math_constant(M_LOG10E
, flags
, retv
);
99 static HRESULT
Math_LN2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
100 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
103 return math_constant(M_LN2
, flags
, retv
);
106 static HRESULT
Math_LN10(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
107 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
110 return math_constant(M_LN10
, flags
, retv
);
113 /* ECMA-262 3rd Edition 15.8.1.6 */
114 static HRESULT
Math_PI(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
115 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
118 return math_constant(M_PI
, flags
, retv
);
121 static HRESULT
Math_SQRT2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
122 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
125 return math_constant(M_SQRT2
, flags
, retv
);
128 static HRESULT
Math_SQRT1_2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
129 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
132 return math_constant(M_SQRT1_2
, flags
, retv
);
135 /* ECMA-262 3rd Edition 15.8.2.12 */
136 static HRESULT
Math_abs(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
137 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
151 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
157 num_set_val(retv
, d
< 0.0 ? -d
: d
);
161 static HRESULT
Math_acos(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
162 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
170 if(retv
) num_set_nan(retv
);
174 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
178 if(retv
) num_set_val(retv
, acos(num_val(&v
)));
182 static HRESULT
Math_asin(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
183 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
191 if(retv
) num_set_nan(retv
);
195 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
199 if(retv
) num_set_val(retv
, asin(num_val(&v
)));
203 static HRESULT
Math_atan(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
204 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
212 if(retv
) num_set_nan(retv
);
216 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
220 if(retv
) num_set_val(retv
, atan(num_val(&v
)));
224 static HRESULT
Math_atan2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
225 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
233 if(retv
) num_set_nan(retv
);
237 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v1
);
241 hres
= to_number(dispex
->ctx
, get_arg(dp
, 1), ei
, &v2
);
245 if(retv
) num_set_val(retv
, atan2(num_val(&v1
), num_val(&v2
)));
249 /* ECMA-262 3rd Edition 15.8.2.6 */
250 static HRESULT
Math_ceil(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
251 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
264 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
269 num_set_val(retv
, ceil(num_val(&v
)));
273 static HRESULT
Math_cos(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
274 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
282 if(retv
) num_set_nan(retv
);
286 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
290 if(retv
) num_set_val(retv
, cos(num_val(&v
)));
294 static HRESULT
Math_exp(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
295 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
303 if(retv
) num_set_nan(retv
);
307 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
311 if(retv
) num_set_val(retv
, exp(num_val(&v
)));
315 static HRESULT
Math_floor(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
316 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
329 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
334 num_set_val(retv
, floor(num_val(&v
)));
338 static HRESULT
Math_log(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
339 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
352 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
357 num_set_val(retv
, log(num_val(&v
)));
361 /* ECMA-262 3rd Edition 15.8.2.11 */
362 static HRESULT
Math_max(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
363 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
374 num_set_inf(retv
, FALSE
);
378 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
383 for(i
=1; i
< arg_cnt(dp
); i
++) {
384 hres
= to_number(dispex
->ctx
, get_arg(dp
, i
), ei
, &v
);
389 if(d
> max
|| isnan(d
))
394 num_set_val(retv
, max
);
398 /* ECMA-262 3rd Edition 15.8.2.12 */
399 static HRESULT
Math_min(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
400 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
411 num_set_inf(retv
, TRUE
);
415 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
420 for(i
=1; i
< arg_cnt(dp
); i
++) {
421 hres
= to_number(dispex
->ctx
, get_arg(dp
, i
), ei
, &v
);
426 if(d
< min
|| isnan(d
))
431 num_set_val(retv
, min
);
435 /* ECMA-262 3rd Edition 15.8.2.13 */
436 static HRESULT
Math_pow(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
437 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
444 if(arg_cnt(dp
) < 2) {
445 if(retv
) num_set_nan(retv
);
449 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &x
);
453 hres
= to_number(dispex
->ctx
, get_arg(dp
, 1), ei
, &y
);
458 num_set_val(retv
, pow(num_val(&x
), num_val(&y
)));
462 /* ECMA-262 3rd Edition 15.8.2.14 */
463 static HRESULT
Math_random(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
464 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
470 if(!RtlGenRandom(&r
, sizeof(r
)))
474 num_set_val(retv
, (DOUBLE
)r
/(DOUBLE
)UINT_MAX
);
479 /* ECMA-262 3rd Edition 15.8.2.15 */
480 static HRESULT
Math_round(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
481 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
493 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
498 num_set_val(retv
, floor(num_val(&v
)+0.5));
502 static HRESULT
Math_sin(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
503 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
511 if(retv
) num_set_nan(retv
);
515 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
519 if(retv
) num_set_val(retv
, sin(num_val(&v
)));
523 static HRESULT
Math_sqrt(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
524 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
532 if(retv
) num_set_nan(retv
);
536 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
540 if(retv
) num_set_val(retv
, sqrt(num_val(&v
)));
544 static HRESULT
Math_tan(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
545 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
553 if(retv
) num_set_nan(retv
);
557 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
561 if(retv
) num_set_val(retv
, tan(num_val(&v
)));
565 static const builtin_prop_t Math_props
[] = {
567 {LN10W
, Math_LN10
, 0},
569 {LOG10EW
, Math_LOG10E
, 0},
570 {LOG2EW
, Math_LOG2E
, 0},
572 {SQRT1_2W
, Math_SQRT1_2
, 0},
573 {SQRT2W
, Math_SQRT2
, 0},
574 {absW
, Math_abs
, PROPF_METHOD
|1},
575 {acosW
, Math_acos
, PROPF_METHOD
|1},
576 {asinW
, Math_asin
, PROPF_METHOD
|1},
577 {atanW
, Math_atan
, PROPF_METHOD
|1},
578 {atan2W
, Math_atan2
, PROPF_METHOD
|2},
579 {ceilW
, Math_ceil
, PROPF_METHOD
|1},
580 {cosW
, Math_cos
, PROPF_METHOD
|1},
581 {expW
, Math_exp
, PROPF_METHOD
|1},
582 {floorW
, Math_floor
, PROPF_METHOD
|1},
583 {logW
, Math_log
, PROPF_METHOD
|1},
584 {maxW
, Math_max
, PROPF_METHOD
|2},
585 {minW
, Math_min
, PROPF_METHOD
|2},
586 {powW
, Math_pow
, PROPF_METHOD
|2},
587 {randomW
, Math_random
, PROPF_METHOD
},
588 {roundW
, Math_round
, PROPF_METHOD
|1},
589 {sinW
, Math_sin
, PROPF_METHOD
|1},
590 {sqrtW
, Math_sqrt
, PROPF_METHOD
|1},
591 {tanW
, Math_tan
, PROPF_METHOD
|1}
594 static const builtin_info_t Math_info
= {
597 sizeof(Math_props
)/sizeof(*Math_props
),
603 HRESULT
create_math(script_ctx_t
*ctx
, DispatchEx
**ret
)
608 math
= heap_alloc_zero(sizeof(DispatchEx
));
610 return E_OUTOFMEMORY
;
612 hres
= init_dispex_from_constr(math
, ctx
, &Math_info
, ctx
->object_constr
);