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, unsigned argc, jsval_t *argv,
71 *r = jsval_number(NAN);
75 hres = to_number(ctx, argv[0], &d);
80 *r = jsval_number(d < 0.0 ? -d : d);
84 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
94 *r = jsval_number(NAN);
98 hres = to_number(ctx, argv[0], &x);
103 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
107 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
117 *r = jsval_number(NAN);
121 hres = to_number(ctx, argv[0], &x);
126 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
130 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
140 *r = jsval_number(NAN);
144 hres = to_number(ctx, argv[0], &x);
149 *r = jsval_number(atan(x));
153 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
163 *r = jsval_number(NAN);
167 hres = to_number(ctx, argv[0], &y);
171 hres = to_number(ctx, argv[1], &x);
176 *r = jsval_number(atan2(y, x));
180 /* ECMA-262 3rd Edition 15.8.2.6 */
181 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
191 *r = jsval_number(NAN);
195 hres = to_number(ctx, argv[0], &x);
200 *r = jsval_number(ceil(x));
204 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
214 *r = jsval_number(NAN);
218 hres = to_number(ctx, argv[0], &x);
223 *r = jsval_number(cos(x));
227 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
237 *r = jsval_number(NAN);
241 hres = to_number(ctx, argv[0], &x);
246 *r = jsval_number(exp(x));
250 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
260 *r = jsval_number(NAN);
264 hres = to_number(ctx, argv[0], &x);
269 *r = jsval_number(floor(x));
273 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
283 *r = jsval_number(NAN);
287 hres = to_number(ctx, argv[0], &x);
292 *r = jsval_number(x < -0.0 ? NAN : log(x));
296 /* ECMA-262 3rd Edition 15.8.2.11 */
297 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
308 *r = jsval_number(-INFINITY);
312 hres = to_number(ctx, argv[0], &max);
316 for(i=1; i < argc; i++) {
317 hres = to_number(ctx, argv[i], &d);
321 if(d > max || isnan(d))
326 *r = jsval_number(max);
330 /* ECMA-262 3rd Edition 15.8.2.12 */
331 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
342 *r = jsval_number(INFINITY);
346 hres = to_number(ctx, argv[0], &min);
350 for(i=1; i < argc; i++) {
351 hres = to_number(ctx, argv[i], &d);
355 if(d < min || isnan(d))
360 *r = jsval_number(min);
364 /* ECMA-262 3rd Edition 15.8.2.13 */
365 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
375 *r = jsval_number(NAN);
379 hres = to_number(ctx, argv[0], &x);
383 hres = to_number(ctx, argv[1], &y);
388 *r = jsval_number(pow(x, y));
392 /* ECMA-262 3rd Edition 15.8.2.14 */
393 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
400 if(!RtlGenRandom(&x, sizeof(x)))
404 *r = jsval_number((double)x/(double)UINT_MAX);
408 /* ECMA-262 3rd Edition 15.8.2.15 */
409 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
419 *r = jsval_number(NAN);
423 hres = to_number(ctx, argv[0], &x);
428 *r = jsval_number(floor(x+0.5));
432 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
442 *r = jsval_number(NAN);
446 hres = to_number(ctx, argv[0], &x);
451 *r = jsval_number(sin(x));
455 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
465 *r = jsval_number(NAN);
469 hres = to_number(ctx, argv[0], &x);
474 *r = jsval_number(sqrt(x));
478 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
488 *r = jsval_number(NAN);
492 hres = to_number(ctx, argv[0], &x);
497 *r = jsval_number(tan(x));
501 static const builtin_prop_t Math_props[] = {
502 {absW, Math_abs, PROPF_METHOD|1},
503 {acosW, Math_acos, PROPF_METHOD|1},
504 {asinW, Math_asin, PROPF_METHOD|1},
505 {atanW, Math_atan, PROPF_METHOD|1},
506 {atan2W, Math_atan2, PROPF_METHOD|2},
507 {ceilW, Math_ceil, PROPF_METHOD|1},
508 {cosW, Math_cos, PROPF_METHOD|1},
509 {expW, Math_exp, PROPF_METHOD|1},
510 {floorW, Math_floor, PROPF_METHOD|1},
511 {logW, Math_log, PROPF_METHOD|1},
512 {maxW, Math_max, PROPF_METHOD|2},
513 {minW, Math_min, PROPF_METHOD|2},
514 {powW, Math_pow, PROPF_METHOD|2},
515 {randomW, Math_random, PROPF_METHOD},
516 {roundW, Math_round, PROPF_METHOD|1},
517 {sinW, Math_sin, PROPF_METHOD|1},
518 {sqrtW, Math_sqrt, PROPF_METHOD|1},
519 {tanW, Math_tan, PROPF_METHOD|1}
522 static const builtin_info_t Math_info = {
525 sizeof(Math_props)/sizeof(*Math_props),
531 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
541 {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
542 {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
543 {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
544 {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
545 {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
546 {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
547 {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
548 {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
551 math = heap_alloc_zero(sizeof(jsdisp_t));
553 return E_OUTOFMEMORY;
555 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
561 for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
562 hres = jsdisp_propput_const(math, constants[i].name, jsval_number(constants[i].val));
564 jsdisp_release(math);