push 556f62bab943c37bb6cbad95a6ecb3d73e04a93f
[wine/hacks.git] / dlls / jscript / math.c
blobac487d133b9e91ffea73c38dc57446a3a4b5a346
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>
23 #include <limits.h>
25 #include "jscript.h"
26 #include "ntsecapi.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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};
59 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
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;
70 FIXME("unhandled flags %x\n", flags);
71 return E_NOTIMPL;
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)
78 TRACE("\n");
79 return math_constant(M_E, flags, retv);
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)
86 TRACE("\n");
87 return math_constant(M_LOG2E, flags, retv);
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)
94 TRACE("\n");
95 return math_constant(M_LOG10E, flags, retv);
98 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 TRACE("\n");
102 return math_constant(M_LN2, flags, retv);
105 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 FIXME("\n");
109 return E_NOTIMPL;
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)
116 TRACE("\n");
117 return math_constant(M_PI, flags, retv);
120 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 FIXME("\n");
124 return E_NOTIMPL;
127 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
130 FIXME("\n");
131 return E_NOTIMPL;
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)
138 VARIANT v;
139 DOUBLE d;
140 HRESULT hres;
142 TRACE("\n");
144 if(!arg_cnt(dp)) {
145 if(retv)
146 num_set_nan(retv);
147 return S_OK;
150 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
151 if(FAILED(hres))
152 return hres;
154 d = num_val(&v);
155 if(retv)
156 num_set_val(retv, d < 0.0 ? -d : d);
157 return S_OK;
160 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 FIXME("\n");
164 return E_NOTIMPL;
167 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
168 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
170 FIXME("\n");
171 return E_NOTIMPL;
174 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 FIXME("\n");
178 return E_NOTIMPL;
181 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 FIXME("\n");
185 return E_NOTIMPL;
188 /* ECMA-262 3rd Edition 15.8.2.6 */
189 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
190 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
192 VARIANT v;
193 HRESULT hres;
195 TRACE("\n");
197 if(!arg_cnt(dp)) {
198 if(retv)
199 num_set_nan(retv);
200 return S_OK;
203 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
204 if(FAILED(hres))
205 return hres;
207 if(retv)
208 num_set_val(retv, ceil(num_val(&v)));
209 return S_OK;
212 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
213 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
215 VARIANT v;
216 HRESULT hres;
218 TRACE("\n");
220 if(!arg_cnt(dp)) {
221 if(retv) num_set_nan(retv);
222 return S_OK;
225 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
226 if(FAILED(hres))
227 return hres;
229 if(retv) num_set_val(retv, cos(num_val(&v)));
230 return S_OK;
233 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
234 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
236 FIXME("\n");
237 return E_NOTIMPL;
240 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
241 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
243 VARIANT v;
244 HRESULT hres;
246 TRACE("\n");
248 if(!arg_cnt(dp)) {
249 if(retv)
250 num_set_nan(retv);
251 return S_OK;
254 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
255 if(FAILED(hres))
256 return hres;
258 if(retv)
259 num_set_val(retv, floor(num_val(&v)));
260 return S_OK;
263 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
266 FIXME("\n");
267 return E_NOTIMPL;
270 /* ECMA-262 3rd Edition 15.8.2.11 */
271 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
272 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
274 DOUBLE max, d;
275 VARIANT v;
276 DWORD i;
277 HRESULT hres;
279 TRACE("\n");
281 if(!arg_cnt(dp)) {
282 if(retv)
283 num_set_inf(retv, FALSE);
284 return S_OK;
287 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
288 if(FAILED(hres))
289 return hres;
291 max = num_val(&v);
292 for(i=1; i < arg_cnt(dp); i++) {
293 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
294 if(FAILED(hres))
295 return hres;
297 d = num_val(&v);
298 if(d > max || isnan(d))
299 max = d;
302 if(retv)
303 num_set_val(retv, max);
304 return S_OK;
307 /* ECMA-262 3rd Edition 15.8.2.12 */
308 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
309 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
311 DOUBLE min, d;
312 VARIANT v;
313 DWORD i;
314 HRESULT hres;
316 TRACE("\n");
318 if(!arg_cnt(dp)) {
319 if(retv)
320 num_set_inf(retv, TRUE);
321 return S_OK;
324 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
325 if(FAILED(hres))
326 return hres;
328 min = num_val(&v);
329 for(i=1; i < arg_cnt(dp); i++) {
330 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
331 if(FAILED(hres))
332 return hres;
334 d = num_val(&v);
335 if(d < min || isnan(d))
336 min = d;
339 if(retv)
340 num_set_val(retv, min);
341 return S_OK;
344 /* ECMA-262 3rd Edition 15.8.2.13 */
345 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
348 VARIANT x, y;
349 HRESULT hres;
351 TRACE("\n");
353 if(arg_cnt(dp) < 2) {
354 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
355 return E_NOTIMPL;
358 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
359 if(FAILED(hres))
360 return hres;
362 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
363 if(FAILED(hres))
364 return hres;
366 if(retv)
367 num_set_val(retv, pow(num_val(&x), num_val(&y)));
368 return S_OK;
371 /* ECMA-262 3rd Edition 15.8.2.14 */
372 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
373 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 UINT r;
377 TRACE("\n");
379 if(!RtlGenRandom(&r, sizeof(r)))
380 return E_UNEXPECTED;
382 if(retv)
383 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
385 return S_OK;
388 /* ECMA-262 3rd Edition 15.8.2.15 */
389 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
390 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
392 VARIANT v;
393 HRESULT hres;
395 TRACE("\n");
397 if(!arg_cnt(dp)) {
398 num_set_nan(retv);
399 return S_OK;
402 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
403 if(FAILED(hres))
404 return hres;
406 if(retv)
407 num_set_val(retv, floor(num_val(&v)+0.5));
408 return S_OK;
411 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
412 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
414 FIXME("\n");
415 return E_NOTIMPL;
418 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
419 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
421 FIXME("\n");
422 return E_NOTIMPL;
425 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
426 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
428 FIXME("\n");
429 return E_NOTIMPL;
432 static const builtin_prop_t Math_props[] = {
433 {EW, Math_E, 0},
434 {LN10W, Math_LN10, 0},
435 {LN2W, Math_LN2, 0},
436 {LOG10EW, Math_LOG10E, 0},
437 {LOG2EW, Math_LOG2E, 0},
438 {PIW, Math_PI, 0},
439 {SQRT1_2W, Math_SQRT1_2, 0},
440 {SQRT2W, Math_SQRT2, 0},
441 {absW, Math_abs, PROPF_METHOD},
442 {acosW, Math_acos, PROPF_METHOD},
443 {asinW, Math_asin, PROPF_METHOD},
444 {atanW, Math_atan, PROPF_METHOD},
445 {atan2W, Math_atan2, PROPF_METHOD},
446 {ceilW, Math_ceil, PROPF_METHOD},
447 {cosW, Math_cos, PROPF_METHOD},
448 {expW, Math_exp, PROPF_METHOD},
449 {floorW, Math_floor, PROPF_METHOD},
450 {logW, Math_log, PROPF_METHOD},
451 {maxW, Math_max, PROPF_METHOD},
452 {minW, Math_min, PROPF_METHOD},
453 {powW, Math_pow, PROPF_METHOD},
454 {randomW, Math_random, PROPF_METHOD},
455 {roundW, Math_round, PROPF_METHOD},
456 {sinW, Math_sin, PROPF_METHOD},
457 {sqrtW, Math_sqrt, PROPF_METHOD},
458 {tanW, Math_tan, PROPF_METHOD}
461 static const builtin_info_t Math_info = {
462 JSCLASS_MATH,
463 {NULL, NULL, 0},
464 sizeof(Math_props)/sizeof(*Math_props),
465 Math_props,
466 NULL,
467 NULL
470 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
472 return create_dispex(ctx, &Math_info, NULL, ret);