jscript: Added implementation of Math_asin.
[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 #include <limits.h>
24
25 #include "jscript.h"
26 #include "ntsecapi.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31
32 static const WCHAR EW[] = {'E',0};
33 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
34 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
35 static const WCHAR LN2W[] = {'L','N','2',0};
36 static const WCHAR LN10W[] = {'L','N','1','0',0};
37 static const WCHAR PIW[] = {'P','I',0};
38 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
39 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
40 static const WCHAR absW[] = {'a','b','s',0};
41 static const WCHAR acosW[] = {'a','c','o','s',0};
42 static const WCHAR asinW[] = {'a','s','i','n',0};
43 static const WCHAR atanW[] = {'a','t','a','n',0};
44 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
45 static const WCHAR ceilW[] = {'c','e','i','l',0};
46 static const WCHAR cosW[] = {'c','o','s',0};
47 static const WCHAR expW[] = {'e','x','p',0};
48 static const WCHAR floorW[] = {'f','l','o','o','r',0};
49 static const WCHAR logW[] = {'l','o','g',0};
50 static const WCHAR maxW[] = {'m','a','x',0};
51 static const WCHAR minW[] = {'m','i','n',0};
52 static const WCHAR powW[] = {'p','o','w',0};
53 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
54 static const WCHAR roundW[] = {'r','o','u','n','d',0};
55 static const WCHAR sinW[] = {'s','i','n',0};
56 static const WCHAR sqrtW[] = {'s','q','r','t',0};
57 static const WCHAR tanW[] = {'t','a','n',0};
58
59 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
60 {
61     switch(flags) {
62     case DISPATCH_PROPERTYGET:
63         V_VT(retv) = VT_R8;
64         V_R8(retv) = val;
65         return S_OK;
66     case DISPATCH_PROPERTYPUT:
67         return S_OK;
68     }
69
70     FIXME("unhandled flags %x\n", flags);
71     return E_NOTIMPL;
72 }
73
74 /* ECMA-262 3rd Edition    15.8.1.1 */
75 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
77 {
78     TRACE("\n");
79     return math_constant(M_E, flags, retv);
80 }
81
82 /* ECMA-262 3rd Edition    15.8.1.4 */
83 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
84         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
85 {
86     TRACE("\n");
87     return math_constant(M_LOG2E, flags, retv);
88 }
89
90 /* ECMA-262 3rd Edition    15.8.1.4 */
91 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
92         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
93 {
94     TRACE("\n");
95     return math_constant(M_LOG10E, flags, retv);
96 }
97
98 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
100 {
101     TRACE("\n");
102     return math_constant(M_LN2, flags, retv);
103 }
104
105 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
107 {
108     TRACE("\n");
109     return math_constant(M_LN10, flags, retv);
110 }
111
112 /* ECMA-262 3rd Edition    15.8.1.6 */
113 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
115 {
116     TRACE("\n");
117     return math_constant(M_PI, flags, retv);
118 }
119
120 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 {
123     TRACE("\n");
124     return math_constant(M_SQRT2, flags, retv);
125 }
126
127 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
129 {
130     TRACE("\n");
131     return math_constant(M_SQRT1_2, flags, retv);
132 }
133
134 /* ECMA-262 3rd Edition    15.8.2.12 */
135 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
136         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
137 {
138     VARIANT v;
139     DOUBLE d;
140     HRESULT hres;
141
142     TRACE("\n");
143
144     if(!arg_cnt(dp)) {
145         if(retv)
146             num_set_nan(retv);
147         return S_OK;
148     }
149
150     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
151     if(FAILED(hres))
152         return hres;
153
154     d = num_val(&v);
155     if(retv)
156         num_set_val(retv, d < 0.0 ? -d : d);
157     return S_OK;
158 }
159
160 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
162 {
163     VARIANT v;
164     HRESULT hres;
165
166     TRACE("\n");
167
168     if(!arg_cnt(dp)) {
169         if(retv) num_set_nan(retv);
170         return S_OK;
171     }
172
173     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
174     if(FAILED(hres))
175         return hres;
176
177     if(retv) num_set_val(retv, acos(num_val(&v)));
178     return S_OK;
179 }
180
181 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
183 {
184     VARIANT v;
185     HRESULT hres;
186
187     TRACE("\n");
188
189     if(!arg_cnt(dp)) {
190         if(retv) num_set_nan(retv);
191         return S_OK;
192     }
193
194     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
195     if(FAILED(hres))
196         return hres;
197
198     if(retv) num_set_val(retv, asin(num_val(&v)));
199     return S_OK;
200 }
201
202 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
203         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
204 {
205     FIXME("\n");
206     return E_NOTIMPL;
207 }
208
209 static HRESULT Math_atan2(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 /* ECMA-262 3rd Edition    15.8.2.6 */
217 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
219 {
220     VARIANT v;
221     HRESULT hres;
222
223     TRACE("\n");
224
225     if(!arg_cnt(dp)) {
226         if(retv)
227             num_set_nan(retv);
228         return S_OK;
229     }
230
231     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
232     if(FAILED(hres))
233         return hres;
234
235     if(retv)
236         num_set_val(retv, ceil(num_val(&v)));
237     return S_OK;
238 }
239
240 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
241         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
242 {
243     VARIANT v;
244     HRESULT hres;
245
246     TRACE("\n");
247
248     if(!arg_cnt(dp)) {
249         if(retv) num_set_nan(retv);
250         return S_OK;
251     }
252
253     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
254     if(FAILED(hres))
255         return hres;
256
257     if(retv) num_set_val(retv, cos(num_val(&v)));
258     return S_OK;
259 }
260
261 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
262         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
263 {
264     FIXME("\n");
265     return E_NOTIMPL;
266 }
267
268 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
269         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
270 {
271     VARIANT v;
272     HRESULT hres;
273
274     TRACE("\n");
275
276     if(!arg_cnt(dp)) {
277         if(retv)
278             num_set_nan(retv);
279         return S_OK;
280     }
281
282     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
283     if(FAILED(hres))
284         return hres;
285
286     if(retv)
287         num_set_val(retv, floor(num_val(&v)));
288     return S_OK;
289 }
290
291 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
292         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
293 {
294     FIXME("\n");
295     return E_NOTIMPL;
296 }
297
298 /* ECMA-262 3rd Edition    15.8.2.11 */
299 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
300         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
301 {
302     DOUBLE max, d;
303     VARIANT v;
304     DWORD i;
305     HRESULT hres;
306
307     TRACE("\n");
308
309     if(!arg_cnt(dp)) {
310         if(retv)
311             num_set_inf(retv, FALSE);
312         return S_OK;
313     }
314
315     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
316     if(FAILED(hres))
317         return hres;
318
319     max = num_val(&v);
320     for(i=1; i < arg_cnt(dp); i++) {
321         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
322         if(FAILED(hres))
323             return hres;
324
325         d = num_val(&v);
326         if(d > max || isnan(d))
327             max = d;
328     }
329
330     if(retv)
331         num_set_val(retv, max);
332     return S_OK;
333 }
334
335 /* ECMA-262 3rd Edition    15.8.2.12 */
336 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
337         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338 {
339     DOUBLE min, d;
340     VARIANT v;
341     DWORD i;
342     HRESULT hres;
343
344     TRACE("\n");
345
346     if(!arg_cnt(dp)) {
347         if(retv)
348             num_set_inf(retv, TRUE);
349         return S_OK;
350     }
351
352     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
353     if(FAILED(hres))
354         return hres;
355
356     min = num_val(&v);
357     for(i=1; i < arg_cnt(dp); i++) {
358         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
359         if(FAILED(hres))
360             return hres;
361
362         d = num_val(&v);
363         if(d < min || isnan(d))
364             min = d;
365     }
366
367     if(retv)
368         num_set_val(retv, min);
369     return S_OK;
370 }
371
372 /* ECMA-262 3rd Edition    15.8.2.13 */
373 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
374         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 {
376     VARIANT x, y;
377     HRESULT hres;
378
379     TRACE("\n");
380
381     if(arg_cnt(dp) < 2) {
382         FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
383         return E_NOTIMPL;
384     }
385
386     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
387     if(FAILED(hres))
388         return hres;
389
390     hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
391     if(FAILED(hres))
392         return hres;
393
394     if(retv)
395         num_set_val(retv, pow(num_val(&x), num_val(&y)));
396     return S_OK;
397 }
398
399 /* ECMA-262 3rd Edition    15.8.2.14 */
400 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
401         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
402 {
403     UINT r;
404
405     TRACE("\n");
406
407     if(!RtlGenRandom(&r, sizeof(r)))
408         return E_UNEXPECTED;
409
410     if(retv)
411         num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
412
413     return S_OK;
414 }
415
416 /* ECMA-262 3rd Edition    15.8.2.15 */
417 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
418         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
419 {
420     VARIANT v;
421     HRESULT hres;
422
423     TRACE("\n");
424
425     if(!arg_cnt(dp)) {
426         num_set_nan(retv);
427         return S_OK;
428     }
429
430     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
431     if(FAILED(hres))
432         return hres;
433
434     if(retv)
435         num_set_val(retv, floor(num_val(&v)+0.5));
436     return S_OK;
437 }
438
439 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
440         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
441 {
442     FIXME("\n");
443     return E_NOTIMPL;
444 }
445
446 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
447         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
448 {
449     FIXME("\n");
450     return E_NOTIMPL;
451 }
452
453 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
454         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
455 {
456     FIXME("\n");
457     return E_NOTIMPL;
458 }
459
460 static const builtin_prop_t Math_props[] = {
461     {EW,        Math_E,        0},
462     {LN10W,     Math_LN10,     0},
463     {LN2W,      Math_LN2,      0},
464     {LOG10EW,   Math_LOG10E,   0},
465     {LOG2EW,    Math_LOG2E,    0},
466     {PIW,       Math_PI,       0},
467     {SQRT1_2W,  Math_SQRT1_2,  0},
468     {SQRT2W,    Math_SQRT2,    0},
469     {absW,      Math_abs,      PROPF_METHOD},
470     {acosW,     Math_acos,     PROPF_METHOD},
471     {asinW,     Math_asin,     PROPF_METHOD},
472     {atanW,     Math_atan,     PROPF_METHOD},
473     {atan2W,    Math_atan2,    PROPF_METHOD},
474     {ceilW,     Math_ceil,     PROPF_METHOD},
475     {cosW,      Math_cos,      PROPF_METHOD},
476     {expW,      Math_exp,      PROPF_METHOD},
477     {floorW,    Math_floor,    PROPF_METHOD},
478     {logW,      Math_log,      PROPF_METHOD},
479     {maxW,      Math_max,      PROPF_METHOD},
480     {minW,      Math_min,      PROPF_METHOD},
481     {powW,      Math_pow,      PROPF_METHOD},
482     {randomW,   Math_random,   PROPF_METHOD},
483     {roundW,    Math_round,    PROPF_METHOD},
484     {sinW,      Math_sin,      PROPF_METHOD},
485     {sqrtW,     Math_sqrt,     PROPF_METHOD},
486     {tanW,      Math_tan,      PROPF_METHOD}
487 };
488
489 static const builtin_info_t Math_info = {
490     JSCLASS_MATH,
491     {NULL, NULL, 0},
492     sizeof(Math_props)/sizeof(*Math_props),
493     Math_props,
494     NULL,
495     NULL
496 };
497
498 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
499 {
500     return create_dispex(ctx, &Math_info, NULL, ret);
501 }