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};
39 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
40 static const WCHAR propertyIsEnumerableW[] =
41 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
42 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
44 #define NUMBER_TOSTRING_BUF_SIZE 64
45 /* ECMA-262 3rd Edition 15.7.4.2 */
46 static HRESULT Number_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
47 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49 NumberInstance *number;
57 if(!is_class(dispex, JSCLASS_NUMBER)) {
58 FIXME("throw TypeError\n");
62 number = (NumberInstance*)dispex;
65 hres = to_int32(dispex->ctx, get_arg(dp, 0), ei, &radix);
69 if(radix<2 || radix>36) {
70 FIXME("throw TypeError\n");
75 if(V_VT(&number->num) == VT_I4)
76 val = V_I4(&number->num);
78 val = V_R8(&number->num);
80 if(radix==10 || isnan(val) || isinf(val)) {
81 hres = to_string(dispex->ctx, &number->num, ei, &str);
87 DOUBLE integ, frac, log_radix = 0;
88 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
102 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
103 buf[idx] = fmod(integ, radix);
104 if(buf[idx]<10) buf[idx] += '0';
105 else buf[idx] += 'a'-10;
110 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
111 INT beg = buf[0]=='-'?1:0;
117 buf[beg++] = buf[end];
122 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
124 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
126 buf[idx] = fmod(frac, radix);
128 if(buf[idx]<10) buf[idx] += '0';
129 else buf[idx] += 'a'-10;
133 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
135 idx = (buf[0]=='-') ? 1 : 0;
136 log_radix = floor(log(val)/log(radix));
137 val *= pow(radix, -log_radix);
144 while(buf[idx-1] == '0') idx--;
145 if(buf[idx-1] == '.') idx--;
151 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
155 log_radix = -log_radix;
159 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
162 else buf[idx] = '\0';
164 str = SysAllocString(buf);
166 return E_OUTOFMEMORY;
170 V_VT(retv) = VT_BSTR;
178 static HRESULT Number_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
179 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 static HRESULT Number_toFixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
186 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
192 static HRESULT Number_toExponential(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
193 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
199 static HRESULT Number_toPrecision(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
200 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
206 static HRESULT Number_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
211 if(!is_class(dispex, JSCLASS_NUMBER)) {
212 FIXME("throw TypeError\n");
217 NumberInstance *number = (NumberInstance*)dispex;
223 static HRESULT Number_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
224 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
230 static HRESULT Number_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
231 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
237 static HRESULT Number_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
238 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
244 static HRESULT Number_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
245 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
247 NumberInstance *number = (NumberInstance*)dispex;
250 case DISPATCH_PROPERTYGET:
255 FIXME("flags %x\n", flags);
262 static const builtin_prop_t Number_props[] = {
263 {hasOwnPropertyW, Number_hasOwnProperty, PROPF_METHOD},
264 {isPrototypeOfW, Number_isPrototypeOf, PROPF_METHOD},
265 {propertyIsEnumerableW, Number_propertyIsEnumerable, PROPF_METHOD},
266 {toExponentialW, Number_toExponential, PROPF_METHOD},
267 {toFixedW, Number_toFixed, PROPF_METHOD},
268 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
269 {toPrecisionW, Number_toPrecision, PROPF_METHOD},
270 {toStringW, Number_toString, PROPF_METHOD},
271 {valueOfW, Number_valueOf, PROPF_METHOD}
274 static const builtin_info_t Number_info = {
276 {NULL, Number_value, 0},
277 sizeof(Number_props)/sizeof(*Number_props),
283 static HRESULT NumberConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
284 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
301 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
309 case DISPATCH_CONSTRUCT: {
313 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
321 hres = create_number(dispex->ctx, &num, &obj);
325 V_VT(retv) = VT_DISPATCH;
326 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
330 FIXME("unimplemented flags %x\n", flags);
337 static HRESULT alloc_number(script_ctx_t *ctx, BOOL use_constr, NumberInstance **ret)
339 NumberInstance *number;
342 number = heap_alloc_zero(sizeof(NumberInstance));
344 return E_OUTOFMEMORY;
347 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
349 hres = init_dispex(&number->dispex, ctx, &Number_info, NULL);
357 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx **ret)
359 NumberInstance *number;
362 hres = alloc_number(ctx, FALSE, &number);
366 V_VT(&number->num) = VT_I4;
367 hres = create_builtin_function(ctx, NumberConstr_value, NULL, PROPF_CONSTR, &number->dispex, ret);
369 jsdisp_release(&number->dispex);
373 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
375 NumberInstance *number;
378 hres = alloc_number(ctx, TRUE, &number);
384 *ret = &number->dispex;