jscript: Added Math.floor implementation.
[wine/multimedia.git] / dlls / jscript / math.c
blobd3d59ae22c62bfe702fa768bd5d6feb63efa0a5c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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',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};
57 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
58 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
60 FIXME("\n");
61 return E_NOTIMPL;
64 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
65 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 FIXME("\n");
68 return E_NOTIMPL;
71 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
74 FIXME("\n");
75 return E_NOTIMPL;
78 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
79 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
81 FIXME("\n");
82 return E_NOTIMPL;
85 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
86 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
88 FIXME("\n");
89 return E_NOTIMPL;
92 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
93 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
95 FIXME("\n");
96 return E_NOTIMPL;
99 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
100 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
102 FIXME("\n");
103 return E_NOTIMPL;
106 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 FIXME("\n");
110 return E_NOTIMPL;
113 /* ECMA-262 3rd Edition 15.8.2.12 */
114 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
115 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 VARIANT v;
118 DOUBLE d;
119 HRESULT hres;
121 TRACE("\n");
123 if(!arg_cnt(dp)) {
124 if(retv)
125 num_set_nan(retv);
126 return S_OK;
129 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
130 if(FAILED(hres))
131 return hres;
133 d = num_val(&v);
134 if(retv)
135 num_set_val(retv, d < 0.0 ? -d : d);
136 return S_OK;
139 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
140 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
142 FIXME("\n");
143 return E_NOTIMPL;
146 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
147 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
149 FIXME("\n");
150 return E_NOTIMPL;
153 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
154 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
156 FIXME("\n");
157 return E_NOTIMPL;
160 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 FIXME("\n");
164 return E_NOTIMPL;
167 /* ECMA-262 3rd Edition 15.8.2.6 */
168 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
169 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
171 VARIANT v;
172 HRESULT hres;
174 TRACE("\n");
176 if(!arg_cnt(dp)) {
177 FIXME("arg_cnt = 0\n");
178 return E_NOTIMPL;
181 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
182 if(FAILED(hres))
183 return hres;
185 if(retv)
186 num_set_val(retv, ceil(num_val(&v)));
187 return S_OK;
190 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
191 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
193 FIXME("\n");
194 return E_NOTIMPL;
197 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
198 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
200 FIXME("\n");
201 return E_NOTIMPL;
204 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
205 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
207 VARIANT v;
208 HRESULT hres;
210 TRACE("\n");
212 if(!arg_cnt(dp)) {
213 if(retv)
214 num_set_nan(retv);
215 return S_OK;
218 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
219 if(FAILED(hres))
220 return hres;
222 if(retv)
223 num_set_val(retv, floor(num_val(&v)));
224 return S_OK;
227 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
228 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
230 FIXME("\n");
231 return E_NOTIMPL;
234 /* ECMA-262 3rd Edition 15.8.2.11 */
235 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
236 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
238 DOUBLE max, d;
239 VARIANT v;
240 DWORD i;
241 HRESULT hres;
243 TRACE("\n");
245 if(!arg_cnt(dp)) {
246 if(retv)
247 num_set_inf(retv, FALSE);
248 return S_OK;
251 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
252 if(FAILED(hres))
253 return hres;
255 max = num_val(&v);
256 for(i=1; i < arg_cnt(dp); i++) {
257 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
258 if(FAILED(hres))
259 return hres;
261 d = num_val(&v);
262 if(d > max || isnan(d))
263 max = d;
266 if(retv)
267 num_set_val(retv, max);
268 return S_OK;
271 /* ECMA-262 3rd Edition 15.8.2.12 */
272 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 DOUBLE min, d;
276 VARIANT v;
277 DWORD i;
278 HRESULT hres;
280 TRACE("\n");
282 if(!arg_cnt(dp)) {
283 if(retv)
284 num_set_inf(retv, TRUE);
285 return S_OK;
288 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
289 if(FAILED(hres))
290 return hres;
292 min = num_val(&v);
293 for(i=1; i < arg_cnt(dp); i++) {
294 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
295 if(FAILED(hres))
296 return hres;
298 d = num_val(&v);
299 if(d < min || isnan(d))
300 min = d;
303 if(retv)
304 num_set_val(retv, min);
305 return S_OK;
308 /* ECMA-262 3rd Edition 15.8.2.13 */
309 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
312 VARIANT x, y;
313 HRESULT hres;
315 TRACE("\n");
317 if(arg_cnt(dp) < 2) {
318 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
319 return E_NOTIMPL;
322 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
323 if(FAILED(hres))
324 return hres;
326 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
327 if(FAILED(hres))
328 return hres;
330 if(retv)
331 num_set_val(retv, pow(num_val(&x), num_val(&y)));
332 return S_OK;
335 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
336 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338 FIXME("\n");
339 return E_NOTIMPL;
342 /* ECMA-262 3rd Edition 15.8.2.15 */
343 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
344 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
346 VARIANT v;
347 HRESULT hres;
349 TRACE("\n");
351 if(!arg_cnt(dp)) {
352 FIXME("arg_cnt = 0\n");
353 return E_NOTIMPL;
356 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
357 if(FAILED(hres))
358 return hres;
360 if(retv)
361 num_set_val(retv, floor(num_val(&v)+0.5));
362 return S_OK;
365 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
366 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
368 FIXME("\n");
369 return E_NOTIMPL;
372 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
373 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 FIXME("\n");
376 return E_NOTIMPL;
379 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
380 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
382 FIXME("\n");
383 return E_NOTIMPL;
386 static const builtin_prop_t Math_props[] = {
387 {EW, Math_E, 0},
388 {LOG2EW, Math_LOG2E, 0},
389 {LOG10EW, Math_LOG10E, 0},
390 {LN2W, Math_LN2, 0},
391 {LN10W, Math_LN10, 0},
392 {PIW, Math_PI, 0},
393 {SQRT1_2W, Math_SQRT1_2, 0},
394 {SQRT2W, Math_SQRT2, 0},
395 {absW, Math_abs, PROPF_METHOD},
396 {acosW, Math_acos, PROPF_METHOD},
397 {asinW, Math_asin, PROPF_METHOD},
398 {atanW, Math_atan, PROPF_METHOD},
399 {atan2W, Math_atan2, PROPF_METHOD},
400 {ceilW, Math_ceil, PROPF_METHOD},
401 {cosW, Math_cos, PROPF_METHOD},
402 {expW, Math_exp, PROPF_METHOD},
403 {floorW, Math_floor, PROPF_METHOD},
404 {logW, Math_log, PROPF_METHOD},
405 {maxW, Math_max, PROPF_METHOD},
406 {minW, Math_min, PROPF_METHOD},
407 {powW, Math_pow, PROPF_METHOD},
408 {randomW, Math_random, PROPF_METHOD},
409 {roundW, Math_round, PROPF_METHOD},
410 {sinW, Math_sin, PROPF_METHOD},
411 {sqrtW, Math_sqrt, PROPF_METHOD},
412 {tanW, Math_tan, PROPF_METHOD}
415 static const builtin_info_t Math_info = {
416 JSCLASS_MATH,
417 {NULL, NULL, 0},
418 sizeof(Math_props)/sizeof(*Math_props),
419 Math_props,
420 NULL,
421 NULL
424 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
426 return create_dispex(ctx, &Math_info, NULL, ret);