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 /* 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
)
76 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
82 num_set_val(retv
, d
< 0.0 ? -d
: d
);
86 static HRESULT
Math_acos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
87 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
95 if(retv
) num_set_nan(retv
);
99 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
103 if(retv
) num_set_val(retv
, acos(num_val(&v
)));
107 static HRESULT
Math_asin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
108 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
116 if(retv
) num_set_nan(retv
);
120 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
124 if(retv
) num_set_val(retv
, asin(num_val(&v
)));
128 static HRESULT
Math_atan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
129 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
137 if(retv
) num_set_nan(retv
);
141 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
145 if(retv
) num_set_val(retv
, atan(num_val(&v
)));
149 static HRESULT
Math_atan2(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
150 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
158 if(retv
) num_set_nan(retv
);
162 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v1
);
166 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v2
);
170 if(retv
) num_set_val(retv
, atan2(num_val(&v1
), num_val(&v2
)));
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
)
189 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
194 num_set_val(retv
, ceil(num_val(&v
)));
198 static HRESULT
Math_cos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
199 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
207 if(retv
) num_set_nan(retv
);
211 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
215 if(retv
) num_set_val(retv
, cos(num_val(&v
)));
219 static HRESULT
Math_exp(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
220 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
228 if(retv
) num_set_nan(retv
);
232 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
236 if(retv
) num_set_val(retv
, exp(num_val(&v
)));
240 static HRESULT
Math_floor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
241 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
254 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
259 num_set_val(retv
, floor(num_val(&v
)));
263 static HRESULT
Math_log(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
264 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
277 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
282 num_set_val(retv
, log(num_val(&v
)));
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
)
299 num_set_inf(retv
, FALSE
);
303 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
308 for(i
=1; i
< arg_cnt(dp
); i
++) {
309 hres
= to_number(ctx
, get_arg(dp
, i
), ei
, &v
);
314 if(d
> max
|| isnan(d
))
319 num_set_val(retv
, max
);
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
)
336 num_set_inf(retv
, TRUE
);
340 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
345 for(i
=1; i
< arg_cnt(dp
); i
++) {
346 hres
= to_number(ctx
, get_arg(dp
, i
), ei
, &v
);
351 if(d
< min
|| isnan(d
))
356 num_set_val(retv
, min
);
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
)
369 if(arg_cnt(dp
) < 2) {
370 if(retv
) num_set_nan(retv
);
374 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &x
);
378 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &y
);
383 num_set_val(retv
, pow(num_val(&x
), num_val(&y
)));
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
)
395 if(!RtlGenRandom(&r
, sizeof(r
)))
399 num_set_val(retv
, (DOUBLE
)r
/(DOUBLE
)UINT_MAX
);
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
)
418 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
423 num_set_val(retv
, floor(num_val(&v
)+0.5));
427 static HRESULT
Math_sin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
428 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
436 if(retv
) num_set_nan(retv
);
440 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
444 if(retv
) num_set_val(retv
, sin(num_val(&v
)));
448 static HRESULT
Math_sqrt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
449 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
457 if(retv
) num_set_nan(retv
);
461 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
465 if(retv
) num_set_val(retv
, sqrt(num_val(&v
)));
469 static HRESULT
Math_tan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
470 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
478 if(retv
) num_set_nan(retv
);
482 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
486 if(retv
) num_set_val(retv
, tan(num_val(&v
)));
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
= {
514 sizeof(Math_props
)/sizeof(*Math_props
),
520 HRESULT
create_math(script_ctx_t
*ctx
, DispatchEx
**ret
)
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
));
543 return E_OUTOFMEMORY
;
545 hres
= init_dispex_from_constr(math
, ctx
, &Math_info
, ctx
->object_constr
);
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
);
556 jsdisp_release(math
);