jscript: Update Russian translation.
[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, DISPPARAMS *dp,
62         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
63 {
64     VARIANT v;
65     DOUBLE d;
66     HRESULT hres;
67
68     TRACE("\n");
69
70     if(!arg_cnt(dp)) {
71         if(retv)
72             num_set_nan(retv);
73         return S_OK;
74     }
75
76     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
77     if(FAILED(hres))
78         return hres;
79
80     d = num_val(&v);
81     if(retv)
82         num_set_val(retv, d < 0.0 ? -d : d);
83     return S_OK;
84 }
85
86 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
87         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
88 {
89     VARIANT v;
90     HRESULT hres;
91
92     TRACE("\n");
93
94     if(!arg_cnt(dp)) {
95         if(retv) num_set_nan(retv);
96         return S_OK;
97     }
98
99     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
100     if(FAILED(hres))
101         return hres;
102
103     if(retv) num_set_val(retv, acos(num_val(&v)));
104     return S_OK;
105 }
106
107 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
108         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 {
110     VARIANT v;
111     HRESULT hres;
112
113     TRACE("\n");
114
115     if(!arg_cnt(dp)) {
116         if(retv) num_set_nan(retv);
117         return S_OK;
118     }
119
120     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
121     if(FAILED(hres))
122         return hres;
123
124     if(retv) num_set_val(retv, asin(num_val(&v)));
125     return S_OK;
126 }
127
128 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
129         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
130 {
131     VARIANT v;
132     HRESULT hres;
133
134     TRACE("\n");
135
136     if(!arg_cnt(dp)) {
137         if(retv) num_set_nan(retv);
138         return S_OK;
139     }
140
141     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
142     if(FAILED(hres))
143         return hres;
144
145     if(retv) num_set_val(retv, atan(num_val(&v)));
146     return S_OK;
147 }
148
149 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
150         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
151 {
152     VARIANT v1, v2;
153     HRESULT hres;
154
155     TRACE("\n");
156
157     if(arg_cnt(dp)<2) {
158         if(retv) num_set_nan(retv);
159         return S_OK;
160     }
161
162     hres = to_number(ctx, get_arg(dp, 0), ei, &v1);
163     if(FAILED(hres))
164         return hres;
165
166     hres = to_number(ctx, get_arg(dp, 1), ei, &v2);
167     if(FAILED(hres))
168         return hres;
169
170     if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
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, DISPPARAMS *dp,
176         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 {
178     VARIANT v;
179     HRESULT hres;
180
181     TRACE("\n");
182
183     if(!arg_cnt(dp)) {
184         if(retv)
185             num_set_nan(retv);
186         return S_OK;
187     }
188
189     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
190     if(FAILED(hres))
191         return hres;
192
193     if(retv)
194         num_set_val(retv, ceil(num_val(&v)));
195     return S_OK;
196 }
197
198 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
199         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
200 {
201     VARIANT v;
202     HRESULT hres;
203
204     TRACE("\n");
205
206     if(!arg_cnt(dp)) {
207         if(retv) num_set_nan(retv);
208         return S_OK;
209     }
210
211     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
212     if(FAILED(hres))
213         return hres;
214
215     if(retv) num_set_val(retv, cos(num_val(&v)));
216     return S_OK;
217 }
218
219 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
220         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
221 {
222     VARIANT v;
223     HRESULT hres;
224
225     TRACE("\n");
226
227     if(!arg_cnt(dp)) {
228         if(retv) num_set_nan(retv);
229         return S_OK;
230     }
231
232     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
233     if(FAILED(hres))
234         return hres;
235
236     if(retv) num_set_val(retv, exp(num_val(&v)));
237     return S_OK;
238 }
239
240 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, 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)
250             num_set_nan(retv);
251         return S_OK;
252     }
253
254     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
255     if(FAILED(hres))
256         return hres;
257
258     if(retv)
259         num_set_val(retv, floor(num_val(&v)));
260     return S_OK;
261 }
262
263 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
264         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
265 {
266     VARIANT v;
267     HRESULT hres;
268
269     TRACE("\n");
270
271     if(!arg_cnt(dp)) {
272         if(retv)
273             num_set_nan(retv);
274         return S_OK;
275     }
276
277     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
278     if(FAILED(hres))
279         return hres;
280
281     if(retv)
282         num_set_val(retv, log(num_val(&v)));
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, DISPPARAMS *dp,
288         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
289 {
290     DOUBLE max, d;
291     VARIANT v;
292     DWORD i;
293     HRESULT hres;
294
295     TRACE("\n");
296
297     if(!arg_cnt(dp)) {
298         if(retv)
299             num_set_inf(retv, FALSE);
300         return S_OK;
301     }
302
303     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
304     if(FAILED(hres))
305         return hres;
306
307     max = num_val(&v);
308     for(i=1; i < arg_cnt(dp); i++) {
309         hres = to_number(ctx, get_arg(dp, i), ei, &v);
310         if(FAILED(hres))
311             return hres;
312
313         d = num_val(&v);
314         if(d > max || isnan(d))
315             max = d;
316     }
317
318     if(retv)
319         num_set_val(retv, max);
320     return S_OK;
321 }
322
323 /* ECMA-262 3rd Edition    15.8.2.12 */
324 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
325         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
326 {
327     DOUBLE min, d;
328     VARIANT v;
329     DWORD i;
330     HRESULT hres;
331
332     TRACE("\n");
333
334     if(!arg_cnt(dp)) {
335         if(retv)
336             num_set_inf(retv, TRUE);
337         return S_OK;
338     }
339
340     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
341     if(FAILED(hres))
342         return hres;
343
344     min = num_val(&v);
345     for(i=1; i < arg_cnt(dp); i++) {
346         hres = to_number(ctx, get_arg(dp, i), ei, &v);
347         if(FAILED(hres))
348             return hres;
349
350         d = num_val(&v);
351         if(d < min || isnan(d))
352             min = d;
353     }
354
355     if(retv)
356         num_set_val(retv, min);
357     return S_OK;
358 }
359
360 /* ECMA-262 3rd Edition    15.8.2.13 */
361 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
362         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
363 {
364     VARIANT x, y;
365     HRESULT hres;
366
367     TRACE("\n");
368
369     if(arg_cnt(dp) < 2) {
370         if(retv) num_set_nan(retv);
371         return S_OK;
372     }
373
374     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
375     if(FAILED(hres))
376         return hres;
377
378     hres = to_number(ctx, get_arg(dp, 1), ei, &y);
379     if(FAILED(hres))
380         return hres;
381
382     if(retv)
383         num_set_val(retv, pow(num_val(&x), num_val(&y)));
384     return S_OK;
385 }
386
387 /* ECMA-262 3rd Edition    15.8.2.14 */
388 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
389         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
390 {
391     UINT r;
392
393     TRACE("\n");
394
395     if(!RtlGenRandom(&r, sizeof(r)))
396         return E_UNEXPECTED;
397
398     if(retv)
399         num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
400
401     return S_OK;
402 }
403
404 /* ECMA-262 3rd Edition    15.8.2.15 */
405 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
406         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
407 {
408     VARIANT v;
409     HRESULT hres;
410
411     TRACE("\n");
412
413     if(!arg_cnt(dp)) {
414         num_set_nan(retv);
415         return S_OK;
416     }
417
418     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
419     if(FAILED(hres))
420         return hres;
421
422     if(retv)
423         num_set_val(retv, floor(num_val(&v)+0.5));
424     return S_OK;
425 }
426
427 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
428         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
429 {
430     VARIANT v;
431     HRESULT hres;
432
433     TRACE("\n");
434
435     if(!arg_cnt(dp)) {
436         if(retv) num_set_nan(retv);
437         return S_OK;
438     }
439
440     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
441     if(FAILED(hres))
442         return hres;
443
444     if(retv) num_set_val(retv, sin(num_val(&v)));
445     return S_OK;
446 }
447
448 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
449         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
450 {
451     VARIANT v;
452     HRESULT hres;
453
454     TRACE("\n");
455
456     if(!arg_cnt(dp)) {
457         if(retv) num_set_nan(retv);
458         return S_OK;
459     }
460
461     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
462     if(FAILED(hres))
463         return hres;
464
465     if(retv) num_set_val(retv, sqrt(num_val(&v)));
466     return S_OK;
467 }
468
469 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
470         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
471 {
472     VARIANT v;
473     HRESULT hres;
474
475     TRACE("\n");
476
477     if(!arg_cnt(dp)) {
478         if(retv) num_set_nan(retv);
479         return S_OK;
480     }
481
482     hres = to_number(ctx, get_arg(dp, 0), ei, &v);
483     if(FAILED(hres))
484         return hres;
485
486     if(retv) num_set_val(retv, tan(num_val(&v)));
487     return S_OK;
488 }
489
490 static const builtin_prop_t Math_props[] = {
491     {absW,      Math_abs,      PROPF_METHOD|1},
492     {acosW,     Math_acos,     PROPF_METHOD|1},
493     {asinW,     Math_asin,     PROPF_METHOD|1},
494     {atanW,     Math_atan,     PROPF_METHOD|1},
495     {atan2W,    Math_atan2,    PROPF_METHOD|2},
496     {ceilW,     Math_ceil,     PROPF_METHOD|1},
497     {cosW,      Math_cos,      PROPF_METHOD|1},
498     {expW,      Math_exp,      PROPF_METHOD|1},
499     {floorW,    Math_floor,    PROPF_METHOD|1},
500     {logW,      Math_log,      PROPF_METHOD|1},
501     {maxW,      Math_max,      PROPF_METHOD|2},
502     {minW,      Math_min,      PROPF_METHOD|2},
503     {powW,      Math_pow,      PROPF_METHOD|2},
504     {randomW,   Math_random,   PROPF_METHOD},
505     {roundW,    Math_round,    PROPF_METHOD|1},
506     {sinW,      Math_sin,      PROPF_METHOD|1},
507     {sqrtW,     Math_sqrt,     PROPF_METHOD|1},
508     {tanW,      Math_tan,      PROPF_METHOD|1}
509 };
510
511 static const builtin_info_t Math_info = {
512     JSCLASS_MATH,
513     {NULL, NULL, 0},
514     sizeof(Math_props)/sizeof(*Math_props),
515     Math_props,
516     NULL,
517     NULL
518 };
519
520 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
521 {
522     jsdisp_t *math;
523     unsigned i;
524     VARIANT v;
525     HRESULT hres;
526
527     struct {
528         const WCHAR *name;
529         DOUBLE val;
530     }constants[] = {
531         {EW,        M_E},        /* ECMA-262 3rd Edition    15.8.1.1 */
532         {LN10W,     M_LN10},     /* ECMA-262 3rd Edition    15.8.1.2 */
533         {LN2W,      M_LN2},      /* ECMA-262 3rd Edition    15.8.1.3 */
534         {LOG2EW,    M_LOG2E},    /* ECMA-262 3rd Edition    15.8.1.4 */
535         {LOG10EW,   M_LOG10E},   /* ECMA-262 3rd Edition    15.8.1.5 */
536         {PIW,       M_PI},       /* ECMA-262 3rd Edition    15.8.1.6 */
537         {SQRT1_2W,  M_SQRT1_2},  /* ECMA-262 3rd Edition    15.8.1.7 */
538         {SQRT2W,    M_SQRT2},    /* ECMA-262 3rd Edition    15.8.1.8 */
539     };
540
541     math = heap_alloc_zero(sizeof(jsdisp_t));
542     if(!math)
543         return E_OUTOFMEMORY;
544
545     hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
546     if(FAILED(hres)) {
547         heap_free(math);
548         return hres;
549     }
550
551     V_VT(&v) = VT_R8;
552     for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
553         V_R8(&v) = constants[i].val;
554         hres = jsdisp_propput_const(math, constants[i].name, &v);
555         if(FAILED(hres)) {
556             jsdisp_release(math);
557             return hres;
558         }
559     }
560
561     *ret = math;
562     return S_OK;
563 }