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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 static const WCHAR EW[] = {'E',0};
28 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
29 static const WCHAR LOG10EW[] = {'L','O','G','1','0',0};
30 static const WCHAR LN2W[] = {'L','N','2',0};
31 static const WCHAR LN10W[] = {'L','N','1','0',0};
32 static const WCHAR PIW[] = {'P','I',0};
33 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
34 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
35 static const WCHAR absW[] = {'a','b','s',0};
36 static const WCHAR acosW[] = {'a','c','o','s',0};
37 static const WCHAR asinW[] = {'a','s','i','n',0};
38 static const WCHAR atanW[] = {'a','t','a','n',0};
39 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
40 static const WCHAR ceilW[] = {'c','e','i','l',0};
41 static const WCHAR cosW[] = {'c','o','s',0};
42 static const WCHAR expW[] = {'e','x','p',0};
43 static const WCHAR floorW[] = {'f','l','o','o','r',0};
44 static const WCHAR logW[] = {'l','o','g',0};
45 static const WCHAR maxW[] = {'m','a','x',0};
46 static const WCHAR minW[] = {'m','i','n',0};
47 static const WCHAR powW[] = {'p','o','w',0};
48 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
49 static const WCHAR roundW[] = {'r','o','u','n','d',0};
50 static const WCHAR sinW[] = {'s','i','n',0};
51 static const WCHAR sqrtW[] = {'s','q','r','t',0};
52 static const WCHAR tanW[] = {'t','a','n',0};
54 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
55 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
61 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
62 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
68 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
69 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
75 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
82 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
83 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
89 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
90 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
96 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
97 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
103 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
104 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
110 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
111 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
124 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
125 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
131 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
132 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
138 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
139 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
145 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
146 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
152 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
153 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
159 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
160 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
166 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
167 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
173 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
174 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
180 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
181 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
187 /* ECMA-262 3rd Edition 15.8.2.12 */
188 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 /* FIXME: Handle NaN */
201 FIXME("arg_cnt = 0\n");
205 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
210 for(i=1; i < arg_cnt(dp); i++) {
211 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
221 num_set_val(retv, min);
225 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
232 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
233 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
239 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
240 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
246 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
247 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
253 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
254 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
260 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
267 static const builtin_prop_t Math_props[] = {
269 {LOG2EW, Math_LOG2E, 0},
270 {LOG10EW, Math_LOG10E, 0},
272 {LN10W, Math_LN10, 0},
274 {SQRT1_2W, Math_SQRT1_2, 0},
275 {SQRT2W, Math_SQRT2, 0},
276 {absW, Math_abs, PROPF_METHOD},
277 {acosW, Math_acos, PROPF_METHOD},
278 {asinW, Math_asin, PROPF_METHOD},
279 {atanW, Math_atan, PROPF_METHOD},
280 {atan2W, Math_atan2, PROPF_METHOD},
281 {ceilW, Math_ceil, PROPF_METHOD},
282 {cosW, Math_cos, PROPF_METHOD},
283 {expW, Math_exp, PROPF_METHOD},
284 {floorW, Math_floor, PROPF_METHOD},
285 {logW, Math_log, PROPF_METHOD},
286 {maxW, Math_max, PROPF_METHOD},
287 {minW, Math_min, PROPF_METHOD},
288 {powW, Math_pow, PROPF_METHOD},
289 {randomW, Math_random, PROPF_METHOD},
290 {roundW, Math_round, PROPF_METHOD},
291 {sinW, Math_sin, PROPF_METHOD},
292 {sqrtW, Math_sqrt, PROPF_METHOD},
293 {tanW, Math_tan, PROPF_METHOD}
296 static const builtin_info_t Math_info = {
299 sizeof(Math_props)/sizeof(*Math_props),
305 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
307 return create_dispex(ctx, &Math_info, NULL, ret);