cfgmgr32: Forward CM_Locate_DevNodeA/W to setupapi.
[wine/wine64.git] / dlls / jscript / math.c
blobf2a3d0c3718671b539378263c13d756889a12341
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','E',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_constant(DOUBLE val, WORD flags, VARIANT *retv)
59 switch(flags) {
60 case DISPATCH_PROPERTYGET:
61 V_VT(retv) = VT_R8;
62 V_R8(retv) = val;
63 return S_OK;
64 case DISPATCH_PROPERTYPUT:
65 return S_OK;
68 FIXME("unhandled flags %x\n", flags);
69 return E_NOTIMPL;
72 /* ECMA-262 3rd Edition 15.8.1.1 */
73 static HRESULT Math_E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
74 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
76 TRACE("\n");
77 return math_constant(M_E, flags, retv);
80 /* ECMA-262 3rd Edition 15.8.1.4 */
81 static HRESULT Math_LOG2E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
82 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
84 TRACE("\n");
85 return math_constant(M_LOG2E, flags, retv);
88 /* ECMA-262 3rd Edition 15.8.1.4 */
89 static HRESULT Math_LOG10E(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
90 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
92 TRACE("\n");
93 return math_constant(M_LOG10E, flags, retv);
96 static HRESULT Math_LN2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
97 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
99 FIXME("\n");
100 return E_NOTIMPL;
103 static HRESULT Math_LN10(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
104 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
106 FIXME("\n");
107 return E_NOTIMPL;
110 /* ECMA-262 3rd Edition 15.8.1.6 */
111 static HRESULT Math_PI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
112 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
114 TRACE("\n");
115 return math_constant(M_PI, flags, retv);
118 static HRESULT Math_SQRT2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
119 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
121 FIXME("\n");
122 return E_NOTIMPL;
125 static HRESULT Math_SQRT1_2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
126 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
128 FIXME("\n");
129 return E_NOTIMPL;
132 /* ECMA-262 3rd Edition 15.8.2.12 */
133 static HRESULT Math_abs(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
134 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
136 VARIANT v;
137 DOUBLE d;
138 HRESULT hres;
140 TRACE("\n");
142 if(!arg_cnt(dp)) {
143 if(retv)
144 num_set_nan(retv);
145 return S_OK;
148 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
149 if(FAILED(hres))
150 return hres;
152 d = num_val(&v);
153 if(retv)
154 num_set_val(retv, d < 0.0 ? -d : d);
155 return S_OK;
158 static HRESULT Math_acos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
159 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
161 FIXME("\n");
162 return E_NOTIMPL;
165 static HRESULT Math_asin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
166 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
168 FIXME("\n");
169 return E_NOTIMPL;
172 static HRESULT Math_atan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
173 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
175 FIXME("\n");
176 return E_NOTIMPL;
179 static HRESULT Math_atan2(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
180 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
182 FIXME("\n");
183 return E_NOTIMPL;
186 /* ECMA-262 3rd Edition 15.8.2.6 */
187 static HRESULT Math_ceil(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
188 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
190 VARIANT v;
191 HRESULT hres;
193 TRACE("\n");
195 if(!arg_cnt(dp)) {
196 if(retv)
197 num_set_nan(retv);
198 return S_OK;
201 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
202 if(FAILED(hres))
203 return hres;
205 if(retv)
206 num_set_val(retv, ceil(num_val(&v)));
207 return S_OK;
210 static HRESULT Math_cos(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
213 FIXME("\n");
214 return E_NOTIMPL;
217 static HRESULT Math_exp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
220 FIXME("\n");
221 return E_NOTIMPL;
224 static HRESULT Math_floor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
227 VARIANT v;
228 HRESULT hres;
230 TRACE("\n");
232 if(!arg_cnt(dp)) {
233 if(retv)
234 num_set_nan(retv);
235 return S_OK;
238 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
239 if(FAILED(hres))
240 return hres;
242 if(retv)
243 num_set_val(retv, floor(num_val(&v)));
244 return S_OK;
247 static HRESULT Math_log(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
250 FIXME("\n");
251 return E_NOTIMPL;
254 /* ECMA-262 3rd Edition 15.8.2.11 */
255 static HRESULT Math_max(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
256 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
258 DOUBLE max, d;
259 VARIANT v;
260 DWORD i;
261 HRESULT hres;
263 TRACE("\n");
265 if(!arg_cnt(dp)) {
266 if(retv)
267 num_set_inf(retv, FALSE);
268 return S_OK;
271 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
272 if(FAILED(hres))
273 return hres;
275 max = num_val(&v);
276 for(i=1; i < arg_cnt(dp); i++) {
277 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
278 if(FAILED(hres))
279 return hres;
281 d = num_val(&v);
282 if(d > max || isnan(d))
283 max = d;
286 if(retv)
287 num_set_val(retv, max);
288 return S_OK;
291 /* ECMA-262 3rd Edition 15.8.2.12 */
292 static HRESULT Math_min(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
293 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
295 DOUBLE min, d;
296 VARIANT v;
297 DWORD i;
298 HRESULT hres;
300 TRACE("\n");
302 if(!arg_cnt(dp)) {
303 if(retv)
304 num_set_inf(retv, TRUE);
305 return S_OK;
308 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
309 if(FAILED(hres))
310 return hres;
312 min = num_val(&v);
313 for(i=1; i < arg_cnt(dp); i++) {
314 hres = to_number(dispex->ctx, get_arg(dp, i), ei, &v);
315 if(FAILED(hres))
316 return hres;
318 d = num_val(&v);
319 if(d < min || isnan(d))
320 min = d;
323 if(retv)
324 num_set_val(retv, min);
325 return S_OK;
328 /* ECMA-262 3rd Edition 15.8.2.13 */
329 static HRESULT Math_pow(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
330 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 VARIANT x, y;
333 HRESULT hres;
335 TRACE("\n");
337 if(arg_cnt(dp) < 2) {
338 FIXME("unimplemented arg_cnt %d\n", arg_cnt(dp));
339 return E_NOTIMPL;
342 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &x);
343 if(FAILED(hres))
344 return hres;
346 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &y);
347 if(FAILED(hres))
348 return hres;
350 if(retv)
351 num_set_val(retv, pow(num_val(&x), num_val(&y)));
352 return S_OK;
355 static HRESULT Math_random(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
356 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
358 FIXME("\n");
359 return E_NOTIMPL;
362 /* ECMA-262 3rd Edition 15.8.2.15 */
363 static HRESULT Math_round(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
364 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
366 VARIANT v;
367 HRESULT hres;
369 TRACE("\n");
371 if(!arg_cnt(dp)) {
372 FIXME("arg_cnt = 0\n");
373 return E_NOTIMPL;
376 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
377 if(FAILED(hres))
378 return hres;
380 if(retv)
381 num_set_val(retv, floor(num_val(&v)+0.5));
382 return S_OK;
385 static HRESULT Math_sin(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
386 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
388 FIXME("\n");
389 return E_NOTIMPL;
392 static HRESULT Math_sqrt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
393 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
395 FIXME("\n");
396 return E_NOTIMPL;
399 static HRESULT Math_tan(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
400 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
402 FIXME("\n");
403 return E_NOTIMPL;
406 static const builtin_prop_t Math_props[] = {
407 {EW, Math_E, 0},
408 {LN10W, Math_LN10, 0},
409 {LN2W, Math_LN2, 0},
410 {LOG10EW, Math_LOG10E, 0},
411 {LOG2EW, Math_LOG2E, 0},
412 {PIW, Math_PI, 0},
413 {SQRT1_2W, Math_SQRT1_2, 0},
414 {SQRT2W, Math_SQRT2, 0},
415 {absW, Math_abs, PROPF_METHOD},
416 {acosW, Math_acos, PROPF_METHOD},
417 {asinW, Math_asin, PROPF_METHOD},
418 {atanW, Math_atan, PROPF_METHOD},
419 {atan2W, Math_atan2, PROPF_METHOD},
420 {ceilW, Math_ceil, PROPF_METHOD},
421 {cosW, Math_cos, PROPF_METHOD},
422 {expW, Math_exp, PROPF_METHOD},
423 {floorW, Math_floor, PROPF_METHOD},
424 {logW, Math_log, PROPF_METHOD},
425 {maxW, Math_max, PROPF_METHOD},
426 {minW, Math_min, PROPF_METHOD},
427 {powW, Math_pow, PROPF_METHOD},
428 {randomW, Math_random, PROPF_METHOD},
429 {roundW, Math_round, PROPF_METHOD},
430 {sinW, Math_sin, PROPF_METHOD},
431 {sqrtW, Math_sqrt, PROPF_METHOD},
432 {tanW, Math_tan, PROPF_METHOD}
435 static const builtin_info_t Math_info = {
436 JSCLASS_MATH,
437 {NULL, NULL, 0},
438 sizeof(Math_props)/sizeof(*Math_props),
439 Math_props,
440 NULL,
441 NULL
444 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
446 return create_dispex(ctx, &Math_info, NULL, ret);