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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
33 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
34 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
35 static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
36 static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
37 static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
38 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
40 #define NUMBER_TOSTRING_BUF_SIZE 64
41 /* ECMA-262 3rd Edition 15.7.4.2 */
42 static HRESULT Number_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
43 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
45 NumberInstance *number;
53 if(!is_class(dispex, JSCLASS_NUMBER))
54 return throw_type_error(dispex->ctx, ei, IDS_NOT_NUM, NULL);
56 number = (NumberInstance*)dispex;
59 hres = to_int32(dispex->ctx, get_arg(dp, 0), ei, &radix);
63 if(radix<2 || radix>36)
64 return throw_type_error(dispex->ctx, ei, IDS_INVALID_CALL_ARG, NULL);
67 if(V_VT(&number->num) == VT_I4)
68 val = V_I4(&number->num);
70 val = V_R8(&number->num);
72 if(radix==10 || isnan(val) || isinf(val)) {
73 hres = to_string(dispex->ctx, &number->num, ei, &str);
79 DOUBLE integ, frac, log_radix = 0;
80 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
94 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
95 buf[idx] = fmod(integ, radix);
96 if(buf[idx]<10) buf[idx] += '0';
97 else buf[idx] += 'a'-10;
102 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
103 INT beg = buf[0]=='-'?1:0;
109 buf[beg++] = buf[end];
114 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
116 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
118 buf[idx] = fmod(frac, radix);
120 if(buf[idx]<10) buf[idx] += '0';
121 else buf[idx] += 'a'-10;
125 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
127 idx = (buf[0]=='-') ? 1 : 0;
128 log_radix = floor(log(val)/log(radix));
129 val *= pow(radix, -log_radix);
136 while(buf[idx-1] == '0') idx--;
137 if(buf[idx-1] == '.') idx--;
143 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
147 log_radix = -log_radix;
151 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
154 else buf[idx] = '\0';
156 str = SysAllocString(buf);
158 return E_OUTOFMEMORY;
162 V_VT(retv) = VT_BSTR;
170 static HRESULT Number_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
171 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 static HRESULT Number_toFixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
178 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 static HRESULT Number_toExponential(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
185 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
191 static HRESULT Number_toPrecision(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
192 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 static HRESULT Number_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
199 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
203 if(!is_class(dispex, JSCLASS_NUMBER))
204 return throw_type_error(dispex->ctx, ei, IDS_NOT_NUM, NULL);
207 NumberInstance *number = (NumberInstance*)dispex;
213 static HRESULT Number_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
214 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
216 NumberInstance *number = (NumberInstance*)dispex;
220 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
221 case DISPATCH_PROPERTYGET:
226 FIXME("flags %x\n", flags);
233 static const builtin_prop_t Number_props[] = {
234 {toExponentialW, Number_toExponential, PROPF_METHOD},
235 {toFixedW, Number_toFixed, PROPF_METHOD},
236 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
237 {toPrecisionW, Number_toPrecision, PROPF_METHOD},
238 {toStringW, Number_toString, PROPF_METHOD},
239 {valueOfW, Number_valueOf, PROPF_METHOD}
242 static const builtin_info_t Number_info = {
244 {NULL, Number_value, 0},
245 sizeof(Number_props)/sizeof(*Number_props),
251 static HRESULT NumberConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
252 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
269 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
277 case DISPATCH_CONSTRUCT: {
281 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
289 hres = create_number(dispex->ctx, &num, &obj);
293 V_VT(retv) = VT_DISPATCH;
294 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
298 FIXME("unimplemented flags %x\n", flags);
305 static HRESULT alloc_number(script_ctx_t *ctx, DispatchEx *object_prototype, NumberInstance **ret)
307 NumberInstance *number;
310 number = heap_alloc_zero(sizeof(NumberInstance));
312 return E_OUTOFMEMORY;
315 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
317 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
325 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
327 NumberInstance *number;
330 hres = alloc_number(ctx, object_prototype, &number);
334 V_VT(&number->num) = VT_I4;
335 hres = create_builtin_function(ctx, NumberConstr_value, NULL, PROPF_CONSTR, &number->dispex, ret);
337 jsdisp_release(&number->dispex);
341 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
343 NumberInstance *number;
346 hres = alloc_number(ctx, NULL, &number);
352 *ret = &number->dispex;