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