2008-09-10 02:34:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2009-08-13 22:32:52 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2009-07-15 12:51:21 +02:00
|
|
|
#include <math.h>
|
2012-04-19 15:28:39 +02:00
|
|
|
#include <assert.h>
|
2009-07-15 12:51:21 +02:00
|
|
|
|
2008-09-10 02:34:33 +02:00
|
|
|
#include "jscript.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
|
|
|
|
|
|
|
typedef struct {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t dispex;
|
2008-09-16 20:45:45 +02:00
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
double value;
|
2008-09-10 02:34:33 +02:00
|
|
|
} NumberInstance;
|
|
|
|
|
|
|
|
static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
|
|
|
|
static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
|
|
|
|
static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
|
|
|
|
static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
|
|
|
|
static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
|
|
|
|
static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
|
|
|
|
|
2009-07-15 12:51:21 +02:00
|
|
|
#define NUMBER_TOSTRING_BUF_SIZE 64
|
2012-04-19 15:28:39 +02:00
|
|
|
#define NUMBER_DTOA_SIZE 18
|
2009-09-23 16:18:39 +02:00
|
|
|
|
|
|
|
static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
|
|
|
|
{
|
|
|
|
return (NumberInstance*)vdisp->u.jsdisp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline NumberInstance *number_this(vdisp_t *jsthis)
|
|
|
|
{
|
|
|
|
return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-19 15:28:39 +02:00
|
|
|
static inline void dtoa(double d, WCHAR *buf, int size, int *dec_point)
|
|
|
|
{
|
|
|
|
ULONGLONG l;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* TODO: this function should print doubles with bigger precision */
|
|
|
|
assert(size>=2 && size<=NUMBER_DTOA_SIZE && d>=0);
|
|
|
|
|
|
|
|
if(d == 0)
|
|
|
|
*dec_point = 0;
|
|
|
|
else
|
|
|
|
*dec_point = floor(log10(d));
|
|
|
|
l = d*pow(10, size-*dec_point-1);
|
|
|
|
|
|
|
|
if(l%10 >= 5)
|
|
|
|
l = l/10+1;
|
|
|
|
else
|
|
|
|
l /= 10;
|
|
|
|
|
|
|
|
buf[size-1] = 0;
|
|
|
|
for(i=size-2; i>=0; i--) {
|
|
|
|
buf[i] = '0'+l%10;
|
|
|
|
l /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* log10 was wrong by 1 or rounding changed number of digits */
|
|
|
|
if(l) {
|
|
|
|
(*dec_point)++;
|
|
|
|
memmove(buf+1, buf, size-2);
|
|
|
|
buf[0] = '0'+l;
|
|
|
|
}else if(buf[0]=='0' && buf[1]>='1' && buf[1]<='9') {
|
|
|
|
(*dec_point)--;
|
|
|
|
memmove(buf, buf+1, size-2);
|
|
|
|
buf[size-2] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
static inline jsstr_t *number_to_fixed(double val, int prec)
|
2012-04-19 15:28:39 +02:00
|
|
|
{
|
|
|
|
WCHAR buf[NUMBER_DTOA_SIZE];
|
|
|
|
int dec_point, size, buf_size, buf_pos;
|
|
|
|
BOOL neg = FALSE;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *ret;
|
|
|
|
WCHAR *str;
|
2012-04-19 15:28:39 +02:00
|
|
|
|
|
|
|
if(val < 0) {
|
|
|
|
neg = TRUE;
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(val<=-1 || val>=1)
|
|
|
|
buf_size = log10(val)+prec+2;
|
|
|
|
else
|
|
|
|
buf_size = prec+1;
|
|
|
|
if(buf_size > NUMBER_DTOA_SIZE)
|
|
|
|
buf_size = NUMBER_DTOA_SIZE;
|
|
|
|
|
|
|
|
dtoa(val, buf, buf_size, &dec_point);
|
|
|
|
dec_point++;
|
|
|
|
size = 0;
|
|
|
|
if(neg)
|
|
|
|
size++;
|
|
|
|
if(dec_point > 0)
|
|
|
|
size += dec_point;
|
|
|
|
else
|
|
|
|
size++;
|
|
|
|
if(prec)
|
|
|
|
size += prec+1;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
ret = jsstr_alloc_buf(size);
|
|
|
|
if(!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = ret->str;
|
2012-04-19 15:28:39 +02:00
|
|
|
size = buf_pos = 0;
|
|
|
|
if(neg)
|
|
|
|
str[size++] = '-';
|
|
|
|
if(dec_point > 0) {
|
|
|
|
for(;buf_pos<buf_size-1 && dec_point; dec_point--)
|
|
|
|
str[size++] = buf[buf_pos++];
|
|
|
|
}else {
|
|
|
|
str[size++] = '0';
|
|
|
|
}
|
|
|
|
for(; dec_point>0; dec_point--)
|
|
|
|
str[size++] = '0';
|
|
|
|
if(prec) {
|
|
|
|
str[size++] = '.';
|
|
|
|
|
|
|
|
for(; dec_point<0 && prec; dec_point++, prec--)
|
|
|
|
str[size++] = '0';
|
|
|
|
for(; buf_pos<buf_size-1 && prec; prec--)
|
|
|
|
str[size++] = buf[buf_pos++];
|
|
|
|
for(; prec; prec--) {
|
|
|
|
str[size++] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str[size++] = 0;
|
2012-10-11 12:16:01 +02:00
|
|
|
return ret;
|
2012-04-19 15:28:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
static inline jsstr_t *number_to_exponential(double val, int prec)
|
2012-04-19 15:28:59 +02:00
|
|
|
{
|
|
|
|
WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
|
|
|
|
int dec_point, size, buf_size, exp_size = 1;
|
|
|
|
BOOL neg = FALSE;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *ret;
|
|
|
|
WCHAR *str;
|
2012-04-19 15:28:59 +02:00
|
|
|
|
|
|
|
if(val < 0) {
|
|
|
|
neg = TRUE;
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_size = prec+2;
|
|
|
|
if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
|
|
|
|
buf_size = NUMBER_DTOA_SIZE;
|
|
|
|
dtoa(val, buf, buf_size, &dec_point);
|
|
|
|
buf_size--;
|
|
|
|
if(prec == -1)
|
|
|
|
for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
|
|
|
|
buf[buf_size-1] = 0;
|
|
|
|
|
|
|
|
size = 10;
|
|
|
|
while(dec_point>=size || dec_point<=-size) {
|
|
|
|
size *= 10;
|
|
|
|
exp_size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(buf_size == 1)
|
|
|
|
size = buf_size+2+exp_size; /* 2 = strlen(e+) */
|
|
|
|
else if(prec == -1)
|
|
|
|
size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
|
|
|
|
else
|
|
|
|
size = prec+4+exp_size; /* 4 = strlen(0.e+) */
|
|
|
|
if(neg)
|
|
|
|
size++;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
ret = jsstr_alloc_buf(size);
|
|
|
|
if(!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = ret->str;
|
2012-04-19 15:28:59 +02:00
|
|
|
size = 0;
|
|
|
|
pbuf = buf;
|
|
|
|
if(neg)
|
|
|
|
str[size++] = '-';
|
|
|
|
str[size++] = *pbuf++;
|
|
|
|
if(buf_size != 1) {
|
|
|
|
str[size++] = '.';
|
|
|
|
while(*pbuf)
|
|
|
|
str[size++] = *pbuf++;
|
|
|
|
for(; prec>buf_size-1; prec--)
|
|
|
|
str[size++] = '0';
|
|
|
|
}
|
|
|
|
str[size++] = 'e';
|
|
|
|
if(dec_point >= 0) {
|
|
|
|
str[size++] = '+';
|
|
|
|
}else {
|
|
|
|
str[size++] = '-';
|
|
|
|
dec_point = -dec_point;
|
|
|
|
}
|
2012-04-25 12:10:18 +02:00
|
|
|
size += exp_size;
|
|
|
|
do {
|
|
|
|
str[--size] = '0'+dec_point%10;
|
|
|
|
dec_point /= 10;
|
|
|
|
}while(dec_point>0);
|
|
|
|
size += exp_size;
|
2012-04-19 15:28:59 +02:00
|
|
|
str[size] = 0;
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
return ret;
|
2012-04-19 15:28:59 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:37:32 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.7.4.2 */
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_toString(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:34:33 +02:00
|
|
|
{
|
2008-09-21 15:37:32 +02:00
|
|
|
NumberInstance *number;
|
2009-07-15 12:51:21 +02:00
|
|
|
INT radix = 10;
|
|
|
|
DOUBLE val;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2008-09-21 15:37:32 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!(number = number_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
|
2008-09-21 15:37:32 +02:00
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_int32(ctx, argv[0], &radix);
|
2009-07-15 12:51:21 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2009-07-20 18:18:15 +02:00
|
|
|
if(radix<2 || radix>36)
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_INVALIDARG, NULL);
|
2008-09-21 15:37:32 +02:00
|
|
|
}
|
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
val = number->value;
|
2009-07-15 12:51:21 +02:00
|
|
|
|
|
|
|
if(radix==10 || isnan(val) || isinf(val)) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, jsval_number(val), &str);
|
2009-07-15 12:51:21 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2012-03-27 11:30:51 +02:00
|
|
|
}else {
|
2009-07-15 12:51:21 +02:00
|
|
|
INT idx = 0;
|
|
|
|
DOUBLE integ, frac, log_radix = 0;
|
|
|
|
WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
|
|
|
|
BOOL exp = FALSE;
|
|
|
|
|
|
|
|
if(val<0) {
|
|
|
|
val = -val;
|
|
|
|
buf[idx++] = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
integ = floor(val);
|
|
|
|
frac = val-integ;
|
|
|
|
|
|
|
|
if(integ == 0)
|
|
|
|
buf[idx++] = '0';
|
|
|
|
while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
|
|
|
|
buf[idx] = fmod(integ, radix);
|
|
|
|
if(buf[idx]<10) buf[idx] += '0';
|
|
|
|
else buf[idx] += 'a'-10;
|
|
|
|
integ /= radix;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(idx<NUMBER_TOSTRING_BUF_SIZE) {
|
|
|
|
INT beg = buf[0]=='-'?1:0;
|
|
|
|
INT end = idx-1;
|
|
|
|
WCHAR wch;
|
|
|
|
|
|
|
|
while(end > beg) {
|
|
|
|
wch = buf[beg];
|
|
|
|
buf[beg++] = buf[end];
|
|
|
|
buf[end--] = wch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
|
|
|
|
|
|
|
|
while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
|
|
|
|
frac *= radix;
|
|
|
|
buf[idx] = fmod(frac, radix);
|
|
|
|
frac -= buf[idx];
|
|
|
|
if(buf[idx]<10) buf[idx] += '0';
|
|
|
|
else buf[idx] += 'a'-10;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
|
|
|
|
exp = TRUE;
|
|
|
|
idx = (buf[0]=='-') ? 1 : 0;
|
|
|
|
log_radix = floor(log(val)/log(radix));
|
|
|
|
val *= pow(radix, -log_radix);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(buf[idx-1] == '0') idx--;
|
|
|
|
if(buf[idx-1] == '.') idx--;
|
|
|
|
|
|
|
|
if(exp) {
|
|
|
|
if(log_radix==0)
|
2011-01-27 13:38:36 +01:00
|
|
|
buf[idx] = 0;
|
2009-07-15 12:51:21 +02:00
|
|
|
else {
|
|
|
|
static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
|
|
|
|
WCHAR ch;
|
|
|
|
|
|
|
|
if(log_radix<0) {
|
|
|
|
log_radix = -log_radix;
|
|
|
|
ch = '-';
|
|
|
|
}
|
|
|
|
else ch = '+';
|
|
|
|
sprintfW(&buf[idx], formatW, ch, (int)log_radix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else buf[idx] = '\0';
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
str = jsstr_alloc(buf);
|
2009-07-15 12:51:21 +02:00
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2008-09-21 15:37:32 +02:00
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_string(str);
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(str);
|
2008-09-21 15:37:32 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_toLocaleString(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:34:33 +02:00
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_toFixed(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:34:33 +02:00
|
|
|
{
|
2012-04-19 15:28:39 +02:00
|
|
|
NumberInstance *number;
|
|
|
|
DOUBLE val;
|
|
|
|
INT prec = 0;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2012-04-19 15:28:39 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(!(number = number_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
|
2012-04-19 15:28:39 +02:00
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_int32(ctx, argv[0], &prec);
|
2012-04-19 15:28:39 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(prec<0 || prec>20)
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
|
2012-04-19 15:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val = number->value;
|
|
|
|
if(isinf(val) || isnan(val)) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, jsval_number(val), &str);
|
2012-04-19 15:28:39 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else {
|
2012-10-11 12:16:01 +02:00
|
|
|
str = number_to_fixed(val, prec);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-04-19 15:28:39 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_string(str);
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(str);
|
2012-04-19 15:28:39 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_toExponential(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:34:33 +02:00
|
|
|
{
|
2012-04-19 15:28:59 +02:00
|
|
|
NumberInstance *number;
|
|
|
|
DOUBLE val;
|
|
|
|
INT prec = 0;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2012-04-19 15:28:59 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(!(number = number_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
|
2012-04-19 15:28:59 +02:00
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_int32(ctx, argv[0], &prec);
|
2012-04-19 15:28:59 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(prec<0 || prec>20)
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL);
|
2012-04-19 15:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val = number->value;
|
|
|
|
if(isinf(val) || isnan(val)) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, jsval_number(val), &str);
|
2012-04-19 15:28:59 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else {
|
|
|
|
if(!prec)
|
|
|
|
prec--;
|
2012-10-11 12:16:01 +02:00
|
|
|
str = number_to_exponential(val, prec);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-04-19 15:28:59 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_string(str);
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(str);
|
2012-04-19 15:28:59 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_toPrecision(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:34:33 +02:00
|
|
|
{
|
2012-04-19 15:29:15 +02:00
|
|
|
NumberInstance *number;
|
|
|
|
INT prec = 0, size;
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *str;
|
2012-04-19 15:29:15 +02:00
|
|
|
DOUBLE val;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(!(number = number_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
|
2012-04-19 15:29:15 +02:00
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_int32(ctx, argv[0], &prec);
|
2012-04-19 15:29:15 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(prec<1 || prec>21)
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_range_error(ctx, JS_E_PRECISION_OUT_OF_RANGE, NULL);
|
2012-04-19 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val = number->value;
|
|
|
|
if(isinf(val) || isnan(val) || !prec) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, jsval_number(val), &str);
|
2012-04-19 15:29:15 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else {
|
|
|
|
if(val != 0)
|
|
|
|
size = floor(log10(val>0 ? val : -val)) + 1;
|
|
|
|
else
|
|
|
|
size = 1;
|
|
|
|
|
|
|
|
if(size > prec)
|
2012-10-11 12:16:01 +02:00
|
|
|
str = number_to_exponential(val, prec-1);
|
2012-04-19 15:29:15 +02:00
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
str = number_to_fixed(val, prec-size);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-04-19 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_string(str);
|
|
|
|
else
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(str);
|
2012-04-19 15:29:15 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_valueOf(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:34:33 +02:00
|
|
|
{
|
2009-09-23 16:18:39 +02:00
|
|
|
NumberInstance *number;
|
|
|
|
|
2008-10-18 00:04:45 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!(number = number_this(jsthis)))
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL);
|
2008-10-18 00:04:45 +02:00
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(number->value);
|
2008-10-18 00:04:45 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Number_value(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:34:33 +02:00
|
|
|
{
|
2009-09-23 16:18:39 +02:00
|
|
|
NumberInstance *number = number_from_vdisp(jsthis);
|
2008-09-25 00:51:21 +02:00
|
|
|
|
|
|
|
switch(flags) {
|
2009-07-22 13:02:30 +02:00
|
|
|
case INVOKE_FUNC:
|
2012-09-18 19:01:49 +02:00
|
|
|
return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
|
2008-09-25 00:51:21 +02:00
|
|
|
case DISPATCH_PROPERTYGET:
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_number(number->value);
|
2008-09-25 00:51:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Number_props[] = {
|
2009-08-17 12:11:51 +02:00
|
|
|
{toExponentialW, Number_toExponential, PROPF_METHOD|1},
|
2008-09-10 02:34:33 +02:00
|
|
|
{toFixedW, Number_toFixed, PROPF_METHOD},
|
|
|
|
{toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
|
2009-08-17 12:11:51 +02:00
|
|
|
{toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
|
|
|
|
{toStringW, Number_toString, PROPF_METHOD|1},
|
2008-09-10 02:34:33 +02:00
|
|
|
{valueOfW, Number_valueOf, PROPF_METHOD}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Number_info = {
|
|
|
|
JSCLASS_NUMBER,
|
|
|
|
{NULL, Number_value, 0},
|
|
|
|
sizeof(Number_props)/sizeof(*Number_props),
|
|
|
|
Number_props,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-07-24 13:17:19 +02:00
|
|
|
static const builtin_info_t NumberInst_info = {
|
|
|
|
JSCLASS_NUMBER,
|
|
|
|
{NULL, Number_value, 0},
|
|
|
|
0, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT NumberConstr_value(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:34:33 +02:00
|
|
|
{
|
2012-03-27 11:28:51 +02:00
|
|
|
double n;
|
2008-09-21 15:37:15 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
2008-09-21 15:37:57 +02:00
|
|
|
case INVOKE_FUNC:
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(0);
|
2008-09-21 15:37:57 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &n);
|
2008-09-21 15:37:57 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_number(n);
|
2008-09-21 15:37:57 +02:00
|
|
|
break;
|
|
|
|
|
2008-09-21 15:37:15 +02:00
|
|
|
case DISPATCH_CONSTRUCT: {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *obj;
|
2008-09-21 15:37:15 +02:00
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_number(ctx, argv[0], &n);
|
2008-09-21 15:37:15 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2008-10-18 00:04:45 +02:00
|
|
|
}else {
|
2012-03-27 11:30:51 +02:00
|
|
|
n = 0;
|
2008-09-21 15:37:15 +02:00
|
|
|
}
|
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
hres = create_number(ctx, n, &obj);
|
2008-09-21 15:37:15 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_obj(obj);
|
2008-09-21 15:37:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:34:33 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
|
2008-09-10 02:34:33 +02:00
|
|
|
{
|
2008-10-06 16:57:20 +02:00
|
|
|
NumberInstance *number;
|
2008-09-10 02:34:33 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2008-10-06 16:57:20 +02:00
|
|
|
number = heap_alloc_zero(sizeof(NumberInstance));
|
|
|
|
if(!number)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2009-08-12 12:48:34 +02:00
|
|
|
if(object_prototype)
|
|
|
|
hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
|
2008-09-10 02:34:33 +02:00
|
|
|
else
|
2012-07-24 13:17:19 +02:00
|
|
|
hres = init_dispex_from_constr(&number->dispex, ctx, &NumberInst_info, ctx->number_constr);
|
2012-10-31 10:21:09 +01:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(number);
|
2008-09-10 02:34:33 +02:00
|
|
|
return hres;
|
2012-10-31 10:21:09 +01:00
|
|
|
}
|
2008-09-10 02:34:33 +02:00
|
|
|
|
|
|
|
*ret = number;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
|
2008-09-10 02:34:33 +02:00
|
|
|
{
|
|
|
|
NumberInstance *number;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-09-17 01:04:44 +02:00
|
|
|
static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
|
|
|
|
|
2009-08-12 12:48:34 +02:00
|
|
|
hres = alloc_number(ctx, object_prototype, &number);
|
2008-09-10 02:34:33 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
number->value = 0;
|
2012-07-16 15:30:54 +02:00
|
|
|
hres = create_builtin_constructor(ctx, NumberConstr_value, NumberW, NULL,
|
2009-10-13 22:08:43 +02:00
|
|
|
PROPF_CONSTR|1, &number->dispex, ret);
|
2008-09-10 02:34:33 +02:00
|
|
|
|
|
|
|
jsdisp_release(&number->dispex);
|
|
|
|
return hres;
|
|
|
|
}
|
2008-09-16 20:45:45 +02:00
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
|
2008-09-16 20:45:45 +02:00
|
|
|
{
|
|
|
|
NumberInstance *number;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-08-12 12:48:34 +02:00
|
|
|
hres = alloc_number(ctx, NULL, &number);
|
2008-09-16 20:45:45 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-03-27 11:30:51 +02:00
|
|
|
number->value = value;
|
2008-09-16 20:45:45 +02:00
|
|
|
|
|
|
|
*ret = &number->dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|