jscript: Added NaN handling to Math.min and Math.max.
[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         FIXME("arg_cnt = 0\n");
125         return E_NOTIMPL;
126     }
127
128     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
129     if(FAILED(hres))
130         return hres;
131
132     d = num_val(&v);
133     if(retv)
134         num_set_val(retv, d < 0.0 ? -d : d);
135     return S_OK;
136 }
137
138 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
139         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
140 {
141     FIXME("\n");
142     return E_NOTIMPL;
143 }
144
145 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
146         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
147 {
148     FIXME("\n");
149     return E_NOTIMPL;
150 }
151
152 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
153         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
154 {
155     FIXME("\n");
156     return E_NOTIMPL;
157 }
158
159 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
160         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
161 {
162     FIXME("\n");
163     return E_NOTIMPL;
164 }
165
166 /* ECMA-262 3rd Edition    15.8.2.6 */
167 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
168         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
169 {
170     VARIANT v;
171     HRESULT hres;
172
173     TRACE("\n");
174
175     if(!arg_cnt(dp)) {
176         FIXME("arg_cnt = 0\n");
177         return E_NOTIMPL;
178     }
179
180     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
181     if(FAILED(hres))
182         return hres;
183
184     if(retv)
185         num_set_val(retv, ceil(num_val(&v)));
186     return S_OK;
187 }
188
189 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
190         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
191 {
192     FIXME("\n");
193     return E_NOTIMPL;
194 }
195
196 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
197         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 {
199     FIXME("\n");
200     return E_NOTIMPL;
201 }
202
203 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
204         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 {
206     FIXME("\n");
207     return E_NOTIMPL;
208 }
209
210 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
211         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
212 {
213     FIXME("\n");
214     return E_NOTIMPL;
215 }
216
217 /* ECMA-262 3rd Edition    15.8.2.11 */
218 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
219         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
220 {
221     DOUBLE max, d;
222     VARIANT v;
223     DWORD i;
224     HRESULT hres;
225
226     TRACE("\n");
227
228     if(!arg_cnt(dp)) {
229         if(retv)
230             num_set_inf(retv, FALSE);
231         return S_OK;
232     }
233
234     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
235     if(FAILED(hres))
236         return hres;
237
238     max = num_val(&v);
239     for(i=1; i < arg_cnt(dp); i++) {
240         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
241         if(FAILED(hres))
242             return hres;
243
244         d = num_val(&v);
245         if(d > max || isnan(d))
246             max = d;
247     }
248
249     if(retv)
250         num_set_val(retv, max);
251     return S_OK;
252 }
253
254 /* ECMA-262 3rd Edition    15.8.2.12 */
255 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
256         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
257 {
258     DOUBLE min, d;
259     VARIANT v;
260     DWORD i;
261     HRESULT hres;
262
263     TRACE("\n");
264
265     if(!arg_cnt(dp)) {
266         if(retv)
267             num_set_inf(retv, TRUE);
268         return S_OK;
269     }
270
271     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
272     if(FAILED(hres))
273         return hres;
274
275     min = num_val(&v);
276     for(i=1; i < arg_cnt(dp); i++) {
277         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
278         if(FAILED(hres))
279             return hres;
280
281         d = num_val(&v);
282         if(d < min || isnan(d))
283             min = d;
284     }
285
286     if(retv)
287         num_set_val(retv, min);
288     return S_OK;
289 }
290
291 /* ECMA-262 3rd Edition    15.8.2.13 */
292 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
293         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
294 {
295     VARIANT x, y;
296     HRESULT hres;
297
298     TRACE("\n");
299
300     if(arg_cnt(dp) < 2) {
301         FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
302         return E_NOTIMPL;
303     }
304
305     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
306     if(FAILED(hres))
307         return hres;
308
309     hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
310     if(FAILED(hres))
311         return hres;
312
313     if(retv)
314         num_set_val(retv, pow(num_val(&x), num_val(&y)));
315     return S_OK;
316 }
317
318 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
319         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
320 {
321     FIXME("\n");
322     return E_NOTIMPL;
323 }
324
325 /* ECMA-262 3rd Edition    15.8.2.15 */
326 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
327         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
328 {
329     VARIANT v;
330     HRESULT hres;
331
332     TRACE("\n");
333
334     if(!arg_cnt(dp)) {
335         FIXME("arg_cnt = 0\n");
336         return E_NOTIMPL;
337     }
338
339     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
340     if(FAILED(hres))
341         return hres;
342
343     if(retv)
344         num_set_val(retv, floor(num_val(&v)+0.5));
345     return S_OK;
346 }
347
348 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
349         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
350 {
351     FIXME("\n");
352     return E_NOTIMPL;
353 }
354
355 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
356         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
357 {
358     FIXME("\n");
359     return E_NOTIMPL;
360 }
361
362 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
363         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 {
365     FIXME("\n");
366     return E_NOTIMPL;
367 }
368
369 static const builtin_prop_t Math_props[] = {
370     {EW,        Math_E,        0},
371     {LOG2EW,    Math_LOG2E,    0},
372     {LOG10EW,   Math_LOG10E,   0},
373     {LN2W,      Math_LN2,      0},
374     {LN10W,     Math_LN10,     0},
375     {PIW,       Math_PI,       0},
376     {SQRT1_2W,  Math_SQRT1_2,  0},
377     {SQRT2W,    Math_SQRT2,    0},
378     {absW,      Math_abs,      PROPF_METHOD},
379     {acosW,     Math_acos,     PROPF_METHOD},
380     {asinW,     Math_asin,     PROPF_METHOD},
381     {atanW,     Math_atan,     PROPF_METHOD},
382     {atan2W,    Math_atan2,    PROPF_METHOD},
383     {ceilW,     Math_ceil,     PROPF_METHOD},
384     {cosW,      Math_cos,      PROPF_METHOD},
385     {expW,      Math_exp,      PROPF_METHOD},
386     {floorW,    Math_floor,    PROPF_METHOD},
387     {logW,      Math_log,      PROPF_METHOD},
388     {maxW,      Math_max,      PROPF_METHOD},
389     {minW,      Math_min,      PROPF_METHOD},
390     {powW,      Math_pow,      PROPF_METHOD},
391     {randomW,   Math_random,   PROPF_METHOD},
392     {roundW,    Math_round,    PROPF_METHOD},
393     {sinW,      Math_sin,      PROPF_METHOD},
394     {sqrtW,     Math_sqrt,     PROPF_METHOD},
395     {tanW,      Math_tan,      PROPF_METHOD}
396 };
397
398 static const builtin_info_t Math_info = {
399     JSCLASS_MATH,
400     {NULL, NULL, 0},
401     sizeof(Math_props)/sizeof(*Math_props),
402     Math_props,
403     NULL,
404     NULL
405 };
406
407 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
408 {
409     return create_dispex(ctx, &Math_info, NULL, ret);
410 }