push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / jscript / math.c
blobe0e88e4ca27411411368ce5eaf1d935be240c976
1 /*
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
19 #include <math.h>
21 #include "jscript.h"
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)
57 FIXME("\n");
58 return E_NOTIMPL;
61 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
62 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
64 FIXME("\n");
65 return E_NOTIMPL;
68 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
69 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
71 FIXME("\n");
72 return E_NOTIMPL;
75 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
78 FIXME("\n");
79 return E_NOTIMPL;
82 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
83 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
85 FIXME("\n");
86 return E_NOTIMPL;
89 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
90 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
92 FIXME("\n");
93 return E_NOTIMPL;
96 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
97 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
99 FIXME("\n");
100 return E_NOTIMPL;
103 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
104 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
106 FIXME("\n");
107 return E_NOTIMPL;
110 /* ECMA-262 3rd Edition 15.8.2.12 */
111 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
112 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
114 VARIANT v;
115 DOUBLE d;
116 HRESULT hres;
118 TRACE("\n");
120 if(!arg_cnt(dp)) {
121 FIXME("arg_cnt = 0\n");
122 return E_NOTIMPL;
125 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
126 if(FAILED(hres))
127 return hres;
129 d = num_val(&v);
130 if(retv)
131 num_set_val(retv, d < 0.0 ? -d : d);
132 return S_OK;
135 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
136 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
138 FIXME("\n");
139 return E_NOTIMPL;
142 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
143 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
145 FIXME("\n");
146 return E_NOTIMPL;
149 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
150 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
152 FIXME("\n");
153 return E_NOTIMPL;
156 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
157 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
159 FIXME("\n");
160 return E_NOTIMPL;
163 /* ECMA-262 3rd Edition 15.8.2.6 */
164 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
165 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
167 VARIANT v;
168 HRESULT hres;
170 TRACE("\n");
172 if(!arg_cnt(dp)) {
173 FIXME("arg_cnt = 0\n");
174 return E_NOTIMPL;
177 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
178 if(FAILED(hres))
179 return hres;
181 if(retv)
182 num_set_val(retv, ceil(num_val(&v)));
183 return S_OK;
186 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
187 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
189 FIXME("\n");
190 return E_NOTIMPL;
193 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
194 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
196 FIXME("\n");
197 return E_NOTIMPL;
200 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
201 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
203 FIXME("\n");
204 return E_NOTIMPL;
207 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
208 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
210 FIXME("\n");
211 return E_NOTIMPL;
214 /* ECMA-262 3rd Edition 15.8.2.11 */
215 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
216 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
218 DOUBLE max, d;
219 VARIANT v;
220 DWORD i;
221 HRESULT hres;
223 TRACE("\n");
225 /* FIXME: Handle NaN */
227 if(!arg_cnt(dp)) {
228 FIXME("arg_cnt = 0\n");
229 return E_NOTIMPL;
232 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
233 if(FAILED(hres))
234 return hres;
236 max = num_val(&v);
237 for(i=1; i < arg_cnt(dp); i++) {
238 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
239 if(FAILED(hres))
240 return hres;
242 d = num_val(&v);
243 if(d > max)
244 max = d;
247 if(retv)
248 num_set_val(retv, max);
249 return S_OK;
252 /* ECMA-262 3rd Edition 15.8.2.12 */
253 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
254 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
256 DOUBLE min, d;
257 VARIANT v;
258 DWORD i;
259 HRESULT hres;
261 TRACE("\n");
263 /* FIXME: Handle NaN */
265 if(!arg_cnt(dp)) {
266 FIXME("arg_cnt = 0\n");
267 return E_NOTIMPL;
270 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
271 if(FAILED(hres))
272 return hres;
274 min = num_val(&v);
275 for(i=1; i < arg_cnt(dp); i++) {
276 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
277 if(FAILED(hres))
278 return hres;
280 d = num_val(&v);
281 if(d < min)
282 min = d;
285 if(retv)
286 num_set_val(retv, min);
287 return S_OK;
290 /* ECMA-262 3rd Edition 15.8.2.13 */
291 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
292 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
294 VARIANT x, y;
295 HRESULT hres;
297 TRACE("\n");
299 if(arg_cnt(dp) < 2) {
300 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
301 return E_NOTIMPL;
304 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
305 if(FAILED(hres))
306 return hres;
308 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
309 if(FAILED(hres))
310 return hres;
312 if(retv)
313 num_set_val(retv, pow(num_val(&x), num_val(&y)));
314 return S_OK;
317 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
318 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
320 FIXME("\n");
321 return E_NOTIMPL;
324 /* ECMA-262 3rd Edition 15.8.2.15 */
325 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
326 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
328 VARIANT v;
329 HRESULT hres;
331 TRACE("\n");
333 if(!arg_cnt(dp)) {
334 FIXME("arg_cnt = 0\n");
335 return E_NOTIMPL;
338 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
339 if(FAILED(hres))
340 return hres;
342 if(retv)
343 num_set_val(retv, floor(num_val(&v)+0.5));
344 return S_OK;
347 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
348 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
350 FIXME("\n");
351 return E_NOTIMPL;
354 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
355 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
357 FIXME("\n");
358 return E_NOTIMPL;
361 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
362 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 FIXME("\n");
365 return E_NOTIMPL;
368 static const builtin_prop_t Math_props[] = {
369 {EW, Math_E, 0},
370 {LOG2EW, Math_LOG2E, 0},
371 {LOG10EW, Math_LOG10E, 0},
372 {LN2W, Math_LN2, 0},
373 {LN10W, Math_LN10, 0},
374 {PIW, Math_PI, 0},
375 {SQRT1_2W, Math_SQRT1_2, 0},
376 {SQRT2W, Math_SQRT2, 0},
377 {absW, Math_abs, PROPF_METHOD},
378 {acosW, Math_acos, PROPF_METHOD},
379 {asinW, Math_asin, PROPF_METHOD},
380 {atanW, Math_atan, PROPF_METHOD},
381 {atan2W, Math_atan2, PROPF_METHOD},
382 {ceilW, Math_ceil, PROPF_METHOD},
383 {cosW, Math_cos, PROPF_METHOD},
384 {expW, Math_exp, PROPF_METHOD},
385 {floorW, Math_floor, PROPF_METHOD},
386 {logW, Math_log, PROPF_METHOD},
387 {maxW, Math_max, PROPF_METHOD},
388 {minW, Math_min, PROPF_METHOD},
389 {powW, Math_pow, PROPF_METHOD},
390 {randomW, Math_random, PROPF_METHOD},
391 {roundW, Math_round, PROPF_METHOD},
392 {sinW, Math_sin, PROPF_METHOD},
393 {sqrtW, Math_sqrt, PROPF_METHOD},
394 {tanW, Math_tan, PROPF_METHOD}
397 static const builtin_info_t Math_info = {
398 JSCLASS_MATH,
399 {NULL, NULL, 0},
400 sizeof(Math_props)/sizeof(*Math_props),
401 Math_props,
402 NULL,
403 NULL
406 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
408 return create_dispex(ctx, &Math_info, NULL, ret);