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
24 static const WCHAR EW
[] = {'E',0};
25 static const WCHAR LOG2EW
[] = {'L','O','G','2','E',0};
26 static const WCHAR LOG10EW
[] = {'L','O','G','1','0','E',0};
27 static const WCHAR LN2W
[] = {'L','N','2',0};
28 static const WCHAR LN10W
[] = {'L','N','1','0',0};
29 static const WCHAR PIW
[] = {'P','I',0};
30 static const WCHAR SQRT2W
[] = {'S','Q','R','T','2',0};
31 static const WCHAR SQRT1_2W
[] = {'S','Q','R','T','1','_','2',0};
32 static const WCHAR absW
[] = {'a','b','s',0};
33 static const WCHAR acosW
[] = {'a','c','o','s',0};
34 static const WCHAR asinW
[] = {'a','s','i','n',0};
35 static const WCHAR atanW
[] = {'a','t','a','n',0};
36 static const WCHAR atan2W
[] = {'a','t','a','n','2',0};
37 static const WCHAR ceilW
[] = {'c','e','i','l',0};
38 static const WCHAR cosW
[] = {'c','o','s',0};
39 static const WCHAR expW
[] = {'e','x','p',0};
40 static const WCHAR floorW
[] = {'f','l','o','o','r',0};
41 static const WCHAR logW
[] = {'l','o','g',0};
42 static const WCHAR maxW
[] = {'m','a','x',0};
43 static const WCHAR minW
[] = {'m','i','n',0};
44 static const WCHAR powW
[] = {'p','o','w',0};
45 static const WCHAR randomW
[] = {'r','a','n','d','o','m',0};
46 static const WCHAR roundW
[] = {'r','o','u','n','d',0};
47 static const WCHAR sinW
[] = {'s','i','n',0};
48 static const WCHAR sqrtW
[] = {'s','q','r','t',0};
49 static const WCHAR tanW
[] = {'t','a','n',0};
51 /* ECMA-262 3rd Edition 15.8.2.12 */
52 static HRESULT
Math_abs(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
62 *r
= jsval_number(NAN
);
66 hres
= to_number(ctx
, argv
[0], &d
);
71 *r
= jsval_number(d
< 0.0 ? -d
: d
);
75 static HRESULT
Math_acos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
85 *r
= jsval_number(NAN
);
89 hres
= to_number(ctx
, argv
[0], &x
);
94 *r
= jsval_number(x
< -1.0 || x
> 1.0 ? NAN
: acos(x
));
98 static HRESULT
Math_asin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
108 *r
= jsval_number(NAN
);
112 hres
= to_number(ctx
, argv
[0], &x
);
117 *r
= jsval_number(x
< -1.0 || x
> 1.0 ? NAN
: asin(x
));
121 static HRESULT
Math_atan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
131 *r
= jsval_number(NAN
);
135 hres
= to_number(ctx
, argv
[0], &x
);
140 *r
= jsval_number(atan(x
));
144 static HRESULT
Math_atan2(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
154 *r
= jsval_number(NAN
);
158 hres
= to_number(ctx
, argv
[0], &y
);
162 hres
= to_number(ctx
, argv
[1], &x
);
167 *r
= jsval_number(atan2(y
, x
));
171 /* ECMA-262 3rd Edition 15.8.2.6 */
172 static HRESULT
Math_ceil(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
182 *r
= jsval_number(NAN
);
186 hres
= to_number(ctx
, argv
[0], &x
);
191 *r
= jsval_number(ceil(x
));
195 static HRESULT
Math_cos(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
205 *r
= jsval_number(NAN
);
209 hres
= to_number(ctx
, argv
[0], &x
);
214 *r
= jsval_number(cos(x
));
218 static HRESULT
Math_exp(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
228 *r
= jsval_number(NAN
);
232 hres
= to_number(ctx
, argv
[0], &x
);
237 *r
= jsval_number(exp(x
));
241 static HRESULT
Math_floor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
251 *r
= jsval_number(NAN
);
255 hres
= to_number(ctx
, argv
[0], &x
);
260 *r
= jsval_number(floor(x
));
264 static HRESULT
Math_log(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
274 *r
= jsval_number(NAN
);
278 hres
= to_number(ctx
, argv
[0], &x
);
283 *r
= jsval_number(x
< -0.0 ? NAN
: log(x
));
287 /* ECMA-262 3rd Edition 15.8.2.11 */
288 static HRESULT
Math_max(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
299 *r
= jsval_number(-INFINITY
);
303 hres
= to_number(ctx
, argv
[0], &max
);
307 for(i
=1; i
< argc
; i
++) {
308 hres
= to_number(ctx
, argv
[i
], &d
);
312 if(d
> max
|| isnan(d
))
317 *r
= jsval_number(max
);
321 /* ECMA-262 3rd Edition 15.8.2.12 */
322 static HRESULT
Math_min(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
333 *r
= jsval_number(INFINITY
);
337 hres
= to_number(ctx
, argv
[0], &min
);
341 for(i
=1; i
< argc
; i
++) {
342 hres
= to_number(ctx
, argv
[i
], &d
);
346 if(d
< min
|| isnan(d
))
351 *r
= jsval_number(min
);
355 /* ECMA-262 3rd Edition 15.8.2.13 */
356 static HRESULT
Math_pow(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
366 *r
= jsval_number(NAN
);
370 hres
= to_number(ctx
, argv
[0], &x
);
374 hres
= to_number(ctx
, argv
[1], &y
);
379 *r
= jsval_number(pow(x
, y
));
383 /* ECMA-262 3rd Edition 15.8.2.14 */
384 static HRESULT
Math_random(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
391 if(!RtlGenRandom(&x
, sizeof(x
)))
395 *r
= jsval_number((double)x
/(double)UINT_MAX
);
399 /* ECMA-262 3rd Edition 15.8.2.15 */
400 static HRESULT
Math_round(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
410 *r
= jsval_number(NAN
);
414 hres
= to_number(ctx
, argv
[0], &x
);
419 *r
= jsval_number(floor(x
+0.5));
423 static HRESULT
Math_sin(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
433 *r
= jsval_number(NAN
);
437 hres
= to_number(ctx
, argv
[0], &x
);
442 *r
= jsval_number(sin(x
));
446 static HRESULT
Math_sqrt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
456 *r
= jsval_number(NAN
);
460 hres
= to_number(ctx
, argv
[0], &x
);
465 *r
= jsval_number(sqrt(x
));
469 static HRESULT
Math_tan(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
479 *r
= jsval_number(NAN
);
483 hres
= to_number(ctx
, argv
[0], &x
);
488 *r
= jsval_number(tan(x
));
492 static const builtin_prop_t Math_props
[] = {
493 {absW
, Math_abs
, PROPF_METHOD
|1},
494 {acosW
, Math_acos
, PROPF_METHOD
|1},
495 {asinW
, Math_asin
, PROPF_METHOD
|1},
496 {atanW
, Math_atan
, PROPF_METHOD
|1},
497 {atan2W
, Math_atan2
, PROPF_METHOD
|2},
498 {ceilW
, Math_ceil
, PROPF_METHOD
|1},
499 {cosW
, Math_cos
, PROPF_METHOD
|1},
500 {expW
, Math_exp
, PROPF_METHOD
|1},
501 {floorW
, Math_floor
, PROPF_METHOD
|1},
502 {logW
, Math_log
, PROPF_METHOD
|1},
503 {maxW
, Math_max
, PROPF_METHOD
|2},
504 {minW
, Math_min
, PROPF_METHOD
|2},
505 {powW
, Math_pow
, PROPF_METHOD
|2},
506 {randomW
, Math_random
, PROPF_METHOD
},
507 {roundW
, Math_round
, PROPF_METHOD
|1},
508 {sinW
, Math_sin
, PROPF_METHOD
|1},
509 {sqrtW
, Math_sqrt
, PROPF_METHOD
|1},
510 {tanW
, Math_tan
, PROPF_METHOD
|1}
513 static const builtin_info_t Math_info
= {
516 sizeof(Math_props
)/sizeof(*Math_props
),
522 HRESULT
create_math(script_ctx_t
*ctx
, jsdisp_t
**ret
)
532 {EW
, M_E
}, /* ECMA-262 3rd Edition 15.8.1.1 */
533 {LN10W
, M_LN10
}, /* ECMA-262 3rd Edition 15.8.1.2 */
534 {LN2W
, M_LN2
}, /* ECMA-262 3rd Edition 15.8.1.3 */
535 {LOG2EW
, M_LOG2E
}, /* ECMA-262 3rd Edition 15.8.1.4 */
536 {LOG10EW
, M_LOG10E
}, /* ECMA-262 3rd Edition 15.8.1.5 */
537 {PIW
, M_PI
}, /* ECMA-262 3rd Edition 15.8.1.6 */
538 {SQRT1_2W
, M_SQRT1_2
}, /* ECMA-262 3rd Edition 15.8.1.7 */
539 {SQRT2W
, M_SQRT2
}, /* ECMA-262 3rd Edition 15.8.1.8 */
542 math
= heap_alloc_zero(sizeof(jsdisp_t
));
544 return E_OUTOFMEMORY
;
546 hres
= init_dispex_from_constr(math
, ctx
, &Math_info
, ctx
->object_constr
);
552 for(i
=0; i
< sizeof(constants
)/sizeof(*constants
); i
++) {
553 hres
= jsdisp_propput_const(math
, constants
[i
].name
, jsval_number(constants
[i
].val
));
555 jsdisp_release(math
);