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