2008-09-10 02:35:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
2009-07-06 11:00:22 +02:00
|
|
|
* Copyright 2009 Piotr Caban
|
2008-09-10 02:35:43 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-10-16 21:31:52 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2008-09-21 15:45:00 +02:00
|
|
|
#include <math.h>
|
2009-01-22 13:47:00 +01:00
|
|
|
#include <limits.h>
|
2008-09-21 15:45:00 +02:00
|
|
|
|
2008-09-10 02:35:43 +02:00
|
|
|
#include "jscript.h"
|
2009-01-22 13:47:00 +01:00
|
|
|
#include "ntsecapi.h"
|
2008-09-10 02:35:43 +02:00
|
|
|
|
|
|
|
#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};
|
2008-12-11 12:28:30 +01:00
|
|
|
static const WCHAR LOG10EW[] = {'L','O','G','1','0','E',0};
|
2008-09-10 02:35:43 +02:00
|
|
|
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};
|
|
|
|
|
2008-09-21 15:45:49 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.12 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_abs(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double d;
|
2008-09-21 15:45:49 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2008-10-16 21:31:58 +02:00
|
|
|
return S_OK;
|
2008-09-21 15:45:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &d);
|
2008-09-21 15:45:49 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(d < 0.0 ? -d : d);
|
2008-09-21 15:45:49 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_acos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-25 00:03:06 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-25 00:03:06 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-25 00:03:06 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(x < -1.0 || x > 1.0 ? NAN : acos(x));
|
2009-05-25 00:03:06 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_asin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-25 22:05:00 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-25 22:05:00 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-25 22:05:00 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(x < -1.0 || x > 1.0 ? NAN : asin(x));
|
2009-05-25 22:05:00 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_atan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-25 22:05:21 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-25 22:05:21 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-25 22:05:21 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(atan(x));
|
2009-05-25 22:05:21 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_atan2(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x, y;
|
2009-05-25 22:05:35 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc<2) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-25 22:05:35 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &y);
|
2009-05-25 22:05:35 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[1], &x);
|
2009-05-25 22:05:35 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(atan2(y, x));
|
2009-05-25 22:05:35 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:46:45 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.6 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_ceil(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2008-09-21 15:46:45 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2008-10-22 01:56:27 +02:00
|
|
|
return S_OK;
|
2008-09-21 15:46:45 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2008-09-21 15:46:45 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(ceil(x));
|
2008-09-21 15:46:45 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_cos(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-04-02 19:55:30 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-04-02 19:55:30 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-04-02 19:55:30 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(cos(x));
|
2009-04-02 19:55:30 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_exp(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-27 01:09:37 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 01:09:37 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-27 01:09:37 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(exp(x));
|
2009-05-27 01:09:37 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_floor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2008-10-22 01:56:15 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2008-10-22 01:56:15 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2008-10-22 01:56:15 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(floor(x));
|
2008-10-22 01:56:15 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_log(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-27 01:09:56 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 01:09:56 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-27 01:09:56 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(x < -0.0 ? NAN : log(x));
|
2009-05-27 01:09:56 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:46:04 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.11 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_max(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2008-09-21 15:46:04 +02:00
|
|
|
DOUBLE max, d;
|
|
|
|
DWORD i;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(-INFINITY);
|
2008-10-16 21:31:52 +02:00
|
|
|
return S_OK;
|
2008-09-21 15:46:04 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &max);
|
2008-09-21 15:46:04 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
for(i=1; i < argc; i++) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[i], &d);
|
2008-09-21 15:46:04 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2008-10-16 21:31:52 +02:00
|
|
|
if(d > max || isnan(d))
|
2008-09-21 15:46:04 +02:00
|
|
|
max = d;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(max);
|
2008-09-21 15:46:04 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:45:00 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.12 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_min(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2008-09-21 15:45:00 +02:00
|
|
|
DOUBLE min, d;
|
|
|
|
DWORD i;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(INFINITY);
|
2008-10-16 21:31:52 +02:00
|
|
|
return S_OK;
|
2008-09-21 15:45:00 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &min);
|
2008-09-21 15:45:00 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
for(i=1; i < argc; i++) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[i], &d);
|
2008-09-21 15:45:00 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2008-10-16 21:31:52 +02:00
|
|
|
if(d < min || isnan(d))
|
2008-09-21 15:45:00 +02:00
|
|
|
min = d;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(min);
|
2008-09-21 15:45:00 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:46:22 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.13 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_pow(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x, y;
|
2008-09-21 15:46:22 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc < 2) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 18:44:00 +02:00
|
|
|
return S_OK;
|
2008-09-21 15:46:22 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2008-09-21 15:46:22 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[1], &y);
|
2008-09-21 15:46:22 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(pow(x, y));
|
2008-09-21 15:46:22 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2009-01-22 13:47:00 +01:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.14 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_random(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-09-17 15:16:20 +02:00
|
|
|
UINT x;
|
2009-01-22 13:47:00 +01:00
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(!RtlGenRandom(&x, sizeof(x)))
|
2009-01-22 13:47:00 +01:00
|
|
|
return E_UNEXPECTED;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number((double)x/(double)UINT_MAX);
|
2009-01-22 13:47:00 +01:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:45:25 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.8.2.15 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_round(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2008-09-21 15:45:25 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-01-22 13:47:13 +01:00
|
|
|
return S_OK;
|
2008-09-21 15:45:25 +02:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2008-09-21 15:45:25 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(floor(x+0.5));
|
2008-09-21 15:45:25 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_sin(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-27 01:10:10 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 01:10:10 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-27 01:10:10 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(sin(x));
|
2009-05-27 01:10:10 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_sqrt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-27 01:10:25 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 01:10:25 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-27 01:10:25 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(sqrt(x));
|
2009-05-27 01:10:25 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Math_tan(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double x;
|
2009-05-27 01:10:36 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(NAN);
|
2009-05-27 01:10:36 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &x);
|
2009-05-27 01:10:36 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(tan(x));
|
2009-05-27 01:10:36 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Math_props[] = {
|
2009-08-17 12:13:10 +02:00
|
|
|
{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},
|
2008-09-10 02:35:43 +02:00
|
|
|
{randomW, Math_random, PROPF_METHOD},
|
2009-08-17 12:13:10 +02:00
|
|
|
{roundW, Math_round, PROPF_METHOD|1},
|
|
|
|
{sinW, Math_sin, PROPF_METHOD|1},
|
|
|
|
{sqrtW, Math_sqrt, PROPF_METHOD|1},
|
|
|
|
{tanW, Math_tan, PROPF_METHOD|1}
|
2008-09-10 02:35:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Math_info = {
|
|
|
|
JSCLASS_MATH,
|
|
|
|
{NULL, NULL, 0},
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(Math_props),
|
2008-09-10 02:35:43 +02:00
|
|
|
Math_props,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_math(script_ctx_t *ctx, jsdisp_t **ret)
|
2008-09-10 02:35:43 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *math;
|
2010-07-27 11:53:49 +02:00
|
|
|
unsigned i;
|
2009-08-14 11:58:54 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2010-07-27 11:53:49 +02:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
math = heap_alloc_zero(sizeof(jsdisp_t));
|
2009-08-14 11:58:54 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:59:12 +02:00
|
|
|
for(i=0; i < ARRAY_SIZE(constants); i++) {
|
2018-05-11 14:43:28 +02:00
|
|
|
hres = jsdisp_define_data_property(math, constants[i].name, 0,
|
|
|
|
jsval_number(constants[i].val));
|
2010-07-27 11:53:49 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
jsdisp_release(math);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:58:54 +02:00
|
|
|
*ret = math;
|
|
|
|
return S_OK;
|
2008-09-10 02:35:43 +02:00
|
|
|
}
|