~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/jscript/math.c

Version: ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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 /* ECMA-262 3rd Edition    15.8.2.12 */
 61 static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
 62         VARIANT *retv, jsexcept_t *ei)
 63 {
 64     double d;
 65     HRESULT hres;
 66 
 67     TRACE("\n");
 68 
 69     if(!arg_cnt(dp)) {
 70         if(retv)
 71             num_set_nan(retv);
 72         return S_OK;
 73     }
 74 
 75     hres = to_number(ctx, get_arg(dp, 0), ei, &d);
 76     if(FAILED(hres))
 77         return hres;
 78 
 79     if(retv)
 80         num_set_val(retv, d < 0.0 ? -d : d);
 81     return S_OK;
 82 }
 83 
 84 static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
 85         VARIANT *retv, jsexcept_t *ei)
 86 {
 87     double x;
 88     HRESULT hres;
 89 
 90     TRACE("\n");
 91 
 92     if(!arg_cnt(dp)) {
 93         if(retv) num_set_nan(retv);
 94         return S_OK;
 95     }
 96 
 97     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
 98     if(FAILED(hres))
 99         return hres;
100 
101     if(retv) {
102         if(x < -1.0 || x > 1.0)
103             num_set_nan(retv);
104         else
105             num_set_val(retv, acos(x));
106     }
107     return S_OK;
108 }
109 
110 static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
111         VARIANT *retv, jsexcept_t *ei)
112 {
113     double x;
114     HRESULT hres;
115 
116     TRACE("\n");
117 
118     if(!arg_cnt(dp)) {
119         if(retv) num_set_nan(retv);
120         return S_OK;
121     }
122 
123     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
124     if(FAILED(hres))
125         return hres;
126 
127     if(retv) {
128         if(x < -1.0 || x > 1.0)
129             num_set_nan(retv);
130         else
131             num_set_val(retv, asin(x));
132     }
133     return S_OK;
134 }
135 
136 static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
137         VARIANT *retv, jsexcept_t *ei)
138 {
139     double x;
140     HRESULT hres;
141 
142     TRACE("\n");
143 
144     if(!arg_cnt(dp)) {
145         if(retv) num_set_nan(retv);
146         return S_OK;
147     }
148 
149     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
150     if(FAILED(hres))
151         return hres;
152 
153     if(retv) num_set_val(retv, atan(x));
154     return S_OK;
155 }
156 
157 static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
158         VARIANT *retv, jsexcept_t *ei)
159 {
160     double x, y;
161     HRESULT hres;
162 
163     TRACE("\n");
164 
165     if(arg_cnt(dp)<2) {
166         if(retv) num_set_nan(retv);
167         return S_OK;
168     }
169 
170     hres = to_number(ctx, get_arg(dp, 0), ei, &y);
171     if(FAILED(hres))
172         return hres;
173 
174     hres = to_number(ctx, get_arg(dp, 1), ei, &x);
175     if(FAILED(hres))
176         return hres;
177 
178     if(retv) num_set_val(retv, atan2(y, x));
179     return S_OK;
180 }
181 
182 /* ECMA-262 3rd Edition    15.8.2.6 */
183 static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
184         VARIANT *retv, jsexcept_t *ei)
185 {
186     double x;
187     HRESULT hres;
188 
189     TRACE("\n");
190 
191     if(!arg_cnt(dp)) {
192         if(retv)
193             num_set_nan(retv);
194         return S_OK;
195     }
196 
197     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
198     if(FAILED(hres))
199         return hres;
200 
201     if(retv)
202         num_set_val(retv, ceil(x));
203     return S_OK;
204 }
205 
206 static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207         VARIANT *retv, jsexcept_t *ei)
208 {
209     double x;
210     HRESULT hres;
211 
212     TRACE("\n");
213 
214     if(!arg_cnt(dp)) {
215         if(retv) num_set_nan(retv);
216         return S_OK;
217     }
218 
219     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
220     if(FAILED(hres))
221         return hres;
222 
223     if(retv) num_set_val(retv, cos(x));
224     return S_OK;
225 }
226 
227 static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
228         VARIANT *retv, jsexcept_t *ei)
229 {
230     double x;
231     HRESULT hres;
232 
233     TRACE("\n");
234 
235     if(!arg_cnt(dp)) {
236         if(retv) num_set_nan(retv);
237         return S_OK;
238     }
239 
240     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
241     if(FAILED(hres))
242         return hres;
243 
244     if(retv) num_set_val(retv, exp(x));
245     return S_OK;
246 }
247 
248 static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
249         VARIANT *retv, jsexcept_t *ei)
250 {
251     double x;
252     HRESULT hres;
253 
254     TRACE("\n");
255 
256     if(!arg_cnt(dp)) {
257         if(retv)
258             num_set_nan(retv);
259         return S_OK;
260     }
261 
262     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
263     if(FAILED(hres))
264         return hres;
265 
266     if(retv)
267         num_set_val(retv, floor(x));
268     return S_OK;
269 }
270 
271 static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
272         VARIANT *retv, jsexcept_t *ei)
273 {
274     double x;
275     HRESULT hres;
276 
277     TRACE("\n");
278 
279     if(!arg_cnt(dp)) {
280         if(retv)
281             num_set_nan(retv);
282         return S_OK;
283     }
284 
285     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
286     if(FAILED(hres))
287         return hres;
288 
289     if(retv) {
290         if(x < -0.0)
291             num_set_nan(retv);
292         else
293             num_set_val(retv, log(x));
294     }
295     return S_OK;
296 }
297 
298 /* ECMA-262 3rd Edition    15.8.2.11 */
299 static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
300         VARIANT *retv, jsexcept_t *ei)
301 {
302     DOUBLE max, d;
303     DWORD i;
304     HRESULT hres;
305 
306     TRACE("\n");
307 
308     if(!arg_cnt(dp)) {
309         if(retv)
310             num_set_inf(retv, FALSE);
311         return S_OK;
312     }
313 
314     hres = to_number(ctx, get_arg(dp, 0), ei, &max);
315     if(FAILED(hres))
316         return hres;
317 
318     for(i=1; i < arg_cnt(dp); i++) {
319         hres = to_number(ctx, get_arg(dp, i), ei, &d);
320         if(FAILED(hres))
321             return hres;
322 
323         if(d > max || isnan(d))
324             max = d;
325     }
326 
327     if(retv)
328         num_set_val(retv, max);
329     return S_OK;
330 }
331 
332 /* ECMA-262 3rd Edition    15.8.2.12 */
333 static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
334         VARIANT *retv, jsexcept_t *ei)
335 {
336     DOUBLE min, d;
337     DWORD i;
338     HRESULT hres;
339 
340     TRACE("\n");
341 
342     if(!arg_cnt(dp)) {
343         if(retv)
344             num_set_inf(retv, TRUE);
345         return S_OK;
346     }
347 
348     hres = to_number(ctx, get_arg(dp, 0), ei, &min);
349     if(FAILED(hres))
350         return hres;
351 
352     for(i=1; i < arg_cnt(dp); i++) {
353         hres = to_number(ctx, get_arg(dp, i), ei, &d);
354         if(FAILED(hres))
355             return hres;
356 
357         if(d < min || isnan(d))
358             min = d;
359     }
360 
361     if(retv)
362         num_set_val(retv, min);
363     return S_OK;
364 }
365 
366 /* ECMA-262 3rd Edition    15.8.2.13 */
367 static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
368         VARIANT *retv, jsexcept_t *ei)
369 {
370     double x, y;
371     HRESULT hres;
372 
373     TRACE("\n");
374 
375     if(arg_cnt(dp) < 2) {
376         if(retv) num_set_nan(retv);
377         return S_OK;
378     }
379 
380     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
381     if(FAILED(hres))
382         return hres;
383 
384     hres = to_number(ctx, get_arg(dp, 1), ei, &y);
385     if(FAILED(hres))
386         return hres;
387 
388     if(retv)
389         num_set_val(retv, pow(x, y));
390     return S_OK;
391 }
392 
393 /* ECMA-262 3rd Edition    15.8.2.14 */
394 static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
395         VARIANT *retv, jsexcept_t *ei)
396 {
397     UINT r;
398 
399     TRACE("\n");
400 
401     if(!RtlGenRandom(&r, sizeof(r)))
402         return E_UNEXPECTED;
403 
404     if(retv)
405         num_set_val(retv, (DOUBLE)r/(DOUBLE)UINT_MAX);
406 
407     return S_OK;
408 }
409 
410 /* ECMA-262 3rd Edition    15.8.2.15 */
411 static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
412         VARIANT *retv, jsexcept_t *ei)
413 {
414     double x;
415     HRESULT hres;
416 
417     TRACE("\n");
418 
419     if(!arg_cnt(dp)) {
420         num_set_nan(retv);
421         return S_OK;
422     }
423 
424     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
425     if(FAILED(hres))
426         return hres;
427 
428     if(retv)
429         num_set_val(retv, floor(x+0.5));
430     return S_OK;
431 }
432 
433 static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
434         VARIANT *retv, jsexcept_t *ei)
435 {
436     double x;
437     HRESULT hres;
438 
439     TRACE("\n");
440 
441     if(!arg_cnt(dp)) {
442         if(retv) num_set_nan(retv);
443         return S_OK;
444     }
445 
446     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
447     if(FAILED(hres))
448         return hres;
449 
450     if(retv) num_set_val(retv, sin(x));
451     return S_OK;
452 }
453 
454 static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
455         VARIANT *retv, jsexcept_t *ei)
456 {
457     double x;
458     HRESULT hres;
459 
460     TRACE("\n");
461 
462     if(!arg_cnt(dp)) {
463         if(retv) num_set_nan(retv);
464         return S_OK;
465     }
466 
467     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
468     if(FAILED(hres))
469         return hres;
470 
471     if(retv) num_set_val(retv, sqrt(x));
472     return S_OK;
473 }
474 
475 static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
476         VARIANT *retv, jsexcept_t *ei)
477 {
478     double x;
479     HRESULT hres;
480 
481     TRACE("\n");
482 
483     if(!arg_cnt(dp)) {
484         if(retv) num_set_nan(retv);
485         return S_OK;
486     }
487 
488     hres = to_number(ctx, get_arg(dp, 0), ei, &x);
489     if(FAILED(hres))
490         return hres;
491 
492     if(retv) num_set_val(retv, tan(x));
493     return S_OK;
494 }
495 
496 static const builtin_prop_t Math_props[] = {
497     {absW,      Math_abs,      PROPF_METHOD|1},
498     {acosW,     Math_acos,     PROPF_METHOD|1},
499     {asinW,     Math_asin,     PROPF_METHOD|1},
500     {atanW,     Math_atan,     PROPF_METHOD|1},
501     {atan2W,    Math_atan2,    PROPF_METHOD|2},
502     {ceilW,     Math_ceil,     PROPF_METHOD|1},
503     {cosW,      Math_cos,      PROPF_METHOD|1},
504     {expW,      Math_exp,      PROPF_METHOD|1},
505     {floorW,    Math_floor,    PROPF_METHOD|1},
506     {logW,      Math_log,      PROPF_METHOD|1},
507     {maxW,      Math_max,      PROPF_METHOD|2},
508     {minW,      Math_min,      PROPF_METHOD|2},
509     {powW,      Math_pow,      PROPF_METHOD|2},
510     {randomW,   Math_random,   PROPF_METHOD},
511     {roundW,    Math_round,    PROPF_METHOD|1},
512     {sinW,      Math_sin,      PROPF_METHOD|1},
513     {sqrtW,     Math_sqrt,     PROPF_METHOD|1},
514     {tanW,      Math_tan,      PROPF_METHOD|1}
515 };
516 
517 static const builtin_info_t Math_info = {
518     JSCLASS_MATH,
519     {NULL, NULL, 0},
520     sizeof(Math_props)/sizeof(*Math_props),
521     Math_props,
522     NULL,
523     NULL
524 };
525 
526 HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
527 {
528     jsdisp_t *math;
529     unsigned i;
530     VARIANT v;
531     HRESULT hres;
532 
533     struct {
534         const WCHAR *name;
535         DOUBLE val;
536     }constants[] = {
537         {EW,        M_E},        /* ECMA-262 3rd Edition    15.8.1.1 */
538         {LN10W,     M_LN10},     /* ECMA-262 3rd Edition    15.8.1.2 */
539         {LN2W,      M_LN2},      /* ECMA-262 3rd Edition    15.8.1.3 */
540         {LOG2EW,    M_LOG2E},    /* ECMA-262 3rd Edition    15.8.1.4 */
541         {LOG10EW,   M_LOG10E},   /* ECMA-262 3rd Edition    15.8.1.5 */
542         {PIW,       M_PI},       /* ECMA-262 3rd Edition    15.8.1.6 */
543         {SQRT1_2W,  M_SQRT1_2},  /* ECMA-262 3rd Edition    15.8.1.7 */
544         {SQRT2W,    M_SQRT2},    /* ECMA-262 3rd Edition    15.8.1.8 */
545     };
546 
547     math = heap_alloc_zero(sizeof(jsdisp_t));
548     if(!math)
549         return E_OUTOFMEMORY;
550 
551     hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
552     if(FAILED(hres)) {
553         heap_free(math);
554         return hres;
555     }
556 
557     V_VT(&v) = VT_R8;
558     for(i=0; i < sizeof(constants)/sizeof(*constants); i++) {
559         V_R8(&v) = constants[i].val;
560         hres = jsdisp_propput_const(math, constants[i].name, &v);
561         if(FAILED(hres)) {
562             jsdisp_release(math);
563             return hres;
564         }
565     }
566 
567     *ret = math;
568     return S_OK;
569 }
570 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.