Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575/* * Copyright 2008 Jacek Caban for CodeWeavers * Copyright 2009 Piotr Caban * * 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 */
#ifdef __REACTOS__#include <wine/config.h>#include <wine/port.h>#endif
#include <math.h>#include <limits.h>
#include "jscript.h"#include "ntsecapi.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
static const WCHAR EW[] = {'E',0};static const WCHAR LOG2EW[] = {'L','O','G','2','E',0};static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};static const WCHAR LN2W[] = {'L','N','2',0};static const WCHAR LN10W[] = {'L','N','1','0',0};static const WCHAR PIW[] = {'P','I',0};static const WCHAR SQRT2W[] = {'S','Q','R','T','2',0};static const WCHAR SQRT1_2W[] = {'S','Q','R','T','1','_','2',0};static const WCHAR absW[] = {'a','b','s',0};static const WCHAR acosW[] = {'a','c','o','s',0};static const WCHAR asinW[] = {'a','s','i','n',0};static const WCHAR atanW[] = {'a','t','a','n',0};static const WCHAR atan2W[] = {'a','t','a','n','2',0};static const WCHAR ceilW[] = {'c','e','i','l',0};static const WCHAR cosW[] = {'c','o','s',0};static const WCHAR expW[] = {'e','x','p',0};static const WCHAR floorW[] = {'f','l','o','o','r',0};static const WCHAR logW[] = {'l','o','g',0};static const WCHAR maxW[] = {'m','a','x',0};static const WCHAR minW[] = {'m','i','n',0};static const WCHAR powW[] = {'p','o','w',0};static const WCHAR randomW[] = {'r','a','n','d','o','m',0};static const WCHAR roundW[] = {'r','o','u','n','d',0};static const WCHAR sinW[] = {'s','i','n',0};static const WCHAR sqrtW[] = {'s','q','r','t',0};static const WCHAR tanW[] = {'t','a','n',0};
/* ECMA-262 3rd Edition 15.8.2.12 */static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double d; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &d); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(d < 0.0 ? -d : d); return S_OK;}
static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x)); return S_OK;}
static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x)); return S_OK;}
static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(atan(x)); return S_OK;}
static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x, y; HRESULT hres;
TRACE("\n");
if(argc<2) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &y); if(FAILED(hres)) return hres;
hres = to_number(ctx, argv[1], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(atan2(y, x)); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.6 */static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(ceil(x)); return S_OK;}
static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(cos(x)); return S_OK;}
static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(exp(x)); return S_OK;}
static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(floor(x)); return S_OK;}
static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(x < -0.0 ? NAN : log(x)); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.11 */static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ DOUBLE max, d; DWORD i; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(-INFINITY); return S_OK; }
hres = to_number(ctx, argv[0], &max); if(FAILED(hres)) return hres;
for(i=1; i < argc; i++) { hres = to_number(ctx, argv[i], &d); if(FAILED(hres)) return hres;
if(d > max || isnan(d)) max = d; }
if(r) *r = jsval_number(max); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.12 */static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ DOUBLE min, d; DWORD i; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(INFINITY); return S_OK; }
hres = to_number(ctx, argv[0], &min); if(FAILED(hres)) return hres;
for(i=1; i < argc; i++) { hres = to_number(ctx, argv[i], &d); if(FAILED(hres)) return hres;
if(d < min || isnan(d)) min = d; }
if(r) *r = jsval_number(min); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.13 */static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x, y; HRESULT hres;
TRACE("\n");
if(argc < 2) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
hres = to_number(ctx, argv[1], &y); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(pow(x, y)); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.14 */static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ UINT x;
TRACE("\n");
if(!RtlGenRandom(&x, sizeof(x))) return E_UNEXPECTED;
if(r) *r = jsval_number((double)x/(double)UINT_MAX); return S_OK;}
/* ECMA-262 3rd Edition 15.8.2.15 */static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(floor(x+0.5)); return S_OK;}
static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(sin(x)); return S_OK;}
static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(sqrt(x)); return S_OK;}
static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r){ double x; HRESULT hres;
TRACE("\n");
if(!argc) { if(r) *r = jsval_number(NAN); return S_OK; }
hres = to_number(ctx, argv[0], &x); if(FAILED(hres)) return hres;
if(r) *r = jsval_number(tan(x)); return S_OK;}
static const builtin_prop_t Math_props[] = { {absW, Math_abs, PROPF_METHOD|1}, {acosW, Math_acos, PROPF_METHOD|1}, {asinW, Math_asin, PROPF_METHOD|1}, {atanW, Math_atan, PROPF_METHOD|1}, {atan2W, Math_atan2, PROPF_METHOD|2}, {ceilW, Math_ceil, PROPF_METHOD|1}, {cosW, Math_cos, PROPF_METHOD|1}, {expW, Math_exp, PROPF_METHOD|1}, {floorW, Math_floor, PROPF_METHOD|1}, {logW, Math_log, PROPF_METHOD|1}, {maxW, Math_max, PROPF_METHOD|2}, {minW, Math_min, PROPF_METHOD|2}, {powW, Math_pow, PROPF_METHOD|2}, {randomW, Math_random, PROPF_METHOD}, {roundW, Math_round, PROPF_METHOD|1}, {sinW, Math_sin, PROPF_METHOD|1}, {sqrtW, Math_sqrt, PROPF_METHOD|1}, {tanW, Math_tan, PROPF_METHOD|1}};
static const builtin_info_t Math_info = { JSCLASS_MATH, {NULL, NULL, 0}, ARRAY_SIZE(Math_props), Math_props, NULL, NULL};
HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret){ jsdisp_t *math; unsigned i; HRESULT hres;
struct { const WCHAR *name; DOUBLE val; }constants[] = { {EW, M_E}, /* ECMA-262 3rd Edition 15.8.1.1 */ {LN10W, M_LN10}, /* ECMA-262 3rd Edition 15.8.1.2 */ {LN2W, M_LN2}, /* ECMA-262 3rd Edition 15.8.1.3 */ {LOG2EW, M_LOG2E}, /* ECMA-262 3rd Edition 15.8.1.4 */ {LOG10EW, M_LOG10E}, /* ECMA-262 3rd Edition 15.8.1.5 */ {PIW, M_PI}, /* ECMA-262 3rd Edition 15.8.1.6 */ {SQRT1_2W, M_SQRT1_2}, /* ECMA-262 3rd Edition 15.8.1.7 */ {SQRT2W, M_SQRT2}, /* ECMA-262 3rd Edition 15.8.1.8 */ };
math = heap_alloc_zero(sizeof(jsdisp_t)); 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; }
for(i=0; i < ARRAY_SIZE(constants); i++) { hres = jsdisp_define_data_property(math, constants[i].name, 0, jsval_number(constants[i].val)); if(FAILED(hres)) { jsdisp_release(math); return hres; } }
*ret = math; return S_OK;}