math.c 11.5 KB
Newer Older
1 2
/*
 * Copyright 2008 Jacek Caban for CodeWeavers
3
 * Copyright 2009 Piotr Caban
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

20

21
#include <math.h>
22
#include <limits.h>
23

24
#include "jscript.h"
25
#include "ntsecapi.h"
26 27 28 29 30

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

31
/* ECMA-262 3rd Edition    15.8.2.12 */
32
static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
33
        jsval_t *r)
34
{
35
    double d;
36 37 38 39
    HRESULT hres;

    TRACE("\n");

40
    if(!argc) {
41 42
        if(r)
            *r = jsval_number(NAN);
43
        return S_OK;
44 45
    }

46
    hres = to_number(ctx, argv[0], &d);
47 48 49
    if(FAILED(hres))
        return hres;

50 51
    if(r)
        *r = jsval_number(d < 0.0 ? -d : d);
52
    return S_OK;
53 54
}

55
static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
56
        jsval_t *r)
57
{
58
    double x;
59 60 61 62
    HRESULT hres;

    TRACE("\n");

63
    if(!argc) {
64 65
        if(r)
            *r = jsval_number(NAN);
66 67 68
        return S_OK;
    }

69
    hres = to_number(ctx, argv[0], &x);
70 71 72
    if(FAILED(hres))
        return hres;

73 74
    if(r)
        *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
75
    return S_OK;
76 77
}

78
static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
79
        jsval_t *r)
80
{
81
    double x;
82 83 84 85
    HRESULT hres;

    TRACE("\n");

86
    if(!argc) {
87 88
        if(r)
            *r = jsval_number(NAN);
89 90 91
        return S_OK;
    }

92
    hres = to_number(ctx, argv[0], &x);
93 94 95
    if(FAILED(hres))
        return hres;

96 97
    if(r)
        *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
98
    return S_OK;
99 100
}

101
static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
102
        jsval_t *r)
103
{
104
    double x;
105 106 107 108
    HRESULT hres;

    TRACE("\n");

109
    if(!argc) {
110 111
        if(r)
            *r = jsval_number(NAN);
112 113 114
        return S_OK;
    }

115
    hres = to_number(ctx, argv[0], &x);
116 117 118
    if(FAILED(hres))
        return hres;

119 120
    if(r)
        *r = jsval_number(atan(x));
121
    return S_OK;
122 123
}

124
static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
125
        jsval_t *r)
126
{
127
    double x, y;
128 129 130 131
    HRESULT hres;

    TRACE("\n");

132
    if(argc<2) {
133 134
        if(r)
            *r = jsval_number(NAN);
135 136 137
        return S_OK;
    }

138
    hres = to_number(ctx, argv[0], &y);
139 140 141
    if(FAILED(hres))
        return hres;

142
    hres = to_number(ctx, argv[1], &x);
143 144 145
    if(FAILED(hres))
        return hres;

146 147
    if(r)
        *r = jsval_number(atan2(y, x));
148
    return S_OK;
149 150
}

151
/* ECMA-262 3rd Edition    15.8.2.6 */
152
static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
153
        jsval_t *r)
154
{
155
    double x;
156 157 158 159
    HRESULT hres;

    TRACE("\n");

160
    if(!argc) {
161 162
        if(r)
            *r = jsval_number(NAN);
163
        return S_OK;
164 165
    }

166
    hres = to_number(ctx, argv[0], &x);
167 168 169
    if(FAILED(hres))
        return hres;

170 171
    if(r)
        *r = jsval_number(ceil(x));
172
    return S_OK;
173 174
}

175
static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
176
        jsval_t *r)
177
{
178
    double x;
179 180 181 182
    HRESULT hres;

    TRACE("\n");

183
    if(!argc) {
184 185
        if(r)
            *r = jsval_number(NAN);
186 187 188
        return S_OK;
    }

189
    hres = to_number(ctx, argv[0], &x);
190 191 192
    if(FAILED(hres))
        return hres;

193 194
    if(r)
        *r = jsval_number(cos(x));
195
    return S_OK;
196 197
}

198
static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
199
        jsval_t *r)
200
{
201
    double x;
202 203 204 205
    HRESULT hres;

    TRACE("\n");

206
    if(!argc) {
207 208
        if(r)
            *r = jsval_number(NAN);
209 210 211
        return S_OK;
    }

212
    hres = to_number(ctx, argv[0], &x);
213 214 215
    if(FAILED(hres))
        return hres;

216 217
    if(r)
        *r = jsval_number(exp(x));
218
    return S_OK;
219 220
}

221
static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
222
        jsval_t *r)
223
{
224
    double x;
225 226 227 228
    HRESULT hres;

    TRACE("\n");

229
    if(!argc) {
230 231
        if(r)
            *r = jsval_number(NAN);
232 233 234
        return S_OK;
    }

235
    hres = to_number(ctx, argv[0], &x);
236 237 238
    if(FAILED(hres))
        return hres;

239 240
    if(r)
        *r = jsval_number(floor(x));
241
    return S_OK;
242 243
}

244
static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
245
        jsval_t *r)
246
{
247
    double x;
248 249 250 251
    HRESULT hres;

    TRACE("\n");

252
    if(!argc) {
253 254
        if(r)
            *r = jsval_number(NAN);
255 256 257
        return S_OK;
    }

258
    hres = to_number(ctx, argv[0], &x);
259 260 261
    if(FAILED(hres))
        return hres;

262 263
    if(r)
        *r = jsval_number(x < -0.0 ? NAN : log(x));
264
    return S_OK;
265 266
}

267
/* ECMA-262 3rd Edition    15.8.2.11 */
268
static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
269
        jsval_t *r)
270
{
271 272 273 274 275 276
    DOUBLE max, d;
    DWORD i;
    HRESULT hres;

    TRACE("\n");

277
    if(!argc) {
278 279
        if(r)
            *r = jsval_number(-INFINITY);
280
        return S_OK;
281 282
    }

283
    hres = to_number(ctx, argv[0], &max);
284 285 286
    if(FAILED(hres))
        return hres;

287
    for(i=1; i < argc; i++) {
288
        hres = to_number(ctx, argv[i], &d);
289 290 291
        if(FAILED(hres))
            return hres;

292
        if(d > max || isnan(d))
293 294 295
            max = d;
    }

296 297
    if(r)
        *r = jsval_number(max);
298
    return S_OK;
299 300
}

301
/* ECMA-262 3rd Edition    15.8.2.12 */
302
static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
303
        jsval_t *r)
304
{
305 306 307 308 309 310
    DOUBLE min, d;
    DWORD i;
    HRESULT hres;

    TRACE("\n");

311
    if(!argc) {
312 313
        if(r)
            *r = jsval_number(INFINITY);
314
        return S_OK;
315 316
    }

317
    hres = to_number(ctx, argv[0], &min);
318 319 320
    if(FAILED(hres))
        return hres;

321
    for(i=1; i < argc; i++) {
322
        hres = to_number(ctx, argv[i], &d);
323 324 325
        if(FAILED(hres))
            return hres;

326
        if(d < min || isnan(d))
327 328 329
            min = d;
    }

330 331
    if(r)
        *r = jsval_number(min);
332
    return S_OK;
333 334
}

335
/* ECMA-262 3rd Edition    15.8.2.13 */
336
static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
337
        jsval_t *r)
338
{
339
    double x, y;
340 341 342 343
    HRESULT hres;

    TRACE("\n");

344
    if(argc < 2) {
345 346
        if(r)
            *r = jsval_number(NAN);
347
        return S_OK;
348 349
    }

350
    hres = to_number(ctx, argv[0], &x);
351 352 353
    if(FAILED(hres))
        return hres;

354
    hres = to_number(ctx, argv[1], &y);
355 356 357
    if(FAILED(hres))
        return hres;

358 359
    if(r)
        *r = jsval_number(pow(x, y));
360
    return S_OK;
361 362
}

363
/* ECMA-262 3rd Edition    15.8.2.14 */
364
static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
365
        jsval_t *r)
366
{
367
    UINT x;
368 369 370

    TRACE("\n");

371
    if(!RtlGenRandom(&x, sizeof(x)))
372 373
        return E_UNEXPECTED;

374 375
    if(r)
        *r = jsval_number((double)x/(double)UINT_MAX);
376
    return S_OK;
377 378
}

379
/* ECMA-262 3rd Edition    15.8.2.15 */
380
static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
381
        jsval_t *r)
382
{
383
    double x;
384 385 386 387
    HRESULT hres;

    TRACE("\n");

388
    if(!argc) {
389 390
        if(r)
            *r = jsval_number(NAN);
391
        return S_OK;
392 393
    }

394
    hres = to_number(ctx, argv[0], &x);
395 396 397
    if(FAILED(hres))
        return hres;

398 399
    if(r)
        *r = jsval_number(floor(x+0.5));
400
    return S_OK;
401 402
}

403
static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
404
        jsval_t *r)
405
{
406
    double x;
407 408 409 410
    HRESULT hres;

    TRACE("\n");

411
    if(!argc) {
412 413
        if(r)
            *r = jsval_number(NAN);
414 415 416
        return S_OK;
    }

417
    hres = to_number(ctx, argv[0], &x);
418 419 420
    if(FAILED(hres))
        return hres;

421 422
    if(r)
        *r = jsval_number(sin(x));
423
    return S_OK;
424 425
}

426
static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
427
        jsval_t *r)
428
{
429
    double x;
430 431 432 433
    HRESULT hres;

    TRACE("\n");

434
    if(!argc) {
435 436
        if(r)
            *r = jsval_number(NAN);
437 438 439
        return S_OK;
    }

440
    hres = to_number(ctx, argv[0], &x);
441 442 443
    if(FAILED(hres))
        return hres;

444 445
    if(r)
        *r = jsval_number(sqrt(x));
446
    return S_OK;
447 448
}

449
static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
450
        jsval_t *r)
451
{
452
    double x;
453 454 455 456
    HRESULT hres;

    TRACE("\n");

457
    if(!argc) {
458 459
        if(r)
            *r = jsval_number(NAN);
460 461 462
        return S_OK;
    }

463
    hres = to_number(ctx, argv[0], &x);
464 465 466
    if(FAILED(hres))
        return hres;

467 468
    if(r)
        *r = jsval_number(tan(x));
469
    return S_OK;
470 471 472
}

static const builtin_prop_t Math_props[] = {
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    {L"abs",      Math_abs,      PROPF_METHOD|1},
    {L"acos",     Math_acos,     PROPF_METHOD|1},
    {L"asin",     Math_asin,     PROPF_METHOD|1},
    {L"atan",     Math_atan,     PROPF_METHOD|1},
    {L"atan2",    Math_atan2,    PROPF_METHOD|2},
    {L"ceil",     Math_ceil,     PROPF_METHOD|1},
    {L"cos",      Math_cos,      PROPF_METHOD|1},
    {L"exp",      Math_exp,      PROPF_METHOD|1},
    {L"floor",    Math_floor,    PROPF_METHOD|1},
    {L"log",      Math_log,      PROPF_METHOD|1},
    {L"max",      Math_max,      PROPF_METHOD|2},
    {L"min",      Math_min,      PROPF_METHOD|2},
    {L"pow",      Math_pow,      PROPF_METHOD|2},
    {L"random",   Math_random,   PROPF_METHOD},
    {L"round",    Math_round,    PROPF_METHOD|1},
    {L"sin",      Math_sin,      PROPF_METHOD|1},
    {L"sqrt",     Math_sqrt,     PROPF_METHOD|1},
    {L"tan",      Math_tan,      PROPF_METHOD|1}
491 492 493 494 495
};

static const builtin_info_t Math_info = {
    JSCLASS_MATH,
    {NULL, NULL, 0},
496
    ARRAY_SIZE(Math_props),
497 498 499 500 501
    Math_props,
    NULL,
    NULL
};

502
HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
503
{
504
    jsdisp_t *math;
505
    unsigned i;
506 507
    HRESULT hres;

508 509 510 511
    struct {
        const WCHAR *name;
        DOUBLE val;
    }constants[] = {
512 513 514 515 516 517 518 519
        {L"E",        M_E},        /* ECMA-262 3rd Edition    15.8.1.1 */
        {L"LN10",     M_LN10},     /* ECMA-262 3rd Edition    15.8.1.2 */
        {L"LN2",      M_LN2},      /* ECMA-262 3rd Edition    15.8.1.3 */
        {L"LOG2E",    M_LOG2E},    /* ECMA-262 3rd Edition    15.8.1.4 */
        {L"LOG10E",   M_LOG10E},   /* ECMA-262 3rd Edition    15.8.1.5 */
        {L"PI",       M_PI},       /* ECMA-262 3rd Edition    15.8.1.6 */
        {L"SQRT1_2",  M_SQRT1_2},  /* ECMA-262 3rd Edition    15.8.1.7 */
        {L"SQRT2",    M_SQRT2},    /* ECMA-262 3rd Edition    15.8.1.8 */
520 521
    };

522
    math = heap_alloc_zero(sizeof(jsdisp_t));
523 524 525 526 527 528 529 530 531
    if(!math)
        return E_OUTOFMEMORY;

    hres = init_dispex_from_constr(math, ctx, &Math_info, ctx->object_constr);
    if(FAILED(hres)) {
        heap_free(math);
        return hres;
    }

532
    for(i=0; i < ARRAY_SIZE(constants); i++) {
533 534
        hres = jsdisp_define_data_property(math, constants[i].name, 0,
                                           jsval_number(constants[i].val));
535 536 537 538 539 540
        if(FAILED(hres)) {
            jsdisp_release(math);
            return hres;
        }
    }

541 542
    *ret = math;
    return S_OK;
543
}