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"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 static const WCHAR EW
[] = {'E',0};
31 static const WCHAR LOG2EW
[] = {'L','O','G','2','E',0};
32 static const WCHAR LOG10EW
[] = {'L','O','G','1','0',0};
33 static const WCHAR LN2W
[] = {'L','N','2',0};
34 static const WCHAR LN10W
[] = {'L','N','1','0',0};
35 static const WCHAR PIW
[] = {'P','I',0};
36 static const WCHAR SQRT2W
[] = {'S','Q','R','T','2',0};
37 static const WCHAR SQRT1_2W
[] = {'S','Q','R','T','1','_','2',0};
38 static const WCHAR absW
[] = {'a','b','s',0};
39 static const WCHAR acosW
[] = {'a','c','o','s',0};
40 static const WCHAR asinW
[] = {'a','s','i','n',0};
41 static const WCHAR atanW
[] = {'a','t','a','n',0};
42 static const WCHAR atan2W
[] = {'a','t','a','n','2',0};
43 static const WCHAR ceilW
[] = {'c','e','i','l',0};
44 static const WCHAR cosW
[] = {'c','o','s',0};
45 static const WCHAR expW
[] = {'e','x','p',0};
46 static const WCHAR floorW
[] = {'f','l','o','o','r',0};
47 static const WCHAR logW
[] = {'l','o','g',0};
48 static const WCHAR maxW
[] = {'m','a','x',0};
49 static const WCHAR minW
[] = {'m','i','n',0};
50 static const WCHAR powW
[] = {'p','o','w',0};
51 static const WCHAR randomW
[] = {'r','a','n','d','o','m',0};
52 static const WCHAR roundW
[] = {'r','o','u','n','d',0};
53 static const WCHAR sinW
[] = {'s','i','n',0};
54 static const WCHAR sqrtW
[] = {'s','q','r','t',0};
55 static const WCHAR tanW
[] = {'t','a','n',0};
57 static HRESULT
Math_E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
58 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
64 static HRESULT
Math_LOG2E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
65 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
71 static HRESULT
Math_LOG10E(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
72 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
78 static HRESULT
Math_LN2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
79 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
85 static HRESULT
Math_LN10(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
86 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
92 static HRESULT
Math_PI(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
93 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
99 static HRESULT
Math_SQRT2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
100 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
106 static HRESULT
Math_SQRT1_2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
107 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
113 /* ECMA-262 3rd Edition 15.8.2.12 */
114 static HRESULT
Math_abs(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
115 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
129 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
135 num_set_val(retv
, d
< 0.0 ? -d
: d
);
139 static HRESULT
Math_acos(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
140 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
146 static HRESULT
Math_asin(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
147 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
153 static HRESULT
Math_atan(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
154 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
160 static HRESULT
Math_atan2(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
161 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
167 /* ECMA-262 3rd Edition 15.8.2.6 */
168 static HRESULT
Math_ceil(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
169 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
177 FIXME("arg_cnt = 0\n");
181 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
186 num_set_val(retv
, ceil(num_val(&v
)));
190 static HRESULT
Math_cos(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
191 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
197 static HRESULT
Math_exp(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
198 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
204 static HRESULT
Math_floor(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
205 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
211 static HRESULT
Math_log(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
212 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
218 /* ECMA-262 3rd Edition 15.8.2.11 */
219 static HRESULT
Math_max(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
220 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
231 num_set_inf(retv
, FALSE
);
235 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
240 for(i
=1; i
< arg_cnt(dp
); i
++) {
241 hres
= to_number(dispex
->ctx
, get_arg(dp
, i
), ei
, &v
);
246 if(d
> max
|| isnan(d
))
251 num_set_val(retv
, max
);
255 /* ECMA-262 3rd Edition 15.8.2.12 */
256 static HRESULT
Math_min(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
257 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
268 num_set_inf(retv
, TRUE
);
272 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
277 for(i
=1; i
< arg_cnt(dp
); i
++) {
278 hres
= to_number(dispex
->ctx
, get_arg(dp
, i
), ei
, &v
);
283 if(d
< min
|| isnan(d
))
288 num_set_val(retv
, min
);
292 /* ECMA-262 3rd Edition 15.8.2.13 */
293 static HRESULT
Math_pow(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
294 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
301 if(arg_cnt(dp
) < 2) {
302 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp
));
306 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &x
);
310 hres
= to_number(dispex
->ctx
, get_arg(dp
, 1), ei
, &y
);
315 num_set_val(retv
, pow(num_val(&x
), num_val(&y
)));
319 static HRESULT
Math_random(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
320 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
326 /* ECMA-262 3rd Edition 15.8.2.15 */
327 static HRESULT
Math_round(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
328 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
336 FIXME("arg_cnt = 0\n");
340 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &v
);
345 num_set_val(retv
, floor(num_val(&v
)+0.5));
349 static HRESULT
Math_sin(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
350 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
356 static HRESULT
Math_sqrt(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
357 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
363 static HRESULT
Math_tan(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
364 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
370 static const builtin_prop_t Math_props
[] = {
372 {LOG2EW
, Math_LOG2E
, 0},
373 {LOG10EW
, Math_LOG10E
, 0},
375 {LN10W
, Math_LN10
, 0},
377 {SQRT1_2W
, Math_SQRT1_2
, 0},
378 {SQRT2W
, Math_SQRT2
, 0},
379 {absW
, Math_abs
, PROPF_METHOD
},
380 {acosW
, Math_acos
, PROPF_METHOD
},
381 {asinW
, Math_asin
, PROPF_METHOD
},
382 {atanW
, Math_atan
, PROPF_METHOD
},
383 {atan2W
, Math_atan2
, PROPF_METHOD
},
384 {ceilW
, Math_ceil
, PROPF_METHOD
},
385 {cosW
, Math_cos
, PROPF_METHOD
},
386 {expW
, Math_exp
, PROPF_METHOD
},
387 {floorW
, Math_floor
, PROPF_METHOD
},
388 {logW
, Math_log
, PROPF_METHOD
},
389 {maxW
, Math_max
, PROPF_METHOD
},
390 {minW
, Math_min
, PROPF_METHOD
},
391 {powW
, Math_pow
, PROPF_METHOD
},
392 {randomW
, Math_random
, PROPF_METHOD
},
393 {roundW
, Math_round
, PROPF_METHOD
},
394 {sinW
, Math_sin
, PROPF_METHOD
},
395 {sqrtW
, Math_sqrt
, PROPF_METHOD
},
396 {tanW
, Math_tan
, PROPF_METHOD
}
399 static const builtin_info_t Math_info
= {
402 sizeof(Math_props
)/sizeof(*Math_props
),
408 HRESULT
create_math(script_ctx_t
*ctx
, DispatchEx
**ret
)
410 return create_dispex(ctx
, &Math_info
, NULL
, ret
);