jscript: Added Math.round 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 <math.h>
20
21 #include "jscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26
27 static const WCHAR EW[] = {'E',0};
28 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
29 static const WCHAR LOG10EW[] = {'L','O','G','1','0',0};
30 static const WCHAR LN2W[] = {'L','N','2',0};
31 static const WCHAR LN10W[] = {'L','N','1','0',0};
32 static const WCHAR PIW[] = {'P','I',0};
33 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
34 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
35 static const WCHAR absW[] = {'a','b','s',0};
36 static const WCHAR acosW[] = {'a','c','o','s',0};
37 static const WCHAR asinW[] = {'a','s','i','n',0};
38 static const WCHAR atanW[] = {'a','t','a','n',0};
39 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
40 static const WCHAR ceilW[] = {'c','e','i','l',0};
41 static const WCHAR cosW[] = {'c','o','s',0};
42 static const WCHAR expW[] = {'e','x','p',0};
43 static const WCHAR floorW[] = {'f','l','o','o','r',0};
44 static const WCHAR logW[] = {'l','o','g',0};
45 static const WCHAR maxW[] = {'m','a','x',0};
46 static const WCHAR minW[] = {'m','i','n',0};
47 static const WCHAR powW[] = {'p','o','w',0};
48 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
49 static const WCHAR roundW[] = {'r','o','u','n','d',0};
50 static const WCHAR sinW[] = {'s','i','n',0};
51 static const WCHAR sqrtW[] = {'s','q','r','t',0};
52 static const WCHAR tanW[] = {'t','a','n',0};
53
54 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
55         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
56 {
57     FIXME("\n");
58     return E_NOTIMPL;
59 }
60
61 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
62         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
63 {
64     FIXME("\n");
65     return E_NOTIMPL;
66 }
67
68 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
69         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
70 {
71     FIXME("\n");
72     return E_NOTIMPL;
73 }
74
75 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
76         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
77 {
78     FIXME("\n");
79     return E_NOTIMPL;
80 }
81
82 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
83         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
84 {
85     FIXME("\n");
86     return E_NOTIMPL;
87 }
88
89 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
90         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
91 {
92     FIXME("\n");
93     return E_NOTIMPL;
94 }
95
96 static HRESULT Math_SQRT2(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_SQRT1_2(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 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
111         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
112 {
113     FIXME("\n");
114     return E_NOTIMPL;
115 }
116
117 static HRESULT Math_acos(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_asin(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 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
132         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
133 {
134     FIXME("\n");
135     return E_NOTIMPL;
136 }
137
138 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
139         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
140 {
141     FIXME("\n");
142     return E_NOTIMPL;
143 }
144
145 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
146         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
147 {
148     FIXME("\n");
149     return E_NOTIMPL;
150 }
151
152 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
153         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
154 {
155     FIXME("\n");
156     return E_NOTIMPL;
157 }
158
159 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
160         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
161 {
162     FIXME("\n");
163     return E_NOTIMPL;
164 }
165
166 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
167         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
168 {
169     FIXME("\n");
170     return E_NOTIMPL;
171 }
172
173 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
174         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
175 {
176     FIXME("\n");
177     return E_NOTIMPL;
178 }
179
180 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
181         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
182 {
183     FIXME("\n");
184     return E_NOTIMPL;
185 }
186
187 /* ECMA-262 3rd Edition    15.8.2.12 */
188 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
189         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
190 {
191     DOUBLE min, d;
192     VARIANT v;
193     DWORD i;
194     HRESULT hres;
195
196     TRACE("\n");
197
198     /* FIXME: Handle NaN */
199
200     if(!arg_cnt(dp)) {
201         FIXME("arg_cnt = 0\n");
202         return E_NOTIMPL;
203     }
204
205     hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
206     if(FAILED(hres))
207         return hres;
208
209     min = num_val(&v);
210     for(i=1; i < arg_cnt(dp); i++) {
211         hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
212         if(FAILED(hres))
213             return hres;
214
215         d = num_val(&v);
216         if(d < min)
217             min = d;
218     }
219
220     if(retv)
221         num_set_val(retv, min);
222     return S_OK;
223 }
224
225 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
226         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
227 {
228     FIXME("\n");
229     return E_NOTIMPL;
230 }
231
232 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
233         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
234 {
235     FIXME("\n");
236     return E_NOTIMPL;
237 }
238
239 /* ECMA-262 3rd Edition    15.8.2.15 */
240 static HRESULT Math_round(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         FIXME("arg_cnt = 0\n");
250         return E_NOTIMPL;
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)
258         num_set_val(retv, floor(num_val(&v)+0.5));
259     return S_OK;
260 }
261
262 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
263         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
264 {
265     FIXME("\n");
266     return E_NOTIMPL;
267 }
268
269 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
270         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
271 {
272     FIXME("\n");
273     return E_NOTIMPL;
274 }
275
276 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
277         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
278 {
279     FIXME("\n");
280     return E_NOTIMPL;
281 }
282
283 static const builtin_prop_t Math_props[] = {
284     {EW,        Math_E,        0},
285     {LOG2EW,    Math_LOG2E,    0},
286     {LOG10EW,   Math_LOG10E,   0},
287     {LN2W,      Math_LN2,      0},
288     {LN10W,     Math_LN10,     0},
289     {PIW,       Math_PI,       0},
290     {SQRT1_2W,  Math_SQRT1_2,  0},
291     {SQRT2W,    Math_SQRT2,    0},
292     {absW,      Math_abs,      PROPF_METHOD},
293     {acosW,     Math_acos,     PROPF_METHOD},
294     {asinW,     Math_asin,     PROPF_METHOD},
295     {atanW,     Math_atan,     PROPF_METHOD},
296     {atan2W,    Math_atan2,    PROPF_METHOD},
297     {ceilW,     Math_ceil,     PROPF_METHOD},
298     {cosW,      Math_cos,      PROPF_METHOD},
299     {expW,      Math_exp,      PROPF_METHOD},
300     {floorW,    Math_floor,    PROPF_METHOD},
301     {logW,      Math_log,      PROPF_METHOD},
302     {maxW,      Math_max,      PROPF_METHOD},
303     {minW,      Math_min,      PROPF_METHOD},
304     {powW,      Math_pow,      PROPF_METHOD},
305     {randomW,   Math_random,   PROPF_METHOD},
306     {roundW,    Math_round,    PROPF_METHOD},
307     {sinW,      Math_sin,      PROPF_METHOD},
308     {sqrtW,     Math_sqrt,     PROPF_METHOD},
309     {tanW,      Math_tan,      PROPF_METHOD}
310 };
311
312 static const builtin_info_t Math_info = {
313     JSCLASS_MATH,
314     {NULL, NULL, 0},
315     sizeof(Math_props)/sizeof(*Math_props),
316     Math_props,
317     NULL,
318     NULL
319 };
320
321 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
322 {
323     return create_dispex(ctx, &Math_info, NULL, ret);
324 }