2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 static const WCHAR EW[] = {'E',0};
33 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
34 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
35 static const WCHAR LN2W[] = {'L','N','2',0};
36 static const WCHAR LN10W[] = {'L','N','1','0',0};
37 static const WCHAR PIW[] = {'P','I',0};
38 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
39 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
40 static const WCHAR absW[] = {'a','b','s',0};
41 static const WCHAR acosW[] = {'a','c','o','s',0};
42 static const WCHAR asinW[] = {'a','s','i','n',0};
43 static const WCHAR atanW[] = {'a','t','a','n',0};
44 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
45 static const WCHAR ceilW[] = {'c','e','i','l',0};
46 static const WCHAR cosW[] = {'c','o','s',0};
47 static const WCHAR expW[] = {'e','x','p',0};
48 static const WCHAR floorW[] = {'f','l','o','o','r',0};
49 static const WCHAR logW[] = {'l','o','g',0};
50 static const WCHAR maxW[] = {'m','a','x',0};
51 static const WCHAR minW[] = {'m','i','n',0};
52 static const WCHAR powW[] = {'p','o','w',0};
53 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
54 static const WCHAR roundW[] = {'r','o','u','n','d',0};
55 static const WCHAR sinW[] = {'s','i','n',0};
56 static const WCHAR sqrtW[] = {'s','q','r','t',0};
57 static const WCHAR tanW[] = {'t','a','n',0};
59 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
62 case DISPATCH_PROPERTYGET:
66 case DISPATCH_PROPERTYPUT:
70 FIXME("unhandled flags %x\n", flags);
74 /* ECMA-262 3rd Edition 15.8.1.1 */
75 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
79 return math_constant(M_E, flags, retv);
82 /* ECMA-262 3rd Edition 15.8.1.4 */
83 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
84 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 return math_constant(M_LOG2E, flags, retv);
90 /* ECMA-262 3rd Edition 15.8.1.4 */
91 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
92 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
95 return math_constant(M_LOG10E, flags, retv);
98 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
102 return math_constant(M_LN2, flags, retv);
105 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 return math_constant(M_LN10, flags, retv);
112 /* ECMA-262 3rd Edition 15.8.1.6 */
113 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 return math_constant(M_PI, flags, retv);
120 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
124 return math_constant(M_SQRT2, flags, retv);
127 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
131 return math_constant(M_SQRT1_2, flags, retv);
134 /* ECMA-262 3rd Edition 15.8.2.12 */
135 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
136 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
150 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
156 num_set_val(retv, d < 0.0 ? -d : d);
160 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
169 if(retv) num_set_nan(retv);
173 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
177 if(retv) num_set_val(retv, acos(num_val(&v)));
181 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
188 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
195 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
196 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
202 /* ECMA-262 3rd Edition 15.8.2.6 */
203 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
217 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
222 num_set_val(retv, ceil(num_val(&v)));
226 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
235 if(retv) num_set_nan(retv);
239 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
243 if(retv) num_set_val(retv, cos(num_val(&v)));
247 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
254 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
255 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
268 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
273 num_set_val(retv, floor(num_val(&v)));
277 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
278 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
284 /* ECMA-262 3rd Edition 15.8.2.11 */
285 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
286 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 num_set_inf(retv, FALSE);
301 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
306 for(i=1; i < arg_cnt(dp); i++) {
307 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
312 if(d > max || isnan(d))
317 num_set_val(retv, max);
321 /* ECMA-262 3rd Edition 15.8.2.12 */
322 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
323 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
334 num_set_inf(retv, TRUE);
338 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
343 for(i=1; i < arg_cnt(dp); i++) {
344 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
349 if(d < min || isnan(d))
354 num_set_val(retv, min);
358 /* ECMA-262 3rd Edition 15.8.2.13 */
359 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
360 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
367 if(arg_cnt(dp) < 2) {
368 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
372 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
376 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
381 num_set_val(retv, pow(num_val(&x), num_val(&y)));
385 /* ECMA-262 3rd Edition 15.8.2.14 */
386 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
387 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
393 if(!RtlGenRandom(&r, sizeof(r)))
397 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
402 /* ECMA-262 3rd Edition 15.8.2.15 */
403 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
404 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
416 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
421 num_set_val(retv, floor(num_val(&v)+0.5));
425 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
426 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
432 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
433 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
439 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
440 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
446 static const builtin_prop_t Math_props[] = {
448 {LN10W, Math_LN10, 0},
450 {LOG10EW, Math_LOG10E, 0},
451 {LOG2EW, Math_LOG2E, 0},
453 {SQRT1_2W, Math_SQRT1_2, 0},
454 {SQRT2W, Math_SQRT2, 0},
455 {absW, Math_abs, PROPF_METHOD},
456 {acosW, Math_acos, PROPF_METHOD},
457 {asinW, Math_asin, PROPF_METHOD},
458 {atanW, Math_atan, PROPF_METHOD},
459 {atan2W, Math_atan2, PROPF_METHOD},
460 {ceilW, Math_ceil, PROPF_METHOD},
461 {cosW, Math_cos, PROPF_METHOD},
462 {expW, Math_exp, PROPF_METHOD},
463 {floorW, Math_floor, PROPF_METHOD},
464 {logW, Math_log, PROPF_METHOD},
465 {maxW, Math_max, PROPF_METHOD},
466 {minW, Math_min, PROPF_METHOD},
467 {powW, Math_pow, PROPF_METHOD},
468 {randomW, Math_random, PROPF_METHOD},
469 {roundW, Math_round, PROPF_METHOD},
470 {sinW, Math_sin, PROPF_METHOD},
471 {sqrtW, Math_sqrt, PROPF_METHOD},
472 {tanW, Math_tan, PROPF_METHOD}
475 static const builtin_info_t Math_info = {
478 sizeof(Math_props)/sizeof(*Math_props),
484 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
486 return create_dispex(ctx, &Math_info, NULL, ret);