2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
36 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
38 static const WCHAR toFixedW
[] = {'t','o','F','i','x','e','d',0};
39 static const WCHAR toExponentialW
[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
40 static const WCHAR toPrecisionW
[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
41 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
43 #define NUMBER_TOSTRING_BUF_SIZE 64
45 static inline NumberInstance
*number_from_vdisp(vdisp_t
*vdisp
)
47 return (NumberInstance
*)vdisp
->u
.jsdisp
;
50 static inline NumberInstance
*number_this(vdisp_t
*jsthis
)
52 return is_vclass(jsthis
, JSCLASS_NUMBER
) ? number_from_vdisp(jsthis
) : NULL
;
55 /* ECMA-262 3rd Edition 15.7.4.2 */
56 static HRESULT
Number_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
57 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
59 NumberInstance
*number
;
67 if(!(number
= number_this(jsthis
)))
68 return throw_type_error(ctx
, ei
, JS_E_NUMBER_EXPECTED
, NULL
);
71 hres
= to_int32(ctx
, get_arg(dp
, 0), ei
, &radix
);
75 if(radix
<2 || radix
>36)
76 return throw_type_error(ctx
, ei
, JS_E_INVALIDARG
, NULL
);
79 if(V_VT(&number
->num
) == VT_I4
)
80 val
= V_I4(&number
->num
);
82 val
= V_R8(&number
->num
);
84 if(radix
==10 || isnan(val
) || isinf(val
)) {
85 hres
= to_string(ctx
, &number
->num
, ei
, &str
);
91 DOUBLE integ
, frac
, log_radix
= 0;
92 WCHAR buf
[NUMBER_TOSTRING_BUF_SIZE
+16];
106 while(integ
>=1 && idx
<NUMBER_TOSTRING_BUF_SIZE
) {
107 buf
[idx
] = fmod(integ
, radix
);
108 if(buf
[idx
]<10) buf
[idx
] += '0';
109 else buf
[idx
] += 'a'-10;
114 if(idx
<NUMBER_TOSTRING_BUF_SIZE
) {
115 INT beg
= buf
[0]=='-'?1:0;
121 buf
[beg
++] = buf
[end
];
126 if(idx
!= NUMBER_TOSTRING_BUF_SIZE
) buf
[idx
++] = '.';
128 while(frac
>0 && idx
<NUMBER_TOSTRING_BUF_SIZE
) {
130 buf
[idx
] = fmod(frac
, radix
);
132 if(buf
[idx
]<10) buf
[idx
] += '0';
133 else buf
[idx
] += 'a'-10;
137 if(idx
==NUMBER_TOSTRING_BUF_SIZE
&& !exp
) {
139 idx
= (buf
[0]=='-') ? 1 : 0;
140 log_radix
= floor(log(val
)/log(radix
));
141 val
*= pow(radix
, -log_radix
);
148 while(buf
[idx
-1] == '0') idx
--;
149 if(buf
[idx
-1] == '.') idx
--;
155 static const WCHAR formatW
[] = {'(','e','%','c','%','d',')',0};
159 log_radix
= -log_radix
;
163 sprintfW(&buf
[idx
], formatW
, ch
, (int)log_radix
);
166 else buf
[idx
] = '\0';
168 str
= SysAllocString(buf
);
170 return E_OUTOFMEMORY
;
174 V_VT(retv
) = VT_BSTR
;
182 static HRESULT
Number_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
183 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
189 static HRESULT
Number_toFixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
190 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
196 static HRESULT
Number_toExponential(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
197 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
203 static HRESULT
Number_toPrecision(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
204 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
210 static HRESULT
Number_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
211 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
213 NumberInstance
*number
;
217 if(!(number
= number_this(jsthis
)))
218 return throw_type_error(ctx
, ei
, JS_E_NUMBER_EXPECTED
, NULL
);
225 static HRESULT
Number_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
226 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
228 NumberInstance
*number
= number_from_vdisp(jsthis
);
232 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
233 case DISPATCH_PROPERTYGET
:
238 FIXME("flags %x\n", flags
);
245 static const builtin_prop_t Number_props
[] = {
246 {toExponentialW
, Number_toExponential
, PROPF_METHOD
|1},
247 {toFixedW
, Number_toFixed
, PROPF_METHOD
},
248 {toLocaleStringW
, Number_toLocaleString
, PROPF_METHOD
},
249 {toPrecisionW
, Number_toPrecision
, PROPF_METHOD
|1},
250 {toStringW
, Number_toString
, PROPF_METHOD
|1},
251 {valueOfW
, Number_valueOf
, PROPF_METHOD
}
254 static const builtin_info_t Number_info
= {
256 {NULL
, Number_value
, 0},
257 sizeof(Number_props
)/sizeof(*Number_props
),
263 static HRESULT
NumberConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
264 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
281 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &num
);
289 case DISPATCH_CONSTRUCT
: {
293 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &num
);
301 hres
= create_number(ctx
, &num
, &obj
);
305 var_set_jsdisp(retv
, obj
);
309 FIXME("unimplemented flags %x\n", flags
);
316 static HRESULT
alloc_number(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, NumberInstance
**ret
)
318 NumberInstance
*number
;
321 number
= heap_alloc_zero(sizeof(NumberInstance
));
323 return E_OUTOFMEMORY
;
326 hres
= init_dispex(&number
->dispex
, ctx
, &Number_info
, object_prototype
);
328 hres
= init_dispex_from_constr(&number
->dispex
, ctx
, &Number_info
, ctx
->number_constr
);
336 HRESULT
create_number_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
338 NumberInstance
*number
;
341 static const WCHAR NumberW
[] = {'N','u','m','b','e','r',0};
343 hres
= alloc_number(ctx
, object_prototype
, &number
);
347 V_VT(&number
->num
) = VT_I4
;
348 hres
= create_builtin_function(ctx
, NumberConstr_value
, NumberW
, NULL
,
349 PROPF_CONSTR
|1, &number
->dispex
, ret
);
351 jsdisp_release(&number
->dispex
);
355 HRESULT
create_number(script_ctx_t
*ctx
, VARIANT
*num
, jsdisp_t
**ret
)
357 NumberInstance
*number
;
360 hres
= alloc_number(ctx
, NULL
, &number
);
366 *ret
= &number
->dispex
;