jscript: Avoid a compiler warning.
[wine] / dlls / jscript / math.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23
24 #include "jscript.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29
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};
56
57 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
58         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
59 {
60     FIXME("\n");
61     return E_NOTIMPL;
62 }
63
64 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
65         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
66 {
67     FIXME("\n");
68     return E_NOTIMPL;
69 }
70
71 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
73 {
74     FIXME("\n");
75     return E_NOTIMPL;
76 }
77
78 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
79         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
80 {
81     FIXME("\n");
82     return E_NOTIMPL;
83 }
84
85 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
86         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 {
88     FIXME("\n");
89     return E_NOTIMPL;
90 }
91
92 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
93         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
94 {
95     FIXME("\n");
96     return E_NOTIMPL;
97 }
98
99 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
100         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 {
102     FIXME("\n");
103     return E_NOTIMPL;
104 }
105
106 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 {
109     FIXME("\n");
110     return E_NOTIMPL;
111 }
112
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)
116 {
117     VARIANT v;
118     DOUBLE d;
119     HRESULT hres;
120
121     TRACE("\n");
122
123     if(!arg_cnt(dp)) {
124         if(retv)
125             num_set_nan(retv);
126         return S_OK;
127     }
128
129     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
130     if(FAILED(hres))
131         return hres;
132
133     d = num_val(&v);
134     if(retv)
135         num_set_val(retv, d < 0.0 ? -d : d);
136     return S_OK;
137 }
138
139 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
140         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
141 {
142     FIXME("\n");
143     return E_NOTIMPL;
144 }
145
146 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
147         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
148 {
149     FIXME("\n");
150     return E_NOTIMPL;
151 }
152
153 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
154         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
155 {
156     FIXME("\n");
157     return E_NOTIMPL;
158 }
159
160 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
162 {
163     FIXME("\n");
164     return E_NOTIMPL;
165 }
166
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)
170 {
171     VARIANT v;
172     HRESULT hres;
173
174     TRACE("\n");
175
176     if(!arg_cnt(dp)) {
177         if(retv)
178             num_set_nan(retv);
179         return S_OK;
180     }
181
182     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
183     if(FAILED(hres))
184         return hres;
185
186     if(retv)
187         num_set_val(retv, ceil(num_val(&v)));
188     return S_OK;
189 }
190
191 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
192         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
193 {
194     FIXME("\n");
195     return E_NOTIMPL;
196 }
197
198 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
199         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
200 {
201     FIXME("\n");
202     return E_NOTIMPL;
203 }
204
205 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
206         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
207 {
208     VARIANT v;
209     HRESULT hres;
210
211     TRACE("\n");
212
213     if(!arg_cnt(dp)) {
214         if(retv)
215             num_set_nan(retv);
216         return S_OK;
217     }
218
219     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
220     if(FAILED(hres))
221         return hres;
222
223     if(retv)
224         num_set_val(retv, floor(num_val(&v)));
225     return S_OK;
226 }
227
228 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
229         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
230 {
231     FIXME("\n");
232     return E_NOTIMPL;
233 }
234
235 /* ECMA-262 3rd Edition    15.8.2.11 */
236 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
237         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
238 {
239     DOUBLE max, d;
240     VARIANT v;
241     DWORD i;
242     HRESULT hres;
243
244     TRACE("\n");
245
246     if(!arg_cnt(dp)) {
247         if(retv)
248             num_set_inf(retv, FALSE);
249         return S_OK;
250     }
251
252     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
253     if(FAILED(hres))
254         return hres;
255
256     max = num_val(&v);
257     for(i=1; i < arg_cnt(dp); i++) {
258         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
259         if(FAILED(hres))
260             return hres;
261
262         d = num_val(&v);
263         if(d > max || isnan(d))
264             max = d;
265     }
266
267     if(retv)
268         num_set_val(retv, max);
269     return S_OK;
270 }
271
272 /* ECMA-262 3rd Edition    15.8.2.12 */
273 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
274         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 {
276     DOUBLE min, d;
277     VARIANT v;
278     DWORD i;
279     HRESULT hres;
280
281     TRACE("\n");
282
283     if(!arg_cnt(dp)) {
284         if(retv)
285             num_set_inf(retv, TRUE);
286         return S_OK;
287     }
288
289     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
290     if(FAILED(hres))
291         return hres;
292
293     min = num_val(&v);
294     for(i=1; i < arg_cnt(dp); i++) {
295         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
296         if(FAILED(hres))
297             return hres;
298
299         d = num_val(&v);
300         if(d < min || isnan(d))
301             min = d;
302     }
303
304     if(retv)
305         num_set_val(retv, min);
306     return S_OK;
307 }
308
309 /* ECMA-262 3rd Edition    15.8.2.13 */
310 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
311         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
312 {
313     VARIANT x, y;
314     HRESULT hres;
315
316     TRACE("\n");
317
318     if(arg_cnt(dp) < 2) {
319         FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
320         return E_NOTIMPL;
321     }
322
323     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
324     if(FAILED(hres))
325         return hres;
326
327     hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
328     if(FAILED(hres))
329         return hres;
330
331     if(retv)
332         num_set_val(retv, pow(num_val(&x), num_val(&y)));
333     return S_OK;
334 }
335
336 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
337         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338 {
339     FIXME("\n");
340     return E_NOTIMPL;
341 }
342
343 /* ECMA-262 3rd Edition    15.8.2.15 */
344 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
345         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
346 {
347     VARIANT v;
348     HRESULT hres;
349
350     TRACE("\n");
351
352     if(!arg_cnt(dp)) {
353         FIXME("arg_cnt = 0\n");
354         return E_NOTIMPL;
355     }
356
357     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
358     if(FAILED(hres))
359         return hres;
360
361     if(retv)
362         num_set_val(retv, floor(num_val(&v)+0.5));
363     return S_OK;
364 }
365
366 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
367         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
368 {
369     FIXME("\n");
370     return E_NOTIMPL;
371 }
372
373 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
374         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 {
376     FIXME("\n");
377     return E_NOTIMPL;
378 }
379
380 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
381         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
382 {
383     FIXME("\n");
384     return E_NOTIMPL;
385 }
386
387 static const builtin_prop_t Math_props[] = {
388     {EW,        Math_E,        0},
389     {LOG2EW,    Math_LOG2E,    0},
390     {LOG10EW,   Math_LOG10E,   0},
391     {LN2W,      Math_LN2,      0},
392     {LN10W,     Math_LN10,     0},
393     {PIW,       Math_PI,       0},
394     {SQRT1_2W,  Math_SQRT1_2,  0},
395     {SQRT2W,    Math_SQRT2,    0},
396     {absW,      Math_abs,      PROPF_METHOD},
397     {acosW,     Math_acos,     PROPF_METHOD},
398     {asinW,     Math_asin,     PROPF_METHOD},
399     {atanW,     Math_atan,     PROPF_METHOD},
400     {atan2W,    Math_atan2,    PROPF_METHOD},
401     {ceilW,     Math_ceil,     PROPF_METHOD},
402     {cosW,      Math_cos,      PROPF_METHOD},
403     {expW,      Math_exp,      PROPF_METHOD},
404     {floorW,    Math_floor,    PROPF_METHOD},
405     {logW,      Math_log,      PROPF_METHOD},
406     {maxW,      Math_max,      PROPF_METHOD},
407     {minW,      Math_min,      PROPF_METHOD},
408     {powW,      Math_pow,      PROPF_METHOD},
409     {randomW,   Math_random,   PROPF_METHOD},
410     {roundW,    Math_round,    PROPF_METHOD},
411     {sinW,      Math_sin,      PROPF_METHOD},
412     {sqrtW,     Math_sqrt,     PROPF_METHOD},
413     {tanW,      Math_tan,      PROPF_METHOD}
414 };
415
416 static const builtin_info_t Math_info = {
417     JSCLASS_MATH,
418     {NULL, NULL, 0},
419     sizeof(Math_props)/sizeof(*Math_props),
420     Math_props,
421     NULL,
422     NULL
423 };
424
425 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
426 {
427     return create_dispex(ctx, &Math_info, NULL, ret);
428 }