push 3fbe09a897e9df6e6f0eb06b20d156a830394152
[wine/hacks.git] / dlls / jscript / math.c
blob4f6c049fa29ca26ffa96bc09958fd6cdd3984501
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 FIXME("\n");
102 return E_NOTIMPL;
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 FIXME("\n");
216 return E_NOTIMPL;
219 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
220 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
222 FIXME("\n");
223 return E_NOTIMPL;
226 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
229 VARIANT v;
230 HRESULT hres;
232 TRACE("\n");
234 if(!arg_cnt(dp)) {
235 if(retv)
236 num_set_nan(retv);
237 return S_OK;
240 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
241 if(FAILED(hres))
242 return hres;
244 if(retv)
245 num_set_val(retv, floor(num_val(&v)));
246 return S_OK;
249 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 FIXME("\n");
253 return E_NOTIMPL;
256 /* ECMA-262 3rd Edition 15.8.2.11 */
257 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
258 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
260 DOUBLE max, d;
261 VARIANT v;
262 DWORD i;
263 HRESULT hres;
265 TRACE("\n");
267 if(!arg_cnt(dp)) {
268 if(retv)
269 num_set_inf(retv, FALSE);
270 return S_OK;
273 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
274 if(FAILED(hres))
275 return hres;
277 max = num_val(&v);
278 for(i=1; i < arg_cnt(dp); i++) {
279 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
280 if(FAILED(hres))
281 return hres;
283 d = num_val(&v);
284 if(d > max || isnan(d))
285 max = d;
288 if(retv)
289 num_set_val(retv, max);
290 return S_OK;
293 /* ECMA-262 3rd Edition 15.8.2.12 */
294 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
295 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 DOUBLE min, d;
298 VARIANT v;
299 DWORD i;
300 HRESULT hres;
302 TRACE("\n");
304 if(!arg_cnt(dp)) {
305 if(retv)
306 num_set_inf(retv, TRUE);
307 return S_OK;
310 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
311 if(FAILED(hres))
312 return hres;
314 min = num_val(&v);
315 for(i=1; i < arg_cnt(dp); i++) {
316 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
317 if(FAILED(hres))
318 return hres;
320 d = num_val(&v);
321 if(d < min || isnan(d))
322 min = d;
325 if(retv)
326 num_set_val(retv, min);
327 return S_OK;
330 /* ECMA-262 3rd Edition 15.8.2.13 */
331 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
332 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
334 VARIANT x, y;
335 HRESULT hres;
337 TRACE("\n");
339 if(arg_cnt(dp) < 2) {
340 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
341 return E_NOTIMPL;
344 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
345 if(FAILED(hres))
346 return hres;
348 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
349 if(FAILED(hres))
350 return hres;
352 if(retv)
353 num_set_val(retv, pow(num_val(&x), num_val(&y)));
354 return S_OK;
357 /* ECMA-262 3rd Edition 15.8.2.14 */
358 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
359 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
361 UINT r;
363 TRACE("\n");
365 if(!RtlGenRandom(&r, sizeof(r)))
366 return E_UNEXPECTED;
368 if(retv)
369 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
371 return S_OK;
374 /* ECMA-262 3rd Edition 15.8.2.15 */
375 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
378 VARIANT v;
379 HRESULT hres;
381 TRACE("\n");
383 if(!arg_cnt(dp)) {
384 num_set_nan(retv);
385 return S_OK;
388 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
389 if(FAILED(hres))
390 return hres;
392 if(retv)
393 num_set_val(retv, floor(num_val(&v)+0.5));
394 return S_OK;
397 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
398 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
400 FIXME("\n");
401 return E_NOTIMPL;
404 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
405 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
407 FIXME("\n");
408 return E_NOTIMPL;
411 static HRESULT Math_tan(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 const builtin_prop_t Math_props[] = {
419 {EW, Math_E, 0},
420 {LN10W, Math_LN10, 0},
421 {LN2W, Math_LN2, 0},
422 {LOG10EW, Math_LOG10E, 0},
423 {LOG2EW, Math_LOG2E, 0},
424 {PIW, Math_PI, 0},
425 {SQRT1_2W, Math_SQRT1_2, 0},
426 {SQRT2W, Math_SQRT2, 0},
427 {absW, Math_abs, PROPF_METHOD},
428 {acosW, Math_acos, PROPF_METHOD},
429 {asinW, Math_asin, PROPF_METHOD},
430 {atanW, Math_atan, PROPF_METHOD},
431 {atan2W, Math_atan2, PROPF_METHOD},
432 {ceilW, Math_ceil, PROPF_METHOD},
433 {cosW, Math_cos, PROPF_METHOD},
434 {expW, Math_exp, PROPF_METHOD},
435 {floorW, Math_floor, PROPF_METHOD},
436 {logW, Math_log, PROPF_METHOD},
437 {maxW, Math_max, PROPF_METHOD},
438 {minW, Math_min, PROPF_METHOD},
439 {powW, Math_pow, PROPF_METHOD},
440 {randomW, Math_random, PROPF_METHOD},
441 {roundW, Math_round, PROPF_METHOD},
442 {sinW, Math_sin, PROPF_METHOD},
443 {sqrtW, Math_sqrt, PROPF_METHOD},
444 {tanW, Math_tan, PROPF_METHOD}
447 static const builtin_info_t Math_info = {
448 JSCLASS_MATH,
449 {NULL, NULL, 0},
450 sizeof(Math_props)/sizeof(*Math_props),
451 Math_props,
452 NULL,
453 NULL
456 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
458 return create_dispex(ctx, &Math_info, NULL, ret);