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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 static const WCHAR EW
[] = {'E',0};
32 static const WCHAR LOG2EW
[] = {'L','O','G','2','E',0};
33 static const WCHAR LOG10EW
[] = {'L','O','G','1','0','E',0};
34 static const WCHAR LN2W
[] = {'L','N','2',0};
35 static const WCHAR LN10W
[] = {'L','N','1','0',0};
36 static const WCHAR PIW
[] = {'P','I',0};
37 static const WCHAR SQRT2W
[] = {'S','Q','R','T','2',0};
38 static const WCHAR SQRT1_2W
[] = {'S','Q','R','T','1','_','2',0};
39 static const WCHAR absW
[] = {'a','b','s',0};
40 static const WCHAR acosW
[] = {'a','c','o','s',0};
41 static const WCHAR asinW
[] = {'a','s','i','n',0};
42 static const WCHAR atanW
[] = {'a','t','a','n',0};
43 static const WCHAR atan2W
[] = {'a','t','a','n','2',0};
44 static const WCHAR ceilW
[] = {'c','e','i','l',0};
45 static const WCHAR cosW
[] = {'c','o','s',0};
46 static const WCHAR expW
[] = {'e','x','p',0};
47 static const WCHAR floorW
[] = {'f','l','o','o','r',0};
48 static const WCHAR logW
[] = {'l','o','g',0};
49 static const WCHAR maxW
[] = {'m','a','x',0};
50 static const WCHAR minW
[] = {'m','i','n',0};
51 static const WCHAR powW
[] = {'p','o','w',0};
52 static const WCHAR randomW
[] = {'r','a','n','d','o','m',0};
53 static const WCHAR roundW
[] = {'r','o','u','n','d',0};
54 static const WCHAR sinW
[] = {'s','i','n',0};
55 static const WCHAR sqrtW
[] = {'s','q','r','t',0};
56 static const WCHAR tanW
[] = {'t','a','n',0};
58 /* ECMA-262 3rd Edition 15.8.2.12 */
59 static HRESULT
Math_abs(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
69 *r
= jsval_number(NAN
);
73 hres
= to_number(ctx
, argv
[0], &d
);
78 *r
= jsval_number(d
< 0.0 ? -d
: d
);
82 static HRESULT
Math_acos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
92 *r
= jsval_number(NAN
);
96 hres
= to_number(ctx
, argv
[0], &x
);
101 *r
= jsval_number(x
< -1.0 || x
> 1.0 ? NAN
: acos(x
));
105 static HRESULT
Math_asin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
115 *r
= jsval_number(NAN
);
119 hres
= to_number(ctx
, argv
[0], &x
);
124 *r
= jsval_number(x
< -1.0 || x
> 1.0 ? NAN
: asin(x
));
128 static HRESULT
Math_atan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
138 *r
= jsval_number(NAN
);
142 hres
= to_number(ctx
, argv
[0], &x
);
147 *r
= jsval_number(atan(x
));
151 static HRESULT
Math_atan2(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
161 *r
= jsval_number(NAN
);
165 hres
= to_number(ctx
, argv
[0], &y
);
169 hres
= to_number(ctx
, argv
[1], &x
);
174 *r
= jsval_number(atan2(y
, x
));
178 /* ECMA-262 3rd Edition 15.8.2.6 */
179 static HRESULT
Math_ceil(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
189 *r
= jsval_number(NAN
);
193 hres
= to_number(ctx
, argv
[0], &x
);
198 *r
= jsval_number(ceil(x
));
202 static HRESULT
Math_cos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
212 *r
= jsval_number(NAN
);
216 hres
= to_number(ctx
, argv
[0], &x
);
221 *r
= jsval_number(cos(x
));
225 static HRESULT
Math_exp(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
235 *r
= jsval_number(NAN
);
239 hres
= to_number(ctx
, argv
[0], &x
);
244 *r
= jsval_number(exp(x
));
248 static HRESULT
Math_floor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
258 *r
= jsval_number(NAN
);
262 hres
= to_number(ctx
, argv
[0], &x
);
267 *r
= jsval_number(floor(x
));
271 static HRESULT
Math_log(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
281 *r
= jsval_number(NAN
);
285 hres
= to_number(ctx
, argv
[0], &x
);
290 *r
= jsval_number(x
< -0.0 ? NAN
: log(x
));
294 /* ECMA-262 3rd Edition 15.8.2.11 */
295 static HRESULT
Math_max(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
306 *r
= jsval_number(-INFINITY
);
310 hres
= to_number(ctx
, argv
[0], &max
);
314 for(i
=1; i
< argc
; i
++) {
315 hres
= to_number(ctx
, argv
[i
], &d
);
319 if(d
> max
|| isnan(d
))
324 *r
= jsval_number(max
);
328 /* ECMA-262 3rd Edition 15.8.2.12 */
329 static HRESULT
Math_min(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
340 *r
= jsval_number(INFINITY
);
344 hres
= to_number(ctx
, argv
[0], &min
);
348 for(i
=1; i
< argc
; i
++) {
349 hres
= to_number(ctx
, argv
[i
], &d
);
353 if(d
< min
|| isnan(d
))
358 *r
= jsval_number(min
);
362 /* ECMA-262 3rd Edition 15.8.2.13 */
363 static HRESULT
Math_pow(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
373 *r
= jsval_number(NAN
);
377 hres
= to_number(ctx
, argv
[0], &x
);
381 hres
= to_number(ctx
, argv
[1], &y
);
386 *r
= jsval_number(pow(x
, y
));
390 /* ECMA-262 3rd Edition 15.8.2.14 */
391 static HRESULT
Math_random(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
398 if(!RtlGenRandom(&x
, sizeof(x
)))
402 *r
= jsval_number((double)x
/(double)UINT_MAX
);
406 /* ECMA-262 3rd Edition 15.8.2.15 */
407 static HRESULT
Math_round(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
417 *r
= jsval_number(NAN
);
421 hres
= to_number(ctx
, argv
[0], &x
);
426 *r
= jsval_number(floor(x
+0.5));
430 static HRESULT
Math_sin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
440 *r
= jsval_number(NAN
);
444 hres
= to_number(ctx
, argv
[0], &x
);
449 *r
= jsval_number(sin(x
));
453 static HRESULT
Math_sqrt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
463 *r
= jsval_number(NAN
);
467 hres
= to_number(ctx
, argv
[0], &x
);
472 *r
= jsval_number(sqrt(x
));
476 static HRESULT
Math_tan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
486 *r
= jsval_number(NAN
);
490 hres
= to_number(ctx
, argv
[0], &x
);
495 *r
= jsval_number(tan(x
));
499 static const builtin_prop_t Math_props
[] = {
500 {absW
, Math_abs
, PROPF_METHOD
|1},
501 {acosW
, Math_acos
, PROPF_METHOD
|1},
502 {asinW
, Math_asin
, PROPF_METHOD
|1},
503 {atanW
, Math_atan
, PROPF_METHOD
|1},
504 {atan2W
, Math_atan2
, PROPF_METHOD
|2},
505 {ceilW
, Math_ceil
, PROPF_METHOD
|1},
506 {cosW
, Math_cos
, PROPF_METHOD
|1},
507 {expW
, Math_exp
, PROPF_METHOD
|1},
508 {floorW
, Math_floor
, PROPF_METHOD
|1},
509 {logW
, Math_log
, PROPF_METHOD
|1},
510 {maxW
, Math_max
, PROPF_METHOD
|2},
511 {minW
, Math_min
, PROPF_METHOD
|2},
512 {powW
, Math_pow
, PROPF_METHOD
|2},
513 {randomW
, Math_random
, PROPF_METHOD
},
514 {roundW
, Math_round
, PROPF_METHOD
|1},
515 {sinW
, Math_sin
, PROPF_METHOD
|1},
516 {sqrtW
, Math_sqrt
, PROPF_METHOD
|1},
517 {tanW
, Math_tan
, PROPF_METHOD
|1}
520 static const builtin_info_t Math_info
= {
523 ARRAY_SIZE(Math_props
),
529 HRESULT
create_math(script_ctx_t
*ctx
, jsdisp_t
**ret
)
539 {EW
, M_E
}, /* ECMA-262 3rd Edition 15.8.1.1 */
540 {LN10W
, M_LN10
}, /* ECMA-262 3rd Edition 15.8.1.2 */
541 {LN2W
, M_LN2
}, /* ECMA-262 3rd Edition 15.8.1.3 */
542 {LOG2EW
, M_LOG2E
}, /* ECMA-262 3rd Edition 15.8.1.4 */
543 {LOG10EW
, M_LOG10E
}, /* ECMA-262 3rd Edition 15.8.1.5 */
544 {PIW
, M_PI
}, /* ECMA-262 3rd Edition 15.8.1.6 */
545 {SQRT1_2W
, M_SQRT1_2
}, /* ECMA-262 3rd Edition 15.8.1.7 */
546 {SQRT2W
, M_SQRT2
}, /* ECMA-262 3rd Edition 15.8.1.8 */
549 math
= heap_alloc_zero(sizeof(jsdisp_t
));
551 return E_OUTOFMEMORY
;
553 hres
= init_dispex_from_constr(math
, ctx
, &Math_info
, ctx
->object_constr
);
559 for(i
=0; i
< ARRAY_SIZE(constants
); i
++) {
560 hres
= jsdisp_define_data_property(math
, constants
[i
].name
, 0,
561 jsval_number(constants
[i
].val
));
563 jsdisp_release(math
);