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