1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <math.h>
24 #include <limits.h>
25
26 #include "jscript.h"
27 #include "ntsecapi.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32
33 static const WCHAR EW[] = {'E',0};
34 static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};
35 static const WCHAR LOG10EW[] = {'L','O','G','1','','E',0};
36 static const WCHAR LN2W[] = {'L','N','2',0};
37 static const WCHAR LN10W[] = {'L','N','1','',0};
38 static const WCHAR PIW[] = {'P','I',0};
39 static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};
40 static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};
41 static const WCHAR absW[] = {'a','b','s',0};
42 static const WCHAR acosW[] = {'a','c','o','s',0};
43 static const WCHAR asinW[] = {'a','s','i','n',0};
44 static const WCHAR atanW[] = {'a','t','a','n',0};
45 static const WCHAR atan2W[] = {'a','t','a','n','2',0};
46 static const WCHAR ceilW[] = {'c','e','i','l',0};
47 static const WCHAR cosW[] = {'c','o','s',0};
48 static const WCHAR expW[] = {'e','x','p',0};
49 static const WCHAR floorW[] = {'f','l','o','o','r',0};
50 static const WCHAR logW[] = {'l','o','g',0};
51 static const WCHAR maxW[] = {'m','a','x',0};
52 static const WCHAR minW[] = {'m','i','n',0};
53 static const WCHAR powW[] = {'p','o','w',0};
54 static const WCHAR randomW[] = {'r','a','n','d','o','m',0};
55 static const WCHAR roundW[] = {'r','o','u','n','d',0};
56 static const WCHAR sinW[] = {'s','i','n',0};
57 static const WCHAR sqrtW[] = {'s','q','r','t',0};
58 static const WCHAR tanW[] = {'t','a','n',0};
59
60 static HRESULT math_constant(DOUBLE val, WORD flags, VARIANT *retv)
61 {
62 switch(flags) {
63 case DISPATCH_PROPERTYGET:
64 V_VT(retv) = VT_R8;
65 V_R8(retv) = val;
66 return S_OK;
67 case DISPATCH_PROPERTYPUT:
68 return S_OK;
69 }
70
71 FIXME("unhandled flags %x\n", flags);
72 return E_NOTIMPL;
73 }
74
75 /* ECMA-262 3rd Edition 15.8.1.1 */
76 static HRESULT Math_E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
77 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
78 {
79 TRACE("\n");
80 return math_constant(M_E, flags, retv);
81 }
82
83 /* ECMA-262 3rd Edition 15.8.1.4 */
84 static HRESULT Math_LOG2E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
85 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
86 {
87 TRACE("\n");
88 return math_constant(M_LOG2E, flags, retv);
89 }
90
91 /* ECMA-262 3rd Edition 15.8.1.4 */
92 static HRESULT Math_LOG10E(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
93 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
94 {
95 TRACE("\n");
96 return math_constant(M_LOG10E, flags, retv);
97 }
98
99 static HRESULT Math_LN2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
100 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 {
102 TRACE("\n");
103 return math_constant(M_LN2, flags, retv);
104 }
105
106 static HRESULT Math_LN10(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
107 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 {
109 TRACE("\n");
110 return math_constant(M_LN10, flags, retv);
111 }
112
113 /* ECMA-262 3rd Edition 15.8.1.6 */
114 static HRESULT Math_PI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
115 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
116 {
117 TRACE("\n");
118 return math_constant(M_PI, flags, retv);
119 }
120
121 static HRESULT Math_SQRT2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
122 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 {
124 TRACE("\n");
125 return math_constant(M_SQRT2, flags, retv);
126 }
127
128 static HRESULT Math_SQRT1_2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
129 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
130 {
131 TRACE("\n");
132 return math_constant(M_SQRT1_2, flags, retv);
133 }
134
135 /* ECMA-262 3rd Edition 15.8.2.12 */
136 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
137 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
138 {
139 VARIANT v;
140 DOUBLE d;
141 HRESULT hres;
142
143 TRACE("\n");
144
145 if(!arg_cnt(dp)) {
146 if(retv)
147 num_set_nan(retv);
148 return S_OK;
149 }
150
151 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
152 if(FAILED(hres))
153 return hres;
154
155 d = num_val(&v);
156 if(retv)
157 num_set_val(retv, d < 0.0 ? -d : d);
158 return S_OK;
159 }
160
161 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
162 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 {
164 VARIANT v;
165 HRESULT hres;
166
167 TRACE("\n");
168
169 if(!arg_cnt(dp)) {
170 if(retv) num_set_nan(retv);
171 return S_OK;
172 }
173
174 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
175 if(FAILED(hres))
176 return hres;
177
178 if(retv) num_set_val(retv, acos(num_val(&v)));
179 return S_OK;
180 }
181
182 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
183 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 {
185 VARIANT v;
186 HRESULT hres;
187
188 TRACE("\n");
189
190 if(!arg_cnt(dp)) {
191 if(retv) num_set_nan(retv);
192 return S_OK;
193 }
194
195 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
196 if(FAILED(hres))
197 return hres;
198
199 if(retv) num_set_val(retv, asin(num_val(&v)));
200 return S_OK;
201 }
202
203 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 {
206 VARIANT v;
207 HRESULT hres;
208
209 TRACE("\n");
210
211 if(!arg_cnt(dp)) {
212 if(retv) num_set_nan(retv);
213 return S_OK;
214 }
215
216 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
217 if(FAILED(hres))
218 return hres;
219
220 if(retv) num_set_val(retv, atan(num_val(&v)));
221 return S_OK;
222 }
223
224 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 {
227 VARIANT v1, v2;
228 HRESULT hres;
229
230 TRACE("\n");
231
232 if(arg_cnt(dp)<2) {
233 if(retv) num_set_nan(retv);
234 return S_OK;
235 }
236
237 hres = to_number(ctx, get_arg(dp, 0), ei, &v1);
238 if(FAILED(hres))
239 return hres;
240
241 hres = to_number(ctx, get_arg(dp, 1), ei, &v2);
242 if(FAILED(hres))
243 return hres;
244
245 if(retv) num_set_val(retv, atan2(num_val(&v1), num_val(&v2)));
246 return S_OK;
247 }
248
249 /* ECMA-262 3rd Edition 15.8.2.6 */
250 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
251 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 {
253 VARIANT v;
254 HRESULT hres;
255
256 TRACE("\n");
257
258 if(!arg_cnt(dp)) {
259 if(retv)
260 num_set_nan(retv);
261 return S_OK;
262 }
263
264 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
265 if(FAILED(hres))
266 return hres;
267
268 if(retv)
269 num_set_val(retv, ceil(num_val(&v)));
270 return S_OK;
271 }
272
273 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
274 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 {
276 VARIANT v;
277 HRESULT hres;
278
279 TRACE("\n");
280
281 if(!arg_cnt(dp)) {
282 if(retv) num_set_nan(retv);
283 return S_OK;
284 }
285
286 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
287 if(FAILED(hres))
288 return hres;
289
290 if(retv) num_set_val(retv, cos(num_val(&v)));
291 return S_OK;
292 }
293
294 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
295 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
296 {
297 VARIANT v;
298 HRESULT hres;
299
300 TRACE("\n");
301
302 if(!arg_cnt(dp)) {
303 if(retv) num_set_nan(retv);
304 return S_OK;
305 }
306
307 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
308 if(FAILED(hres))
309 return hres;
310
311 if(retv) num_set_val(retv, exp(num_val(&v)));
312 return S_OK;
313 }
314
315 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
316 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
317 {
318 VARIANT v;
319 HRESULT hres;
320
321 TRACE("\n");
322
323 if(!arg_cnt(dp)) {
324 if(retv)
325 num_set_nan(retv);
326 return S_OK;
327 }
328
329 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
330 if(FAILED(hres))
331 return hres;
332
333 if(retv)
334 num_set_val(retv, floor(num_val(&v)));
335 return S_OK;
336 }
337
338 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
339 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 {
341 VARIANT v;
342 HRESULT hres;
343
344 TRACE("\n");
345
346 if(!arg_cnt(dp)) {
347 if(retv)
348 num_set_nan(retv);
349 return S_OK;
350 }
351
352 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
353 if(FAILED(hres))
354 return hres;
355
356 if(retv)
357 num_set_val(retv, log(num_val(&v)));
358 return S_OK;
359 }
360
361 /* ECMA-262 3rd Edition 15.8.2.11 */
362 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
363 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 {
365 DOUBLE max, d;
366 VARIANT v;
367 DWORD i;
368 HRESULT hres;
369
370 TRACE("\n");
371
372 if(!arg_cnt(dp)) {
373 if(retv)
374 num_set_inf(retv, FALSE);
375 return S_OK;
376 }
377
378 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
379 if(FAILED(hres))
380 return hres;
381
382 max = num_val(&v);
383 for(i=1; i < arg_cnt(dp); i++) {
384 hres = to_number(ctx, get_arg(dp, i), ei, &v);
385 if(FAILED(hres))
386 return hres;
387
388 d = num_val(&v);
389 if(d > max || isnan(d))
390 max = d;
391 }
392
393 if(retv)
394 num_set_val(retv, max);
395 return S_OK;
396 }
397
398 /* ECMA-262 3rd Edition 15.8.2.12 */
399 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
400 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
401 {
402 DOUBLE min, d;
403 VARIANT v;
404 DWORD i;
405 HRESULT hres;
406
407 TRACE("\n");
408
409 if(!arg_cnt(dp)) {
410 if(retv)
411 num_set_inf(retv, TRUE);
412 return S_OK;
413 }
414
415 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
416 if(FAILED(hres))
417 return hres;
418
419 min = num_val(&v);
420 for(i=1; i < arg_cnt(dp); i++) {
421 hres = to_number(ctx, get_arg(dp, i), ei, &v);
422 if(FAILED(hres))
423 return hres;
424
425 d = num_val(&v);
426 if(d < min || isnan(d))
427 min = d;
428 }
429
430 if(retv)
431 num_set_val(retv, min);
432 return S_OK;
433 }
434
435 /* ECMA-262 3rd Edition 15.8.2.13 */
436 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
437 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
438 {
439 VARIANT x, y;
440 HRESULT hres;
441
442 TRACE("\n");
443
444 if(arg_cnt(dp) < 2) {
445 if(retv) num_set_nan(retv);
446 return S_OK;
447 }
448
449 hres = to_number(ctx, get_arg(dp, 0), ei, &x);
450 if(FAILED(hres))
451 return hres;
452
453 hres = to_number(ctx, get_arg(dp, 1), ei, &y);
454 if(FAILED(hres))
455 return hres;
456
457 if(retv)
458 num_set_val(retv, pow(num_val(&x), num_val(&y)));
459 return S_OK;
460 }
461
462 /* ECMA-262 3rd Edition 15.8.2.14 */
463 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
464 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
465 {
466 UINT r;
467
468 TRACE("\n");
469
470 if(!RtlGenRandom(&r, sizeof(r)))
471 return E_UNEXPECTED;
472
473 if(retv)
474 num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
475
476 return S_OK;
477 }
478
479 /* ECMA-262 3rd Edition 15.8.2.15 */
480 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
481 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
482 {
483 VARIANT v;
484 HRESULT hres;
485
486 TRACE("\n");
487
488 if(!arg_cnt(dp)) {
489 num_set_nan(retv);
490 return S_OK;
491 }
492
493 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
494 if(FAILED(hres))
495 return hres;
496
497 if(retv)
498 num_set_val(retv, floor(num_val(&v)+0.5));
499 return S_OK;
500 }
501
502 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
503 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
504 {
505 VARIANT v;
506 HRESULT hres;
507
508 TRACE("\n");
509
510 if(!arg_cnt(dp)) {
511 if(retv) num_set_nan(retv);
512 return S_OK;
513 }
514
515 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
516 if(FAILED(hres))
517 return hres;
518
519 if(retv) num_set_val(retv, sin(num_val(&v)));
520 return S_OK;
521 }
522
523 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
524 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
525 {
526 VARIANT v;
527 HRESULT hres;
528
529 TRACE("\n");
530
531 if(!arg_cnt(dp)) {
532 if(retv) num_set_nan(retv);
533 return S_OK;
534 }
535
536 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
537 if(FAILED(hres))
538 return hres;
539
540 if(retv) num_set_val(retv, sqrt(num_val(&v)));
541 return S_OK;
542 }
543
544 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
545 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
546 {
547 VARIANT v;
548 HRESULT hres;
549
550 TRACE("\n");
551
552 if(!arg_cnt(dp)) {
553 if(retv) num_set_nan(retv);
554 return S_OK;
555 }
556
557 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
558 if(FAILED(hres))
559 return hres;
560
561 if(retv) num_set_val(retv, tan(num_val(&v)));
562 return S_OK;
563 }
564
565 static const builtin_prop_t Math_props[] = {
566 {EW, Math_E, 0},
567 {LN10W, Math_LN10, 0},
568 {LN2W, Math_LN2, 0},
569 {LOG10EW, Math_LOG10E, 0},
570 {LOG2EW, Math_LOG2E, 0},
571 {PIW, Math_PI, 0},
572 {SQRT1_2W, Math_SQRT1_2, 0},
573 {SQRT2W, Math_SQRT2, 0},
574 {absW, Math_abs, PROPF_METHOD|1},
575 {acosW, Math_acos, PROPF_METHOD|1},
576 {asinW, Math_asin, PROPF_METHOD|1},
577 {atanW, Math_atan, PROPF_METHOD|1},
578 {atan2W, Math_atan2, PROPF_METHOD|2},
579 {ceilW, Math_ceil, PROPF_METHOD|1},
580 {cosW, Math_cos, PROPF_METHOD|1},
581 {expW, Math_exp, PROPF_METHOD|1},
582 {floorW, Math_floor, PROPF_METHOD|1},
583 {logW, Math_log, PROPF_METHOD|1},
584 {maxW, Math_max, PROPF_METHOD|2},
585 {minW, Math_min, PROPF_METHOD|2},
586 {powW, Math_pow, PROPF_METHOD|2},
587 {randomW, Math_random, PROPF_METHOD},
588 {roundW, Math_round, PROPF_METHOD|1},
589 {sinW, Math_sin, PROPF_METHOD|1},
590 {sqrtW, Math_sqrt, PROPF_METHOD|1},
591 {tanW, Math_tan, PROPF_METHOD|1}
592 };
593
594 static const builtin_info_t Math_info = {
595 JSCLASS_MATH,
596 {NULL, NULL, 0},
597 sizeof(Math_props)/sizeof(*Math_props),
598 Math_props,
599 NULL,
600 NULL
601 };
602
603 HRESULT create_math(script_ctx_t *ctx, DispatchEx **ret)
604 {
605 DispatchEx *math;
606 HRESULT hres;
607
608 math = heap_alloc_zero(sizeof(DispatchEx));
609 if(!math)
610 return E_OUTOFMEMORY;
611
612 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
613 if(FAILED(hres)) {
614 heap_free(math);
615 return hres;
616 }
617
618 *ret = math;
619 return S_OK;
620 }
621
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.