ReactOS 0.4.17-dev-357-ga8f14ff
math.c
Go to the documentation of this file.
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#ifdef __REACTOS__
21#define _USE_MATH_DEFINES
22#include <wine/config.h>
23#include <wine/port.h>
24#endif
25
26#include <math.h>
27#include <limits.h>
28
29#include "jscript.h"
30#include "ntsecapi.h"
31
32#include "wine/debug.h"
33
35
36/* ECMA-262 3rd Edition 15.8.2.12 */
38 jsval_t *r)
39{
40 double d;
42
43 TRACE("\n");
44
45 if(!argc) {
46 if(r)
47 *r = jsval_number(NAN);
48 return S_OK;
49 }
50
51 hres = to_number(ctx, argv[0], &d);
52 if(FAILED(hres))
53 return hres;
54
55 if(r)
56 *r = jsval_number(d < 0.0 ? -d : d);
57 return S_OK;
58}
59
61 jsval_t *r)
62{
63 double x;
65
66 TRACE("\n");
67
68 if(!argc) {
69 if(r)
70 *r = jsval_number(NAN);
71 return S_OK;
72 }
73
74 hres = to_number(ctx, argv[0], &x);
75 if(FAILED(hres))
76 return hres;
77
78 if(r)
79 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
80 return S_OK;
81}
82
84 jsval_t *r)
85{
86 double x;
88
89 TRACE("\n");
90
91 if(!argc) {
92 if(r)
93 *r = jsval_number(NAN);
94 return S_OK;
95 }
96
97 hres = to_number(ctx, argv[0], &x);
98 if(FAILED(hres))
99 return hres;
100
101 if(r)
102 *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
103 return S_OK;
104}
105
107 jsval_t *r)
108{
109 double x;
111
112 TRACE("\n");
113
114 if(!argc) {
115 if(r)
116 *r = jsval_number(NAN);
117 return S_OK;
118 }
119
120 hres = to_number(ctx, argv[0], &x);
121 if(FAILED(hres))
122 return hres;
123
124 if(r)
125 *r = jsval_number(atan(x));
126 return S_OK;
127}
128
130 jsval_t *r)
131{
132 double x, y;
134
135 TRACE("\n");
136
137 if(argc<2) {
138 if(r)
139 *r = jsval_number(NAN);
140 return S_OK;
141 }
142
143 hres = to_number(ctx, argv[0], &y);
144 if(FAILED(hres))
145 return hres;
146
147 hres = to_number(ctx, argv[1], &x);
148 if(FAILED(hres))
149 return hres;
150
151 if(r)
152 *r = jsval_number(atan2(y, x));
153 return S_OK;
154}
155
156/* ECMA-262 3rd Edition 15.8.2.6 */
158 jsval_t *r)
159{
160 double x;
162
163 TRACE("\n");
164
165 if(!argc) {
166 if(r)
167 *r = jsval_number(NAN);
168 return S_OK;
169 }
170
171 hres = to_number(ctx, argv[0], &x);
172 if(FAILED(hres))
173 return hres;
174
175 if(r)
176 *r = jsval_number(ceil(x));
177 return S_OK;
178}
179
181 jsval_t *r)
182{
183 double x;
185
186 TRACE("\n");
187
188 if(!argc) {
189 if(r)
190 *r = jsval_number(NAN);
191 return S_OK;
192 }
193
194 hres = to_number(ctx, argv[0], &x);
195 if(FAILED(hres))
196 return hres;
197
198 if(r)
199 *r = jsval_number(cos(x));
200 return S_OK;
201}
202
204 jsval_t *r)
205{
206 double x;
208
209 TRACE("\n");
210
211 if(!argc) {
212 if(r)
213 *r = jsval_number(NAN);
214 return S_OK;
215 }
216
217 hres = to_number(ctx, argv[0], &x);
218 if(FAILED(hres))
219 return hres;
220
221 if(r)
222 *r = jsval_number(exp(x));
223 return S_OK;
224}
225
227 jsval_t *r)
228{
229 double x;
231
232 TRACE("\n");
233
234 if(!argc) {
235 if(r)
236 *r = jsval_number(NAN);
237 return S_OK;
238 }
239
240 hres = to_number(ctx, argv[0], &x);
241 if(FAILED(hres))
242 return hres;
243
244 if(r)
245 *r = jsval_number(floor(x));
246 return S_OK;
247}
248
250 jsval_t *r)
251{
252 double x;
254
255 TRACE("\n");
256
257 if(!argc) {
258 if(r)
259 *r = jsval_number(NAN);
260 return S_OK;
261 }
262
263 hres = to_number(ctx, argv[0], &x);
264 if(FAILED(hres))
265 return hres;
266
267 if(r)
268 *r = jsval_number(x < -0.0 ? NAN : log(x));
269 return S_OK;
270}
271
272/* ECMA-262 3rd Edition 15.8.2.11 */
274 jsval_t *r)
275{
276 DOUBLE max, d;
277 DWORD i;
279
280 TRACE("\n");
281
282 if(!argc) {
283 if(r)
285 return S_OK;
286 }
287
288 hres = to_number(ctx, argv[0], &max);
289 if(FAILED(hres))
290 return hres;
291
292 for(i=1; i < argc; i++) {
293 hres = to_number(ctx, argv[i], &d);
294 if(FAILED(hres))
295 return hres;
296
297 if(d > max || isnan(d))
298 max = d;
299 }
300
301 if(r)
302 *r = jsval_number(max);
303 return S_OK;
304}
305
306/* ECMA-262 3rd Edition 15.8.2.12 */
308 jsval_t *r)
309{
310 DOUBLE min, d;
311 DWORD i;
313
314 TRACE("\n");
315
316 if(!argc) {
317 if(r)
319 return S_OK;
320 }
321
322 hres = to_number(ctx, argv[0], &min);
323 if(FAILED(hres))
324 return hres;
325
326 for(i=1; i < argc; i++) {
327 hres = to_number(ctx, argv[i], &d);
328 if(FAILED(hres))
329 return hres;
330
331 if(d < min || isnan(d))
332 min = d;
333 }
334
335 if(r)
336 *r = jsval_number(min);
337 return S_OK;
338}
339
340/* ECMA-262 3rd Edition 15.8.2.13 */
342 jsval_t *r)
343{
344 double x, y;
346
347 TRACE("\n");
348
349 if(argc < 2) {
350 if(r)
351 *r = jsval_number(NAN);
352 return S_OK;
353 }
354
355 hres = to_number(ctx, argv[0], &x);
356 if(FAILED(hres))
357 return hres;
358
359 hres = to_number(ctx, argv[1], &y);
360 if(FAILED(hres))
361 return hres;
362
363 if(r)
364 *r = jsval_number(pow(x, y));
365 return S_OK;
366}
367
368/* ECMA-262 3rd Edition 15.8.2.14 */
370 jsval_t *r)
371{
372 UINT x;
373
374 TRACE("\n");
375
376 if(!RtlGenRandom(&x, sizeof(x)))
377 return E_UNEXPECTED;
378
379 if(r)
380 *r = jsval_number((double)x/(double)UINT_MAX);
381 return S_OK;
382}
383
384/* ECMA-262 3rd Edition 15.8.2.15 */
386 jsval_t *r)
387{
388 double x;
390
391 TRACE("\n");
392
393 if(!argc) {
394 if(r)
395 *r = jsval_number(NAN);
396 return S_OK;
397 }
398
399 hres = to_number(ctx, argv[0], &x);
400 if(FAILED(hres))
401 return hres;
402
403 if(r)
404 *r = jsval_number(floor(x+0.5));
405 return S_OK;
406}
407
409 jsval_t *r)
410{
411 double x;
413
414 TRACE("\n");
415
416 if(!argc) {
417 if(r)
418 *r = jsval_number(NAN);
419 return S_OK;
420 }
421
422 hres = to_number(ctx, argv[0], &x);
423 if(FAILED(hres))
424 return hres;
425
426 if(r)
427 *r = jsval_number(sin(x));
428 return S_OK;
429}
430
432 jsval_t *r)
433{
434 double x;
436
437 TRACE("\n");
438
439 if(!argc) {
440 if(r)
441 *r = jsval_number(NAN);
442 return S_OK;
443 }
444
445 hres = to_number(ctx, argv[0], &x);
446 if(FAILED(hres))
447 return hres;
448
449 if(r)
450 *r = jsval_number(sqrt(x));
451 return S_OK;
452}
453
455 jsval_t *r)
456{
457 double x;
459
460 TRACE("\n");
461
462 if(!argc) {
463 if(r)
464 *r = jsval_number(NAN);
465 return S_OK;
466 }
467
468 hres = to_number(ctx, argv[0], &x);
469 if(FAILED(hres))
470 return hres;
471
472 if(r)
473 *r = jsval_number(tan(x));
474 return S_OK;
475}
476
477static const builtin_prop_t Math_props[] = {
478 {L"abs", Math_abs, PROPF_METHOD|1},
479 {L"acos", Math_acos, PROPF_METHOD|1},
480 {L"asin", Math_asin, PROPF_METHOD|1},
481 {L"atan", Math_atan, PROPF_METHOD|1},
482 {L"atan2", Math_atan2, PROPF_METHOD|2},
483 {L"ceil", Math_ceil, PROPF_METHOD|1},
484 {L"cos", Math_cos, PROPF_METHOD|1},
485 {L"exp", Math_exp, PROPF_METHOD|1},
486 {L"floor", Math_floor, PROPF_METHOD|1},
487 {L"log", Math_log, PROPF_METHOD|1},
488 {L"max", Math_max, PROPF_METHOD|2},
489 {L"min", Math_min, PROPF_METHOD|2},
490 {L"pow", Math_pow, PROPF_METHOD|2},
491 {L"random", Math_random, PROPF_METHOD},
492 {L"round", Math_round, PROPF_METHOD|1},
493 {L"sin", Math_sin, PROPF_METHOD|1},
494 {L"sqrt", Math_sqrt, PROPF_METHOD|1},
495 {L"tan", Math_tan, PROPF_METHOD|1}
496};
497
498static const builtin_info_t Math_info = {
499 .class = JSCLASS_MATH, .props_cnt = ARRAY_SIZE(Math_props),
500 .props = Math_props,
501};
502
504{
505 jsdisp_t *math;
506 unsigned i;
508
509 struct {
510 const WCHAR *name;
511 DOUBLE val;
512 }constants[] = {
513 {L"E", M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */
514 {L"LN10", M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */
515 {L"LN2", M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */
516 {L"LOG2E", M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */
517 {L"LOG10E", M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */
518 {L"PI", M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */
519 {L"SQRT1_2", M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */
520 {L"SQRT2", M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */
521 };
522
523 math = calloc(1, sizeof(jsdisp_t));
524 if(!math)
525 return E_OUTOFMEMORY;
526
527 hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
528 if(FAILED(hres)) {
529 free(math);
530 return hres;
531 }
532
533 for(i=0; i < ARRAY_SIZE(constants); i++) {
536 if(FAILED(hres)) {
537 jsdisp_release(math);
538 return hres;
539 }
540 }
541
542 *ret = math;
543 return S_OK;
544}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ARRAY_SIZE(A)
Definition: main.h:20
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define free
Definition: debug_ros.c:5
static HRESULT Math_cos(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:180
static HRESULT Math_random(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:369
static HRESULT Math_abs(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:37
static HRESULT Math_atan(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:106
static HRESULT Math_log(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:249
static HRESULT Math_pow(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:341
static HRESULT Math_round(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:385
static HRESULT Math_ceil(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:157
static const builtin_info_t Math_info
Definition: math.c:498
static HRESULT Math_atan2(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:129
static HRESULT Math_sqrt(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:431
static HRESULT Math_acos(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:60
static HRESULT Math_floor(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:226
HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
Definition: math.c:503
static HRESULT Math_sin(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:408
static HRESULT Math_exp(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:203
static const builtin_prop_t Math_props[]
Definition: math.c:477
static HRESULT Math_min(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:307
static HRESULT Math_asin(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:83
static HRESULT Math_tan(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:454
static HRESULT Math_max(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
Definition: math.c:273
MonoAssembly int argc
Definition: metahost.c:107
#define UINT_MAX
Definition: limits.h:27
#define M_LN10
Definition: math.h:408
#define isnan(x)
Definition: math.h:360
_ACRTIMP double __cdecl sqrt(double)
Definition: sqrt.c:5
_ACRTIMP double __cdecl atan(double)
Definition: atan.c:44
_ACRTIMP double __cdecl sin(double)
Definition: sin.c:21
#define M_SQRT2
Definition: math.h:415
_ACRTIMP double __cdecl ceil(double)
Definition: ceil.c:18
#define NAN
Definition: math.h:273
#define M_LN2
Definition: math.h:407
_ACRTIMP double __cdecl tan(double)
Definition: tan.c:122
#define INFINITY
Definition: math.h:272
#define M_E
Definition: math.h:404
#define M_LOG10E
Definition: math.h:406
_ACRTIMP double __cdecl floor(double)
Definition: floor.c:18
_ACRTIMP double __cdecl asin(double)
Definition: asin.c:31
#define M_LOG2E
Definition: math.h:405
#define M_SQRT1_2
Definition: math.h:416
_ACRTIMP double __cdecl cos(double)
Definition: cos.c:21
_ACRTIMP double __cdecl atan2(double, double)
Definition: atan2.c:52
_ACRTIMP double __cdecl acos(double)
Definition: acos.c:28
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
double pow(double x, double y)
Definition: freeldr.c:179
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLbitfield flags
Definition: glext.h:7161
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
HRESULT jsdisp_define_data_property(jsdisp_t *obj, const WCHAR *name, unsigned flags, jsval_t value)
Definition: dispex.c:3349
HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
Definition: dispex.c:2512
ULONG jsdisp_release(jsdisp_t *obj)
Definition: dispex.c:1911
HRESULT to_number(script_ctx_t *, jsval_t, double *)
Definition: jsutils.c:630
@ JSCLASS_MATH
Definition: jscript.h:111
const unsigned int PROPF_METHOD
Definition: jsdisp.idl:33
static jsval_t jsval_number(double n)
Definition: jsval.h:153
#define d
Definition: ke_i.h:81
#define M_PI
Definition: macros.h:263
HRESULT hres
Definition: protocol.c:465
constants
Definition: resource.c:30
DWORD exp
Definition: msg.c:18625
#define min(a, b)
Definition: monoChain.cc:55
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
short WCHAR
Definition: pedump.c:58
#define RtlGenRandom
Definition: ntsecapi.h:691
#define calloc
Definition: rosglue.h:14
#define log(outFile, fmt,...)
Definition: util.h:15
#define TRACE(s)
Definition: solgame.cpp:4
Definition: jsval.h:54
jsclass_t class
Definition: jscript.h:183
Definition: name.c:39
#define max(a, b)
Definition: svc.c:63
double DOUBLE
Definition: typedefs.h:70
#define E_UNEXPECTED
Definition: winerror.h:3528