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