windowscodecs: Implement IWICImagingFactory::CreateEncoder.
[wine] / dlls / jscript / math.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  * Copyright 2009 Piotr Caban
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <math.h>
24 #include <limits.h>
25
26 #include "jscript.h"
27 #include "ntsecapi.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32
33 static const WCHAR EW[] = {'E',0};
34 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
35 static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
36 static const WCHAR LN2W[] = {'L','N','2',0};
37 static const WCHAR LN10W[] = {'L','N','1','0',0};
38 static const WCHAR PIW[] = {'P','I',0};
39 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
40 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
41 static const WCHAR absW[] = {'a','b','s',0};
42 static const WCHAR acosW[] = {'a','c','o','s',0};
43 static const WCHAR asinW[] = {'a','s','i','n',0};
44 static const WCHAR atanW[] = {'a','t','a','n',0};
45 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
46 static const WCHAR ceilW[] = {'c','e','i','l',0};
47 static const WCHAR cosW[] = {'c','o','s',0};
48 static const WCHAR expW[] = {'e','x','p',0};
49 static const WCHAR floorW[] = {'f','l','o','o','r',0};
50 static const WCHAR logW[] = {'l','o','g',0};
51 static const WCHAR maxW[] = {'m','a','x',0};
52 static const WCHAR minW[] = {'m','i','n',0};
53 static const WCHAR powW[] = {'p','o','w',0};
54 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
55 static const WCHAR roundW[] = {'r','o','u','n','d',0};
56 static const WCHAR sinW[] = {'s','i','n',0};
57 static const WCHAR sqrtW[] = {'s','q','r','t',0};
58 static const WCHAR tanW[] = {'t','a','n',0};
59
60 /* ECMA-262 3rd Edition    15.8.2.12 */
61 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
62         VARIANT *retv, jsexcept_t *ei)
63 {
64     double d;
65     HRESULT hres;
66
67     TRACE("\n");
68
69     if(!argc) {
70         if(retv)
71             num_set_val(retv, NAN);
72         return S_OK;
73     }
74
75     hres = to_number(ctx, argv, ei, &d);
76     if(FAILED(hres))
77         return hres;
78
79     if(retv)
80         num_set_val(retv, d < 0.0 ? -d : d);
81     return S_OK;
82 }
83
84 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
85         VARIANT *retv, jsexcept_t *ei)
86 {
87     double x;
88     HRESULT hres;
89
90     TRACE("\n");
91
92     if(!argc) {
93         if(retv) num_set_val(retv, NAN);
94         return S_OK;
95     }
96
97     hres = to_number(ctx, argv, ei, &x);
98     if(FAILED(hres))
99         return hres;
100
101     if(retv)
102         num_set_val(retv, x < -1.0 || x > 1.0 ? NAN : acos(x));
103     return S_OK;
104 }
105
106 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
107         VARIANT *retv, jsexcept_t *ei)
108 {
109     double x;
110     HRESULT hres;
111
112     TRACE("\n");
113
114     if(!argc) {
115         if(retv) num_set_val(retv, NAN);
116         return S_OK;
117     }
118
119     hres = to_number(ctx, argv, ei, &x);
120     if(FAILED(hres))
121         return hres;
122
123     if(retv)
124         num_set_val(retv, x < -1.0 || x > 1.0 ? NAN : asin(x));
125     return S_OK;
126 }
127
128 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
129         VARIANT *retv, jsexcept_t *ei)
130 {
131     double x;
132     HRESULT hres;
133
134     TRACE("\n");
135
136     if(!argc) {
137         if(retv) num_set_val(retv, NAN);
138         return S_OK;
139     }
140
141     hres = to_number(ctx, argv, ei, &x);
142     if(FAILED(hres))
143         return hres;
144
145     if(retv) num_set_val(retv, atan(x));
146     return S_OK;
147 }
148
149 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
150         VARIANT *retv, jsexcept_t *ei)
151 {
152     double x, y;
153     HRESULT hres;
154
155     TRACE("\n");
156
157     if(argc<2) {
158         if(retv) num_set_val(retv, NAN);
159         return S_OK;
160     }
161
162     hres = to_number(ctx, argv, ei, &y);
163     if(FAILED(hres))
164         return hres;
165
166     hres = to_number(ctx, argv+1, ei, &x);
167     if(FAILED(hres))
168         return hres;
169
170     if(retv) num_set_val(retv, atan2(y, x));
171     return S_OK;
172 }
173
174 /* ECMA-262 3rd Edition    15.8.2.6 */
175 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
176         VARIANT *retv, jsexcept_t *ei)
177 {
178     double x;
179     HRESULT hres;
180
181     TRACE("\n");
182
183     if(!argc) {
184         if(retv)
185             num_set_val(retv, NAN);
186         return S_OK;
187     }
188
189     hres = to_number(ctx, argv, ei, &x);
190     if(FAILED(hres))
191         return hres;
192
193     if(retv)
194         num_set_val(retv, ceil(x));
195     return S_OK;
196 }
197
198 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
199         VARIANT *retv, jsexcept_t *ei)
200 {
201     double x;
202     HRESULT hres;
203
204     TRACE("\n");
205
206     if(!argc) {
207         if(retv) num_set_val(retv, NAN);
208         return S_OK;
209     }
210
211     hres = to_number(ctx, argv, ei, &x);
212     if(FAILED(hres))
213         return hres;
214
215     if(retv) num_set_val(retv, cos(x));
216     return S_OK;
217 }
218
219 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
220         VARIANT *retv, jsexcept_t *ei)
221 {
222     double x;
223     HRESULT hres;
224
225     TRACE("\n");
226
227     if(!argc) {
228         if(retv) num_set_val(retv, NAN);
229         return S_OK;
230     }
231
232     hres = to_number(ctx, argv, ei, &x);
233     if(FAILED(hres))
234         return hres;
235
236     if(retv) num_set_val(retv, exp(x));
237     return S_OK;
238 }
239
240 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
241         VARIANT *retv, jsexcept_t *ei)
242 {
243     double x;
244     HRESULT hres;
245
246     TRACE("\n");
247
248     if(!argc) {
249         if(retv)
250             num_set_val(retv, NAN);
251         return S_OK;
252     }
253
254     hres = to_number(ctx, argv, ei, &x);
255     if(FAILED(hres))
256         return hres;
257
258     if(retv)
259         num_set_val(retv, floor(x));
260     return S_OK;
261 }
262
263 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
264         VARIANT *retv, jsexcept_t *ei)
265 {
266     double x;
267     HRESULT hres;
268
269     TRACE("\n");
270
271     if(!argc) {
272         if(retv)
273             num_set_val(retv, NAN);
274         return S_OK;
275     }
276
277     hres = to_number(ctx, argv, ei, &x);
278     if(FAILED(hres))
279         return hres;
280
281     if(retv)
282         num_set_val(retv, x < -0.0 ? NAN : log(x));
283     return S_OK;
284 }
285
286 /* ECMA-262 3rd Edition    15.8.2.11 */
287 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
288         VARIANT *retv, jsexcept_t *ei)
289 {
290     DOUBLE max, d;
291     DWORD i;
292     HRESULT hres;
293
294     TRACE("\n");
295
296     if(!argc) {
297         if(retv)
298             num_set_val(retv, -INFINITY);
299         return S_OK;
300     }
301
302     hres = to_number(ctx, argv, ei, &max);
303     if(FAILED(hres))
304         return hres;
305
306     for(i=1; i < argc; i++) {
307         hres = to_number(ctx, argv+i, ei, &d);
308         if(FAILED(hres))
309             return hres;
310
311         if(d > max || isnan(d))
312             max = d;
313     }
314
315     if(retv)
316         num_set_val(retv, max);
317     return S_OK;
318 }
319
320 /* ECMA-262 3rd Edition    15.8.2.12 */
321 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
322         VARIANT *retv, jsexcept_t *ei)
323 {
324     DOUBLE min, d;
325     DWORD i;
326     HRESULT hres;
327
328     TRACE("\n");
329
330     if(!argc) {
331         if(retv)
332             num_set_val(retv, INFINITY);
333         return S_OK;
334     }
335
336     hres = to_number(ctx, argv, ei, &min);
337     if(FAILED(hres))
338         return hres;
339
340     for(i=1; i < argc; i++) {
341         hres = to_number(ctx, argv+i, ei, &d);
342         if(FAILED(hres))
343             return hres;
344
345         if(d < min || isnan(d))
346             min = d;
347     }
348
349     if(retv)
350         num_set_val(retv, min);
351     return S_OK;
352 }
353
354 /* ECMA-262 3rd Edition    15.8.2.13 */
355 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
356         VARIANT *retv, jsexcept_t *ei)
357 {
358     double x, y;
359     HRESULT hres;
360
361     TRACE("\n");
362
363     if(argc < 2) {
364         if(retv) num_set_val(retv, NAN);
365         return S_OK;
366     }
367
368     hres = to_number(ctx, argv, ei, &x);
369     if(FAILED(hres))
370         return hres;
371
372     hres = to_number(ctx, argv+1, ei, &y);
373     if(FAILED(hres))
374         return hres;
375
376     if(retv)
377         num_set_val(retv, pow(x, y));
378     return S_OK;
379 }
380
381 /* ECMA-262 3rd Edition    15.8.2.14 */
382 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
383         VARIANT *retv, jsexcept_t *ei)
384 {
385     UINT r;
386
387     TRACE("\n");
388
389     if(!RtlGenRandom(&r, sizeof(r)))
390         return E_UNEXPECTED;
391
392     if(retv)
393         num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
394
395     return S_OK;
396 }
397
398 /* ECMA-262 3rd Edition    15.8.2.15 */
399 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
400         VARIANT *retv, jsexcept_t *ei)
401 {
402     double x;
403     HRESULT hres;
404
405     TRACE("\n");
406
407     if(!argc) {
408         if(retv)
409             num_set_val(retv, NAN);
410         return S_OK;
411     }
412
413     hres = to_number(ctx, argv, ei, &x);
414     if(FAILED(hres))
415         return hres;
416
417     if(retv)
418         num_set_val(retv, floor(x+0.5));
419     return S_OK;
420 }
421
422 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
423         VARIANT *retv, jsexcept_t *ei)
424 {
425     double x;
426     HRESULT hres;
427
428     TRACE("\n");
429
430     if(!argc) {
431         if(retv) num_set_val(retv, NAN);
432         return S_OK;
433     }
434
435     hres = to_number(ctx, argv, ei, &x);
436     if(FAILED(hres))
437         return hres;
438
439     if(retv) num_set_val(retv, sin(x));
440     return S_OK;
441 }
442
443 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
444         VARIANT *retv, jsexcept_t *ei)
445 {
446     double x;
447     HRESULT hres;
448
449     TRACE("\n");
450
451     if(!argc) {
452         if(retv) num_set_val(retv, NAN);
453         return S_OK;
454     }
455
456     hres = to_number(ctx, argv, ei, &x);
457     if(FAILED(hres))
458         return hres;
459
460     if(retv) num_set_val(retv, sqrt(x));
461     return S_OK;
462 }
463
464 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
465         VARIANT *retv, jsexcept_t *ei)
466 {
467     double x;
468     HRESULT hres;
469
470     TRACE("\n");
471
472     if(!argc) {
473         if(retv) num_set_val(retv, NAN);
474         return S_OK;
475     }
476
477     hres = to_number(ctx, argv, ei, &x);
478     if(FAILED(hres))
479         return hres;
480
481     if(retv) num_set_val(retv, tan(x));
482     return S_OK;
483 }
484
485 static const builtin_prop_t Math_props[] = {
486     {absW,      Math_abs,      PROPF_METHOD|1},
487     {acosW,     Math_acos,     PROPF_METHOD|1},
488     {asinW,     Math_asin,     PROPF_METHOD|1},
489     {atanW,     Math_atan,     PROPF_METHOD|1},
490     {atan2W,    Math_atan2,    PROPF_METHOD|2},
491     {ceilW,     Math_ceil,     PROPF_METHOD|1},
492     {cosW,      Math_cos,      PROPF_METHOD|1},
493     {expW,      Math_exp,      PROPF_METHOD|1},
494     {floorW,    Math_floor,    PROPF_METHOD|1},
495     {logW,      Math_log,      PROPF_METHOD|1},
496     {maxW,      Math_max,      PROPF_METHOD|2},
497     {minW,      Math_min,      PROPF_METHOD|2},
498     {powW,      Math_pow,      PROPF_METHOD|2},
499     {randomW,   Math_random,   PROPF_METHOD},
500     {roundW,    Math_round,    PROPF_METHOD|1},
501     {sinW,      Math_sin,      PROPF_METHOD|1},
502     {sqrtW,     Math_sqrt,     PROPF_METHOD|1},
503     {tanW,      Math_tan,      PROPF_METHOD|1}
504 };
505
506 static const builtin_info_t Math_info = {
507     JSCLASS_MATH,
508     {NULL, NULL, 0},
509     sizeof(Math_props)/sizeof(*Math_props),
510     Math_props,
511     NULL,
512     NULL
513 };
514
515 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
516 {
517     jsdisp_t *math;
518     unsigned i;
519     VARIANT v;
520     HRESULT hres;
521
522     struct {
523         const WCHAR *name;
524         DOUBLE val;
525     }constants[] = {
526         {EW,        M_E},        /* ECMA-262 3rd Edition    15.8.1.1 */
527         {LN10W,     M_LN10},     /* ECMA-262 3rd Edition    15.8.1.2 */
528         {LN2W,      M_LN2},      /* ECMA-262 3rd Edition    15.8.1.3 */
529         {LOG2EW,    M_LOG2E},    /* ECMA-262 3rd Edition    15.8.1.4 */
530         {LOG10EW,   M_LOG10E},   /* ECMA-262 3rd Edition    15.8.1.5 */
531         {PIW,       M_PI},       /* ECMA-262 3rd Edition    15.8.1.6 */
532         {SQRT1_2W,  M_SQRT1_2},  /* ECMA-262 3rd Edition    15.8.1.7 */
533         {SQRT2W,    M_SQRT2},    /* ECMA-262 3rd Edition    15.8.1.8 */
534     };
535
536     math = heap_alloc_zero(sizeof(jsdisp_t));
537     if(!math)
538         return E_OUTOFMEMORY;
539
540     hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
541     if(FAILED(hres)) {
542         heap_free(math);
543         return hres;
544     }
545
546     V_VT(&v) = VT_R8;
547     for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
548         V_R8(&v) = constants[i].val;
549         hres = jsdisp_propput_const(math, constants[i].name, &v);
550         if(FAILED(hres)) {
551             jsdisp_release(math);
552             return hres;
553         }
554     }
555
556     *ret = math;
557     return S_OK;
558 }